@@ -2,7 +2,7 @@ import fetch from "sync-fetch";
2
2
import { PubSub , Topic } from "@google-cloud/pubsub" ;
3
3
import { hrtime } from "node:process" ;
4
4
import { v4 as uuidv4 } from "uuid" ;
5
- import axios , { AxiosInstance , AxiosStatic } from "axios" ;
5
+ import { AxiosInstance , AxiosStatic } from "axios" ;
6
6
import { FastifyInstance } from "fastify" ;
7
7
import {
8
8
buildPayload ,
@@ -55,9 +55,9 @@ type Payload = {
55
55
} ;
56
56
57
57
class APIToolkit {
58
- #topic: string ;
59
- #pubsub: PubSub ;
60
- #project_id: string ;
58
+ #topic: string | undefined ;
59
+ #pubsub: PubSub | undefined ;
60
+ #project_id: string | undefined ;
61
61
#redactHeaders: string [ ] ;
62
62
#redactRequestBody: string [ ] ;
63
63
#redactResponseBody: string [ ] ;
@@ -69,9 +69,9 @@ class APIToolkit {
69
69
#axios: AxiosInstance | undefined ;
70
70
71
71
constructor (
72
- pubsub : PubSub ,
73
- topic : string ,
74
- project_id : string ,
72
+ pubsub : PubSub | undefined ,
73
+ topic : string | undefined ,
74
+ project_id : string | undefined ,
75
75
fastify : FastifyInstance ,
76
76
redactHeaders : string [ ] ,
77
77
redactReqBody : string [ ] ,
@@ -114,10 +114,26 @@ class APIToolkit {
114
114
Accept : "application/json" ,
115
115
} ,
116
116
} ) ;
117
- if ( ! resp . ok )
118
- throw new Error (
119
- `Error getting apitoolkit client_metadata ${ resp . status } `
117
+ if ( ! resp . ok ) {
118
+ if ( resp . status === 401 ) {
119
+ throw new Error ( "APIToolkit: Invalid API Key" ) ;
120
+ } else {
121
+ console . log ( `Error getting apitoolkit client_metadata ${ resp . status } ` ) ;
122
+ }
123
+ return new APIToolkit (
124
+ undefined ,
125
+ undefined ,
126
+ undefined ,
127
+ fastify ,
128
+ redactHeaders ,
129
+ redactRequestBody ,
130
+ redactResponseBody ,
131
+ service_version ,
132
+ tags ,
133
+ debug ,
134
+ monitorAxios
120
135
) ;
136
+ }
121
137
122
138
const clientMetadata = resp . json ( ) as ClientMetadata ;
123
139
const {
@@ -176,7 +192,9 @@ class APIToolkit {
176
192
console . log ( "apitoolkit: publishing message" ) ;
177
193
console . log ( payload ) ;
178
194
}
179
- this . #pubsub. topic ( this . #topic) . publishMessage ( { json : payload } ) ;
195
+ if ( this . #pubsub && this . #topic) {
196
+ this . #pubsub. topic ( this . #topic) . publishMessage ( { json : payload } ) ;
197
+ }
180
198
}
181
199
182
200
public getConfig ( ) {
@@ -253,6 +271,9 @@ class APIToolkit {
253
271
if ( this . #debug) {
254
272
console . log ( "apitoolkit: onSend hook called" ) ;
255
273
}
274
+ if ( ! this . #project_id) {
275
+ return data ;
276
+ }
256
277
257
278
try {
258
279
const reqBody = this . getStringValue ( request . body ) ;
0 commit comments