Skip to content

Commit d033008

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents f88a06f + 5677f5b commit d033008

24 files changed

Lines changed: 97 additions & 83 deletions

File tree

docs/generators/typescript-axios.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2727
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase|
2828
|enumPropertyNamingReplaceSpecialChar|Set to true to replace '-' and '+' symbols with 'minus_' and 'plus_' in enum of type string| |false|
2929
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
30+
|importFileExtension|File extension to use with relative imports. Set it to '.js' or '.mjs' when using [ESM](https://nodejs.org/api/esm.html).| ||
3031
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
3132
|licenseName|The name of the license| |Unlicense|
3233
|modelPackage|package for generated models| |null|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
4747
public static final String WITH_NODE_IMPORTS = "withNodeImports";
4848
public static final String STRING_ENUMS = "stringEnums";
4949
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
50+
public static final String IMPORT_FILE_EXTENSION_SWITCH = "importFileExtension";
51+
public static final String IMPORT_FILE_EXTENSION_SWITCH_DESC = "File extension to use with relative imports. Set it to '.js' or '.mjs' when using [ESM](https://nodejs.org/api/esm.html).";
5052
public static final String USE_SQUARE_BRACKETS_IN_ARRAY_NAMES = "useSquareBracketsInArrayNames";
5153
public static final String AXIOS_VERSION = "axiosVersion";
5254
public static final String DEFAULT_AXIOS_VERSION = "^1.6.1";
5355

5456
@Getter @Setter
5557
protected String npmRepository = null;
5658
protected Boolean stringEnums = false;
59+
protected String importFileExtension = "";
5760

5861
@Getter @Setter
5962
protected String axiosVersion = DEFAULT_AXIOS_VERSION;
@@ -88,6 +91,7 @@ public TypeScriptAxiosClientCodegen() {
8891
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
8992
this.cliOptions.add(new CliOption(WITH_NODE_IMPORTS, "Setting this property to true adds imports for NodeJS", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
9093
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
94+
this.cliOptions.add(new CliOption(IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_SWITCH_DESC, SchemaTypeUtil.STRING_TYPE).defaultValue(this.importFileExtension));
9195
this.cliOptions.add(new CliOption(USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, "Setting this property to true will add brackets to array attribute names, e.g. my_values[].", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
9296
this.cliOptions.add(new CliOption(AXIOS_VERSION, "Use this property to override the axios version in package.json").defaultValue(DEFAULT_AXIOS_VERSION));
9397
// Templates have no mapping between formatted property names and original base names so use only "original" and remove this option
@@ -162,6 +166,14 @@ public void processOpts() {
162166
additionalProperties.put("stringEnums", this.stringEnums);
163167
}
164168

169+
if (additionalProperties.containsKey(IMPORT_FILE_EXTENSION_SWITCH)) {
170+
this.importFileExtension = additionalProperties.get(IMPORT_FILE_EXTENSION_SWITCH).toString();
171+
if (!this.importFileExtension.isEmpty() && !this.importFileExtension.startsWith(".")) {
172+
this.importFileExtension = "." + this.importFileExtension;
173+
}
174+
additionalProperties.put("importFileExtension", this.importFileExtension);
175+
}
176+
165177
if (additionalProperties.containsKey(NPM_NAME)) {
166178
addNpmPackageGeneration();
167179
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
{{^withSeparateModelsAndApi}}
7-
import type { Configuration } from './configuration';
7+
import type { Configuration } from './configuration{{importFileExtension}}';
88
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from 'axios';
99
import globalAxios from 'axios';
1010
{{#withNodeImports}}
@@ -18,9 +18,9 @@ import FormData from 'form-data'
1818
// Some imports not used depending on template conditions
1919
// @ts-ignore
2020
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
21-
import type { RequestArgs } from './base';
21+
import type { RequestArgs } from './base{{importFileExtension}}';
2222
// @ts-ignore
23-
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base';
23+
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base{{importFileExtension}}';
2424

2525
{{#models}}
2626
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}{{/model}}
@@ -30,6 +30,6 @@ import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerM
3030

3131
{{/apis}}{{/apiInfo}}
3232
{{/withSeparateModelsAndApi}}{{#withSeparateModelsAndApi}}
33-
{{#apiInfo}}{{#apis}}{{#operations}}export * from './{{tsApiPackage}}/{{classFilename}}';
33+
{{#apiInfo}}{{#apis}}{{#operations}}export * from './{{tsApiPackage}}/{{classFilename}}{{importFileExtension}}';
3434
{{/operations}}{{/apis}}{{/apiInfo}}
3535
{{/withSeparateModelsAndApi}}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{>licenseInfo}}
55

66

7-
import type { Configuration } from '{{apiRelativeToRoot}}configuration';
7+
import type { Configuration } from '{{apiRelativeToRoot}}configuration{{importFileExtension}}';
88
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from 'axios';
99
import globalAxios from 'axios';
1010
{{#withNodeImports}}
@@ -17,12 +17,12 @@ import FormData from 'form-data'
1717
{{/withNodeImports}}
1818
// Some imports not used depending on template conditions
1919
// @ts-ignore
20-
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '{{apiRelativeToRoot}}common';
20+
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '{{apiRelativeToRoot}}common{{importFileExtension}}';
2121
// @ts-ignore
22-
import { BASE_PATH, COLLECTION_FORMATS, type RequestArgs, BaseAPI, RequiredError, operationServerMap } from '{{apiRelativeToRoot}}base';
22+
import { BASE_PATH, COLLECTION_FORMATS, type RequestArgs, BaseAPI, RequiredError, operationServerMap } from '{{apiRelativeToRoot}}base{{importFileExtension}}';
2323
{{#imports}}
2424
// @ts-ignore
25-
import type { {{classname}} } from '{{apiRelativeToRoot}}{{tsModelPackage}}';
25+
import type { {{classname}} } from '{{apiRelativeToRoot}}{{tsModelPackage}}{{importFileExtension}}';
2626
{{/imports}}
2727
{{/withSeparateModelsAndApi}}
2828
{{^withSeparateModelsAndApi}}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
{{>licenseInfo}}
44

55

6-
import type { Configuration } from './configuration';
6+
7+
import type { Configuration } from './configuration{{importFileExtension}}';
78
// Some imports not used depending on template conditions
89
// @ts-ignore
910
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from 'axios';

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
{{>licenseInfo}}
44

55

6-
import type { Configuration } from "./configuration";
7-
import type { RequestArgs } from "./base";
6+
import type { Configuration } from "./configuration{{importFileExtension}}";
7+
import type { RequestArgs } from "./base{{importFileExtension}}";
88
import type { AxiosInstance, AxiosResponse } from 'axios';
9-
import { RequiredError } from "./base";
9+
import { RequiredError } from "./base{{importFileExtension}}";
1010
{{#withNodeImports}}
1111
import { URL, URLSearchParams } from 'url';
1212
{{/withNodeImports}}
@@ -82,17 +82,17 @@ function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: an
8282
if (typeof parameter === "object") {
8383
if (Array.isArray(parameter)) {
8484
(parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key));
85-
}
85+
}
8686
else {
87-
Object.keys(parameter).forEach(currentKey =>
87+
Object.keys(parameter).forEach(currentKey =>
8888
setFlattenedQueryParams(urlSearchParams, parameter[currentKey], `${key}${key !== '' ? '.' : ''}${currentKey}`)
8989
);
9090
}
91-
}
91+
}
9292
else {
9393
if (urlSearchParams.has(key)) {
9494
urlSearchParams.append(key, parameter);
95-
}
95+
}
9696
else {
9797
urlSearchParams.set(key, parameter);
9898
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
{{>licenseInfo}}
44

55

6-
export * from "./api";
7-
export * from "./configuration";
8-
{{#withSeparateModelsAndApi}}export * from "./{{tsModelPackage}}";{{/withSeparateModelsAndApi}}
6+
export * from "./api{{importFileExtension}}";
7+
export * from "./configuration{{importFileExtension}}";
8+
{{#withSeparateModelsAndApi}}export * from "./{{tsModelPackage}}{{importFileExtension}}";{{/withSeparateModelsAndApi}}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
{{#withSeparateModelsAndApi}}{{#hasAllOf}}{{#allOf}}
66
// May contain unused imports in some cases
77
// @ts-ignore
8-
import type { {{class}} } from './{{filename}}';{{/allOf}}{{/hasAllOf}}{{#hasOneOf}}{{#oneOf}}
8+
import type { {{class}} } from './{{filename}}{{importFileExtension}}';{{/allOf}}{{/hasAllOf}}{{#hasOneOf}}{{#oneOf}}
99
// May contain unused imports in some cases
1010
// @ts-ignore
11-
import type { {{class}} } from './{{filename}}';{{/oneOf}}{{/hasOneOf}}{{^hasAllOf}}{{^hasOneOf}}{{#imports}}
11+
import type { {{class}} } from './{{filename}}{{importFileExtension}}';{{/oneOf}}{{/hasOneOf}}{{^hasAllOf}}{{^hasOneOf}}{{#imports}}
1212
// May contain unused imports in some cases
1313
// @ts-ignore
14-
import type { {{class}} } from './{{filename}}';{{/imports}}{{/hasOneOf}}{{/hasAllOf}}{{/withSeparateModelsAndApi}}
14+
import type { {{class}} } from './{{filename}}{{importFileExtension}}';{{/imports}}{{/hasOneOf}}{{/hasAllOf}}{{/withSeparateModelsAndApi}}
1515
{{#models}}{{#model}}
1616
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{#allOf}}{{#-first}}{{>modelAllOf}}{{/-first}}{{/allOf}}{{^isEnum}}{{^oneOf}}{{^allOf}}{{>modelGeneric}}{{/allOf}}{{/oneOf}}{{/isEnum}}
1717
{{/model}}{{/models}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{{#models}}{{#model}}export * from './{{classFilename}}';{{/model}}
1+
{{#models}}{{#model}}export * from './{{classFilename}}{{importFileExtension}}';{{/model}}
22
{{/models}}

samples/client/echo_api/typescript-axios/build/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: an
8989
if (typeof parameter === "object") {
9090
if (Array.isArray(parameter)) {
9191
(parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key));
92-
}
92+
}
9393
else {
94-
Object.keys(parameter).forEach(currentKey =>
94+
Object.keys(parameter).forEach(currentKey =>
9595
setFlattenedQueryParams(urlSearchParams, parameter[currentKey], `${key}${key !== '' ? '.' : ''}${currentKey}`)
9696
);
9797
}
98-
}
98+
}
9999
else {
100100
if (urlSearchParams.has(key)) {
101101
urlSearchParams.append(key, parameter);
102-
}
102+
}
103103
else {
104104
urlSearchParams.set(key, parameter);
105105
}

0 commit comments

Comments
 (0)