diff --git a/docs/generators/typescript.md b/docs/generators/typescript.md index 68028c6230a..5fd3402dc6c 100644 --- a/docs/generators/typescript.md +++ b/docs/generators/typescript.md @@ -20,8 +20,12 @@ When this flag is set to true: - The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document. Note to developers supporting a language generator in OpenAPITools: to fully support the discriminator attribute as defined in the OAS specification 3.x, language generators should set this flag to true by default; however this requires updating the mustache templates to generate a language-specific discriminator lookup function that iterates over {{#mappedModels}} and does not iterate over {{children}}, {{#anyOf}}, or {{#oneOf}}.| |true| |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase| +|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null| +|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null| +|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0| |platform|Specifies the platform the code should run on. The default is 'node' for the 'request' framework and 'browser' otherwise.| |null| |prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false| +|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false| |sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true| |sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true| |supportsES6|Generate code that conforms to ES6.| |false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java index d91d447183a..322d41e38d3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java @@ -33,6 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.text.SimpleDateFormat; import java.util.*; import java.util.Map.Entry; import static org.openapitools.codegen.utils.StringUtils.camelize; @@ -58,6 +59,19 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo private final Map frameworkToHttpLibMap; + // NPM Options + private static final String SNAPSHOT = "snapshot"; + @SuppressWarnings("squid:S5164") + protected static final ThreadLocal SNAPSHOT_SUFFIX_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT)); + private static final String NPM_REPOSITORY = "npmRepository"; + private static final String NPM_NAME = "npmName"; + private static final String NPM_VERSION = "npmVersion"; + + // NPM Option Values + protected String npmRepository = null; + protected String snapshot = null; + protected String npmName = null; + protected String npmVersion = "1.0.0"; protected String modelPropertyNaming = "camelCase"; protected HashSet languageGenericTypes; @@ -137,6 +151,15 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo typeMapping.put("UUID", "string"); typeMapping.put("Error", "Error"); + + cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package." + + " Required to generate a full package")); + cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package. If not provided, using the version from the OpenAPI specification file.").defaultValue(this.getNpmVersion())); + cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); + cliOptions.add(CliOption.newBoolean(SNAPSHOT, + "When setting this property to true, the version will be suffixed with -SNAPSHOT." + this.SNAPSHOT_SUFFIX_FORMAT.get().toPattern(), + false)); + cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase")); cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue("false")); cliOptions.add(new CliOption(TypeScriptClientCodegen.FILE_CONTENT_DATA_TYPE, TypeScriptClientCodegen.FILE_CONTENT_DATA_TYPE_DESC).defaultValue("Buffer")); @@ -198,12 +221,60 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo this.apiTemplateFiles.put("api/api.mustache", ".ts"); } + public String getNpmName() { + return npmName; + } + public void setNpmName(String npmName) { + this.npmName = npmName; + } + + public String getNpmRepository() { + return npmRepository; + } + + public void setNpmRepository(String npmRepository) { + this.npmRepository = npmRepository; + } + + public String getNpmVersion() { + return npmVersion; + } + + public void setNpmVersion(String npmVersion) { + this.npmVersion = npmVersion; + } + @Override public CodegenType getTag() { return CodegenType.CLIENT; } + @Override + public void preprocessOpenAPI(OpenAPI openAPI) { + + if (additionalProperties.containsKey(NPM_NAME)) { + + // If no npmVersion is provided in additional properties, version from API specification is used. + // If none of them is provided then fallbacks to default version + if (additionalProperties.containsKey(NPM_VERSION)) { + this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString()); + } else if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) { + this.setNpmVersion(openAPI.getInfo().getVersion()); + } + + if (additionalProperties.containsKey(SNAPSHOT) && Boolean.parseBoolean(additionalProperties.get(SNAPSHOT).toString())) { + if (npmVersion.toUpperCase(Locale.ROOT).matches("^.*-SNAPSHOT$")) { + this.setNpmVersion(npmVersion + "." + SNAPSHOT_SUFFIX_FORMAT.get().format(new Date())); + } else { + this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.get().format(new Date())); + } + } + additionalProperties.put(NPM_VERSION, npmVersion); + + } + } + @Override public Map postProcessSupportingFileData(Map objs) { final Object propFramework = additionalProperties.get(FRAMEWORK_SWITCH); @@ -749,6 +820,22 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo supportingFiles.add(new SupportingFile("rxjsStub.mustache", "", "rxjsStub.ts")); } + // NPM Settings + if (additionalProperties.containsKey(NPM_NAME)) { + setNpmName(additionalProperties.get(NPM_NAME).toString()); + } + + if (additionalProperties.containsKey(NPM_VERSION)) { + setNpmVersion(additionalProperties.get(NPM_VERSION).toString()); + } + + if (additionalProperties.containsKey(NPM_REPOSITORY)) { + setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString()); + } + + + + } private String getHttpLibForFramework(String object) { diff --git a/modules/openapi-generator/src/main/resources/typescript/README.mustache b/modules/openapi-generator/src/main/resources/typescript/README.mustache index ea786ff2cf6..d1c61395920 100644 --- a/modules/openapi-generator/src/main/resources/typescript/README.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/README.mustache @@ -1 +1,30 @@ -readme \ No newline at end of file +## {{npmName}}@{{npmVersion}} + +This generator creates TypeScript/JavaScript client that utilizes {{framework}}. + +### Building + +To build and compile the typescript sources to javascript use: +``` +npm install +npm run build +``` + +### Publishing + +First build the package then run ```npm publish``` + +### Consuming + +navigate to the folder of your consuming project and run one of the following commands. + +_published:_ + +``` +npm install {{npmName}}@{{npmVersion}} --save +``` + +_unPublished (not recommended):_ + +``` +npm install PATH_TO_GENERATED_PACKAGE --save diff --git a/modules/openapi-generator/src/main/resources/typescript/package.mustache b/modules/openapi-generator/src/main/resources/typescript/package.mustache index ec0e6303f98..67d11757208 100644 --- a/modules/openapi-generator/src/main/resources/typescript/package.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/package.mustache @@ -1,6 +1,6 @@ { "name": "{{npmName}}", - "version": "1.0.0", + "version": "{{npmVersion}}", "description": "OpenAPI client for {{npmName}}", "author": "OpenAPI-Generator Contributors", "keywords": [ diff --git a/samples/client/petstore/typescript/builds/default/README.md b/samples/client/petstore/typescript/builds/default/README.md index ea786ff2cf6..80b1ac5cc98 100644 --- a/samples/client/petstore/typescript/builds/default/README.md +++ b/samples/client/petstore/typescript/builds/default/README.md @@ -1 +1,30 @@ -readme \ No newline at end of file +## ts-petstore-client@1.0.0 + +This generator creates TypeScript/JavaScript client that utilizes fetch-api. + +### Building + +To build and compile the typescript sources to javascript use: +``` +npm install +npm run build +``` + +### Publishing + +First build the package then run ```npm publish``` + +### Consuming + +navigate to the folder of your consuming project and run one of the following commands. + +_published:_ + +``` +npm install ts-petstore-client@1.0.0 --save +``` + +_unPublished (not recommended):_ + +``` +npm install PATH_TO_GENERATED_PACKAGE --save diff --git a/samples/client/petstore/typescript/builds/jquery/README.md b/samples/client/petstore/typescript/builds/jquery/README.md index ea786ff2cf6..9cccd82f833 100644 --- a/samples/client/petstore/typescript/builds/jquery/README.md +++ b/samples/client/petstore/typescript/builds/jquery/README.md @@ -1 +1,30 @@ -readme \ No newline at end of file +## ts-petstore-client@1.0.0 + +This generator creates TypeScript/JavaScript client that utilizes jquery. + +### Building + +To build and compile the typescript sources to javascript use: +``` +npm install +npm run build +``` + +### Publishing + +First build the package then run ```npm publish``` + +### Consuming + +navigate to the folder of your consuming project and run one of the following commands. + +_published:_ + +``` +npm install ts-petstore-client@1.0.0 --save +``` + +_unPublished (not recommended):_ + +``` +npm install PATH_TO_GENERATED_PACKAGE --save