hors
Version:
Node.js API framework
26 lines (25 loc) • 986 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const metadata_1 = require("../metadata");
exports.apiEndpoint = (settings) => {
return (target) => {
const { method, url, public: isPublic } = settings;
const middleware = settings.middleware || [];
const endpoints = Reflect.getMetadata(metadata_1.ENDPOINTS_METADATA_SYMBOL, Reflect) || [];
// Protect against duplicate endpoints
endpoints.forEach(endpoint => {
if (endpoint.method === method && endpoint.url === url) {
throw new Error(`Duplicate endpoint registered (${method.toLocaleUpperCase()} ${url})`);
}
});
endpoints.push({
url,
method,
target,
middleware,
public: isPublic
});
Reflect.defineMetadata(metadata_1.ENDPOINTS_METADATA_SYMBOL, endpoints, Reflect);
};
};