@foal/core
Version:
Full-featured Node.js framework, with no complexity
56 lines (55 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetadata = getMetadata;
exports.getHttpMethod = getHttpMethod;
exports.getPath = getPath;
exports.join = join;
// 3p
require("reflect-metadata");
/**
* Get metadata of a class or a class method.
*
* @export
* @param {string} metadataKey - The name of the metadata.
* @param {Class} target - The class.
* @param {string} [propertyKey] - The class method name if relevant.
* @returns The metadata value.
*/
function getMetadata(metadataKey, target, propertyKey) {
if (propertyKey === undefined) {
return Reflect.getMetadata(metadataKey, target);
}
return Reflect.getMetadata(metadataKey, target.prototype, propertyKey);
}
/**
* Get the HTTP method of a controller method.
*
* @export
* @param {Class} target - The controller class.
* @param {string} [propertyKey] - The method name.
* @returns {(string|undefined)} - The HTTP method or undefined if none was defined.
*/
function getHttpMethod(target, propertyKey) {
return getMetadata('httpMethod', target, propertyKey);
}
/**
* Get the path of a controller method.
*
* @export
* @param {Class} target - The controller class.
* @param {string} [propertyKey] - The method name.
* @returns {(string|undefined)} - The path or undefined if none was defined.
*/
function getPath(target, propertyKey) {
return getMetadata('path', target, propertyKey);
}
/**
* Join several HTTP request paths together.
*
* @export
* @param {(...(string|undefined)[])} paths - The paths.
* @returns {string} The resulted path.
*/
function join(...paths) {
return paths.join('').replace(/(\/)+/g, '/');
}