swagger-typescript-api 12.0.4 → 13.0.0

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.
Files changed (75) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +1 -1
  3. package/cli/constants.js +3 -3
  4. package/cli/execute.js +52 -31
  5. package/cli/index.d.ts +1 -2
  6. package/cli/index.js +18 -17
  7. package/cli/operations/display-help.js +51 -29
  8. package/cli/parse-args.js +3 -3
  9. package/cli/process-option.js +28 -20
  10. package/index.d.ts +99 -7
  11. package/index.js +158 -136
  12. package/package.json +35 -30
  13. package/src/code-formatter.js +28 -13
  14. package/src/code-gen-process.js +367 -264
  15. package/src/commands/generate-templates/configuration.js +2 -2
  16. package/src/commands/generate-templates/index.js +1 -2
  17. package/src/commands/generate-templates/templates-gen-process.js +62 -35
  18. package/src/component-type-name-resolver.js +44 -0
  19. package/src/configuration.js +166 -98
  20. package/src/constants.js +28 -22
  21. package/src/index.js +3 -4
  22. package/src/schema-components-map.js +39 -23
  23. package/src/schema-parser/base-schema-parsers/array.js +43 -0
  24. package/src/schema-parser/base-schema-parsers/complex.js +51 -0
  25. package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
  26. package/src/schema-parser/base-schema-parsers/enum.js +158 -0
  27. package/src/schema-parser/base-schema-parsers/object.js +105 -0
  28. package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
  29. package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
  30. package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
  31. package/src/schema-parser/complex-schema-parsers/not.js +9 -0
  32. package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
  33. package/src/schema-parser/mono-schema-parser.js +48 -0
  34. package/src/schema-parser/schema-formatters.js +58 -44
  35. package/src/schema-parser/schema-parser-fabric.js +131 -0
  36. package/src/schema-parser/schema-parser.js +212 -361
  37. package/src/schema-parser/schema-utils.js +158 -33
  38. package/src/schema-parser/util/enum-key-resolver.js +26 -0
  39. package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
  40. package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
  41. package/src/schema-walker.js +93 -0
  42. package/src/swagger-schema-resolver.js +61 -28
  43. package/src/templates-worker.js +240 -0
  44. package/src/translators/javascript.js +83 -0
  45. package/src/translators/translator.js +35 -0
  46. package/src/{type-name.js → type-name-formatter.js} +35 -20
  47. package/src/util/file-system.js +30 -14
  48. package/src/util/id.js +2 -2
  49. package/src/util/internal-case.js +1 -1
  50. package/src/util/logger.js +46 -20
  51. package/src/util/name-resolver.js +50 -58
  52. package/src/util/object-assign.js +7 -3
  53. package/src/util/pascal-case.js +1 -1
  54. package/src/util/request.js +5 -5
  55. package/src/util/sort-by-property.js +17 -0
  56. package/templates/base/data-contract-jsdoc.ejs +37 -37
  57. package/templates/base/data-contracts.ejs +40 -28
  58. package/templates/base/enum-data-contract.ejs +12 -12
  59. package/templates/base/http-client.ejs +2 -2
  60. package/templates/base/http-clients/axios-http-client.ejs +139 -138
  61. package/templates/base/http-clients/fetch-http-client.ejs +224 -224
  62. package/templates/base/interface-data-contract.ejs +10 -10
  63. package/templates/base/object-field-jsdoc.ejs +28 -28
  64. package/templates/base/route-docs.ejs +30 -30
  65. package/templates/base/route-name.ejs +42 -42
  66. package/templates/base/route-type.ejs +22 -21
  67. package/templates/base/type-data-contract.ejs +15 -15
  68. package/templates/default/api.ejs +69 -65
  69. package/templates/default/procedure-call.ejs +100 -100
  70. package/templates/default/route-types.ejs +32 -28
  71. package/templates/modular/api.ejs +28 -28
  72. package/templates/modular/procedure-call.ejs +100 -100
  73. package/templates/modular/route-types.ejs +18 -18
  74. package/src/templates.js +0 -177
  75. package/src/translators/JavaScript.js +0 -60
package/package.json CHANGED
@@ -1,22 +1,25 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "12.0.4",
3
+ "version": "13.0.0",
4
4
  "description": "Generate typescript/javascript api from swagger schema",
5
5
  "scripts": {
6
+ "update-deps-to-latest": "npx --yes npm-check-updates && npm i",
7
+ "lint": "eslint .",
8
+ "lint:fix": "eslint . --fix",
6
9
  "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
7
10
  "cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8
11
  "node": "node swagger-test-cli/generate.js",
9
12
  "node:debug": "node --nolazy swagger-test-cli/generate.js",
10
13
  "contributors": "all-contributors generate",
11
14
  "cli:help": "node index.js -h",
12
- "test-all": "node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
13
- "test-all(update-snapshots)": "cross-env UPDATE_SNAPSHOTS=true node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
14
- "test-all-extended": "node --unhandled-rejections=strict ./scriptsRunner.js generate-extended validate generate validate test:*",
15
- "test-specific": "node ./scriptsRunner.js generate validate test:*",
16
- "test-specific-only": "node ./scriptsRunner.js test:*",
15
+ "test-all": "npm run test-extended && npm run test-simple && npm run test-specific",
16
+ "test-all(update-snapshots)": "cross-env UPDATE_SNAPSHOTS=true npm run test-all",
17
+ "test-simple": "node --unhandled-rejections=strict scripts_runner.js generate validate",
18
+ "test-extended": "node --unhandled-rejections=strict scripts_runner.js generate-extended validate",
19
+ "test-specific": "node --unhandled-rejections=strict scripts_runner.js test:*",
20
+ "test-specific(update-snapshots)": "cross-env UPDATE_SNAPSHOTS=true node --unhandled-rejections=strict scripts_runner.js test:*",
17
21
  "generate": "node tests/generate.js",
18
22
  "generate-extended": "node tests/generate-extended.js",
19
- "generate:debug": "node --nolazy tests/generate.js",
20
23
  "validate": "node tests/validate.js",
21
24
  "validate:debug": "node --nolazy tests/validate.js",
22
25
  "test:--route-types": "node tests/spec/routeTypes/test.js",
@@ -47,7 +50,7 @@
47
50
  "test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
48
51
  "test:--dot-path-params": "node tests/spec/dot-path-params/test.js",
49
52
  "test:--primitive-type-constructs": "node tests/spec/primitive-type-constructs/test.js",
50
- "test:--cli": "node index.js -p tests/spec/cli/schema.json -o tests/spec/cli -n schema.ts --extract-response-body --extract-response-error --api-class-name MySuperApi --type-prefix Prefix && node tests/spec/cli/test.js",
53
+ "test:--cli": "rimraf tests/spec/cli/schema.ts && node index.js -p tests/spec/cli/schema.json -o tests/spec/cli -n schema.ts --extract-response-body --extract-response-error --api-class-name MySuperApi --type-prefix Prefix && node tests/spec/cli/test.js",
51
54
  "test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
52
55
  "test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
53
56
  "test:--patch": "node tests/spec/patch/test.js",
@@ -59,49 +62,51 @@
59
62
  "test:another-query-params": "node tests/spec/another-query-params/test.js",
60
63
  "test:on-insert-path-param": "node tests/spec/on-insert-path-param/test.js",
61
64
  "test:extra-templates": "node tests/spec/extra-templates/test.js",
62
- "test:extract-enums": "node tests/spec/extract-enums/test.js"
65
+ "test:extract-enums": "node tests/spec/extract-enums/test.js",
66
+ "test:discriminator": "node tests/spec/discriminator/test.js",
67
+ "test:sort-types": "node tests/spec/sortTypes/test.js",
68
+ "test:sort-types-false": "node tests/spec/sortTypes-false/test.js"
63
69
  },
64
70
  "author": "acacode",
65
71
  "license": "MIT",
66
72
  "typings": "./index.d.ts",
67
73
  "main": "src/index.js",
68
74
  "devDependencies": {
69
- "@types/lodash": "^4.14.186",
70
- "@types/node": "^18.11.7",
71
- "@types/prettier": "^2.7.1",
72
- "all-contributors-cli": "^6.20.0",
73
- "axios": "^1.1.3",
75
+ "eslint": "^8.44.0",
76
+ "eslint-config-prettier": "^8.8.0",
77
+ "eslint-plugin-prettier": "^5.0.0",
78
+ "@types/lodash": "^4.14.195",
79
+ "@types/node": "^20.4.1",
80
+ "@types/prettier": "^2.7.3",
81
+ "all-contributors-cli": "^6.26.1",
82
+ "axios": "^1.4.0",
74
83
  "cross-env": "^7.0.3",
84
+ "dotenv": "^16.3.1",
75
85
  "git-diff": "^2.0.6",
76
- "husky": "^4.3.6",
77
- "pretty-quick": "^3.1.0"
86
+ "husky": "^8.0.3",
87
+ "pretty-quick": "^3.1.3",
88
+ "rimraf": "^5.0.1"
78
89
  },
79
90
  "dependencies": {
80
91
  "@types/swagger-schema-official": "2.0.22",
81
- "cosmiconfig": "7.0.1",
92
+ "cosmiconfig": "8.2.0",
82
93
  "didyoumean": "^1.2.2",
83
- "eta": "^2.0.0",
94
+ "eta": "^2.2.0",
84
95
  "js-yaml": "4.1.0",
85
96
  "lodash": "4.17.21",
86
- "make-dir": "3.1.0",
87
- "nanoid": "3.3.4",
88
- "node-emoji": "1.11.0",
89
- "node-fetch": "^3.2.10",
90
- "prettier": "2.7.1",
97
+ "make-dir": "4.0.0",
98
+ "nanoid": "3.3.6",
99
+ "node-emoji": "2.1.0",
100
+ "node-fetch": "^3.3.1",
101
+ "prettier": "3.0.0",
91
102
  "swagger-schema-official": "2.0.0-bab6bed",
92
103
  "swagger2openapi": "7.0.8",
93
- "typescript": "4.8.4"
104
+ "typescript": "5.1.6"
94
105
  },
95
106
  "bin": {
96
107
  "swagger-typescript-api": "index.js",
97
108
  "sta": "index.js"
98
109
  },
99
- "husky": {
100
- "hooks": {
101
- "pre-commit": "pretty-quick --staged",
102
- "post-commit": "git update-index -g"
103
- }
104
- },
105
110
  "keywords": [
106
111
  "openapi",
107
112
  "swagger",
@@ -1,6 +1,6 @@
1
- const _ = require("lodash");
2
- const ts = require("typescript");
3
- const prettier = require("prettier");
1
+ const _ = require('lodash');
2
+ const ts = require('typescript');
3
+ const prettier = require('prettier');
4
4
 
5
5
  class CodeFormatter {
6
6
  /**
@@ -8,18 +8,18 @@ class CodeFormatter {
8
8
  */
9
9
  config;
10
10
 
11
- constructor(config) {
11
+ constructor({ config }) {
12
12
  this.config = config;
13
13
  }
14
14
 
15
15
  removeUnusedImports = (content) => {
16
- const tempFileName = "file.ts";
16
+ const tempFileName = 'file.ts';
17
17
 
18
18
  const host = new TsLanguageServiceHost(tempFileName, content);
19
19
  const languageService = ts.createLanguageService(host);
20
20
 
21
21
  const fileTextChanges = languageService.organizeImports(
22
- { type: "file", fileName: tempFileName },
22
+ { type: 'file', fileName: tempFileName },
23
23
  { newLineCharacter: ts.sys.newLine },
24
24
  )[0];
25
25
 
@@ -27,7 +27,9 @@ class CodeFormatter {
27
27
  return _.reduceRight(
28
28
  fileTextChanges.textChanges,
29
29
  (content, { span, newText }) =>
30
- `${content.slice(0, span.start)}${newText}${content.slice(span.start + span.length)}`,
30
+ `${content.slice(0, span.start)}${newText}${content.slice(
31
+ span.start + span.length,
32
+ )}`,
31
33
  content,
32
34
  );
33
35
  }
@@ -35,16 +37,27 @@ class CodeFormatter {
35
37
  return content;
36
38
  };
37
39
 
38
- prettierFormat = (content) => {
39
- return prettier.format(content, this.config.prettierOptions);
40
+ /**
41
+ * @param content
42
+ * @returns {Promise<string>}
43
+ */
44
+ prettierFormat = async (content) => {
45
+ const formatted = await prettier.format(
46
+ content,
47
+ this.config.prettierOptions,
48
+ );
49
+ return formatted;
40
50
  };
41
51
 
42
- formatCode = (code, { removeUnusedImports = true, prettierFormat = true } = {}) => {
52
+ formatCode = async (
53
+ code,
54
+ { removeUnusedImports = true, prettierFormat = true } = {},
55
+ ) => {
43
56
  if (removeUnusedImports) {
44
57
  code = this.removeUnusedImports(code);
45
58
  }
46
59
  if (prettierFormat) {
47
- code = this.prettierFormat(code);
60
+ code = await this.prettierFormat(code);
48
61
  }
49
62
  return code;
50
63
  };
@@ -58,13 +71,15 @@ class TsLanguageServiceHost {
58
71
  fileName,
59
72
  content,
60
73
  compilerOptions: tsconfig
61
- ? ts.convertCompilerOptionsFromJson(ts.readConfigFile(tsconfig, ts.sys.readFile).config.compilerOptions).options
74
+ ? ts.convertCompilerOptionsFromJson(
75
+ ts.readConfigFile(tsconfig, ts.sys.readFile).config.compilerOptions,
76
+ ).options
62
77
  : ts.getDefaultCompilerOptions(),
63
78
  });
64
79
  }
65
80
 
66
81
  getNewLine() {
67
- return "newLine" in ts.sys ? ts.sys.newLine : "\n";
82
+ return 'newLine' in ts.sys ? ts.sys.newLine : '\n';
68
83
  }
69
84
  getScriptFileNames() {
70
85
  return [this.fileName];