[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
This commit is contained in:
Minira Samadova
2022-10-07 17:14:56 +09:00
committed by GitHub
parent 74bb35d909
commit 2f8b7a054f
13 changed files with 64 additions and 8 deletions

View File

@@ -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

View File

@@ -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": {

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}
}

View File

@@ -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": ".",

View File

@@ -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"));
}
}

View File

@@ -8,4 +8,5 @@ configuration.ts
git_push.sh
index.ts
package.json
tsconfig.esm.json
tsconfig.json

View File

@@ -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": {

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}
}

View File

@@ -2,7 +2,7 @@
"compilerOptions": {
"declaration": true,
"target": "ES6",
"module": "ES6",
"module": "commonjs",
"noImplicitAny": true,
"outDir": "dist",
"rootDir": ".",

View File

@@ -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": {

View File

@@ -2,7 +2,7 @@
"compilerOptions": {
"declaration": true,
"target": "ES5",
"module": "CommonJS",
"module": "commonjs",
"noImplicitAny": true,
"outDir": "dist",
"rootDir": ".",

View File

@@ -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": {

View File

@@ -2,7 +2,7 @@
"compilerOptions": {
"declaration": true,
"target": "ES5",
"module": "CommonJS",
"module": "commonjs",
"noImplicitAny": true,
"outDir": "dist",
"rootDir": ".",