Comparing version 2.0.20 to 3.0.0
@@ -6,5 +6,18 @@ import type { KobpServiceContext, KobpServiceState } from '../context'; | ||
export interface RouteMapMeta { | ||
/** | ||
* The HTTP Method supported in this function | ||
*/ | ||
method: HttpMethod | HttpMethod[]; | ||
/** | ||
* the route string | ||
*/ | ||
path?: string; | ||
/** | ||
* All middlewares attached to this path | ||
*/ | ||
middlewares?: Middleware[]; | ||
/** | ||
* Instruct the system NOT to handle success result as JSON payload | ||
*/ | ||
doNotHandleSuccess?: boolean; | ||
} | ||
@@ -11,0 +24,0 @@ export interface RouteMap { |
@@ -17,3 +17,3 @@ "use strict"; | ||
return { | ||
...(this.__drm || {}), /* will be injected from decorators package */ | ||
...(this.__drm || {}) /* will be injected from decorators package */, | ||
}; | ||
@@ -25,3 +25,3 @@ } | ||
success: true, | ||
data | ||
data, | ||
}; | ||
@@ -45,3 +45,4 @@ } | ||
for (const fname in map) { | ||
let { method, path } = map[fname]; | ||
let { method, path, doNotHandleSuccess } = map[fname]; | ||
const autoHandleSuccess = !Boolean(doNotHandleSuccess); | ||
const { middlewares } = map[fname]; | ||
@@ -53,18 +54,18 @@ path = path || `/${fname}`; | ||
for (const _m of method) { | ||
const mw = [...this.allRoutesMiddlewares, ...(middlewares || [])]; | ||
for (let i = 0; i < mw.length; i += 1) { | ||
router[_m](path, mw[i]); | ||
} | ||
router[_m](path, async (ctx, _next) => { | ||
try { | ||
const out = await this[fname || 'index'](ctx); | ||
const res = ctx.response; | ||
if (!res.doNotHandleSuccess) { | ||
await this.handleSuccess(ctx, out); | ||
const allMiddlewares = [ | ||
...this.allRoutesMiddlewares, | ||
...(middlewares || []), | ||
async (ctx, _next) => { | ||
try { | ||
const out = await this[fname || 'index'](ctx); | ||
if (autoHandleSuccess) { | ||
await this.handleSuccess(ctx, out); | ||
} | ||
} | ||
} | ||
catch (error) { | ||
ctx.throw(error); | ||
} | ||
}); | ||
catch (error) { | ||
ctx.throw(error); | ||
} | ||
}, | ||
]; | ||
router[_m](path, ...allMiddlewares); | ||
} | ||
@@ -89,2 +90,3 @@ } | ||
setDoNotHandleSuccess(context) { | ||
; | ||
context.response.doNotHandleSuccess = true; | ||
@@ -91,0 +93,0 @@ } |
@@ -0,2 +1,3 @@ | ||
export * from './decorators'; | ||
export * from './base.controller'; | ||
export * from './decorators/route'; | ||
export * from './swagger.controller'; |
@@ -17,4 +17,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./decorators"), exports); | ||
__exportStar(require("./base.controller"), exports); | ||
__exportStar(require("./decorators/route"), exports); | ||
__exportStar(require("./swagger.controller"), exports); | ||
//# sourceMappingURL=index.js.map |
export * from './withJson'; | ||
export * from './withDocument'; |
@@ -18,2 +18,3 @@ "use strict"; | ||
__exportStar(require("./withJson"), exports); | ||
__exportStar(require("./withDocument"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -6,5 +6,18 @@ import type { KobpServiceContext, KobpServiceState } from '../context'; | ||
export interface RouteMapMeta { | ||
/** | ||
* The HTTP Method supported in this function | ||
*/ | ||
method: HttpMethod | HttpMethod[]; | ||
/** | ||
* the route string | ||
*/ | ||
path?: string; | ||
/** | ||
* All middlewares attached to this path | ||
*/ | ||
middlewares?: Middleware[]; | ||
/** | ||
* Instruct the system NOT to handle success result as JSON payload | ||
*/ | ||
doNotHandleSuccess?: boolean; | ||
} | ||
@@ -11,0 +24,0 @@ export interface RouteMap { |
@@ -10,3 +10,3 @@ import Router from 'koa-router'; | ||
return { | ||
...(this.__drm || {}), /* will be injected from decorators package */ | ||
...(this.__drm || {}) /* will be injected from decorators package */, | ||
}; | ||
@@ -18,3 +18,3 @@ } | ||
success: true, | ||
data | ||
data, | ||
}; | ||
@@ -38,3 +38,4 @@ } | ||
for (const fname in map) { | ||
let { method, path } = map[fname]; | ||
let { method, path, doNotHandleSuccess } = map[fname]; | ||
const autoHandleSuccess = !Boolean(doNotHandleSuccess); | ||
const { middlewares } = map[fname]; | ||
@@ -46,18 +47,18 @@ path = path || `/${fname}`; | ||
for (const _m of method) { | ||
const mw = [...this.allRoutesMiddlewares, ...(middlewares || [])]; | ||
for (let i = 0; i < mw.length; i += 1) { | ||
router[_m](path, mw[i]); | ||
} | ||
router[_m](path, async (ctx, _next) => { | ||
try { | ||
const out = await this[fname || 'index'](ctx); | ||
const res = ctx.response; | ||
if (!res.doNotHandleSuccess) { | ||
await this.handleSuccess(ctx, out); | ||
const allMiddlewares = [ | ||
...this.allRoutesMiddlewares, | ||
...(middlewares || []), | ||
async (ctx, _next) => { | ||
try { | ||
const out = await this[fname || 'index'](ctx); | ||
if (autoHandleSuccess) { | ||
await this.handleSuccess(ctx, out); | ||
} | ||
} | ||
} | ||
catch (error) { | ||
ctx.throw(error); | ||
} | ||
}); | ||
catch (error) { | ||
ctx.throw(error); | ||
} | ||
}, | ||
]; | ||
router[_m](path, ...allMiddlewares); | ||
} | ||
@@ -82,2 +83,3 @@ } | ||
setDoNotHandleSuccess(context) { | ||
; | ||
context.response.doNotHandleSuccess = true; | ||
@@ -84,0 +86,0 @@ } |
@@ -0,2 +1,3 @@ | ||
export * from './decorators'; | ||
export * from './base.controller'; | ||
export * from './decorators/route'; | ||
export * from './swagger.controller'; |
@@ -0,3 +1,4 @@ | ||
export * from './decorators'; | ||
export * from './base.controller'; | ||
export * from './decorators/route'; | ||
export * from './swagger.controller'; | ||
//# sourceMappingURL=index.js.map |
export * from './withJson'; | ||
export * from './withDocument'; |
export * from './withJson'; | ||
export * from './withDocument'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "kobp", | ||
"version": "2.0.20", | ||
"version": "3.0.0", | ||
"description": "Koa Boilerplate with MikroORM", | ||
@@ -30,4 +30,10 @@ "main": "lib/cjs/src/index.js", | ||
"koa-router": "^9.4.0", | ||
"lodash": "^4.17.21" | ||
"lodash": "^4.17.21", | ||
"openapi3-ts": "^4.2.2" | ||
}, | ||
"peerDependenciesMeta": { | ||
"openapi3-ts": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
@@ -34,0 +40,0 @@ "@types/koa": "^2.11.6", |
@@ -105,3 +105,3 @@ # Kobp | ||
app.listen(9000, '0.0.0.0') | ||
app.listen(9005, '0.0.0.0') | ||
} | ||
@@ -115,7 +115,7 @@ | ||
```bash | ||
curl http://localhost:9000/hello/ | ||
curl http://localhost:9005/hello/ | ||
# OR | ||
curl -XPOST http://localhost:9000/hello/echo -H 'content-type: application/json' -d '{"some":"key","json":"value"}' | ||
curl -XPOST http://localhost:9005/hello/echo -H 'content-type: application/json' -d '{"some":"key","json":"value"}' | ||
``` | ||
@@ -147,2 +147,31 @@ | ||
## Using Swagger | ||
Make sure you have this only dependencies | ||
```bash | ||
npm install openapi3-ts | ||
``` | ||
Here is the example to use it. | ||
```ts | ||
import type { KobpServiceContext } from 'kobp' | ||
import { Route, BaseRoutedController } from 'kobp' | ||
export class HelloController extends BaseRoutedController { | ||
@Route('post', '/echo', withDocument({ tags: ['hello'], description: 'run migration script' })) | ||
async migrate(context: KobpServiceContext) { | ||
return context.request.body | ||
} | ||
@Route() | ||
async index(context: KobpServiceContext) { | ||
return { | ||
hello: 'world' | ||
} | ||
} | ||
} | ||
``` | ||
Note that most of the time AWS's Lambda doesn't support return the Response with Binary content. To make it so please make sure you enabled `binary` mode as per example above. | ||
@@ -165,3 +194,4 @@ | ||
[/] Lambda Handler | ||
[/] Swagger Support | ||
[ ] SNS/SQS Handler | ||
``` |
@@ -5,8 +5,21 @@ import type { KobpServiceContext, KobpServiceState } from '../context' | ||
export type HttpMethod = 'post'|'get'|'delete'|'put'|'patch' | ||
export type HttpMethod = 'post' | 'get' | 'delete' | 'put' | 'patch' | ||
export interface RouteMapMeta { | ||
/** | ||
* The HTTP Method supported in this function | ||
*/ | ||
method: HttpMethod | HttpMethod[] | ||
/** | ||
* the route string | ||
*/ | ||
path?: string | ||
/** | ||
* All middlewares attached to this path | ||
*/ | ||
middlewares?: Middleware[] | ||
/** | ||
* Instruct the system NOT to handle success result as JSON payload | ||
*/ | ||
doNotHandleSuccess?: boolean | ||
} | ||
@@ -18,13 +31,10 @@ | ||
export class KobpRouter extends Router<KobpServiceState, KobpServiceContext> { | ||
} | ||
export class KobpRouter extends Router<KobpServiceState, KobpServiceContext> {} | ||
export class BaseRoutedController { | ||
constructor(protected allRoutesMiddlewares: Middleware[] = []) {} | ||
constructor(protected allRoutesMiddlewares: Middleware[] = []) { | ||
} | ||
getRouteMaps(): RouteMap { | ||
return { | ||
...((<any>this).__drm || {}), /* will be injected from decorators package */ | ||
...((<any>this).__drm || {}) /* will be injected from decorators package */, | ||
} | ||
@@ -37,3 +47,3 @@ } | ||
success: true, | ||
data | ||
data, | ||
} | ||
@@ -44,11 +54,15 @@ } | ||
* Counter path of getRouter(). Use this method to register the controller to given router. | ||
* | ||
* @param path | ||
* @param koaRouter | ||
* | ||
* @param path | ||
* @param koaRouter | ||
*/ | ||
public register(path: string, koaRouter: KobpRouter, ...middlewares: Router.IMiddleware<KobpServiceState, KobpServiceContext>[]) { | ||
public register( | ||
path: string, | ||
koaRouter: KobpRouter, | ||
...middlewares: Router.IMiddleware<KobpServiceState, KobpServiceContext>[] | ||
) { | ||
const r = this.getRouter() | ||
// clean the path - path may ended with extra slashes that we don't need. | ||
const cleanPath = path.trim().replace(/\/*$/, '') | ||
koaRouter.use(cleanPath , ...middlewares, r.routes(), r.allowedMethods()) | ||
koaRouter.use(cleanPath, ...middlewares, r.routes(), r.allowedMethods()) | ||
} | ||
@@ -59,4 +73,5 @@ | ||
const map = this.getRouteMaps() | ||
for(const fname in map) { | ||
let { method, path } = map[fname] | ||
for (const fname in map) { | ||
let { method, path, doNotHandleSuccess } = map[fname] | ||
const autoHandleSuccess = !Boolean(doNotHandleSuccess) | ||
const { middlewares } = map[fname] | ||
@@ -67,18 +82,18 @@ path = path || `/${fname}` | ||
} | ||
for(const _m of method) { | ||
const mw = [...this.allRoutesMiddlewares, ...(middlewares || [])] | ||
for(let i = 0; i < mw.length; i += 1) { | ||
router[_m](path, mw[i]) | ||
} | ||
router[_m](path, async (ctx, _next): Promise<void> => { | ||
try { | ||
const out = await this[fname || 'index'](ctx) | ||
const res = ctx.response | ||
if (!(res as any).doNotHandleSuccess) { | ||
await this.handleSuccess(ctx, out) | ||
for (const _m of method) { | ||
const allMiddlewares = [ | ||
...this.allRoutesMiddlewares, | ||
...(middlewares || []), | ||
async (ctx, _next): Promise<void> => { | ||
try { | ||
const out = await this[fname || 'index'](ctx) | ||
if (autoHandleSuccess) { | ||
await this.handleSuccess(ctx, out) | ||
} | ||
} catch (error) { | ||
ctx.throw(error) | ||
} | ||
} catch(error) { | ||
ctx.throw(error) | ||
} | ||
}) | ||
}, | ||
] | ||
router[_m](path, ...allMiddlewares) | ||
} | ||
@@ -104,3 +119,3 @@ } | ||
protected setDoNotHandleSuccess(context: KobpServiceContext | Context) { | ||
(context.response as any).doNotHandleSuccess = true | ||
;(context.response as any).doNotHandleSuccess = true | ||
} | ||
@@ -107,0 +122,0 @@ |
@@ -0,2 +1,4 @@ | ||
export * from './decorators' | ||
export * from './base.controller' | ||
export * from './decorators/route' | ||
export * from './swagger.controller' |
@@ -1,1 +0,3 @@ | ||
export * from './withJson' | ||
export * from './withJson' | ||
export * from './withDocument' | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
174
4240
194
289070
5