UNPKG

@anglr/rest

Version:

Angular module representing rest services

39 lines 1.8 kB
import { isBlank, isPresent, isFunction, isString } from '@jscrpt/common'; import { ResponseTransformMiddleware } from '../middlewares'; /** * Defines method name that will be called and modifies response * @param methodNameOrFuncs - Name of method that will be called to modify response, method takes Observable and returns required type, or method directly or array of methods that will be called sequentialy */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function ResponseTransform(methodNameOrFuncs) { return function (target, propertyKey, descriptor) { const descr = descriptor; if (isBlank(methodNameOrFuncs)) { methodNameOrFuncs = `${propertyKey}ResponseTransform`; } let responseFunctions = []; if (isString(methodNameOrFuncs)) { const trgt = target; if (isPresent(trgt[methodNameOrFuncs]) && isFunction(trgt[methodNameOrFuncs])) { responseFunctions = [trgt[methodNameOrFuncs]]; } } else if (isFunction(methodNameOrFuncs)) { responseFunctions = [methodNameOrFuncs]; } else if (Array.isArray(methodNameOrFuncs)) { responseFunctions = methodNameOrFuncs.filter(itm => isFunction(itm)); } if (responseFunctions?.length) { descr.middlewareTypes.push(ResponseTransformMiddleware); descr.responseTransform = function (observable, ...args) { for (let x = 0; x < responseFunctions.length; x++) { observable = responseFunctions[x].apply(this, [observable, ...args]); } return observable; }; } return descr; }; } //# sourceMappingURL=responseTransform.decorator.js.map