@@ -67,27 +67,18 @@ type ApiRejectType = {
6767 error : Error ;
6868} ;
6969
70- interface FetchHeaders {
71- forEach ( callback : ( value : string , key : string ) => void ) : void ;
72- }
73-
74- interface FetchResponse {
75- status : number ;
76- statusText : string ;
77- headers : FetchHeaders ;
78- ok : boolean ;
79- arrayBuffer ( ) : Promise < ArrayBuffer > ;
80- }
70+ export class ApiClient {
71+ private readonly _fetcher : typeof fetch ;
8172
82- interface FetchOptions {
83- method ?: string ;
84- headers ?: StringMap ;
85- body ?: any ;
86- }
73+ constructor ( ) {
74+ const resolvedFetch = ( globalThis as { fetch ?: typeof fetch } ) . fetch ;
75+ if ( ! resolvedFetch ) {
76+ throw new Error ( 'Global fetch API is not available. Please use Node.js 18+.' ) ;
77+ }
8778
88- type Fetcher = ( input : string | URL , options ?: FetchOptions ) => Promise < FetchResponse > ;
79+ this . _fetcher = resolvedFetch ;
80+ }
8981
90- export class ApiClient {
9182 public requestAsync ( options : ApiRequestOptions ) : Promise < ApiResult > {
9283 const url : URL = options . qs
9384 ? new URL ( `?${ new URLSearchParams ( options . qs ) . toString ( ) } ` , options . uri )
@@ -97,7 +88,7 @@ export class ApiClient {
9788
9889 const responseEncoding : BufferEncoding | null = options . encoding === null ? null : options . encoding || 'utf-8' ;
9990
100- const requestOptions : FetchOptions = {
91+ const requestOptions : RequestInit = {
10192 method : options . method || 'GET' ,
10293 headers : options . headers ,
10394 } ;
@@ -136,13 +127,12 @@ export class ApiClient {
136127
137128 private async doFetchRequest (
138129 url : URL ,
139- requestOptions : FetchOptions ,
130+ requestOptions : RequestInit ,
140131 responseEncoding : BufferEncoding | null
141132 ) : Promise < ApiResult > {
142- const fetcher = this . getFetch ( ) ;
143- let response : FetchResponse ;
133+ let response : Response ;
144134 try {
145- response = await fetcher ( url . toString ( ) , requestOptions ) ;
135+ response = await this . _fetcher ( url . toString ( ) , requestOptions ) ;
146136 } catch ( error ) {
147137 return Promise . reject ( {
148138 response : null ,
@@ -188,7 +178,7 @@ export class ApiClient {
188178 }
189179
190180 private async readResponseBody (
191- response : FetchResponse ,
181+ response : Response ,
192182 responseEncoding : BufferEncoding | null
193183 ) : Promise < string | Buffer > {
194184 const arrayBuffer = await response . arrayBuffer ( ) ;
@@ -201,7 +191,7 @@ export class ApiClient {
201191 return buffer . toString ( responseEncoding ) ;
202192 }
203193
204- private toHeaderDict ( headers : FetchHeaders ) : NodeJS . Dict < string | string [ ] > {
194+ private toHeaderDict ( headers : Headers ) : NodeJS . Dict < string | string [ ] > {
205195 const normalizedHeaders : NodeJS . Dict < string | string [ ] > = { } ;
206196
207197 headers . forEach ( ( value , key ) => {
@@ -223,15 +213,6 @@ export class ApiClient {
223213 return normalizedHeaders ;
224214 }
225215
226- private getFetch ( ) : Fetcher {
227- const fetcher = ( globalThis as { fetch ?: Fetcher } ) . fetch ;
228- if ( ! fetcher ) {
229- throw new Error ( 'Global fetch API is not available. Please use Node.js 18+.' ) ;
230- }
231-
232- return fetcher ;
233- }
234-
235216 private normalizeFetchError ( error : unknown ) : Error {
236217 if ( error instanceof Error ) {
237218 const mutableError = error as Error & { code ?: string ; cause ?: unknown ; name : string } ;
0 commit comments