Skip to content

Commit 473c626

Browse files
committed
Added custom options to typescript-axios generator
- (generator/typescript-axios) Added npmName, npmVersion, and npmRepository custom options support - (templates/typescript-axios) Minor style improvements - (docs) Fixed typos in README.md
1 parent 5743a36 commit 473c626

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## Overview
88
**Swagger Codegen Generators** project is a set of classes and templates ([Handlebars](https://jknack.github.io/handlebars.java)) used by [Swagger Codegen 3.0.0 project](https://github.com/swagger-api/swagger-codegen/tree/3.0.0) in its code generation process for a specific language or language framework. The main differents with **Swagger Codegen 2.x.x** are:
99

10-
- **Handlebars as template engine:** with Handelbars feature is possible to create more logic-less templates.
10+
- **Handlebars as template engine:** with Handlebars feature is possible to create more logic-less templates.
1111
- **OAS 3 support:** generator classes work with OpenAPI Specification V3.
1212

1313
More details about these and more differences are referenced at [https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.0](https://github.com/swagger-api/swagger-codegen/releases/tag/v3.0.0)
@@ -18,16 +18,16 @@ You need the following installed and available in your $PATH:
1818
* Java 8 (http://java.oracle.com)
1919
* Apache maven 3.0.4 or greater (http://maven.apache.org/)
2020

21-
## How to contribute.
22-
Right now the templates and generators classes are migrated from [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) **3.0.0** branch.
21+
## How to Contribute.
22+
Right now the templates and generators classes are migrated from [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) **3.0.0** branch.
2323
If you want to migrate an existing language/framework, you can follow this [guide](https://github.com/swagger-api/swagger-codegen/wiki/Swagger-Codegen-migration-(swagger-codegen-generators-repository)).
24-
Also you need to keep in mind that **Handlebars** is used as template engines and besides it's pretty similar to **Mustache** there are different that can not be ignored. So you can follow this [guide](https://github.com/swagger-api/swagger-codegen/wiki/Swagger-Codegen-migration-from-Mustache-and-Handlebars-templates.) which explains steps to migrate templates from **Mustaches** to **Handelbars**.
24+
Also you need to keep in mind that **Handlebars** is used as the template engine. It's pretty similar to **Mustache**, but there are differences that can not be ignored. So you can follow this [guide](https://github.com/swagger-api/swagger-codegen/wiki/Swagger-Codegen-migration-from-Mustache-and-Handlebars-templates.) which explains steps to migrate templates from **Mustaches** to **Handlebars**.
2525

26-
## Security contact
26+
## Security Contact
2727

2828
Please disclose any security-related issues or vulnerabilities by emailing [[email protected]](mailto:[email protected]), instead of using the public issue tracker.
2929

30-
## License information on Generated Code
30+
## License Information on Generated Code
3131

3232
The Swagger Codegen project is intended as a benefit for users of the Swagger / Open API Specification. The project itself has the [License](#license) as specified. In addition, please understand the following points:
3333

src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAxiosClientCodegen.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodegen {
2020

2121
public static final String NPM_NAME = "npmName";
22+
public static final String NPM_VERSION = "npmVersion";
2223
public static final String NPM_REPOSITORY = "npmRepository";
2324
public static final String DEFAULT_API_PACKAGE = "apis";
2425
public static final String DEFAULT_MODEL_PACKAGE = "models";
2526

2627
protected String npmRepository = null;
28+
protected String npmName = null;
29+
protected String npmVersion = "1.0.0";
30+
2731

2832
private String tsModelPackage = "";
2933

@@ -34,6 +38,12 @@ public TypeScriptAxiosClientCodegen() {
3438
LOGGER.info("Template folder: " + this.templateDir());
3539
LOGGER.info("Template engine: " + this.getTemplateEngine());
3640
reservedWords.add("query");
41+
42+
// Custom CLI options
43+
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
44+
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package. Defaults to 1.0.0"));
45+
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
46+
"Use this property to set an url your private npm registry in the package.json"));
3747
}
3848

3949
@Override
@@ -46,14 +56,14 @@ public String getHelp() {
4656
return "Generates a TypeScript Axios client library.";
4757
}
4858

49-
public String getNpmRepository() {
50-
return npmRepository;
51-
}
52-
53-
public void setNpmRepository(String npmRepository) {
54-
this.npmRepository = npmRepository;
55-
}
56-
59+
/**
60+
* Creates a relative path to a file or folder. The resulting path is
61+
* relative to the root directory of the final client library.
62+
*
63+
* @param path The path to the file or folder.
64+
* @return A path to the file or folder which is relative to the client
65+
* library root directory.
66+
*/
5767
private static String getRelativeToRoot(String path) {
5868
StringBuilder sb = new StringBuilder();
5969
int slashCount = path.split("/").length;
@@ -102,6 +112,8 @@ public void processOpts() {
102112
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
103113
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
104114
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
115+
116+
addNpmPackageGeneration();
105117
}
106118

107119
@Override
@@ -194,6 +206,31 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
194206
return objs;
195207
}
196208

209+
/**
210+
* Extracts npm package fields from `additionalProperties`. These fields
211+
* are provided as custom CLI options.
212+
*/
213+
private void addNpmPackageGeneration() {
214+
// Name of the NPM package
215+
if (additionalProperties.containsKey(NPM_NAME)) {
216+
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
217+
}
218+
219+
// NPM package version (SemVer)
220+
if (additionalProperties.containsKey(NPM_VERSION)) {
221+
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
222+
}
223+
/* Package version has default value. Make internal version and
224+
* additionalProperties version consistent.
225+
*/
226+
additionalProperties.put(NPM_VERSION, npmVersion);
227+
228+
// NPM registry the package is pushed to
229+
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
230+
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
231+
}
232+
}
233+
197234
/**
198235
* Overriding toRegularExpression() to avoid escapeText() being called,
199236
* as it would return a broken regular expression if any escaped character / metacharacter were present.
@@ -217,4 +254,44 @@ public String toApiFilename(String name) {
217254
public String getDefaultTemplateDir() {
218255
return "typescript-axios";
219256
}
257+
258+
/**
259+
* Gets the name of the generated NPM package.
260+
*
261+
* @return The NPM package name.
262+
*/
263+
public String getNpmName() {
264+
return this.npmName;
265+
}
266+
267+
public void setNpmName(String npmName) {
268+
this.npmName = npmName;
269+
}
270+
271+
/**
272+
* Gets the generated NPM package SemVer string.
273+
*
274+
* @return The package version.
275+
*/
276+
public String getNpmVersion() {
277+
return this.npmVersion;
278+
}
279+
280+
public void setNpmVersion(String npmVersion) {
281+
this.npmVersion = npmVersion;
282+
}
283+
284+
/**
285+
* Gets the name of the NPM registry the package is published to.
286+
*
287+
* @return The NPM registry name.
288+
*/
289+
public String getNpmRepository() {
290+
return this.npmRepository;
291+
}
292+
293+
public void setNpmRepository(String npmRepository) {
294+
this.npmRepository = npmRepository;
295+
}
296+
220297
}

src/main/resources/handlebars/typescript-axios/apiInner.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } fr
1111
import { {{import}} } from '{{apiRelativeToRoot}}{{tsModelPackage}}';
1212
{{/imports}}
1313
{{#operations}}
14+
1415
/**
1516
* {{classname}} - axios parameter creator{{#description}}
1617
* {{&description}}{{/description}}
@@ -22,13 +23,16 @@ export const {{classname}}AxiosParamCreator = (configuration?: Configuration) =>
2223

2324
/**
2425
* {{&notes}}
26+
*
2527
{{#summary}}
2628
* @summary {{&summary}}
29+
*
2730
{{/summary}}
2831
{{#parameters}}
2932
* @param {{braces "left"}}{{{dataType}}}{{braces "right"}} {{^required}}[{{/required}}{{paramName}}{{^required}}]{{/required}} {{description}}
3033
{{/parameters}}
3134
* @param {*} [options] Override http request option.
35+
*
3236
* @throws {RequiredError}
3337
*/
3438
{{nickname}}: async ({{#parameters}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/parameters}}options: any = {}): Promise<RequestArgs> => {

src/main/resources/handlebars/typescript-axios/configuration.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,48 @@ export interface ConfigurationParameters {
1212
}
1313

1414
export class Configuration {
15+
1516
/**
1617
* parameter for apiKey security
18+
*
1719
* @param name security name
1820
* @memberof Configuration
1921
*/
2022
apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
23+
2124
/**
2225
* parameter for basic security
2326
*
2427
* @type {string}
2528
* @memberof Configuration
2629
*/
2730
username?: string;
31+
2832
/**
2933
* parameter for basic security
3034
*
3135
* @type {string}
3236
* @memberof Configuration
3337
*/
3438
password?: string;
39+
3540
/**
3641
* parameter for oauth2 security
42+
*
3743
* @param name security name
3844
* @param scopes oauth2 scope
3945
* @memberof Configuration
4046
*/
4147
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
48+
4249
/**
4350
* override base path
4451
*
4552
* @type {string}
4653
* @memberof Configuration
4754
*/
4855
basePath?: string;
56+
4957
/**
5058
* base options for axios calls
5159
*

0 commit comments

Comments
 (0)