UNPKG

@nodeswork/kiws

Version:

Koa-based Injectable Web Service

42 lines (40 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("reflect-metadata"); const _ = require("underscore"); const injection_1 = require("./injection"); exports.HANDLER = 'HANDLER'; const HANDLER_TAGS = ['handler', 'provider']; exports.ENDPOINT_METADATA_KEY = Symbol('kiws:endpoint'); /** * Handler class decorator will register the endpoints declared in the provider * to KoaRouter. */ function Handler(options = {}) { return (constructor) => { const endpoints = Reflect.getOwnMetadata(exports.ENDPOINT_METADATA_KEY, constructor.prototype); const injectable = injection_1.Injectable({ inputs: true, tags: _.union(HANDLER_TAGS, options.tags || []), meta: _.extend({}, options.meta, { endpoints }), }); injectable(constructor); constructor.prototype.$getEndpoints = $getEndpoints; }; } exports.Handler = Handler; function Endpoint(options) { return (target, propertyKey, descriptor) => { const endpoints = Reflect.getOwnMetadata(exports.ENDPOINT_METADATA_KEY, target) || []; endpoints.push(_.extend({}, options, { name: propertyKey, handler: target.constructor.name, })); Reflect.defineMetadata(exports.ENDPOINT_METADATA_KEY, endpoints, target); }; } exports.Endpoint = Endpoint; function $getEndpoints() { return Reflect.getOwnMetadata(exports.ENDPOINT_METADATA_KEY, this.constructor.prototype) || []; } //# sourceMappingURL=handler.js.map