From 2f8b7a054f869de7d372ec7a2df96ed7b344a4cf Mon Sep 17 00:00:00 2001 From: Minira Samadova Date: Fri, 7 Oct 2022 17:14:56 +0900 Subject: [PATCH] [typescript-axios] / #12828: Add ESM support to typescript-axios client (#13586) * add ESM support to typescript-axios client * add module path to the package json template * module must be commonjs * generate samples --- .../TypeScriptAxiosClientCodegen.java | 4 +++ .../typescript-axios/package.mustache | 6 +++- .../typescript-axios/tsconfig.esm.mustache | 7 +++++ .../typescript-axios/tsconfig.mustache | 2 +- .../TypeScriptAxiosClientCodegenTest.java | 31 +++++++++++++++++++ .../es6-target/.openapi-generator/FILES | 1 + .../builds/es6-target/package.json | 4 ++- .../builds/es6-target/tsconfig.esm.json | 7 +++++ .../builds/es6-target/tsconfig.json | 2 +- .../package.json | 2 +- .../tsconfig.json | 2 +- .../builds/with-npm-version/package.json | 2 +- .../builds/with-npm-version/tsconfig.json | 2 +- 13 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.esm.mustache create mode 100644 samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.esm.json diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java index 41574681763b..73448a4cbef1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java @@ -266,6 +266,10 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("package.mustache", "", "package.json")); supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + // in case ECMAScript 6 is supported add another tsconfig for an ESM (ECMAScript Module) + if (supportsES6) { + supportingFiles.add(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } } @Override diff --git a/modules/openapi-generator/src/main/resources/typescript-axios/package.mustache b/modules/openapi-generator/src/main/resources/typescript-axios/package.mustache index c4fc5b8cd406..1232bced6cc3 100644 --- a/modules/openapi-generator/src/main/resources/typescript-axios/package.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-axios/package.mustache @@ -17,8 +17,12 @@ "license": "Unlicense", "main": "./dist/index.js", "typings": "./dist/index.d.ts", +{{#supportsES6}} + "module": "./dist/esm/index.js", + "sideEffects": false, +{{/supportsES6}} "scripts": { - "build": "tsc --outDir dist/", + "build": "tsc {{#supportsES6}}&& tsc -p tsconfig.esm.json{{/supportsES6}}", "prepare": "npm run build" }, "dependencies": { diff --git a/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.esm.mustache b/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.esm.mustache new file mode 100644 index 000000000000..2c0331cce040 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.esm.mustache @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm" + } +} diff --git a/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache b/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache index c17ae7c76759..d0ebbd47a6b6 100644 --- a/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache @@ -2,7 +2,7 @@ "compilerOptions": { "declaration": true, "target": "{{#supportsES6}}ES6{{/supportsES6}}{{^supportsES6}}ES5{{/supportsES6}}", - "module": "{{#supportsES6}}ES6{{/supportsES6}}{{^supportsES6}}CommonJS{{/supportsES6}}", + "module": "commonjs", "noImplicitAny": true, "outDir": "dist", "rootDir": ".", diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosClientCodegenTest.java index b7040a60171a..da55a175fbc9 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosClientCodegenTest.java @@ -1,11 +1,13 @@ package org.openapitools.codegen.typescript.axios; import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen; import org.openapitools.codegen.typescript.TypeScriptGroups; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; @Test(groups = {TypeScriptGroups.TYPESCRIPT, TypeScriptGroups.TYPESCRIPT_AXIOS}) public class TypeScriptAxiosClientCodegenTest { @@ -80,4 +82,33 @@ public class TypeScriptAxiosClientCodegenTest { assertEquals(codegen.toEnumVarName("b", "string"), "B"); } + @Test + public void containsESMTSConfigFileInCaseOfES6AndNPM() { + TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen(); + + codegen.additionalProperties().put("npmName", "@openapi/typescript-axios-petstore"); + codegen.additionalProperties().put("snapshot", false); + codegen.additionalProperties().put("npmVersion", "1.0.0-SNAPSHOT"); + codegen.setSupportsES6(true); + + codegen.processOpts(); + + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } + + @Test + public void doesNotContainESMTSConfigFileInCaseOfES5AndNPM() { + TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen(); + + codegen.additionalProperties().put("npmName", "@openapi/typescript-axios-petstore"); + codegen.additionalProperties().put("snapshot", false); + codegen.additionalProperties().put("npmVersion", "1.0.0-SNAPSHOT"); + codegen.setSupportsES6(false); + + codegen.processOpts(); + + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + assertThat(codegen.supportingFiles()).doesNotContain(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } } diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/.openapi-generator/FILES b/samples/client/petstore/typescript-axios/builds/es6-target/.openapi-generator/FILES index 534fae710fba..37591a69f79b 100644 --- a/samples/client/petstore/typescript-axios/builds/es6-target/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-axios/builds/es6-target/.openapi-generator/FILES @@ -8,4 +8,5 @@ configuration.ts git_push.sh index.ts package.json +tsconfig.esm.json tsconfig.json diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/package.json b/samples/client/petstore/typescript-axios/builds/es6-target/package.json index dc8188688f5c..97126b0ed29a 100644 --- a/samples/client/petstore/typescript-axios/builds/es6-target/package.json +++ b/samples/client/petstore/typescript-axios/builds/es6-target/package.json @@ -17,8 +17,10 @@ "license": "Unlicense", "main": "./dist/index.js", "typings": "./dist/index.d.ts", + "module": "./dist/esm/index.js", + "sideEffects": false, "scripts": { - "build": "tsc --outDir dist/", + "build": "tsc && tsc -p tsconfig.esm.json", "prepare": "npm run build" }, "dependencies": { diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.esm.json b/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.esm.json new file mode 100644 index 000000000000..2c0331cce040 --- /dev/null +++ b/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm" + } +} diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.json b/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.json index 85a028fc8492..30dc264ec731 100644 --- a/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.json +++ b/samples/client/petstore/typescript-axios/builds/es6-target/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "declaration": true, "target": "ES6", - "module": "ES6", + "module": "commonjs", "noImplicitAny": true, "outDir": "dist", "rootDir": ".", diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/package.json b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/package.json index dc8188688f5c..d64b81bc9c84 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/package.json +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/package.json @@ -18,7 +18,7 @@ "main": "./dist/index.js", "typings": "./dist/index.d.ts", "scripts": { - "build": "tsc --outDir dist/", + "build": "tsc ", "prepare": "npm run build" }, "dependencies": { diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/tsconfig.json b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/tsconfig.json index 383a290d8b6e..d953a374d812 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/tsconfig.json +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "declaration": true, "target": "ES5", - "module": "CommonJS", + "module": "commonjs", "noImplicitAny": true, "outDir": "dist", "rootDir": ".", diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version/package.json b/samples/client/petstore/typescript-axios/builds/with-npm-version/package.json index dc8188688f5c..d64b81bc9c84 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version/package.json +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version/package.json @@ -18,7 +18,7 @@ "main": "./dist/index.js", "typings": "./dist/index.d.ts", "scripts": { - "build": "tsc --outDir dist/", + "build": "tsc ", "prepare": "npm run build" }, "dependencies": { diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version/tsconfig.json b/samples/client/petstore/typescript-axios/builds/with-npm-version/tsconfig.json index 383a290d8b6e..d953a374d812 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version/tsconfig.json +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "declaration": true, "target": "ES5", - "module": "CommonJS", + "module": "commonjs", "noImplicitAny": true, "outDir": "dist", "rootDir": ".",