Skip to content

Commit 8d4e076

Browse files
fix: update typescript-fetch runtime template
1 parent 8c49447 commit 8d4e076

19 files changed

Lines changed: 456 additions & 304 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
export const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, "");
77

88
export interface ConfigurationParameters {
9-
basePath?: string; // override base path
10-
fetchApi?: FetchAPI; // override for fetch implementation
11-
middleware?: Middleware[]; // middleware to apply before/after fetch requests
12-
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
13-
username?: string; // parameter for basic security
14-
password?: string; // parameter for basic security
15-
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>); // parameter for apiKey security
16-
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
17-
headers?: HTTPHeaders; //header params we want to use on every request
18-
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
9+
basePath?: string | undefined; // override base path
10+
fetchApi?: FetchAPI | undefined; // override for fetch implementation
11+
middleware?: Middleware[] | undefined; // middleware to apply before/after fetch requests
12+
queryParamsStringify?: ((params: HTTPQuery) => string) | undefined; // stringify function for query strings
13+
username?: string | undefined; // parameter for basic security
14+
password?: string | undefined; // parameter for basic security
15+
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>) | undefined; // parameter for apiKey security
16+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined; // parameter for oauth2 security
17+
headers?: HTTPHeaders | undefined; //header params we want to use on every request
18+
credentials?: RequestCredentials | undefined; //value for the credentials param we want to use on each request
1919
}
2020

2121
export class Configuration {
@@ -151,7 +151,6 @@ export class BaseAPI {
151151
method: context.method,
152152
headers,
153153
body: context.body,
154-
credentials: this.configuration.credentials,
155154
};
156155

157156
const overriddenInit: RequestInit = {
@@ -162,6 +161,10 @@ export class BaseAPI {
162161
}))
163162
};
164163

164+
if (!overriddenInit.credentials && this.configuration.credentials) {
165+
overriddenInit.credentials = this.configuration.credentials;
166+
}
167+
165168
let body: any;
166169
if (isFormData(overriddenInit.body)
167170
|| (overriddenInit.body instanceof URLSearchParams)
@@ -282,7 +285,12 @@ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
282285
export type HTTPHeaders = { [key: string]: string };
283286
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | Set<string | number | null | boolean> | HTTPQuery };
284287
export type HTTPBody = Json | FormData | URLSearchParams;
285-
export type HTTPRequestInit = { headers?: HTTPHeaders; method: HTTPMethod; credentials?: RequestCredentials; body?: HTTPBody };
288+
export type HTTPRequestInit = {
289+
headers?: HTTPHeaders | undefined;
290+
method: HTTPMethod;
291+
credentials?: RequestCredentials | undefined;
292+
body?: HTTPBody | undefined;
293+
};
286294
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
287295

288296
export type InitOverrideFunction = (requestContext: { init: HTTPRequestInit, context: RequestOpts }) => Promise<RequestInit>
@@ -373,13 +381,13 @@ export interface ErrorContext {
373381
url: string;
374382
init: RequestInit;
375383
error: unknown;
376-
response?: Response;
384+
response?: Response | undefined;
377385
}
378386

379387
export interface Middleware {
380-
pre?(context: RequestContext): Promise<FetchParams | void>;
381-
post?(context: ResponseContext): Promise<Response | void>;
382-
onError?(context: ErrorContext): Promise<Response | void>;
388+
pre?: ((context: RequestContext) => Promise<FetchParams | void>) | undefined;
389+
post?: ((context: ResponseContext) => Promise<Response | void>) | undefined;
390+
onError?: ((context: ErrorContext) => Promise<Response | void>) | undefined;
383391
}
384392

385393
export interface ApiResponse<T> {

samples/client/others/typescript-fetch/infinite-recursion-issue/runtime.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
export const BASE_PATH = "http://localhost:8080".replace(/\/+$/, "");
1717

1818
export interface ConfigurationParameters {
19-
basePath?: string; // override base path
20-
fetchApi?: FetchAPI; // override for fetch implementation
21-
middleware?: Middleware[]; // middleware to apply before/after fetch requests
22-
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
23-
username?: string; // parameter for basic security
24-
password?: string; // parameter for basic security
25-
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>); // parameter for apiKey security
26-
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
27-
headers?: HTTPHeaders; //header params we want to use on every request
28-
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
19+
basePath?: string | undefined; // override base path
20+
fetchApi?: FetchAPI | undefined; // override for fetch implementation
21+
middleware?: Middleware[] | undefined; // middleware to apply before/after fetch requests
22+
queryParamsStringify?: ((params: HTTPQuery) => string) | undefined; // stringify function for query strings
23+
username?: string | undefined; // parameter for basic security
24+
password?: string | undefined; // parameter for basic security
25+
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>) | undefined; // parameter for apiKey security
26+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined; // parameter for oauth2 security
27+
headers?: HTTPHeaders | undefined; //header params we want to use on every request
28+
credentials?: RequestCredentials | undefined; //value for the credentials param we want to use on each request
2929
}
3030

3131
export class Configuration {
@@ -161,7 +161,6 @@ export class BaseAPI {
161161
method: context.method,
162162
headers,
163163
body: context.body,
164-
credentials: this.configuration.credentials,
165164
};
166165

167166
const overriddenInit: RequestInit = {
@@ -172,6 +171,10 @@ export class BaseAPI {
172171
}))
173172
};
174173

174+
if (!overriddenInit.credentials && this.configuration.credentials) {
175+
overriddenInit.credentials = this.configuration.credentials;
176+
}
177+
175178
let body: any;
176179
if (isFormData(overriddenInit.body)
177180
|| (overriddenInit.body instanceof URLSearchParams)
@@ -292,7 +295,12 @@ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
292295
export type HTTPHeaders = { [key: string]: string };
293296
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | Set<string | number | null | boolean> | HTTPQuery };
294297
export type HTTPBody = Json | FormData | URLSearchParams;
295-
export type HTTPRequestInit = { headers?: HTTPHeaders; method: HTTPMethod; credentials?: RequestCredentials; body?: HTTPBody };
298+
export type HTTPRequestInit = {
299+
headers?: HTTPHeaders | undefined;
300+
method: HTTPMethod;
301+
credentials?: RequestCredentials | undefined;
302+
body?: HTTPBody | undefined;
303+
};
296304
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
297305

298306
export type InitOverrideFunction = (requestContext: { init: HTTPRequestInit, context: RequestOpts }) => Promise<RequestInit>
@@ -381,13 +389,13 @@ export interface ErrorContext {
381389
url: string;
382390
init: RequestInit;
383391
error: unknown;
384-
response?: Response;
392+
response?: Response | undefined;
385393
}
386394

387395
export interface Middleware {
388-
pre?(context: RequestContext): Promise<FetchParams | void>;
389-
post?(context: ResponseContext): Promise<Response | void>;
390-
onError?(context: ErrorContext): Promise<Response | void>;
396+
pre?: ((context: RequestContext) => Promise<FetchParams | void>) | undefined;
397+
post?: ((context: ResponseContext) => Promise<Response | void>) | undefined;
398+
onError?: ((context: ErrorContext) => Promise<Response | void>) | undefined;
391399
}
392400

393401
export interface ApiResponse<T> {

samples/client/others/typescript-fetch/self-import-issue/runtime.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
export const BASE_PATH = "http://localhost".replace(/\/+$/, "");
1717

1818
export interface ConfigurationParameters {
19-
basePath?: string; // override base path
20-
fetchApi?: FetchAPI; // override for fetch implementation
21-
middleware?: Middleware[]; // middleware to apply before/after fetch requests
22-
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
23-
username?: string; // parameter for basic security
24-
password?: string; // parameter for basic security
25-
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>); // parameter for apiKey security
26-
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
27-
headers?: HTTPHeaders; //header params we want to use on every request
28-
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
19+
basePath?: string | undefined; // override base path
20+
fetchApi?: FetchAPI | undefined; // override for fetch implementation
21+
middleware?: Middleware[] | undefined; // middleware to apply before/after fetch requests
22+
queryParamsStringify?: ((params: HTTPQuery) => string) | undefined; // stringify function for query strings
23+
username?: string | undefined; // parameter for basic security
24+
password?: string | undefined; // parameter for basic security
25+
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>) | undefined; // parameter for apiKey security
26+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined; // parameter for oauth2 security
27+
headers?: HTTPHeaders | undefined; //header params we want to use on every request
28+
credentials?: RequestCredentials | undefined; //value for the credentials param we want to use on each request
2929
}
3030

3131
export class Configuration {
@@ -161,7 +161,6 @@ export class BaseAPI {
161161
method: context.method,
162162
headers,
163163
body: context.body,
164-
credentials: this.configuration.credentials,
165164
};
166165

167166
const overriddenInit: RequestInit = {
@@ -172,6 +171,10 @@ export class BaseAPI {
172171
}))
173172
};
174173

174+
if (!overriddenInit.credentials && this.configuration.credentials) {
175+
overriddenInit.credentials = this.configuration.credentials;
176+
}
177+
175178
let body: any;
176179
if (isFormData(overriddenInit.body)
177180
|| (overriddenInit.body instanceof URLSearchParams)
@@ -292,7 +295,12 @@ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
292295
export type HTTPHeaders = { [key: string]: string };
293296
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | Set<string | number | null | boolean> | HTTPQuery };
294297
export type HTTPBody = Json | FormData | URLSearchParams;
295-
export type HTTPRequestInit = { headers?: HTTPHeaders; method: HTTPMethod; credentials?: RequestCredentials; body?: HTTPBody };
298+
export type HTTPRequestInit = {
299+
headers?: HTTPHeaders | undefined;
300+
method: HTTPMethod;
301+
credentials?: RequestCredentials | undefined;
302+
body?: HTTPBody | undefined;
303+
};
296304
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
297305

298306
export type InitOverrideFunction = (requestContext: { init: HTTPRequestInit, context: RequestOpts }) => Promise<RequestInit>
@@ -381,13 +389,13 @@ export interface ErrorContext {
381389
url: string;
382390
init: RequestInit;
383391
error: unknown;
384-
response?: Response;
392+
response?: Response | undefined;
385393
}
386394

387395
export interface Middleware {
388-
pre?(context: RequestContext): Promise<FetchParams | void>;
389-
post?(context: ResponseContext): Promise<Response | void>;
390-
onError?(context: ErrorContext): Promise<Response | void>;
396+
pre?: ((context: RequestContext) => Promise<FetchParams | void>) | undefined;
397+
post?: ((context: ResponseContext) => Promise<Response | void>) | undefined;
398+
onError?: ((context: ErrorContext) => Promise<Response | void>) | undefined;
391399
}
392400

393401
export interface ApiResponse<T> {

samples/client/petstore/typescript-fetch/builds/allOf-nullable/runtime.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
export const BASE_PATH = "http://api.example.xyz/v1".replace(/\/+$/, "");
1717

1818
export interface ConfigurationParameters {
19-
basePath?: string; // override base path
20-
fetchApi?: FetchAPI; // override for fetch implementation
21-
middleware?: Middleware[]; // middleware to apply before/after fetch requests
22-
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
23-
username?: string; // parameter for basic security
24-
password?: string; // parameter for basic security
25-
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>); // parameter for apiKey security
26-
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
27-
headers?: HTTPHeaders; //header params we want to use on every request
28-
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
19+
basePath?: string | undefined; // override base path
20+
fetchApi?: FetchAPI | undefined; // override for fetch implementation
21+
middleware?: Middleware[] | undefined; // middleware to apply before/after fetch requests
22+
queryParamsStringify?: ((params: HTTPQuery) => string) | undefined; // stringify function for query strings
23+
username?: string | undefined; // parameter for basic security
24+
password?: string | undefined; // parameter for basic security
25+
apiKey?: string | Promise<string> | ((name: string) => string | Promise<string>) | undefined; // parameter for apiKey security
26+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined; // parameter for oauth2 security
27+
headers?: HTTPHeaders | undefined; //header params we want to use on every request
28+
credentials?: RequestCredentials | undefined; //value for the credentials param we want to use on each request
2929
}
3030

3131
export class Configuration {
@@ -161,7 +161,6 @@ export class BaseAPI {
161161
method: context.method,
162162
headers,
163163
body: context.body,
164-
credentials: this.configuration.credentials,
165164
};
166165

167166
const overriddenInit: RequestInit = {
@@ -172,6 +171,10 @@ export class BaseAPI {
172171
}))
173172
};
174173

174+
if (!overriddenInit.credentials && this.configuration.credentials) {
175+
overriddenInit.credentials = this.configuration.credentials;
176+
}
177+
175178
let body: any;
176179
if (isFormData(overriddenInit.body)
177180
|| (overriddenInit.body instanceof URLSearchParams)
@@ -292,7 +295,12 @@ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'
292295
export type HTTPHeaders = { [key: string]: string };
293296
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | Set<string | number | null | boolean> | HTTPQuery };
294297
export type HTTPBody = Json | FormData | URLSearchParams;
295-
export type HTTPRequestInit = { headers?: HTTPHeaders; method: HTTPMethod; credentials?: RequestCredentials; body?: HTTPBody };
298+
export type HTTPRequestInit = {
299+
headers?: HTTPHeaders | undefined;
300+
method: HTTPMethod;
301+
credentials?: RequestCredentials | undefined;
302+
body?: HTTPBody | undefined;
303+
};
296304
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
297305

298306
export type InitOverrideFunction = (requestContext: { init: HTTPRequestInit, context: RequestOpts }) => Promise<RequestInit>
@@ -381,13 +389,13 @@ export interface ErrorContext {
381389
url: string;
382390
init: RequestInit;
383391
error: unknown;
384-
response?: Response;
392+
response?: Response | undefined;
385393
}
386394

387395
export interface Middleware {
388-
pre?(context: RequestContext): Promise<FetchParams | void>;
389-
post?(context: ResponseContext): Promise<Response | void>;
390-
onError?(context: ErrorContext): Promise<Response | void>;
396+
pre?: ((context: RequestContext) => Promise<FetchParams | void>) | undefined;
397+
post?: ((context: ResponseContext) => Promise<Response | void>) | undefined;
398+
onError?: ((context: ErrorContext) => Promise<Response | void>) | undefined;
391399
}
392400

393401
export interface ApiResponse<T> {

0 commit comments

Comments
 (0)