@opra/common
Version:
Opra common package
143 lines (142 loc) • 4.58 kB
JavaScript
import { merge, omit } from '@jsopen/objects';
import { OpraSchema } from '../../schema/index.js';
import { HTTP_CONTROLLER_METADATA } from '../constants.js';
const CLASS_NAME_PATTERN = /^(.*)(Controller)$/;
export 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(HTTP_CONTROLLER_METADATA, Object.getPrototypeOf(target));
if (baseMetadata)
merge(metadata, baseMetadata, { deep: true });
const oldMetadata = Reflect.getOwnMetadata(HTTP_CONTROLLER_METADATA, target);
if (oldMetadata)
merge(metadata, oldMetadata, { deep: true });
merge(metadata, {
kind: OpraSchema.HttpController.Kind,
name,
path: name,
...omit(options, [
'kind',
'name',
'instance',
'endpoints',
'key',
]),
}, { deep: true });
Reflect.defineMetadata(HTTP_CONTROLLER_METADATA, metadata, target);
for (const fn of decoratorChain)
fn(metadata, target);
Reflect.defineMetadata(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;
}