Skip to content

Commit eb5b781

Browse files
authored
[typescript-axios] Add Support for Operation Servers (OpenAPITools#16782)
* [typescript-axios] Add Support for Operation Servers * add missed import * redo indexing to be safer * generate samples
1 parent eae2051 commit eb5b781

File tree

44 files changed

+1211
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1211
-280
lines changed

modules/openapi-generator/src/main/resources/typescript-axios/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import FormData from 'form-data'
1919
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
2020
import type { RequestArgs } from './base';
2121
// @ts-ignore
22-
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError } from './base';
22+
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base';
2323

2424
{{#models}}
2525
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}{{/model}}

modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache

100755100644
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import FormData from 'form-data'
1818
// @ts-ignore
1919
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '{{apiRelativeToRoot}}common';
2020
// @ts-ignore
21-
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '{{apiRelativeToRoot}}base';
21+
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError, operationServerMap } from '{{apiRelativeToRoot}}base';
2222
{{#imports}}
2323
// @ts-ignore
2424
import { {{classname}} } from '{{apiRelativeToRoot}}{{tsModelPackage}}';
@@ -250,7 +250,9 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
250250
*/
251251
async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}, {{/allParams}}options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
252252
const localVarAxiosArgs = await localVarAxiosParamCreator.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options);
253-
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
253+
const index = configuration?.serverIndex ?? 0;
254+
const operationBasePath = operationServerMap['{{classname}}.{{nickname}}']?.[index]?.url;
255+
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, operationBasePath || basePath);
254256
},
255257
{{/operation}}
256258
}

modules/openapi-generator/src/main/resources/typescript-axios/baseApi.mustache

100755100644
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,58 @@ export class RequiredError extends Error {
5959
this.name = "RequiredError"
6060
}
6161
}
62+
63+
interface ServerMap {
64+
[key: string]: {
65+
url: string,
66+
description: string,
67+
}[];
68+
}
69+
70+
/**
71+
*
72+
* @export
73+
*/
74+
export const operationServerMap: ServerMap = {
75+
{{#apiInfo}}
76+
{{#apis}}
77+
{{#operations}}
78+
{{#operation}}
79+
{{#servers}}
80+
{{#-first}}
81+
"{{{classname}}}.{{{nickname}}}": [
82+
{{/-first}}
83+
{
84+
url: "{{{url}}}",
85+
description: "{{{description}}}{{^description}}No description provided{{/description}}",
86+
{{#variables}}
87+
{{#-first}}
88+
variables: {
89+
{{/-first}}
90+
{{{name}}}: {
91+
description: "{{{description}}}{{^description}}No description provided{{/description}}",
92+
default_value: "{{{defaultValue}}}",
93+
{{#enumValues}}
94+
{{#-first}}
95+
enum_values: [
96+
{{/-first}}
97+
"{{{.}}}"{{^-last}},{{/-last}}
98+
{{#-last}}
99+
]
100+
{{/-last}}
101+
{{/enumValues}}
102+
}{{^-last}},{{/-last}}
103+
{{#-last}}
104+
}
105+
{{/-last}}
106+
{{/variables}}
107+
}{{^-last}},{{/-last}}
108+
{{#-last}}
109+
],
110+
{{/-last}}
111+
{{/servers}}
112+
{{/operation}}
113+
{{/operations}}
114+
{{/apis}}
115+
{{/apiInfo}}
116+
}

modules/openapi-generator/src/main/resources/typescript-axios/configuration.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ConfigurationParameters {
88
password?: string;
99
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
1010
basePath?: string;
11+
serverIndex?: number;
1112
baseOptions?: any;
1213
formDataCtor?: new () => any;
1314
}
@@ -47,6 +48,13 @@ export class Configuration {
4748
* @memberof Configuration
4849
*/
4950
basePath?: string;
51+
/**
52+
* override server index
53+
*
54+
* @type {number}
55+
* @memberof Configuration
56+
*/
57+
serverIndex?: number;
5058
/**
5159
* base options for axios calls
5260
*
@@ -69,6 +77,7 @@ export class Configuration {
6977
this.password = param.password;
7078
this.accessToken = param.accessToken;
7179
this.basePath = param.basePath;
80+
this.serverIndex = param.serverIndex;
7281
this.baseOptions = param.baseOptions;
7382
this.formDataCtor = param.formDataCtor;
7483
}

samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/base.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,17 @@ export class RequiredError extends Error {
7070
this.name = "RequiredError"
7171
}
7272
}
73+
74+
interface ServerMap {
75+
[key: string]: {
76+
url: string,
77+
description: string,
78+
}[];
79+
}
80+
81+
/**
82+
*
83+
* @export
84+
*/
85+
export const operationServerMap: ServerMap = {
86+
}

samples/client/others/typescript-axios/with-separate-models-and-api-inheritance/configuration.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ConfigurationParameters {
1919
password?: string;
2020
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
2121
basePath?: string;
22+
serverIndex?: number;
2223
baseOptions?: any;
2324
formDataCtor?: new () => any;
2425
}
@@ -58,6 +59,13 @@ export class Configuration {
5859
* @memberof Configuration
5960
*/
6061
basePath?: string;
62+
/**
63+
* override server index
64+
*
65+
* @type {number}
66+
* @memberof Configuration
67+
*/
68+
serverIndex?: number;
6169
/**
6270
* base options for axios calls
6371
*
@@ -80,6 +88,7 @@ export class Configuration {
8088
this.password = param.password;
8189
this.accessToken = param.accessToken;
8290
this.basePath = param.basePath;
91+
this.serverIndex = param.serverIndex;
8392
this.baseOptions = param.baseOptions;
8493
this.formDataCtor = param.formDataCtor;
8594
}

samples/client/petstore/typescript-axios/builds/composed-schemas/api.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import globalAxios from 'axios';
2121
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
2222
import type { RequestArgs } from './base';
2323
// @ts-ignore
24-
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError } from './base';
24+
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base';
2525

2626
/**
2727
*
@@ -299,7 +299,9 @@ export const DefaultApiFp = function(configuration?: Configuration) {
299299
*/
300300
async filePost(filePostRequest?: FilePostRequest, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
301301
const localVarAxiosArgs = await localVarAxiosParamCreator.filePost(filePostRequest, options);
302-
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
302+
const index = configuration?.serverIndex ?? 0;
303+
const operationBasePath = operationServerMap['DefaultApi.filePost']?.[index]?.url;
304+
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, operationBasePath || basePath);
303305
},
304306
/**
305307
*
@@ -309,7 +311,9 @@ export const DefaultApiFp = function(configuration?: Configuration) {
309311
*/
310312
async petsFilteredPatch(petsFilteredPatchRequest?: PetsFilteredPatchRequest | null, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
311313
const localVarAxiosArgs = await localVarAxiosParamCreator.petsFilteredPatch(petsFilteredPatchRequest, options);
312-
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
314+
const index = configuration?.serverIndex ?? 0;
315+
const operationBasePath = operationServerMap['DefaultApi.petsFilteredPatch']?.[index]?.url;
316+
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, operationBasePath || basePath);
313317
},
314318
/**
315319
*
@@ -319,7 +323,9 @@ export const DefaultApiFp = function(configuration?: Configuration) {
319323
*/
320324
async petsPatch(petsPatchRequest?: PetsPatchRequest | null, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
321325
const localVarAxiosArgs = await localVarAxiosParamCreator.petsPatch(petsPatchRequest, options);
322-
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
326+
const index = configuration?.serverIndex ?? 0;
327+
const operationBasePath = operationServerMap['DefaultApi.petsPatch']?.[index]?.url;
328+
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, operationBasePath || basePath);
323329
},
324330
}
325331
};

samples/client/petstore/typescript-axios/builds/composed-schemas/base.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,17 @@ export class RequiredError extends Error {
7070
this.name = "RequiredError"
7171
}
7272
}
73+
74+
interface ServerMap {
75+
[key: string]: {
76+
url: string,
77+
description: string,
78+
}[];
79+
}
80+
81+
/**
82+
*
83+
* @export
84+
*/
85+
export const operationServerMap: ServerMap = {
86+
}

samples/client/petstore/typescript-axios/builds/composed-schemas/configuration.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ConfigurationParameters {
1919
password?: string;
2020
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
2121
basePath?: string;
22+
serverIndex?: number;
2223
baseOptions?: any;
2324
formDataCtor?: new () => any;
2425
}
@@ -58,6 +59,13 @@ export class Configuration {
5859
* @memberof Configuration
5960
*/
6061
basePath?: string;
62+
/**
63+
* override server index
64+
*
65+
* @type {number}
66+
* @memberof Configuration
67+
*/
68+
serverIndex?: number;
6169
/**
6270
* base options for axios calls
6371
*
@@ -80,6 +88,7 @@ export class Configuration {
8088
this.password = param.password;
8189
this.accessToken = param.accessToken;
8290
this.basePath = param.basePath;
91+
this.serverIndex = param.serverIndex;
8392
this.baseOptions = param.baseOptions;
8493
this.formDataCtor = param.formDataCtor;
8594
}

0 commit comments

Comments
 (0)