UNPKG

@cashfarm/tractor

Version:

A Hapi server with superpowers

52 lines 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const debug = require('debug')('tractor:ioc'); exports.EndpointMetadataKey = Symbol.for('tractor:controller:endpoints'); /** * The endpoint metadata class * * When an endpoint is declared in Tractor using the `@Endpoint` decorator, * an instance of EndpointMetadata will be created to hold the metadata * and stored using `Reflect.defineMetada` in an array of endpoints referenced * by the metadata key `'tractor:controller:endpoint'`. */ class EndpointMetadata { constructor(method, path, { vhost, description, notes, tags, validate, response, auth } = {}) { this.method = method; this.path = path; this.description = description; this.notes = notes; this.tags = tags; this.validate = validate; this.response = response; this.vhost = vhost; this.auth = auth; } } exports.EndpointMetadata = EndpointMetadata; /** * The `@Endpoint` decorator * * Marks a controller method as an API endpoint * * @param method The http methods this endpoint will respond to * @param path The endpoint's path * @param options Hapi endpoint options */ function Endpoint(method, path, options) { const metadata = new EndpointMetadata(method, path, options); // when called via `new Endpoint()` or `Endpoint()` just return the decorator instance // tslint:disable-next-line:no-invalid-this if (this instanceof Endpoint) { return metadata; } return (target, propertyKey, descriptor) => { const klass = target.constructor === Function ? target : target.constructor; const meta = Reflect.getOwnMetadata(exports.EndpointMetadataKey, klass) || {}; meta[propertyKey] = metadata; debug(`Registering endpoint ${metadata.method} ${metadata.path ? metadata.path : "''"} to ${target.constructor.name}.${String(propertyKey)}`); Reflect.defineMetadata(exports.EndpointMetadataKey, meta, target.constructor); }; } exports.Endpoint = Endpoint; //# sourceMappingURL=endpoint.js.map