@anglr/rest
Version:
Angular module representing rest services
38 lines • 1.9 kB
JavaScript
import { isBlank, isPresent, isFunction, isString } from '@jscrpt/common';
/**
* Parameter descriptor that is used for transforming parameter before QueryObject serialization
* @param methodNameOrFuncs - Name of method that will be called to modify parameter, method takes any type of object and returns transformed object, or method directly or array of methods that will be called sequentialy
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function ParameterTransform(methodNameOrFuncs) {
return function (target, propertyKey, parameterIndex) {
if (isBlank(methodNameOrFuncs)) {
methodNameOrFuncs = `${propertyKey}ParameterTransform`;
}
let paramFunctions = [];
if (isString(methodNameOrFuncs)) {
const trgt = target;
if (isPresent(trgt[methodNameOrFuncs]) && isFunction(trgt[methodNameOrFuncs])) {
paramFunctions = [trgt[methodNameOrFuncs]];
}
}
else if (isFunction(methodNameOrFuncs)) {
paramFunctions = [methodNameOrFuncs];
}
else if (Array.isArray(methodNameOrFuncs)) {
paramFunctions = methodNameOrFuncs.filter(itm => isFunction(itm));
}
if (paramFunctions.length) {
target.parameters = target.parameters ?? {};
target.parameters[propertyKey] = target.parameters[propertyKey] ?? {};
const transforms = target.parameters[propertyKey].transforms = target.parameters[propertyKey].transforms ?? {};
transforms[parameterIndex] = async function (input, ...args) {
for (let x = 0; x < paramFunctions.length; x++) {
input = await paramFunctions[x].apply(this, [input, ...args]);
}
return input;
};
}
};
}
//# sourceMappingURL=parameterTransform.decorator.js.map