Skip to content

Commit cd6fe50

Browse files
authored
feat(interceptor): pass in service options to methods of abstract parser (#1844)
also ensures that all implementations of `AbstractInterceptorService` matches the new interface
2 parents 1f1b8ef + 03b36ee commit cd6fe50

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

.changeset/cool-phones-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ogma/nestjs-module': patch
3+
---
4+
5+
pass in service options to methods of abstract parser

packages/nestjs-module/src/interceptor/providers/abstract-interceptor.service.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export abstract class AbstractInterceptorService implements InterceptorService {
2828
const stringifiedData = data ? JSON.stringify(data) : '';
2929
const dataLength = Buffer.from(stringifiedData).byteLength;
3030
return {
31-
callerAddress: this.getCallerIp(context),
32-
method: this.getMethod(context),
33-
callPoint: this.getCallPoint(context),
34-
responseTime: this.getResponseTime(startTime),
31+
callerAddress: this.getCallerIp(context, options),
32+
method: this.getMethod(context, options),
33+
callPoint: this.getCallPoint(context, options),
34+
responseTime: this.getResponseTime(startTime, options),
3535
contentLength: dataLength,
36-
protocol: this.getProtocol(context),
37-
status: this.getStatus(context, options.color && !options.json),
38-
meta: this.getMeta(context, data),
36+
protocol: this.getProtocol(context, options),
37+
status: this.getStatus(context, options.color && !options.json, undefined, options),
38+
meta: this.getMeta(context, data, options),
3939
};
4040
}
4141

@@ -55,14 +55,14 @@ export abstract class AbstractInterceptorService implements InterceptorService {
5555
options: OgmaInterceptorServiceOptions,
5656
): MetaLogObject {
5757
return {
58-
callerAddress: this.getCallerIp(context),
59-
method: this.getMethod(context),
60-
callPoint: this.getCallPoint(context),
61-
status: this.getStatus(context, options.color && !options.json, error),
62-
responseTime: this.getResponseTime(startTime),
58+
callerAddress: this.getCallerIp(context, options),
59+
method: this.getMethod(context, options),
60+
callPoint: this.getCallPoint(context, options),
61+
status: this.getStatus(context, options.color && !options.json, error, options),
62+
responseTime: this.getResponseTime(startTime, options),
6363
contentLength: Buffer.from(JSON.stringify(error.message)).byteLength,
64-
protocol: this.getProtocol(context),
65-
meta: this.getMeta(context, error),
64+
protocol: this.getProtocol(context, options),
65+
meta: this.getMeta(context, error, options),
6666
};
6767
}
6868

@@ -71,28 +71,44 @@ export abstract class AbstractInterceptorService implements InterceptorService {
7171
* @param _context the execution context
7272
* @param inColor if the status should be in color
7373
* @param error if it was an error
74+
* @param options the options passed to the interceptor
7475
* @returns a string representing the status
7576
*/
76-
getStatus(_context: ArgumentsHost, inColor: boolean, error?: HttpException | Error): string {
77+
getStatus(
78+
_context: ArgumentsHost,
79+
inColor: boolean,
80+
error?: HttpException | Error,
81+
_options?: OgmaInterceptorServiceOptions,
82+
): string {
7783
const status = error ? 500 : 200;
84+
7885
return inColor ? this.wrapInColor(status) : status.toString();
7986
}
8087

8188
/**
8289
* A helper method to allow devs the ability to pass in extra metadata when it comes to the interceptor
8390
* @param _context The ArgumentsHost
8491
* @param _data the response body or the error being returned
92+
* @param _options the options passed to the interceptor
8593
* @returns whatever metadata you want to add in on a second log line. This can be a string, an object, anything
8694
*/
87-
getMeta(_context: ArgumentsHost, _data: unknown): unknown {
95+
getMeta(
96+
_context: ArgumentsHost,
97+
_data: unknown,
98+
_options?: OgmaInterceptorServiceOptions,
99+
): unknown {
88100
return;
89101
}
90102

91103
/**
92104
* A helper method to get the Ip of the calling client
93105
* @param context the execution context
106+
* @param options the options passed to the interceptor
94107
*/
95-
abstract getCallerIp(context: ArgumentsHost): string[] | string;
108+
abstract getCallerIp(
109+
context: ArgumentsHost,
110+
options?: OgmaInterceptorServiceOptions,
111+
): string[] | string;
96112

97113
/**
98114
* A helper method to get the method type of the request
@@ -106,18 +122,20 @@ export abstract class AbstractInterceptorService implements InterceptorService {
106122
* Websockets: unknown at moment
107123
*
108124
* @param context the execution context
125+
* @param options the options passed to the interceptor
109126
*/
110-
abstract getMethod(context: ArgumentsHost): string;
127+
abstract getMethod(context: ArgumentsHost, options?: OgmaInterceptorServiceOptions): string;
111128

112-
private getResponseTime(startTime: number): number {
129+
private getResponseTime(startTime: number, _options?: OgmaInterceptorServiceOptions): number {
113130
return Date.now() - startTime;
114131
}
115132

116133
/**
117134
* A helper method to get the protocol of the request
118135
* @param context execution context from Nest
136+
* @param options the options passed to the interceptor
119137
*/
120-
abstract getProtocol(context: ArgumentsHost): string;
138+
abstract getProtocol(context: ArgumentsHost, options?: OgmaInterceptorServiceOptions): string;
121139

122140
/**
123141
* A helper method to get what was called
@@ -130,8 +148,9 @@ export abstract class AbstractInterceptorService implements InterceptorService {
130148
*
131149
* WebSockets: Subscription Event name
132150
* @param context execution context from Nest
151+
* @param options the options passed to the interceptor
133152
*/
134-
abstract getCallPoint(context: ArgumentsHost): string;
153+
abstract getCallPoint(context: ArgumentsHost, options?: OgmaInterceptorServiceOptions): string;
135154

136155
/**
137156
* A helper method for setting the correlationId to later be retrieved when logging

packages/nestjs-module/src/interceptor/providers/http-interceptor.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { ArgumentsHost, ExecutionContext, HttpException, Injectable } from '@nestjs/common';
22
import { HTTP_CODE_METADATA } from '@nestjs/common/constants';
33

4+
import { OgmaInterceptorServiceOptions } from '../../interfaces';
45
import { AbstractInterceptorService } from './abstract-interceptor.service';
56

67
@Injectable()
78
export abstract class HttpInterceptorService extends AbstractInterceptorService {
8-
getStatus(context: ExecutionContext, inColor: boolean, error?: HttpException | Error): string {
9+
getStatus(
10+
context: ExecutionContext,
11+
inColor: boolean,
12+
error?: HttpException | Error,
13+
_options?: OgmaInterceptorServiceOptions,
14+
): string {
915
let status: number;
1016
const res = this.getResponse(context);
1117
status = res.statusCode;

0 commit comments

Comments
 (0)