Upgrade to V2
Koa-smart v2 is the current release and it introduces some breaking changes. The following guide lists some of the changes to upgrade from v1 to v2.
Breaking Changes
Body parametter: change params
key to bodyType
Change param types description by using Types
Old
@Route.Post({ params: { email: true, name: false, }, }) async add(ctx) { this.sendCreated(ctx, this.body(ctx)); }
New
@Route.Post({ bodyType: Types.object().keys({ email: Types.string().required(), name: Types.string().uppercase(), }), }) async add(ctx) { this.sendCreated(ctx, this.body(ctx)); }
Class Route: change functions
- Change function
sendBadRequest(ctx, data, message)
bythrowBadRequest(error, translate = false)
- Change function
sendUnauthorized(ctx, data, message)
bythrowUnauthorized(error, translate = false)
- Change function
sendForbidden(ctx, data, message)
bythrowForbidden(error, translate = false)
- Change function
sendNotFound(ctx, data, message)
bythrowNotFound(error, translate = false)
- Remove function
sendInternalServerError(ctx, data, message)
New in v2
Manage query parameters with queryType
// call => /view?email=myemail@email.com&name=myName
@Route.Get({
queryType: Types.object().keys({
email: Types.string().required(),
name: Types.string().uppercase(),
}),
})
async view(ctx) {
this.sendCreated(ctx, this.queryParam(ctx));
}
Add new assert functions
assertBadRequest
assertUnauthorized
assertForbidden
Example :
@Route.Get({}) async view(ctx) { this.assertUnauthorized(!!ctx.user); this.sendCreated(ctx, 'OK'); }
Automatic doc generation
Koa-smart can now automatically generate documentation, by adding the following parameter at the App's creation:
const app = new App({
port: 3001,
generateDoc: true, // indicates we want koa-smart to generate documentation
});
for more details, see the relevant documentation