swagger-typescript-api 11.0.0--beta-6 → 11.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # next release
2
2
 
3
+ # 11.0.1
4
+
5
+ - fix: problems with --http-client option in `generate-templates` command
6
+ - fix: rewrite file content in `generate-templates` command
7
+
3
8
  # 11.0.0
4
9
 
5
10
  ## Breaking changes
@@ -8,6 +13,10 @@
8
13
  - new codebase (class way)
9
14
  - ability to change everything in codegen process configuration with using NodeJS Api
10
15
  - ability to call `generateApi` function 2 and more times per 1 NodeJS process.
16
+ - new command `generate-templates` to create source templates
17
+
18
+ ## [feature] Ability to generate source templates
19
+ New command `generate-templates` which allow you to generate source templates which using with option `--templates`
11
20
 
12
21
  ## [feature] Ability to modify internal codegen typescript structs
13
22
  Everything which creates codegen about output typescript code now contains in `Ts` field in [`src/configuration`](src/configuration.js).
package/README.md CHANGED
@@ -34,6 +34,7 @@ All examples you can find [**here**](https://github.com/acacode/swagger-typescri
34
34
  ```muse
35
35
  Usage: sta [options]
36
36
  Usage: swagger-typescript-api [options]
37
+ Usage: swagger-typescript-api generate-templates [options]
37
38
 
38
39
  Options:
39
40
  -v, --version output the current version
@@ -73,7 +74,18 @@ Options:
73
74
  --patch fix up small errors in the swagger source definition (default: false)
74
75
  --debug additional information about processes inside this tool (default: false)
75
76
  --another-array-type generate array types as Array<Type> (by default Type[]) (default: false)
77
+ --sort-types sort fields and types (default: false)
76
78
  -h, --help display help for command
79
+
80
+ Commands:
81
+ generate-templates Generate ".ejs" templates needed for generate api
82
+ -o, --output <string> output path of generated templates
83
+ -m, --modular generate templates needed to separate files for http client, data contracts, and routes (default: false)
84
+ --http-client <string> http client type (possible values: "fetch", "axios") (default: "fetch")
85
+ -c, --clean-output clean output folder before generate template. WARNING: May cause data loss (default: false)
86
+ -r, --rewrite rewrite content in existing templates (default: false)
87
+ --silent Output only errors to console (default: false)
88
+ -h, --help display help for command
77
89
  ```
78
90
 
79
91
  Also you can use `npx`:
@@ -83,7 +95,7 @@ Also you can use `npx`:
83
95
 
84
96
  You can use this package from nodejs:
85
97
  ```js
86
- const { generateApi } = require('swagger-typescript-api');
98
+ const { generateApi, generateTemplates } = require('swagger-typescript-api');
87
99
  const path = require("path");
88
100
  const fs = require("fs");
89
101
 
@@ -158,6 +170,16 @@ generateApi({
158
170
  })
159
171
  .catch(e => console.error(e))
160
172
 
173
+
174
+ generateTemplates({
175
+ cleanOutput: false,
176
+ output: PATH_TO_OUTPUT_DIR,
177
+ httpClientType: "fetch",
178
+ modular: false,
179
+ silent: false,
180
+ rewrite: false,
181
+ })
182
+
161
183
  ```
162
184
 
163
185
 
@@ -220,6 +242,9 @@ When we change it to `--module-name-index 1` then Api class have two properties
220
242
  This option will group your API operations based on their first tag - mirroring how the Swagger UI groups displayed operations
221
243
 
222
244
 
245
+ ## `generate-templates` command
246
+ This command allows you to generate source templates which using with option `--templates`
247
+
223
248
  ## Modification internal codegen structs with NodeJS API:
224
249
 
225
250
  You are able to modify TypeScript internal structs using for generating output with using `generateApi` options `codeGenConstructs` and `primitiveTypeConstructs`.
package/index.js CHANGED
@@ -290,7 +290,14 @@ const main = async () => {
290
290
  break;
291
291
  }
292
292
  case "generate-templates": {
293
- await generateTemplates(options);
293
+ await generateTemplates({
294
+ cleanOutput: options.cleanOutput,
295
+ output: options.output,
296
+ httpClientType: options.httpClient,
297
+ modular: options.modular,
298
+ silent: options.silent,
299
+ rewrite: options.rewrite,
300
+ });
294
301
  break;
295
302
  }
296
303
  default: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "11.0.0--beta-6",
3
+ "version": "11.0.1",
4
4
  "description": "Generate typescript/javascript api from swagger schema",
5
5
  "scripts": {
6
6
  "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -56,17 +56,37 @@ class TemplatesGenProcess {
56
56
  }
57
57
 
58
58
  templates.forEach((template) => {
59
- const templateExist = this.fileSystem.pathIsExist(path.resolve(outputPath, template.name));
60
- if (!templateExist || this.config.rewrite) {
59
+ const templateName = this.fileSystem.cropExtension(template.name);
60
+ const templateEjsPath = path.resolve(outputPath, `${templateName}.ejs`);
61
+ const templateEtaPath = path.resolve(outputPath, `${templateName}.eta`);
62
+ const templateEjsPathExist = this.fileSystem.pathIsExist(templateEjsPath);
63
+ const templateEtaPathExist = this.fileSystem.pathIsExist(templateEtaPath);
64
+
65
+ if (this.config.rewrite || (!templateEjsPathExist && !templateEtaPathExist)) {
61
66
  this.fileSystem.createFile({
62
67
  path: outputPath,
63
68
  fileName: template.name,
64
69
  content: template.content,
65
70
  withPrefix: false,
66
71
  });
72
+ } else if (templateEjsPathExist) {
73
+ this.fileSystem.createFile({
74
+ path: outputPath,
75
+ fileName: `${templateName}.ejs`,
76
+ content: template.content,
77
+ withPrefix: false,
78
+ });
79
+ } else if (templateEtaPathExist) {
80
+ this.fileSystem.createFile({
81
+ path: outputPath,
82
+ fileName: `${templateName}.eta`,
83
+ content: template.content,
84
+ withPrefix: false,
85
+ });
67
86
  }
68
87
  });
69
- this.logger.event(`source templates has been successfully created in "${outputPath}"`);
88
+
89
+ this.logger.success(`source templates has been successfully created in "${outputPath}"`);
70
90
  }
71
91
 
72
92
  return {
@@ -83,23 +103,26 @@ class TemplatesGenProcess {
83
103
  const apiTemplatesPath = this.config.modular ? this.paths.moduleApiTemplates : this.paths.defaultApiTemplates;
84
104
  const apiTemplates = this.getTemplateNamesFromDir(apiTemplatesPath);
85
105
 
86
- for (const fileName of baseTemplates) {
87
- outputFiles.push({
88
- name: fileName,
89
- content: this.fixTemplateContent(this.getTemplateContent(`${this.paths.baseTemplates}/${fileName}`)),
90
- });
91
- }
92
-
93
106
  const usingHttpClientTemplate = httpClientTemplates.find((template) =>
94
107
  template.startsWith(`${this.config.httpClientType}-`),
95
108
  );
96
109
 
110
+ let httpClientTemplateContent = "";
111
+
97
112
  if (usingHttpClientTemplate) {
113
+ httpClientTemplateContent = this.fixTemplateContent(
114
+ this.getTemplateContent(`${this.paths.httpClientTemplates}/${usingHttpClientTemplate}`),
115
+ );
116
+ }
117
+
118
+ for (const fileName of baseTemplates) {
119
+ const templateContent =
120
+ (fileName === "http-client.ejs" && httpClientTemplateContent) ||
121
+ this.fixTemplateContent(this.getTemplateContent(`${this.paths.baseTemplates}/${fileName}`));
122
+
98
123
  outputFiles.push({
99
- name: usingHttpClientTemplate,
100
- content: this.fixTemplateContent(
101
- this.getTemplateContent(`${this.paths.httpClientTemplates}/${usingHttpClientTemplate}`),
102
- ),
124
+ name: fileName,
125
+ content: templateContent,
103
126
  });
104
127
  }
105
128