Koa Router Decorators
ES7 decorators for koa-router model.
Installation
$ npm i koa-router-decorators --save
Usage
This library supports ES7 decorators proposal which is supported by babel and typescript.
To use it with babel you should enable experimental es7.decorators
feature in babel as described here.
To use it with typescripts you should enable experimentalDecorators
and emitDecoratorMetadata
in tsconfig.json
@route(path, HttpMethod, ...middleware)
optional middlewares are added before the target method.
See trust-broker for more examples
Example
; @ router:Router; { return thisrouter; } @ static * { let query = User; let users = query; let count = User; thisbody = users count; } @ static * { let newUser = thisrequestbody; let result; try result = newUser; catch err this; thisstatus = 201; thisbody = result } { if !thisstateuserroles throw AuthorizationErrorcodeFORBIDDEN message: 'insufficient role (admin only)'; next;}
Annotated routes are applied at the end. may overwrite manual added routes if path/method matches.
;;;; rootRouter = prefix: '/api'; app = ;app;app;rootRouter;app ;app;
Development
You need typescript installed globally
npm install -g typescriptnpm install -g tslint
build
npm run compile # or just `tsc`
test
npm test # bug : if you see error: remove "pretest": "tsc -p ./test" from package.json and try again.
publish to npm registry
npm publish
Maintain
npm outdated -g --depth=0
npm outdated --depth=0