Skip to content

Commit 3e455b0

Browse files
Ivan KamkinIvan Kamkin
authored andcommitted
Update templates
1 parent 06f5c45 commit 3e455b0

4 files changed

Lines changed: 27 additions & 45 deletions

File tree

codegen/Templates/nodejs/api.mustache

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Configuration } from './Configuration';
2-
import { Multipart, RequestFile, FormParamsType } from './multipart';
2+
import { Multipart, RequestFile, FormParamPairs } from './multipart';
33

44
export * from './models';
55

@@ -8,17 +8,17 @@ import { {{#models}} {{#model}}{{classname}},{{/model}} {{/models}} } from './mo
88
import { {{#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

1313
type 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

2424
type 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}}

codegen/Templates/nodejs/multipart.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import crypto from 'crypto';
22

3-
type StringKeyWithStringValue = Record<string, string>;
3+
type StringMap = Record<string, string>;
44

5-
export interface FormParamsType extends Array<Array<string>> {}
5+
export interface FormParamPairs extends Array<Array<string>> {}
66

77
interface IRequestFile {
88
name: string;
@@ -23,9 +23,9 @@ export class RequestFile implements IRequestFile {
2323
export class Multipart {
2424
readonly boundary: string;
2525
readonly body: Buffer;
26-
readonly headers: StringKeyWithStringValue;
26+
readonly headers: StringMap;
2727
28-
constructor(textFields: FormParamsType, files?: IRequestFile[]) {
28+
constructor(textFields: FormParamPairs, files?: IRequestFile[]) {
2929
const random = crypto.randomUUID();
3030
this.boundary = '------------------------' + random.replace(/-/g, '');
3131

codegen/Templates/nodejs/package.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
},
134134
"scripts": {
135135
"test": "npx jest",
136+
"typecheck": "npx tsc --noEmit",
136137
"cover": "npx jest --coverage",
137138
"lint": "npx eslint src test snippets",
138139
"format": "npx eslint src test snippets eslint.config.mjs --fix",

submodules/node

Submodule node updated from efe5331 to 3768468

0 commit comments

Comments
 (0)