Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
fleek-router
Advanced tools
Middleware for routing to controllers using swagger schema's.
Requirements:
This package is to be used as middleware for Koa2 to route paths defined in swagger documentation using ctx.fleek.context
defined by fleek-context or an equivalent custom middleware.
npm install --save fleek-router
For a swagger example, refer to the test swagger json
const Koa = require('koa');
const fleekCtx = require('fleek-context');
const fleekRouter = require('fleek-router');
const SWAGGER = require('./swagger.json');
let app = new Koa();
app.use(fleekCtx(SWAGGER));
let router = fleekRouter({ controllers: `${__dirname}/controllers` });
ctx.use(router.tag('authenticated', (ctx, next) => {
if (someAuthFunction(ctx)) {
ctx.body = 'Not authorized';
ctx.status = 401;
return Promise.resolve();
} else return next();
}))
app.use(router.controllers({
controller: {
bar: {
get: (ctx, next) => { /* routes for tags ['bar', ...] and method GET */ },
},
foo: {
biz: {
post: (ctx, next) => { /* routes for tags ['foo', 'biz' ...] and method POST */ }
}
}
},
operation: {
createBar: (ctx, next) => { /* routes for operationId 'createBar' */ }
}
}));
// OR
app.use(router.controllers(`${__driname}/controllers`));
// TO MATCH EXAMPLE ABOVE:
// controllers/
// ├── bar.js [exports: get(ctx, next)]
// ├── foo
// | └── biz.js [exports: post(ctx, next)]
// └── */** [exports: createBar(ctx, next)]
app.listen(3000);
let myRouter = new fleekRouter.Router();
ctx.fleek.context.tags[]
, the operation will be executedctx
(context) of the requestnext
returns promise when calledapp.use(router.tag('authenticated', (ctx, next) => {
console.log(ctx.fleek.context.tags) // => [ ..., 'authenticated', ... ]
let isAuthenticated = someAuthFunction(ctx);
if (isAuthenticated) return next();
else return Promise.reject(Error('401: not authorized'));
}));
app.use((ctx, next) => {
ctx.body = 'passed authentication';
return Promise.resolve();
})
ctx.fleek.context.operationId
, the operation will be executedctx
(context) of the requestnext
returns promise when calledapp.use(router.tag('getUser', (ctx, next) => {
console.log(ctx.fleek.context.operationId) // => 'getUser'
ctx.body = DB.get('user', ctx.params.user_id);
return Promise.resolve();
}));
ctx.fleek.context
to perform validation.js
files exporting operation functionsctx.fleek.context
must contain data mapping it to the controller/operationPOST
, GET
, PUT
, DELETE
), the swagger documentation must specify the controller as the first tag(tags: []
) for the path+methodctx.fleek.context.tags[]
to route to the given operation
controllers/foo/bar.js[get(ctx, next) => { ... }]
will execute for ctx.fleek.context = { method: 'get', tags: ['foo', 'bar'] }
module.exports[operationId]
), if it does not match a method type (POST
, GET
, PUT
, DELETE
)operationId
in the path+method configuration to match the exported function name// Assuming directory structure:
// controllers/
// ├── bar.js [exports: get(ctx, next) => { ... }]
// ├── foo
// | └── biz.js [exports: post(ctx, next) => { ... }]
// └── */** [exports: createBar(ctx, next) => { ... }]
app.use(router.controllers(`${__dirname}/controllers`));
// OR
app.use(router.controllers({ path: `${__dirname}/controllers` }));
controller
: Object - shallow or nested controllers, containg method operations (post|create
, get|read
, put|update
, delete|destroy
)`operation
: Object - shallow operations. propery keys expected to map to operationId
ctx.fleek.context
to perform validationctx.fleek.context
must contain data mapping it to the controller/operationPOST
, GET
, PUT
, DELETE
), the swagger documentation must specify the controller as the first tag(tags: []
) for the path+methodctx.fleek.context.tags[]
to route to the given operation
controller.foo.bar.get: (ctx, next) => { ... }
will execute for ctx.fleek.context = { method: 'get', tags: ['foo', 'bar'] }
module.exports[operationId]
), if it does not match a method type (POST
, GET
, PUT
, DELETE
)operationId
in the path+method configuration to match the exported function nameapp.use(router.controllers({
controller: {
bar: get(ctx, next) => { /* ... */ },
foo: {
biz: {
post: (ctx, next) => { /* ... */ }
}
}
},
operation: {
createBar: (ctx, next) => { /* ... */ }
}
}));
Built and maintained with by the Hart team.
FAQs
A router for the fleek framework
We found that fleek-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.