swagger-typescript-api 10.0.1 → 10.0.3

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 (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +288 -262
  3. package/index.d.ts +11 -1
  4. package/index.js +2 -0
  5. package/package.json +119 -114
  6. package/src/components.js +3 -5
  7. package/src/config.js +6 -0
  8. package/src/constants.js +7 -0
  9. package/src/files.js +6 -6
  10. package/src/formatFileContent.js +13 -6
  11. package/src/index.js +8 -7
  12. package/src/logger.js +9 -0
  13. package/src/modelNames.js +1 -5
  14. package/src/modelTypes.js +7 -6
  15. package/src/output.js +22 -23
  16. package/src/render/utils/index.js +9 -1
  17. package/src/routes.js +4 -1
  18. package/src/schema.js +87 -64
  19. package/src/swagger.js +4 -1
  20. package/src/templates.js +46 -23
  21. package/src/translators/JavaScript.js +3 -14
  22. package/src/typeFormatters.js +81 -35
  23. package/src/utils/resolveName.js +1 -4
  24. package/templates/README.md +17 -13
  25. package/templates/base/README.md +7 -7
  26. package/templates/base/data-contract-jsdoc.ejs +32 -0
  27. package/templates/base/data-contracts.ejs +28 -0
  28. package/templates/base/enum-data-contract.ejs +15 -0
  29. package/templates/base/{http-client.eta → http-client.ejs} +2 -2
  30. package/templates/base/http-clients/{axios-http-client.eta → axios-http-client.ejs} +133 -145
  31. package/templates/base/http-clients/{fetch-http-client.eta → fetch-http-client.ejs} +222 -222
  32. package/templates/base/interface-data-contract.ejs +10 -0
  33. package/templates/base/object-field-jsdoc.ejs +28 -0
  34. package/templates/base/{route-docs.eta → route-docs.ejs} +30 -31
  35. package/templates/base/{route-name.eta → route-name.ejs} +42 -42
  36. package/templates/base/{route-type.eta → route-type.ejs} +21 -21
  37. package/templates/base/type-data-contract.ejs +15 -0
  38. package/templates/default/README.md +6 -6
  39. package/templates/default/{api.eta → api.ejs} +65 -65
  40. package/templates/default/{procedure-call.eta → procedure-call.ejs} +99 -99
  41. package/templates/default/{route-types.eta → route-types.ejs} +28 -28
  42. package/templates/modular/README.md +6 -6
  43. package/templates/modular/{api.eta → api.ejs} +28 -28
  44. package/templates/modular/{procedure-call.eta → procedure-call.ejs} +99 -99
  45. package/templates/modular/{route-types.eta → route-types.ejs} +18 -18
  46. package/CHANGELOG.md +0 -872
  47. package/templates/base/data-contracts.eta +0 -45
package/index.d.ts CHANGED
@@ -44,6 +44,11 @@ interface GenerateApiParamsBase {
44
44
  */
45
45
  generateResponses?: boolean;
46
46
 
47
+ /**
48
+ * unwrap the data item from the response
49
+ */
50
+ unwrapResponseData?: boolean;
51
+
47
52
  /**
48
53
  * generate js api module with declaration file (default: false)
49
54
  */
@@ -118,6 +123,10 @@ interface GenerateApiParamsBase {
118
123
  * authorization token
119
124
  */
120
125
  authorizationToken?: string;
126
+ /**
127
+ * generate readonly properties (default: false)
128
+ */
129
+ addReadonly?: boolean;
121
130
  }
122
131
 
123
132
  interface GenerateApiParamsFromPath extends GenerateApiParamsBase {
@@ -329,6 +338,7 @@ export interface GenerateApiConfiguration {
329
338
  disableStrictSSL: boolean;
330
339
  disableProxy: boolean;
331
340
  extractRequestParams: boolean;
341
+ unwrapResponseData: boolean;
332
342
  fileNames: {
333
343
  dataContracts: string;
334
344
  routeTypes: string;
@@ -343,7 +353,7 @@ export interface GenerateApiConfiguration {
343
353
  routeName: string;
344
354
  };
345
355
  routeNameDuplicatesMap: Map<string, string>;
346
- apiClassName: string
356
+ apiClassName: string;
347
357
  };
348
358
  modelTypes: ModelType[];
349
359
  rawModelTypes: SchemaComponent[];
package/index.js CHANGED
@@ -67,6 +67,7 @@ const options = program
67
67
  .option("--clean-output", "clean output folder before generate api. WARNING: May cause data loss", false)
68
68
  .option("--api-class-name <string>", "name of the api class")
69
69
  .option("--patch", "fix up small errors in the swagger source definition", false)
70
+ .option("--debug", "additional information about processes inside this tool", false)
70
71
  .parse(process.argv)
71
72
  .opts();
72
73
 
@@ -105,6 +106,7 @@ generateApi({
105
106
  typeSuffix: options.typeSuffix,
106
107
  patch: !!options.patch,
107
108
  apiClassName: options.apiClassName,
109
+ debug: options.debug,
108
110
  }).catch((err) => {
109
111
  // NOTE collect all errors on top level and shows to users in any case
110
112
  console.error(err);
package/package.json CHANGED
@@ -1,114 +1,119 @@
1
- {
2
- "name": "swagger-typescript-api",
3
- "version": "10.0.1",
4
- "description": "Generate typescript/javascript api from swagger schema",
5
- "scripts": {
6
- "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
7
- "cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8
- "node": "node swagger-test-cli/generate.js",
9
- "node:debug": "node --nolazy swagger-test-cli/generate.js",
10
- "contributors": "all-contributors generate",
11
- "cli:help": "node index.js -h",
12
- "test-all": "node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
13
- "test-all-extended": "node --unhandled-rejections=strict ./scriptsRunner.js generate-extended validate generate validate test:*",
14
- "test-specific": "node ./scriptsRunner.js generate validate test:*",
15
- "prepare": "npm run test-all-extended",
16
- "generate": "node tests/generate.js",
17
- "generate-extended": "node tests/generate-extended.js",
18
- "generate:debug": "node --nolazy tests/generate.js",
19
- "validate": "node tests/validate.js",
20
- "validate:debug": "node --nolazy tests/validate.js",
21
- "test:--route-types": "node tests/spec/routeTypes/test.js",
22
- "test:--no-client": "node tests/spec/noClient/test.js",
23
- "test:--default-as-success": "node tests/spec/defaultAsSuccess/test.js",
24
- "test:--templates": "node tests/spec/templates/test.js",
25
- "test:--union-enums": "node tests/spec/unionEnums/test.js",
26
- "test:--add-readonly": "node tests/spec/readonly/test.js",
27
- "test:--responses": "node tests/spec/responses/test.js",
28
- "test:specProperty": "node tests/spec/specProperty/test.js",
29
- "test:--module-name-index": "node tests/spec/moduleNameIndex/test.js",
30
- "test:--module-name-first-tag": "node tests/spec/moduleNameFirstTag/test.js",
31
- "test:--modular": "node tests/spec/modular/test.js",
32
- "test:--single-http-client": "node tests/spec/singleHttpClient/test.js",
33
- "test:--extract-request-params": "node tests/spec/extractRequestParams/test.js",
34
- "test:--extract-request-body": "node tests/spec/extractRequestBody/test.js",
35
- "test:--extract-response-body": "node tests/spec/extractResponseBody/test.js",
36
- "test:--extract-response-error": "node tests/spec/extractResponseError/test.js",
37
- "test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
38
- "test:--default-response": "node tests/spec/defaultResponse/test.js",
39
- "test:--js": "node tests/spec/js/test.js",
40
- "test:--js--axios": "node tests/spec/jsAxios/test.js",
41
- "test:--axios": "node tests/spec/axios/test.js",
42
- "test:--axios--single-http-client": "node tests/spec/axiosSingleHttpClient/test.js",
43
- "test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
44
- "test:--cli": "node index.js -p tests/spec/cli/schema.json -o tests/spec/cli -n schema.ts --extract-response-body --extract-response-error --type-prefix Prefix --api-class-name MySuperApi --no-client",
45
- "test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
46
- "test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
47
- "test:--patch": "node tests/spec/patch/test.js"
48
- },
49
- "author": "acacode",
50
- "license": "MIT",
51
- "typings": "./index.d.ts",
52
- "main": "src/index.js",
53
- "devDependencies": {
54
- "@types/axios": "^0.14.0",
55
- "@types/lodash": "^4.14.182",
56
- "@types/node": "^15.0.2",
57
- "@types/prettier": "^2.7.0",
58
- "all-contributors-cli": "^6.20.0",
59
- "husky": "^4.3.6",
60
- "pretty-quick": "^3.1.0"
61
- },
62
- "dependencies": {
63
- "axios": "^0.27.2",
64
- "@types/swagger-schema-official": "2.0.22",
65
- "commander": "^9.4.0",
66
- "cosmiconfig": "^7.0.1",
67
- "eta": "^1.12.3",
68
- "js-yaml": "^4.1.0",
69
- "lodash": "^4.17.21",
70
- "make-dir": "^3.1.0",
71
- "nanoid": "^3.3.4",
72
- "node-emoji": "^1.11.0",
73
- "prettier": "^2.7.1",
74
- "swagger-schema-official": "2.0.0-bab6bed",
75
- "swagger2openapi": "^7.0.8",
76
- "typescript": "^4.7.4"
77
- },
78
- "bin": {
79
- "swagger-typescript-api": "index.js",
80
- "sta": "index.js"
81
- },
82
- "husky": {
83
- "hooks": {
84
- "pre-commit": "pretty-quick --staged",
85
- "post-commit": "git update-index -g"
86
- }
87
- },
88
- "keywords": [
89
- "openapi",
90
- "swagger",
91
- "typescript",
92
- "api",
93
- "javascript",
94
- "rest",
95
- "codegen",
96
- "generation",
97
- "http"
98
- ],
99
- "files": [
100
- "src",
101
- "index.js",
102
- "index.d.ts",
103
- "templates",
104
- "LICENSE"
105
- ],
106
- "bugs": {
107
- "url": "https://github.com/acacode/swagger-typescript-api/issues"
108
- },
109
- "homepage": "https://github.com/acacode/swagger-typescript-api",
110
- "repository": {
111
- "type": "git",
112
- "url": "git://github.com/acacode/swagger-typescript-api"
113
- }
114
- }
1
+ {
2
+ "name": "swagger-typescript-api",
3
+ "version": "10.0.3",
4
+ "description": "Generate typescript/javascript api from swagger schema",
5
+ "scripts": {
6
+ "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
7
+ "cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8
+ "node": "node swagger-test-cli/generate.js",
9
+ "node:debug": "node --nolazy swagger-test-cli/generate.js",
10
+ "contributors": "all-contributors generate",
11
+ "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:*",
17
+ "prepare": "npm run test-all-extended",
18
+ "generate": "node tests/generate.js",
19
+ "generate-extended": "node tests/generate-extended.js",
20
+ "generate:debug": "node --nolazy tests/generate.js",
21
+ "validate": "node tests/validate.js",
22
+ "validate:debug": "node --nolazy tests/validate.js",
23
+ "test:--route-types": "node tests/spec/routeTypes/test.js",
24
+ "test:--no-client": "node tests/spec/noClient/test.js",
25
+ "test:--default-as-success": "node tests/spec/defaultAsSuccess/test.js",
26
+ "test:--templates": "node tests/spec/templates/test.js",
27
+ "test:--union-enums": "node tests/spec/unionEnums/test.js",
28
+ "test:--add-readonly": "node tests/spec/readonly/test.js",
29
+ "test:--responses": "node tests/spec/responses/test.js",
30
+ "test:specProperty": "node tests/spec/specProperty/test.js",
31
+ "test:--module-name-index": "node tests/spec/moduleNameIndex/test.js",
32
+ "test:--module-name-first-tag": "node tests/spec/moduleNameFirstTag/test.js",
33
+ "test:--modular": "node tests/spec/modular/test.js",
34
+ "test:--single-http-client": "node tests/spec/singleHttpClient/test.js",
35
+ "test:--extract-request-params": "node tests/spec/extractRequestParams/test.js",
36
+ "test:--extract-request-body": "node tests/spec/extractRequestBody/test.js",
37
+ "test:--extract-response-body": "node tests/spec/extractResponseBody/test.js",
38
+ "test:--extract-response-error": "node tests/spec/extractResponseError/test.js",
39
+ "test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
40
+ "test:--default-response": "node tests/spec/defaultResponse/test.js",
41
+ "test:--js": "node tests/spec/js/test.js",
42
+ "test:--js--axios": "node tests/spec/jsAxios/test.js",
43
+ "test:--axios": "node tests/spec/axios/test.js",
44
+ "test:--object-types": "node tests/spec/object-types/test.js",
45
+ "test:--axios--single-http-client": "node tests/spec/axiosSingleHttpClient/test.js",
46
+ "test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
47
+ "test:--cli": "node index.js -p tests/spec/cli/schema.json -o tests/spec/cli -n schema.ts --extract-response-body --extract-response-error --type-prefix Prefix --api-class-name MySuperApi --no-client",
48
+ "test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
49
+ "test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
50
+ "test:--patch": "node tests/spec/patch/test.js"
51
+ },
52
+ "author": "acacode",
53
+ "license": "MIT",
54
+ "typings": "./index.d.ts",
55
+ "main": "src/index.js",
56
+ "devDependencies": {
57
+ "cross-env": "^7.0.3",
58
+ "@types/axios": "^0.14.0",
59
+ "@types/lodash": "^4.14.182",
60
+ "@types/node": "^15.0.2",
61
+ "@types/prettier": "^2.7.0",
62
+ "all-contributors-cli": "^6.20.0",
63
+ "git-diff": "^2.0.6",
64
+ "husky": "^4.3.6",
65
+ "pretty-quick": "^3.1.0"
66
+ },
67
+ "dependencies": {
68
+ "@types/swagger-schema-official": "2.0.22",
69
+ "axios": "0.27.2",
70
+ "commander": "9.4.1",
71
+ "cosmiconfig": "7.0.1",
72
+ "eta": "1.12.3",
73
+ "js-yaml": "4.1.0",
74
+ "lodash": "4.17.21",
75
+ "make-dir": "3.1.0",
76
+ "nanoid": "3.3.4",
77
+ "node-emoji": "1.11.0",
78
+ "prettier": "2.7.1",
79
+ "swagger-schema-official": "2.0.0-bab6bed",
80
+ "swagger2openapi": "7.0.8",
81
+ "typescript": "4.8.4"
82
+ },
83
+ "bin": {
84
+ "swagger-typescript-api": "index.js",
85
+ "sta": "index.js"
86
+ },
87
+ "husky": {
88
+ "hooks": {
89
+ "pre-commit": "pretty-quick --staged",
90
+ "post-commit": "git update-index -g"
91
+ }
92
+ },
93
+ "keywords": [
94
+ "openapi",
95
+ "swagger",
96
+ "typescript",
97
+ "api",
98
+ "javascript",
99
+ "rest",
100
+ "codegen",
101
+ "generation",
102
+ "http"
103
+ ],
104
+ "files": [
105
+ "src",
106
+ "index.js",
107
+ "index.d.ts",
108
+ "templates",
109
+ "LICENSE"
110
+ ],
111
+ "bugs": {
112
+ "url": "https://github.com/acacode/swagger-typescript-api/issues"
113
+ },
114
+ "homepage": "https://github.com/acacode/swagger-typescript-api",
115
+ "repository": {
116
+ "type": "git",
117
+ "url": "git://github.com/acacode/swagger-typescript-api"
118
+ }
119
+ }
package/src/components.js CHANGED
@@ -47,9 +47,7 @@ const createComponentsMap = (components) => {
47
47
  config.componentsMap = {};
48
48
 
49
49
  _.each(components, (component, componentName) =>
50
- _.each(component, (rawTypeData, typeName) =>
51
- createComponent(componentName, typeName, rawTypeData),
52
- ),
50
+ _.each(component, (rawTypeData, typeName) => createComponent(componentName, typeName, rawTypeData)),
53
51
  );
54
52
 
55
53
  return config.componentsMap;
@@ -80,9 +78,9 @@ const getTypeData = (typeInfo) => {
80
78
  /**
81
79
  *
82
80
  * @param {string} ref
83
- * @returns {TypeInfo | undefined}
81
+ * @returns {TypeInfo | null}
84
82
  */
85
- const getComponentByRef = (ref) => config.componentsMap[ref];
83
+ const getComponentByRef = (ref) => config.componentsMap[ref] || null;
86
84
 
87
85
  module.exports = {
88
86
  getTypeData,
package/src/config.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");
2
2
  const { NameResolver } = require("./utils/resolveName");
3
+ const packageJson = require("../package.json");
3
4
 
4
5
  const config = {
6
+ version: packageJson.version,
5
7
  /** CLI flag */
6
8
  templates: "../templates/default",
7
9
  /** CLI flag */
@@ -93,6 +95,10 @@ const config = {
93
95
  componentTypeNameResolver: new NameResolver([]),
94
96
  /** name of the main exported class */
95
97
  apiClassName: "Api",
98
+ debug: false,
99
+ internalTemplateOptions: {
100
+ addUtilRequiredKeysType: false,
101
+ },
96
102
  };
97
103
 
98
104
  /** needs to use data everywhere in project */
package/src/constants.js CHANGED
@@ -13,6 +13,12 @@ const TS_KEYWORDS = {
13
13
  TYPE: "type",
14
14
  ENUM: "enum",
15
15
  INTERFACE: "interface",
16
+ TYPE_AND_OPERATOR: " & ",
17
+ TYPE_OR_OPERATOR: " | ",
18
+ };
19
+
20
+ const TS_EXTERNAL = {
21
+ RECORD: "Record<string, any>",
16
22
  };
17
23
 
18
24
  const JS_PRIMITIVE_TYPES = [TS_KEYWORDS.NUMBER, TS_KEYWORDS.STRING, TS_KEYWORDS.BOOLEAN];
@@ -49,6 +55,7 @@ module.exports = {
49
55
  JS_PRIMITIVE_TYPES,
50
56
  JS_EMPTY_TYPES,
51
57
  TS_KEYWORDS,
58
+ TS_EXTERNAL,
52
59
  SCHEMA_TYPES,
53
60
  HTTP_CLIENT,
54
61
  RESERVED_QUERY_ARG_NAMES,
package/src/files.js CHANGED
@@ -21,7 +21,11 @@ const pathIsDir = (path) => {
21
21
 
22
22
  const removeDir = (path) => {
23
23
  try {
24
- fs.rmdirSync(path, { recursive: true });
24
+ if (typeof fs.rmSync === "function") {
25
+ fs.rmSync(path, { recursive: true });
26
+ } else {
27
+ fs.rmdirSync(path, { recursive: true });
28
+ }
25
29
  } catch (e) {}
26
30
  };
27
31
 
@@ -39,11 +43,7 @@ const cleanDir = (path) => {
39
43
  const pathIsExist = (path) => path && fs.existsSync(path);
40
44
 
41
45
  const createFile = ({ path, fileName, content, withPrefix }) =>
42
- fs.writeFileSync(
43
- resolve(__dirname, path, `./${fileName}`),
44
- `${withPrefix ? filePrefix : ""}${content}`,
45
- _.noop,
46
- );
46
+ fs.writeFileSync(resolve(__dirname, path, `./${fileName}`), `${withPrefix ? filePrefix : ""}${content}`, _.noop);
47
47
 
48
48
  module.exports = {
49
49
  createFile,
@@ -11,15 +11,13 @@ class LanguageServiceHost {
11
11
  fileName,
12
12
  content,
13
13
  compilerOptions: tsconfig
14
- ? ts.convertCompilerOptionsFromJson(
15
- ts.readConfigFile(tsconfig, ts.sys.readFile).config.compilerOptions,
16
- ).options
14
+ ? ts.convertCompilerOptionsFromJson(ts.readConfigFile(tsconfig, ts.sys.readFile).config.compilerOptions).options
17
15
  : ts.getDefaultCompilerOptions(),
18
16
  });
19
17
  }
20
18
 
21
19
  getNewLine() {
22
- return "\n";
20
+ return "newLine" in ts.sys ? ts.sys.newLine : "\n";
23
21
  }
24
22
  getScriptFileNames() {
25
23
  return [this.fileName];
@@ -39,6 +37,16 @@ class LanguageServiceHost {
39
37
  getScriptSnapshot() {
40
38
  return ts.ScriptSnapshot.fromString(this.content);
41
39
  }
40
+ readFile(fileName, encoding) {
41
+ if (fileName === this.fileName) {
42
+ return this.content;
43
+ }
44
+
45
+ return ts.sys.readFile(fileName, encoding);
46
+ }
47
+ fileExists(path) {
48
+ return ts.sys.fileExists(path);
49
+ }
42
50
  }
43
51
 
44
52
  const removeUnusedImports = (content) => {
@@ -70,5 +78,4 @@ const prettierFormat = (content) => {
70
78
 
71
79
  const formatters = [removeUnusedImports, prettierFormat];
72
80
 
73
- module.exports = (content) =>
74
- formatters.reduce((fixedContent, formatter) => formatter(fixedContent), content);
81
+ module.exports = (content) => formatters.reduce((fixedContent, formatter) => formatter(fixedContent), content);
package/src/index.js CHANGED
@@ -65,6 +65,7 @@ module.exports = {
65
65
  patch = config.patch,
66
66
  authorizationToken,
67
67
  apiClassName = config.apiClassName,
68
+ debug = config.debug,
68
69
  }) =>
69
70
  new Promise((resolve, reject) => {
70
71
  addToConfig({
@@ -101,6 +102,7 @@ module.exports = {
101
102
  typeSuffix,
102
103
  patch,
103
104
  apiClassName,
105
+ debug,
104
106
  });
105
107
  (spec
106
108
  ? convertSwaggerObject(spec, { patch })
@@ -152,7 +154,7 @@ module.exports = {
152
154
  const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);
153
155
 
154
156
  const usageComponentSchemas = filterComponentsMap(componentsMap, "schemas");
155
- const sortByProperty = (o1, o2, propertyName) => {
157
+ const sortByProperty = (propertyName) => (o1, o2) => {
156
158
  if (o1[propertyName] > o2[propertyName]) {
157
159
  return 1;
158
160
  }
@@ -161,21 +163,20 @@ module.exports = {
161
163
  }
162
164
  return 0;
163
165
  };
164
- const sortByTypeName = (o1, o2) => sortByProperty(o1, o2, "typeName");
165
-
166
- const sortByName = (o1, o2) => sortByProperty(o1, o2, "name");
167
166
 
168
167
  const sortSchemas = (schemas) => {
169
168
  if (config.sortTypes) {
170
- return schemas.sort(sortByTypeName).map((schema) => {
169
+ return schemas.sort(sortByProperty("typeName")).map((schema) => {
171
170
  if (schema.rawTypeData?.properties) {
172
171
  return {
173
172
  ...schema,
174
173
  rawTypeData: {
175
174
  ...schema.rawTypeData,
176
- $parsed: {
175
+ $parsed: schema.rawTypeData["$parsed"] && {
177
176
  ...schema.rawTypeData["$parsed"],
178
- content: schema.rawTypeData["$parsed"].content.sort(sortByName),
177
+ content: Array.isArray(schema.rawTypeData["$parsed"].content)
178
+ ? schema.rawTypeData["$parsed"].content.sort(sortByProperty("name"))
179
+ : schema.rawTypeData["$parsed"].content,
179
180
  },
180
181
  },
181
182
  };
package/src/logger.js CHANGED
@@ -2,6 +2,8 @@ const _ = require("lodash");
2
2
  const { config } = require("./config");
3
3
  const { emojify, emoji } = require("node-emoji");
4
4
 
5
+ let firstLog = true;
6
+
5
7
  /**
6
8
  *
7
9
  * @param {{ type: "warn" | "log" | "error", emojiName: keyof emoji, messages: unknown[] }} payload
@@ -12,6 +14,13 @@ const createLogMessage = ({ type, emojiName, messages }) => {
12
14
 
13
15
  const emoji = emojify(emojiName);
14
16
 
17
+ if (firstLog) {
18
+ firstLog = false;
19
+ logger.log(
20
+ `swagger-typescript-api(${config.version}),${process.env.npm_config_user_agent || `nodejs(${process.version})`}`,
21
+ );
22
+ }
23
+
15
24
  console[type](
16
25
  emoji,
17
26
  " ",
package/src/modelNames.js CHANGED
@@ -53,11 +53,7 @@ const formatModelName = (name, options) => {
53
53
 
54
54
  const fixedModelName = fixModelName(name);
55
55
 
56
- const formattedModelName = _.replace(
57
- _.startCase(`${typePrefix}_${fixedModelName}_${typeSuffix}`),
58
- /\s/g,
59
- "",
60
- );
56
+ const formattedModelName = _.replace(_.startCase(`${typePrefix}_${fixedModelName}_${typeSuffix}`), /\s/g, "");
61
57
  const modelName = config.hooks.onFormatTypeName(formattedModelName, name) || formattedModelName;
62
58
 
63
59
  formattedModelNamesMap.set(hashKey, modelName);
package/src/modelTypes.js CHANGED
@@ -9,18 +9,19 @@ const { getTypeData } = require("./components");
9
9
  * @returns
10
10
  */
11
11
  const prepareModelType = (typeInfo) => {
12
- const typeData = getTypeData(typeInfo);
13
- let { typeIdentifier, name: originalName, content, type, description } = typeData;
14
-
15
- const resultContent = formatters[type] ? formatters[type](content) : content;
12
+ const rawTypeData = getTypeData(typeInfo);
13
+ const typeData = formatters[rawTypeData.type] ? formatters[rawTypeData.type](rawTypeData) : rawTypeData;
14
+ let { typeIdentifier, name: originalName, content, description } = typeData;
16
15
  const name = formatModelName(originalName);
17
16
 
18
17
  return {
18
+ ...typeData,
19
19
  typeIdentifier,
20
20
  name,
21
21
  description,
22
- rawContent: content,
23
- content: resultContent,
22
+ $content: rawTypeData.content,
23
+ rawContent: rawTypeData.content,
24
+ content: content,
24
25
  typeData,
25
26
  };
26
27
  };
package/src/output.js CHANGED
@@ -5,6 +5,7 @@ const { renderTemplate } = require("./templates");
5
5
  const { translate: translateToJS } = require("./translators/JavaScript");
6
6
  const ts = require("typescript");
7
7
  const formatFileContent = require("./formatFileContent");
8
+ const { config } = require("./config");
8
9
 
9
10
  const getFileNameWithoutExt = (fileName) => {
10
11
  const fileNameParts = _.split(fileName, ".");
@@ -20,10 +21,17 @@ const createFileInfo = (configuration, fileName, content) => {
20
21
  const fixedFileName = getFileNameWithoutExt(fileName);
21
22
 
22
23
  if (configuration.translateToJavaScript) {
23
- const { sourceContent, declarationContent } = translateToJS(
24
- `${fixedFileName}${ts.Extension.Ts}`,
25
- content,
26
- );
24
+ const { sourceContent, declarationContent } = translateToJS(`${fixedFileName}${ts.Extension.Ts}`, content);
25
+
26
+ if (config.debug) {
27
+ console.info("generating output for", `${fixedFileName}${ts.Extension.Js}`);
28
+ console.info(sourceContent);
29
+ }
30
+
31
+ if (config.debug) {
32
+ console.info("generating output for", `${fixedFileName}${ts.Extension.Dts}`);
33
+ console.info(declarationContent);
34
+ }
27
35
 
28
36
  return {
29
37
  name: `${fixedFileName}${ts.Extension.Js}`,
@@ -35,6 +43,11 @@ const createFileInfo = (configuration, fileName, content) => {
35
43
  };
36
44
  }
37
45
 
46
+ if (config.debug) {
47
+ console.info("generating output for", `${fixedFileName}${ts.Extension.Ts}`);
48
+ console.info(content);
49
+ }
50
+
38
51
  return {
39
52
  name: `${fixedFileName}${ts.Extension.Ts}`,
40
53
  content: formatFileContent(content),
@@ -54,9 +67,7 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
54
67
  route: configuration.routes.$outOfModule,
55
68
  });
56
69
 
57
- modularApiFileInfos.push(
58
- createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleRouteContent),
59
- );
70
+ modularApiFileInfos.push(createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleRouteContent));
60
71
  }
61
72
  if (generateClient) {
62
73
  const outOfModuleApiContent = renderTemplate(templatesToRender.api, {
@@ -64,9 +75,7 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
64
75
  route: configuration.routes.$outOfModule,
65
76
  });
66
77
 
67
- modularApiFileInfos.push(
68
- createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent),
69
- );
78
+ modularApiFileInfos.push(createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent));
70
79
  }
71
80
  }
72
81
 
@@ -82,11 +91,7 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
82
91
  });
83
92
 
84
93
  apiFileInfos.push(
85
- createFileInfo(
86
- configuration,
87
- classNameCase(`${route.moduleName}_Route`),
88
- routeModuleContent,
89
- ),
94
+ createFileInfo(configuration, classNameCase(`${route.moduleName}_Route`), routeModuleContent),
90
95
  );
91
96
  }
92
97
 
@@ -96,9 +101,7 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
96
101
  route,
97
102
  });
98
103
 
99
- apiFileInfos.push(
100
- createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent),
101
- );
104
+ apiFileInfos.push(createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent));
102
105
  }
103
106
 
104
107
  return apiFileInfos;
@@ -115,11 +118,7 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
115
118
  renderTemplate(templatesToRender.dataContracts, configuration),
116
119
  ),
117
120
  generateClient &&
118
- createFileInfo(
119
- configuration,
120
- fileNames.httpClient,
121
- renderTemplate(templatesToRender.httpClient, configuration),
122
- ),
121
+ createFileInfo(configuration, fileNames.httpClient, renderTemplate(templatesToRender.httpClient, configuration)),
123
122
  ...modularApiFileInfos,
124
123
  ];
125
124
  };