@@ -55,6 +55,18 @@ var axios_1 = __importDefault(require("axios"));
5555var zod_1 = require ( "zod" ) ;
5656var logger_1 = require ( "../common/logger" ) ;
5757var errors_1 = require ( "../common/errors" ) ;
58+ /**
59+ * Detect if we're running in the Cloudflare Workers (workerd) runtime.
60+ * Workers sets navigator.userAgent to "Cloudflare-Workers" — this is the
61+ * documented and stable detection signal:
62+ * https://developers.cloudflare.com/workers/runtime-apis/web-standards/
63+ *
64+ * axios 0.27.2 has no fetch adapter and requires Node's http module, so it
65+ * crashes inside Workers. When we detect Workers, route HTTP through native
66+ * fetch instead — preserved error shape so errorHandler keeps working.
67+ */
68+ var isCloudflareWorkers = typeof navigator !== "undefined" &&
69+ navigator . userAgent === "Cloudflare-Workers" ;
5870var APIResponse = zod_1 . z . object ( { } ) . catchall ( zod_1 . z . any ( ) ) ;
5971var APIErrorResponse = zod_1 . z
6072 . object ( {
@@ -70,8 +82,119 @@ var BaseClient = /** @class */ (function () {
7082 this . apiUrl = options . apiUrl || apiUrl ;
7183 this . key = key ;
7284 this . timeout = options . timeout || 5000 ;
73- axios_1 . default . defaults . timeout = this . timeout ;
85+ if ( ! isCloudflareWorkers ) {
86+ // axios.defaults is process-global state and is meaningless in Workers
87+ // (we don't use axios there). Skip in Workers to avoid touching axios's
88+ // internal config which can drag in Node-only deps during import.
89+ axios_1 . default . defaults . timeout = this . timeout ;
90+ }
7491 }
92+ /**
93+ * Issue an HTTP request. Routes through axios in Node/Lambda environments
94+ * and native fetch in Cloudflare Workers. Both paths return / throw
95+ * axios-compatible shapes so errorHandler() and the response.data parsing
96+ * downstream work unchanged.
97+ */
98+ BaseClient . prototype . httpRequest = function ( method , url , options ) {
99+ if ( options === void 0 ) { options = { } ; }
100+ return __awaiter ( this , void 0 , void 0 , function ( ) {
101+ var response_1 , finalUrl , search , _i , _a , _b , k , v , init , hasContentType , controller , timeoutId , response , err_1 , wrapped , contentType , data , _c , text , headersObj_1 , wrapped , headersObj ;
102+ return __generator ( this , function ( _d ) {
103+ switch ( _d . label ) {
104+ case 0 :
105+ if ( ! ! isCloudflareWorkers ) return [ 3 /*break*/ , 2 ] ;
106+ return [ 4 /*yield*/ , axios_1 . default . request ( {
107+ method : method ,
108+ url : url ,
109+ params : options . params ,
110+ data : options . body ,
111+ headers : options . headers ,
112+ } ) ] ;
113+ case 1 :
114+ response_1 = _d . sent ( ) ;
115+ return [ 2 /*return*/ , { data : response_1 . data , status : response_1 . status , headers : response_1 . headers } ] ;
116+ case 2 :
117+ finalUrl = url ;
118+ if ( options . params && Object . keys ( options . params ) . length > 0 ) {
119+ search = new URLSearchParams ( ) ;
120+ for ( _i = 0 , _a = Object . entries ( options . params ) ; _i < _a . length ; _i ++ ) {
121+ _b = _a [ _i ] , k = _b [ 0 ] , v = _b [ 1 ] ;
122+ if ( v !== undefined && v !== null )
123+ search . append ( k , String ( v ) ) ;
124+ }
125+ finalUrl += ( finalUrl . includes ( "?" ) ? "&" : "?" ) + search . toString ( ) ;
126+ }
127+ init = {
128+ method : method ,
129+ headers : options . headers ,
130+ } ;
131+ if ( options . body !== undefined && method !== "GET" && method !== "DELETE" ) {
132+ init . body = typeof options . body === "string" ? options . body : JSON . stringify ( options . body ) ;
133+ hasContentType = options . headers && Object . keys ( options . headers )
134+ . some ( function ( h ) { return h . toLowerCase ( ) === "content-type" ; } ) ;
135+ if ( ! hasContentType ) {
136+ init . headers = __assign ( __assign ( { } , ( options . headers || { } ) ) , { "content-type" : "application/json" } ) ;
137+ }
138+ }
139+ controller = new AbortController ( ) ;
140+ timeoutId = setTimeout ( function ( ) { return controller . abort ( ) ; } , this . timeout ) ;
141+ init . signal = controller . signal ;
142+ _d . label = 3 ;
143+ case 3 :
144+ _d . trys . push ( [ 3 , 5 , , 6 ] ) ;
145+ return [ 4 /*yield*/ , fetch ( finalUrl , init ) ] ;
146+ case 4 :
147+ response = _d . sent ( ) ;
148+ return [ 3 /*break*/ , 6 ] ;
149+ case 5 :
150+ err_1 = _d . sent ( ) ;
151+ clearTimeout ( timeoutId ) ;
152+ wrapped = new Error ( ( err_1 === null || err_1 === void 0 ? void 0 : err_1 . message ) || "Network request failed" ) ;
153+ wrapped . request = { url : finalUrl , method : method } ;
154+ wrapped . config = { url : finalUrl , method : method } ;
155+ throw wrapped ;
156+ case 6 :
157+ clearTimeout ( timeoutId ) ;
158+ contentType = response . headers . get ( "content-type" ) || "" ;
159+ if ( ! contentType . includes ( "application/json" ) ) return [ 3 /*break*/ , 11 ] ;
160+ _d . label = 7 ;
161+ case 7 :
162+ _d . trys . push ( [ 7 , 9 , , 10 ] ) ;
163+ return [ 4 /*yield*/ , response . json ( ) ] ;
164+ case 8 :
165+ data = _d . sent ( ) ;
166+ return [ 3 /*break*/ , 10 ] ;
167+ case 9 :
168+ _c = _d . sent ( ) ;
169+ data = null ;
170+ return [ 3 /*break*/ , 10 ] ;
171+ case 10 : return [ 3 /*break*/ , 13 ] ;
172+ case 11 : return [ 4 /*yield*/ , response . text ( ) ] ;
173+ case 12 :
174+ text = _d . sent ( ) ;
175+ try {
176+ data = JSON . parse ( text ) ;
177+ }
178+ catch ( _e ) {
179+ data = text ;
180+ }
181+ _d . label = 13 ;
182+ case 13 :
183+ if ( response . status < 200 || response . status >= 300 ) {
184+ headersObj_1 = { } ;
185+ response . headers . forEach ( function ( v , k ) { headersObj_1 [ k ] = v ; } ) ;
186+ wrapped = new Error ( "Request failed with status " . concat ( response . status ) ) ;
187+ wrapped . response = { status : response . status , data : data , headers : headersObj_1 } ;
188+ wrapped . config = { url : finalUrl , method : method } ;
189+ throw wrapped ;
190+ }
191+ headersObj = { } ;
192+ response . headers . forEach ( function ( v , k ) { headersObj [ k ] = v ; } ) ;
193+ return [ 2 /*return*/ , { data : data , status : response . status , headers : headersObj } ] ;
194+ }
195+ } ) ;
196+ } ) ;
197+ } ;
75198 /**
76199 * Wraps any error into a CrowdHandlerError
77200 */
@@ -168,7 +291,7 @@ var BaseClient = /** @class */ (function () {
168291 switch ( _a . label ) {
169292 case 0 :
170293 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
171- return [ 4 /*yield*/ , axios_1 . default . delete ( this . apiUrl + path , {
294+ return [ 4 /*yield*/ , this . httpRequest ( "DELETE" , this . apiUrl + path , {
172295 headers : {
173296 "x-api-key" : this . key ,
174297 } ,
@@ -200,7 +323,7 @@ var BaseClient = /** @class */ (function () {
200323 switch ( _a . label ) {
201324 case 0 :
202325 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
203- return [ 4 /*yield*/ , axios_1 . default . get ( this . apiUrl + path , {
326+ return [ 4 /*yield*/ , this . httpRequest ( "GET" , this . apiUrl + path , {
204327 params : params ,
205328 headers : {
206329 "x-api-key" : this . key ,
@@ -234,7 +357,8 @@ var BaseClient = /** @class */ (function () {
234357 switch ( _a . label ) {
235358 case 0 :
236359 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
237- return [ 4 /*yield*/ , axios_1 . default . post ( this . apiUrl + path , body , {
360+ return [ 4 /*yield*/ , this . httpRequest ( "POST" , this . apiUrl + path , {
361+ body : body ,
238362 headers : __assign ( { "x-api-key" : this . key } , headers ) ,
239363 } ) ] ;
240364 case 1 :
@@ -264,7 +388,8 @@ var BaseClient = /** @class */ (function () {
264388 switch ( _a . label ) {
265389 case 0 :
266390 _a . trys . push ( [ 0 , 2 , , 3 ] ) ;
267- return [ 4 /*yield*/ , axios_1 . default . put ( this . apiUrl + path , body , {
391+ return [ 4 /*yield*/ , this . httpRequest ( "PUT" , this . apiUrl + path , {
392+ body : body ,
268393 headers : {
269394 "x-api-key" : this . key ,
270395 } ,
0 commit comments