@@ -55,6 +55,11 @@ var axios_1 = __importDefault(require("axios"));
5555var zod_1 = require ( "zod" ) ;
5656var logger_1 = require ( "../common/logger" ) ;
5757var errors_1 = require ( "../common/errors" ) ;
58+ var runtime_1 = require ( "../common/runtime" ) ;
59+ // axios 0.27.2 has no fetch adapter and requires Node's http module, so it
60+ // crashes inside Workers. When isCloudflareWorkers is true we route HTTP
61+ // through native fetch instead — preserved error shape so errorHandler keeps
62+ // working.
5863var APIResponse = zod_1 . z . object ( { } ) . catchall ( zod_1 . z . any ( ) ) ;
5964var APIErrorResponse = zod_1 . z
6065 . object ( {
@@ -70,8 +75,125 @@ var BaseClient = /** @class */ (function () {
7075 this . apiUrl = options . apiUrl || apiUrl ;
7176 this . key = key ;
7277 this . timeout = options . timeout || 5000 ;
73- axios_1 . default . defaults . timeout = this . timeout ;
78+ if ( ! runtime_1 . isCloudflareWorkers ) {
79+ // axios.defaults is process-global state and is meaningless in Workers
80+ // (we don't use axios there). Skip in Workers to avoid touching axios's
81+ // internal config which can drag in Node-only deps during import.
82+ axios_1 . default . defaults . timeout = this . timeout ;
83+ }
7484 }
85+ /**
86+ * Issue an HTTP request. Routes through axios in Node/Lambda environments
87+ * and native fetch in Cloudflare Workers. Both paths return / throw
88+ * axios-compatible shapes so errorHandler() and the response.data parsing
89+ * downstream work unchanged.
90+ */
91+ BaseClient . prototype . httpRequest = function ( method , url , options ) {
92+ var _a ;
93+ if ( options === void 0 ) { options = { } ; }
94+ return __awaiter ( this , void 0 , void 0 , function ( ) {
95+ var requestTimeout , response_1 , finalUrl , search , _i , _b , _c , k , v , init , hasContentType , controller , timeoutId , response , err_1 , wrapped , contentType , data , _d , text , headersObj_1 , wrapped , headersObj ;
96+ return __generator ( this , function ( _e ) {
97+ switch ( _e . label ) {
98+ case 0 :
99+ requestTimeout = ( _a = options . timeout ) !== null && _a !== void 0 ? _a : this . timeout ;
100+ if ( ! ! runtime_1 . isCloudflareWorkers ) return [ 3 /*break*/ , 2 ] ;
101+ return [ 4 /*yield*/ , axios_1 . default . request ( {
102+ method : method ,
103+ url : url ,
104+ params : options . params ,
105+ data : options . body ,
106+ headers : options . headers ,
107+ timeout : requestTimeout ,
108+ } ) ] ;
109+ case 1 :
110+ response_1 = _e . sent ( ) ;
111+ return [ 2 /*return*/ , { data : response_1 . data , status : response_1 . status , headers : response_1 . headers } ] ;
112+ case 2 :
113+ finalUrl = url ;
114+ if ( options . params && Object . keys ( options . params ) . length > 0 ) {
115+ search = new URLSearchParams ( ) ;
116+ for ( _i = 0 , _b = Object . entries ( options . params ) ; _i < _b . length ; _i ++ ) {
117+ _c = _b [ _i ] , k = _c [ 0 ] , v = _c [ 1 ] ;
118+ if ( v !== undefined && v !== null )
119+ search . append ( k , String ( v ) ) ;
120+ }
121+ finalUrl += ( finalUrl . includes ( "?" ) ? "&" : "?" ) + search . toString ( ) ;
122+ }
123+ init = {
124+ method : method ,
125+ headers : options . headers ,
126+ } ;
127+ if ( options . body !== undefined && method !== "GET" && method !== "DELETE" ) {
128+ init . body = typeof options . body === "string" ? options . body : JSON . stringify ( options . body ) ;
129+ hasContentType = options . headers && Object . keys ( options . headers )
130+ . some ( function ( h ) { return h . toLowerCase ( ) === "content-type" ; } ) ;
131+ if ( ! hasContentType ) {
132+ init . headers = __assign ( __assign ( { } , ( options . headers || { } ) ) , { "content-type" : "application/json" } ) ;
133+ }
134+ }
135+ controller = new AbortController ( ) ;
136+ timeoutId = setTimeout ( function ( ) { return controller . abort ( ) ; } , requestTimeout ) ;
137+ init . signal = controller . signal ;
138+ _e . label = 3 ;
139+ case 3 :
140+ _e . trys . push ( [ 3 , 5 , , 6 ] ) ;
141+ return [ 4 /*yield*/ , fetch ( finalUrl , init ) ] ;
142+ case 4 :
143+ response = _e . sent ( ) ;
144+ return [ 3 /*break*/ , 6 ] ;
145+ case 5 :
146+ err_1 = _e . sent ( ) ;
147+ clearTimeout ( timeoutId ) ;
148+ wrapped = new Error ( ( err_1 === null || err_1 === void 0 ? void 0 : err_1 . message ) || "Network request failed" ) ;
149+ if ( controller . signal . aborted || ( err_1 === null || err_1 === void 0 ? void 0 : err_1 . name ) === "AbortError" ) {
150+ wrapped . code = "ECONNABORTED" ;
151+ }
152+ wrapped . request = { url : finalUrl , method : method } ;
153+ wrapped . config = { url : finalUrl , method : method } ;
154+ throw wrapped ;
155+ case 6 :
156+ clearTimeout ( timeoutId ) ;
157+ contentType = response . headers . get ( "content-type" ) || "" ;
158+ if ( ! contentType . includes ( "application/json" ) ) return [ 3 /*break*/ , 11 ] ;
159+ _e . label = 7 ;
160+ case 7 :
161+ _e . trys . push ( [ 7 , 9 , , 10 ] ) ;
162+ return [ 4 /*yield*/ , response . json ( ) ] ;
163+ case 8 :
164+ data = _e . sent ( ) ;
165+ return [ 3 /*break*/ , 10 ] ;
166+ case 9 :
167+ _d = _e . sent ( ) ;
168+ data = null ;
169+ return [ 3 /*break*/ , 10 ] ;
170+ case 10 : return [ 3 /*break*/ , 13 ] ;
171+ case 11 : return [ 4 /*yield*/ , response . text ( ) ] ;
172+ case 12 :
173+ text = _e . sent ( ) ;
174+ try {
175+ data = JSON . parse ( text ) ;
176+ }
177+ catch ( _f ) {
178+ data = text ;
179+ }
180+ _e . label = 13 ;
181+ case 13 :
182+ if ( response . status < 200 || response . status >= 300 ) {
183+ headersObj_1 = { } ;
184+ response . headers . forEach ( function ( v , k ) { headersObj_1 [ k ] = v ; } ) ;
185+ wrapped = new Error ( "Request failed with status " . concat ( response . status ) ) ;
186+ wrapped . response = { status : response . status , data : data , headers : headersObj_1 } ;
187+ wrapped . config = { url : finalUrl , method : method } ;
188+ throw wrapped ;
189+ }
190+ headersObj = { } ;
191+ response . headers . forEach ( function ( v , k ) { headersObj [ k ] = v ; } ) ;
192+ return [ 2 /*return*/ , { data : data , status : response . status , headers : headersObj } ] ;
193+ }
194+ } ) ;
195+ } ) ;
196+ } ;
75197 /**
76198 * Wraps any error into a CrowdHandlerError
77199 */
@@ -168,7 +290,7 @@ var BaseClient = /** @class */ (function () {
168290 switch ( _a . label ) {
169291 case 0 :
170292 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
171- return [ 4 /*yield*/ , axios_1 . default . delete ( this . apiUrl + path , {
293+ return [ 4 /*yield*/ , this . httpRequest ( "DELETE" , this . apiUrl + path , {
172294 headers : {
173295 "x-api-key" : this . key ,
174296 } ,
@@ -200,7 +322,7 @@ var BaseClient = /** @class */ (function () {
200322 switch ( _a . label ) {
201323 case 0 :
202324 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
203- return [ 4 /*yield*/ , axios_1 . default . get ( this . apiUrl + path , {
325+ return [ 4 /*yield*/ , this . httpRequest ( "GET" , this . apiUrl + path , {
204326 params : params ,
205327 headers : {
206328 "x-api-key" : this . key ,
@@ -234,7 +356,8 @@ var BaseClient = /** @class */ (function () {
234356 switch ( _a . label ) {
235357 case 0 :
236358 _a . trys . push ( [ 0 , 2 , , 4 ] ) ;
237- return [ 4 /*yield*/ , axios_1 . default . post ( this . apiUrl + path , body , {
359+ return [ 4 /*yield*/ , this . httpRequest ( "POST" , this . apiUrl + path , {
360+ body : body ,
238361 headers : __assign ( { "x-api-key" : this . key } , headers ) ,
239362 } ) ] ;
240363 case 1 :
@@ -257,17 +380,19 @@ var BaseClient = /** @class */ (function () {
257380 } ) ;
258381 } ) ;
259382 } ;
260- BaseClient . prototype . httpPUT = function ( path , body ) {
383+ BaseClient . prototype . httpPUT = function ( path , body , options ) {
261384 return __awaiter ( this , void 0 , void 0 , function ( ) {
262385 var response , error_4 ;
263386 return __generator ( this , function ( _a ) {
264387 switch ( _a . label ) {
265388 case 0 :
266389 _a . trys . push ( [ 0 , 2 , , 3 ] ) ;
267- return [ 4 /*yield*/ , axios_1 . default . put ( this . apiUrl + path , body , {
390+ return [ 4 /*yield*/ , this . httpRequest ( "PUT" , this . apiUrl + path , {
391+ body : body ,
268392 headers : {
269393 "x-api-key" : this . key ,
270394 } ,
395+ timeout : options === null || options === void 0 ? void 0 : options . timeout ,
271396 } ) ] ;
272397 case 1 :
273398 response = _a . sent ( ) ;
0 commit comments