@anglr/rest
Version:
Angular module representing rest services
85 lines • 3.16 kB
JavaScript
import { HttpHeaders, HttpRequest } from '@angular/common/http';
import { generateId, isFunction } from '@jscrpt/common';
import { buildMiddlewares } from '../misc/utils';
/**
* Method builder used for building decorators for http methods
* @param method - Name of http method
*/
function methodBuilder(method) {
return function (url) {
return function (target, propertyKey, descriptor) {
const descr = descriptor;
if (isFunction(descr.value)) {
descr.originalParamsCount = descr.value.length;
}
descr.middlewareTypes = descr.middlewareTypes ?? [];
const id = `${method}-${url}-${target.constructor.name}-${propertyKey}`;
const parameters = target.parameters;
let parametersMiddlewares = [];
if (parameters) {
parametersMiddlewares = parameters[propertyKey]?.middlewareTypes ?? [];
}
descr.value = function (...args) {
//get middlewares definition only during first call
if (!descr.middlewares) {
descr.middlewares = buildMiddlewares([
...descr.middlewareTypes ?? [],
...parametersMiddlewares,
...this.methodMiddlewares
], this.middlewaresOrder);
}
const reqId = `${id}-${generateId(6)}`;
const httpRequest = new HttpRequest(method, this.baseUrl + this.getBaseUrl() + url, null, {
headers: new HttpHeaders(this.getDefaultHeaders()),
responseType: 'json',
reportProgress: false,
});
//run all middlewares
const call = (httpReq, index) => {
if (!descr.middlewares[index]) {
httpReq = this.requestInterceptor(httpReq);
let response = this.http.request(httpReq);
response = this.responseInterceptor(response);
return response;
}
else {
return descr.middlewares[index].call(this, reqId, target, propertyKey, descr, args, httpReq, request => call(request, ++index));
}
};
return call(httpRequest, 0);
};
return descr;
};
};
}
/**
* GET method
* @param url - resource url of the method
*/
export const GET = methodBuilder('GET');
/**
* POST method
* @param url - resource url of the method
*/
export const POST = methodBuilder('POST');
/**
* PUT method
* @param url - resource url of the method
*/
export const PUT = methodBuilder('PUT');
/**
* DELETE method
* @param url - resource url of the method
*/
export const DELETE = methodBuilder('DELETE');
/**
* HEAD method
* @param url - resource url of the method
*/
export const HEAD = methodBuilder('HEAD');
/**
* PATCH method
* @param url - resource url of the method
*/
export const PATCH = methodBuilder('PATCH');
//# sourceMappingURL=method.decorator.js.map