@opra/common
Version:
Opra common package
146 lines (145 loc) • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpControllerDecoratorFactory = HttpControllerDecoratorFactory;
const objects_1 = require("@jsopen/objects");
const index_js_1 = require("../../schema/index.js");
const constants_js_1 = require("../constants.js");
const CLASS_NAME_PATTERN = /^(.*)(Controller)$/;
function HttpControllerDecoratorFactory(options) {
const decoratorChain = [];
/**
*
*/
const decorator = function (target) {
let name = options?.name;
if (!name)
name = CLASS_NAME_PATTERN.exec(target.name)?.[1] || target.name;
const metadata = {};
const baseMetadata = Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, Object.getPrototypeOf(target));
if (baseMetadata)
(0, objects_1.merge)(metadata, baseMetadata, { deep: true });
const oldMetadata = Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target);
if (oldMetadata)
(0, objects_1.merge)(metadata, oldMetadata, { deep: true });
(0, objects_1.merge)(metadata, {
kind: index_js_1.OpraSchema.HttpController.Kind,
name,
path: name,
...(0, objects_1.omit)(options, [
'kind',
'name',
'instance',
'endpoints',
'key',
]),
}, { deep: true });
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, metadata, target);
for (const fn of decoratorChain)
fn(metadata, target);
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, metadata, target);
};
/**
*
*/
decorator.Cookie = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'cookie',
type: arg1,
}
: { ...arg1, name, location: 'cookie' };
meta.parameters = meta.parameters || [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.Header = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'header',
type: arg1,
}
: { ...arg1, name, location: 'header' };
meta.parameters = meta.parameters || [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.QueryParam = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'query',
type: arg1,
}
: { ...arg1, name, location: 'query' };
meta.parameters = meta.parameters || [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.PathParam = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'path',
type: arg1,
}
: { ...arg1, name, location: 'path' };
meta.parameters = meta.parameters || [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.KeyParam = (name, arg1) => {
decoratorChain.push((meta) => {
if (!meta.path?.includes(':' + name))
meta.path = (meta.path || '') + '@:' + name;
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'path',
type: arg1,
keyParam: true,
}
: {
...arg1,
name,
location: 'path',
keyParam: true,
};
meta.parameters = meta.parameters || [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.UseType = (...type) => {
decoratorChain.push((meta) => {
meta.types = meta.types || [];
meta.types.push(...type);
});
return decorator;
};
return decorator;
}