11import { logger , logSchema , redactQueryParamFromRequest } from '@internal/monitoring'
2+ import { RouteGenericInterface } from 'fastify'
23import { FastifyReply } from 'fastify/types/reply'
34import { FastifyRequest } from 'fastify/types/request'
45import fastifyPlugin from 'fastify-plugin'
@@ -7,6 +8,11 @@ interface RequestLoggerOptions {
78 excludeUrls ?: string [ ]
89}
910
11+ type RawRequestMetadata = FastifyRequest [ 'raw' ] & {
12+ executionError ?: Error
13+ resources ?: string [ ]
14+ }
15+
1016declare module 'fastify' {
1117 interface FastifyRequest {
1218 executionError ?: Error
@@ -18,8 +24,8 @@ declare module 'fastify' {
1824
1925 interface FastifyContextConfig {
2026 operation ?: { type : string }
21- resources ?: ( req : FastifyRequest < any > ) => string [ ]
22- logMetadata ?: ( req : FastifyRequest < any > ) => Record < string , unknown >
27+ resources ?( req : FastifyRequest < RouteGenericInterface > ) : string [ ]
28+ logMetadata ?( req : FastifyRequest < RouteGenericInterface > ) : Record < string , unknown >
2329 }
2430}
2531
@@ -57,39 +63,13 @@ export const logRequest = (options: RequestLoggerOptions) =>
5763 * Adds req.resources and req.operation to the request object
5864 */
5965 fastify . addHook ( 'preHandler' , async ( req ) => {
60- let resources = req . resources
61-
62- if ( resources === undefined ) {
63- resources = req . routeOptions . config . resources ?.( req )
64- }
65-
66- if ( resources === undefined ) {
67- resources = ( req . raw as any ) . resources
68- }
69-
70- if ( resources === undefined ) {
71- const params = req . params as Record < string , unknown > | undefined
72- let resourceFromParams = ''
73-
74- if ( params ) {
75- let first = true
76- for ( const key in params ) {
77- if ( ! Object . prototype . hasOwnProperty . call ( params , key ) ) {
78- continue
79- }
80-
81- if ( ! first ) {
82- resourceFromParams += '/'
83- }
84-
85- const value = params [ key ]
86- resourceFromParams += value == null ? '' : String ( value )
87- first = false
88- }
89- }
90-
91- resources = resourceFromParams ? [ resourceFromParams ] : [ ]
92- }
66+ const resourceFromParams = getResourceFromParams ( req . params )
67+ const resources = getFirstDefined < string [ ] > (
68+ req . resources ,
69+ req . routeOptions . config . resources ?.( req ) ,
70+ getRawRequest ( req ) . resources ,
71+ resourceFromParams ? [ resourceFromParams ] : [ ]
72+ )
9373
9474 if ( resources && resources . length > 0 ) {
9575 for ( let index = 0 ; index < resources . length ; index ++ ) {
@@ -150,7 +130,7 @@ function doRequestLog(req: FastifyRequest, options: LogRequestOptions) {
150130 const rId = req . id
151131 const cIP = req . ip
152132 const statusCode = options . statusCode
153- const error = ( req . raw as any ) . executionError || req . executionError
133+ const error = getRawRequest ( req ) . executionError || req . executionError
154134 const tenantId = req . tenantId
155135
156136 let reqMetadata : Record < string , unknown > = { }
@@ -197,3 +177,41 @@ function doRequestLog(req: FastifyRequest, options: LogRequestOptions) {
197177 serverTimes : req . serverTimings ,
198178 } )
199179}
180+
181+ function getRawRequest ( req : FastifyRequest ) : RawRequestMetadata {
182+ return req . raw as RawRequestMetadata
183+ }
184+
185+ function getResourceFromParams ( params : unknown ) : string {
186+ if ( ! params || typeof params !== 'object' ) {
187+ return ''
188+ }
189+
190+ let resource = ''
191+ let first = true
192+
193+ for ( const key in params ) {
194+ if ( ! Object . prototype . hasOwnProperty . call ( params , key ) ) {
195+ continue
196+ }
197+
198+ if ( ! first ) {
199+ resource += '/'
200+ }
201+
202+ const value = ( params as Record < string , unknown > ) [ key ]
203+ resource += value == null ? '' : String ( value )
204+ first = false
205+ }
206+
207+ return resource
208+ }
209+
210+ function getFirstDefined < T > ( ...values : ( T | undefined ) [ ] ) : T | undefined {
211+ for ( const value of values ) {
212+ if ( value !== undefined ) {
213+ return value
214+ }
215+ }
216+ return undefined
217+ }
0 commit comments