@@ -120,75 +120,88 @@ export default class APIToolkitMiddleware {
120120 return resp . json ( ) as ClientMetadata
121121 }
122122
123- public async handle ( { request, response } : HttpContext , next : NextFn ) {
124- if ( this . #config?. disable ) {
125- await next ( )
126- return
127- }
128- const ctx = HttpContext . get ( )
129- const msg_id : string = uuidv4 ( )
130- const start_time = process . hrtime . bigint ( )
131- if ( ctx ) {
132- ctx . apitoolkitData = {
133- client : this ,
134- msg_id,
135- errors : [ ] ,
136- config : this . #config,
137- project_id : this . #project_id,
123+ public middleware ( ) {
124+ const project_id = this . #project_id
125+ const config = this . #config
126+ const client = this
127+ class middleware {
128+ #project_id: string
129+ #config: APIToolkitConfig
130+ constructor ( ) {
131+ this . #project_id = project_id
132+ this . #config = config
138133 }
139- }
140- const reqBody = this . getSafeBody ( request . body ( ) )
141- await next ( )
142- if ( this . #config?. debug ) {
143- console . log ( 'APIToolkit: adonisjs middleware called' )
144- }
145- const respBody = this . getSafeBody ( response . getBody ( ) )
146- const errors = ctx ?. apitoolkitData . errors || [ ]
147- if ( this . #project_id) {
148- const payload = buildPayload ( {
149- start_time,
150- reqBody,
151- respBody,
152- requestHeaders : request . headers ( ) ,
153- responseHeaders : response . getHeaders ( ) ,
154- reqParams : request . params ( ) ,
155- status_code : response . response . statusCode ,
156- raw_url : request . url ( true ) ,
157- url_path : request . ctx ?. route ?. pattern || '' ,
158- reqQuery : request . qs ( ) ,
159- method : request . method ( ) ,
160- host : request . hostname ( ) || '' ,
161- redactHeaderLists : this . #config?. redactHeaders ?? [ ] ,
162- redactRequestBody : this . #config?. redactRequestBody ?? [ ] ,
163- redactResponseBody : this . #config?. redactResponseBody ?? [ ] ,
164- errors,
165- sdk_type : 'JsAdonis' ,
166- service_version : this . #config?. serviceVersion ,
167- tags : this . #config?. tags ?? [ ] ,
168- msg_id,
169- parent_id : undefined ,
170- project_id : this . #project_id,
171- } )
134+ public async handle ( { request, response } : HttpContext , next : NextFn ) {
135+ if ( this . #config?. disable ) {
136+ await next ( )
137+ return
138+ }
139+ const ctx = HttpContext . get ( )
140+ const msg_id : string = uuidv4 ( )
141+ const start_time = process . hrtime . bigint ( )
142+ if ( ctx ) {
143+ ctx . apitoolkitData = {
144+ client,
145+ msg_id,
146+ errors : [ ] ,
147+ config : this . #config,
148+ project_id : this . #project_id,
149+ }
150+ }
151+ const reqBody = this . getSafeBody ( request . body ( ) )
152+ await next ( )
153+ if ( this . #config?. debug ) {
154+ console . log ( 'APIToolkit: adonisjs middleware called' )
155+ }
156+ const respBody = this . getSafeBody ( response . getBody ( ) )
157+ const errors = ctx ?. apitoolkitData . errors || [ ]
158+ if ( this . #project_id) {
159+ const payload = buildPayload ( {
160+ start_time,
161+ reqBody,
162+ respBody,
163+ requestHeaders : request . headers ( ) ,
164+ responseHeaders : response . getHeaders ( ) ,
165+ reqParams : request . params ( ) ,
166+ status_code : response . response . statusCode ,
167+ raw_url : request . url ( true ) ,
168+ url_path : request . ctx ?. route ?. pattern || '' ,
169+ reqQuery : request . qs ( ) ,
170+ method : request . method ( ) ,
171+ host : request . hostname ( ) || '' ,
172+ redactHeaderLists : this . #config?. redactHeaders ?? [ ] ,
173+ redactRequestBody : this . #config?. redactRequestBody ?? [ ] ,
174+ redactResponseBody : this . #config?. redactResponseBody ?? [ ] ,
175+ errors,
176+ sdk_type : 'JsAdonis' ,
177+ service_version : this . #config?. serviceVersion ,
178+ tags : this . #config?. tags ?? [ ] ,
179+ msg_id,
180+ parent_id : undefined ,
181+ project_id : this . #project_id,
182+ } )
172183
173- if ( this . #config?. debug ) {
174- console . log ( 'APIToolkit: publish prepared payload ' )
175- console . dir ( payload )
184+ if ( this . #config?. debug ) {
185+ console . log ( 'APIToolkit: publish prepared payload ' )
186+ console . dir ( payload )
187+ }
188+ client . publishMessage ( payload )
189+ }
176190 }
177- this . publishMessage ( payload )
178- }
179- }
180-
181- getSafeBody ( rqb : any ) : string {
182- let result = ''
183- if ( typeof rqb === 'object' ) {
184- try {
185- result = JSON . stringify ( rqb )
186- } catch ( error ) {
187- result = String ( rqb )
191+ getSafeBody ( rqb : any ) : string {
192+ let result = ''
193+ if ( typeof rqb === 'object' ) {
194+ try {
195+ result = JSON . stringify ( rqb )
196+ } catch ( error ) {
197+ result = String ( rqb )
198+ }
199+ } else {
200+ result = String ( result )
201+ }
202+ return result
188203 }
189- } else {
190- result = String ( result )
191204 }
192- return result
205+ return Promise . resolve ( { default : middleware } )
193206 }
194207}
0 commit comments