@anglr/rest
Version:
Angular module representing rest services
47 lines • 2.07 kB
JavaScript
import { Observable } from 'rxjs';
/**
* Middleware that is used for adding body to request
*/
export class BodyParameterMiddleware {
//######################### public static properties #########################
/**
* String identification of middleware
*/
static { this.id = 'BodyParameterMiddleware'; }
//######################### public methods - implementation of RestMiddleware #########################
/**
* Runs code that is defined for this rest middleware, in this method you can modify request and response
* @param this - Method is bound to RESTClient
* @param id - Unique id that identifies request method
* @param target - Prototype of class that are decorators applied to
* @param methodName - Name of method that is being modified
* @param descriptor - Descriptor of method that is being modified
* @param args - Array of arguments passed to called method
* @param request - Http request that you can modify
* @param next - Used for calling next middleware with modified request
*/
run(_id, target, methodName, _descriptor, args, request, next) {
return new Observable(observer => {
(async () => {
const parameters = target.parameters;
let pBody;
let pTransforms;
if (parameters) {
pBody = parameters[methodName]?.body;
pTransforms = parameters[methodName]?.transforms;
}
if (pBody) {
let body = args[pBody[0].parameterIndex];
if (pTransforms && pTransforms[pBody[0].parameterIndex]) {
body = await pTransforms[pBody[0].parameterIndex].bind(this)(body, ...args);
}
request = request.clone({
body
});
}
next(request).subscribe(observer);
})();
});
}
}
//# sourceMappingURL=bodyParameter.middleware.js.map