11import { Configuration } from './Configuration';
2- import { Multipart, RequestFile, FormParamsType } from './multipart';
2+ import { Multipart, RequestFile, FormParamPairs } from './multipart';
33
44export * from './models';
55
@@ -8,17 +8,17 @@ import { {{#models}} {{#model}}{{classname}},{{/model}} {{/models}} } from './mo
88import { {{#apiInfo} }{ {#apis} }{ {#operations} }{ {#operation} } { {operationIdCamelCase} }RequestWrapper, { {/operation} }{ {/operations} }{ {/apis} }{ {/apiInfo} } } from './models';
99
1010
11- type StringKeyWithStringValue = Record<string , string >;
11+ type StringMap = Record<string , string >;
1212
1313type ApiRequestOptions = {
1414 uri: string;
1515 body?: any;
1616 encoding?: BufferEncoding | null;
17- form?: StringKeyWithStringValue ;
18- headers?: StringKeyWithStringValue ;
17+ form?: StringMap ;
18+ headers?: StringMap ;
1919 json?: boolean;
2020 method?: string;
21- qs?: StringKeyWithStringValue ;
21+ qs?: StringMap ;
2222} ;
2323
2424type ApiResponse = {
@@ -39,27 +39,18 @@ type ApiRejectType = {
3939 error: Error;
4040} ;
4141
42- interface FetchHeaders {
43- forEach(callback: (value: string, key: string) => void): void;
44- }
45-
46- interface FetchResponse {
47- status: number;
48- statusText: string;
49- headers: FetchHeaders;
50- ok: boolean;
51- arrayBuffer(): Promise< ArrayBuffer> ;
52- }
42+ export class ApiClient {
43+ private readonly _fetcher: typeof fetch ;
5344
54- interface FetchRequestInit {
55- method ?: string ;
56- headers?: StringKeyWithStringValue;
57- body?: any ;
58- }
45+ constructor() {
46+ const resolvedFetch = (globalThis as { fetch ?: typeof fetch } ).fetch ;
47+ if (!resolvedFetch) {
48+ throw new Error( ' Global fetch API is not available. Please use Node.js 18+. ' ) ;
49+ }
5950
60- type Fetcher = (input: string | URL, init?: FetchRequestInit) => Promise<FetchResponse >;
51+ this._fetcher = resolvedFetch;
52+ }
6153
62- export class ApiClient {
6354 public requestAsync(options: ApiRequestOptions): Promise<ApiResult > {
6455 const url: URL = options.qs
6556 ? new URL(`?${new URLSearchParams(options.qs).toString()} `, options.uri)
@@ -69,7 +60,7 @@ export class ApiClient {
6960
7061 const responseEncoding: BufferEncoding | null = options.encoding === null ? null : options.encoding || 'utf-8';
7162
72- const requestOptions: FetchRequestInit = {
63+ const requestOptions: RequestInit = {
7364 method: options.method || ' GET' ,
7465 headers: options.headers,
7566 } ;
@@ -108,13 +99,12 @@ export class ApiClient {
10899
109100 private async doFetchRequest(
110101 url: URL,
111- requestOptions: FetchRequestInit ,
102+ requestOptions: RequestInit ,
112103 responseEncoding: BufferEncoding | null
113104 ): Promise<ApiResult > {
114- const fetcher = this.getFetch();
115- let response: FetchResponse;
105+ let response: Response;
116106 try {
117- response = await fetcher (url.toString(), requestOptions);
107+ response = await this._fetcher (url.toString(), requestOptions);
118108 } catch (error) {
119109 return Promise.reject({
120110 response: null,
@@ -160,7 +150,7 @@ export class ApiClient {
160150 }
161151
162152 private async readResponseBody(
163- response: FetchResponse ,
153+ response: Response ,
164154 responseEncoding: BufferEncoding | null
165155 ): Promise<string | Buffer > {
166156 const arrayBuffer = await response.arrayBuffer();
@@ -173,7 +163,7 @@ export class ApiClient {
173163 return buffer.toString(responseEncoding);
174164 }
175165
176- private toHeaderDict(headers: FetchHeaders ): NodeJS.Dict<string | string[] > {
166+ private toHeaderDict(headers: Headers ): NodeJS.Dict<string | string[] > {
177167 const normalizedHeaders: NodeJS.Dict< string | string[]> = {} ;
178168
179169 headers.forEach((value, key) => {
@@ -195,15 +185,6 @@ export class ApiClient {
195185 return normalizedHeaders;
196186 }
197187
198- private getFetch(): Fetcher {
199- const fetcher = (globalThis as { fetch ?: Fetcher } ).fetch;
200- if (!fetcher) {
201- throw new Error(' Global fetch API is not available. Please use Node.js 18+.' );
202- }
203-
204- return fetcher;
205- }
206-
207188 private normalizeFetchError(error: unknown): Error {
208189 if (error instanceof Error) {
209190 const mutableError = error as Error & { code?: string; cause?: unknown; name: string } ;
@@ -447,7 +428,7 @@ export class {{classname}} {
447428 let queryParameters: any = { } ;
448429 let headerParams: any = (Object as any).assign({ } , this.defaultHeaders);
449430{ {#hasFormParams} }
450- const formParams: FormParamsType = [];
431+ const formParams: FormParamPairs = [];
451432{ {/hasFormParams} }
452433
453434{ {#allParams} }
0 commit comments