@anglr/rest
Version:
Angular module representing rest services
58 lines • 2.15 kB
JavaScript
import { tap } from 'rxjs';
import { REST_MOCK_LOGGER } from '../misc/tokens';
/**
* Middleware that is used for logging responses for mock usages
*
* @example
* Change providers for `REST_METHOD_MIDDLEWARES` that way, that you add `...jsDevMode ? [MockLoggerMiddleware] : []` to `REST_METHOD_MIDDLEWARES`
*
* For example
*
* ```ts
* <ValueProvider>
* {
* provide: REST_METHOD_MIDDLEWARES,
* useValue:
* [
* LoggerMiddleware,
* ResponseTypeMiddleware,
* ReportProgressMiddleware,
* ...jsDevMode ? [MockLoggerMiddleware] : [],
* ]
* },
* ```
*/
export class MockLoggerMiddleware {
//######################### public static properties #########################
/**
* String identification of middleware
*/
static { this.id = 'MockLoggerMiddleware'; }
//######################### 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) {
if (!jsDevMode) {
return next(request);
}
else {
const $this = this;
$this.ɵMockLogger ??= this.injector.get(REST_MOCK_LOGGER, null);
if (!$this.ɵMockLogger) {
return next(request);
}
return next(request)
.pipe(tap(response => $this.ɵMockLogger?.logResponse(request, response)));
}
}
}
//# sourceMappingURL=mockLogger.middleware.js.map