@anglr/rest
Version:
Angular module representing rest services
47 lines • 1.96 kB
JavaScript
import { LOGGER } from '@anglr/common';
import { tap } from 'rxjs';
/**
* Middleware that is used for logging requests and responses
*/
export class LoggerMiddleware {
//######################### public static properties #########################
/**
* String identification of middleware
*/
static { this.id = 'LoggerMiddleware'; }
//######################### 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) {
const $this = this;
$this.ɵLogger = $this.ɵLogger ?? this.injector.get(LOGGER, null);
if (!$this.ɵLogger) {
return next(request);
}
$this.ɵLogger.verbose(`RESTClient ${methodName}: Request {{}}`, {
request: {
args,
request,
}
});
return next(request)
.pipe(tap({
next: response => {
$this.ɵLogger?.verbose(`RESTClient ${methodName}: Response {{}}`, { response });
},
error: error => {
$this.ɵLogger?.verbose(`RESTClient ${methodName}: ErrorResponse {{}}`, { error });
}
}));
}
}
//# sourceMappingURL=logger.middleware.js.map