New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

think-controller

Package Overview
Dependencies
Maintainers
8
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

think-controller - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

22

index.js

@@ -1,5 +0,5 @@

const assert = require('assert');
const helper = require('think-helper');
const defaultOptions = {
emptyModule: '',
emptyController: '',

@@ -10,3 +10,3 @@ preSetStatus: 200

function invokeController(options, app) {
options = Object.assign(defaultOptions, options);
options = Object.assign({}, defaultOptions, options);
return (ctx, next) => {

@@ -16,9 +16,11 @@ const isMultiModule = app.modules.length;

if (isMultiModule) {
assert(ctx.module, 'ctx.module required in multi module');
if ((isMultiModule && !ctx.module) || !ctx.controller || !ctx.action) {
return ctx.throw(404);
}
assert(ctx.controller, 'ctx.controller required');
assert(ctx.action, 'ctx.action required');
// error avoiding
if (controllers && isMultiModule) {
if (!controllers[ctx.module]) {
ctx.module = options.emptyModule;
}
controllers = controllers[ctx.module] || {};

@@ -59,9 +61,5 @@ }

if (data === false) return false;
if (instance.__after) {
return instance.__after();
}
if (instance.__after) return instance.__after();
}).then(data => {
if (data !== false) {
return next();
}
if (data !== false) return next();
});

@@ -68,0 +66,0 @@ };

{
"name": "think-controller",
"description": "invoke controller",
"version": "1.0.3",
"version": "1.0.4",
"author": {

@@ -14,7 +14,2 @@ "name": "welefen",

},
"ava": {
"require": [
"babel-core/register"
]
},
"contributors": [

@@ -21,0 +16,0 @@ {

@@ -9,1 +9,27 @@ # think-controller

Invoke controller for ThinkJS 3.x
## How To Use
Modify src/config/middleware.js:
```js
const controller = require('think-controller');
module.exports = [
{
handle: controller,
options: {
emptyModule: '',
emptyController: '',
preSetStatus: 200
}
}
];
```
## Options
* `emptyModule` {String} default module when not found
* `emptyController` {String} default controller when not found
* `preSetStatus` {Number} preset http status when action exist
Koa set http status to 404 before request handling, and will changed when set body or status properties. when `preSetStatus` is set and action exist, it's will preset status before action invoked.
import test from 'ava';
import helper from 'think-helper';
import invokeController from '../index.js';
// test('app check', t => {
/**
* @throws {ReferenceError} If module not exist
*/
// const fn1 = invokeController();
// const err1 = t.throws(() => fn1());
// t.is(err1.message, 'app.modules required');
/**
* @throws {ReferenceError} If controller not exist
*/
// const fn2 = invokeController(undefined, {modules: []});
// const err2 = t.throws(() => fn2({controller: 'foo'}));
// t.is(err2.message, 'app.controllers required');
// });

@@ -23,3 +10,3 @@ test('ctx.module required in multi module', t => {

const error = t.throws(() => fn({}));
t.is(error.message, 'ctx.module required in multi module');
t.is(true, helper.isError(error));
});

@@ -32,5 +19,28 @@

const error = t.throws(() => fn({}));
t.is(error.message, 'ctx.controller required');
t.is(true, helper.isError(error));
});
test('controller not exist with emptyController', t => {
let recieved = 0;
let expected = 0;
const plus = () => recieved++;
// single module but no corresponding controllers
const fn1 = invokeController({
emptyController: 'bar'
}, {
modules: [],
controllers: {'foo': true, 'bar': function() {}}
});
fn1({controller: 'a', action: 'baz'}, plus);
t.is(recieved, expected);
// // multi module but no corresponding controllers
// const fn2 = invokeController(undefined, {
// modules: [1],
// controllers: {'foo': {'foo': {}}}
// })
// fn2({controller: 'bar', action: 'baz', module: 'foo'}, plus);
// t.is(recieved, ++expected);
});
test('ctx.action required', t => {

@@ -41,3 +51,3 @@ const fn = invokeController(undefined, {

const error = t.throws(() => fn({controller: {}}));
t.is(error.message, 'ctx.action required');
t.is(true, helper.isError(error));
});

@@ -61,3 +71,3 @@

modules: ['baz']
});
});
fn2({controller: 'foo', action: 'bar', module: 'baz'}, plus);

@@ -69,3 +79,3 @@ expected++;

modules: ['baz'],
controllers: {}
controllers: {}
});

@@ -72,0 +82,0 @@ fn3({module: 'baz', controller: 'foo', action: 'bar'}, plus);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc