@ark7/router
Version:
Ark7 Router layer framework.
1 lines • 3.05 kB
Source Map (JSON)
{"version":3,"sources":["../src/handlers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,YAAY,CAAC;AAErC,OAAO,EAIL,MAAM,EACP,MAAM,gBAAgB,CAAC;AAGxB;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;CACxD;AAED,wBAAgB,OAAO,CACrB,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,GAAG,eAAe,CAoCrC;AAED,wBAAgB,GAAG,CACjB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,CAOnB;AAED,wBAAgB,IAAI,CAClB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,CAOnB;AAED,wBAAgB,GAAG,CACjB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,CAOnB;AAED,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,CAOnB","file":"handlers.d.ts","sourcesContent":["import * as _ from 'underscore';\nimport * as Router from 'koa-router';\n\nimport {\n A7ControllerMetadata,\n A7ControllerSubsidiaryType,\n METADATA_KEY,\n Method,\n} from './declarations';\nimport { Middleware } from './decorators';\n\n/**\n * Options to define a handler.\n */\nexport interface HandlerOptions {\n method?: Method;\n path?: string;\n name?: string;\n middleware?: Router.IMiddleware | Router.IMiddleware[];\n}\n\nexport function Handler(\n options: HandlerOptions = {},\n): PropertyDecorator & MethodDecorator {\n const m = Middleware([]) as MethodDecorator;\n\n return (\n target: any,\n propertyKey: string,\n descriptor?: TypedPropertyDescriptor<any>,\n ) => {\n const desc = m(target, propertyKey, descriptor);\n\n const cls = target.constructor;\n const meta = A7ControllerMetadata.getMetadata(cls);\n const subsidiary = meta.getOrCreateSubsidiary(propertyKey, '/');\n\n subsidiary.type = A7ControllerSubsidiaryType.HANDLER;\n\n if (options.middleware) {\n subsidiary.pushMiddlewaresInFront(_.flatten([options.middleware]));\n }\n\n if (options.method) {\n subsidiary.method = options.method;\n }\n\n if (options.path) {\n subsidiary.path = options.path;\n }\n\n if (options.name) {\n subsidiary.name = options.name;\n }\n\n Reflect.defineMetadata(METADATA_KEY, meta, cls);\n\n return desc;\n };\n}\n\nexport function Get(\n path: string,\n options: HandlerOptions = {},\n): PropertyDecorator {\n return Handler(\n _.extend({}, options, {\n path,\n method: Method.GET,\n }),\n );\n}\n\nexport function Post(\n path: string,\n options: HandlerOptions = {},\n): PropertyDecorator {\n return Handler(\n _.extend({}, options, {\n path,\n method: Method.POST,\n }),\n );\n}\n\nexport function Put(\n path: string,\n options: HandlerOptions = {},\n): PropertyDecorator {\n return Handler(\n _.extend({}, options, {\n path,\n method: Method.PUT,\n }),\n );\n}\n\nexport function Delete(\n path: string,\n options: HandlerOptions = {},\n): PropertyDecorator {\n return Handler(\n _.extend({}, options, {\n path,\n method: Method.DELETE,\n }),\n );\n}\n"],"sourceRoot":"/Users/yz/workspace/router/src"}