swagger-typescript-api 13.0.9 → 13.0.11

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 (69) hide show
  1. package/README.md +3 -3
  2. package/dist/chunk-3OGOYA5P.cjs +104 -0
  3. package/dist/chunk-3OGOYA5P.cjs.map +1 -0
  4. package/dist/chunk-R4CYI34W.js +65 -0
  5. package/dist/chunk-R4CYI34W.js.map +1 -0
  6. package/dist/cli.cjs +43 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/cli.js +36 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/lib.cjs +21 -0
  11. package/dist/lib.cjs.map +1 -0
  12. package/dist/lib.js +4 -0
  13. package/dist/lib.js.map +1 -0
  14. package/index.d.ts +1 -1
  15. package/package.json +25 -14
  16. package/templates/base/http-clients/axios-http-client.ejs +3 -0
  17. package/templates/base/http-clients/fetch-http-client.ejs +3 -3
  18. package/cli/constants.js +0 -6
  19. package/cli/execute.js +0 -180
  20. package/cli/index.js +0 -94
  21. package/cli/operations/display-help.js +0 -177
  22. package/cli/operations/display-version.js +0 -5
  23. package/cli/parse-args.js +0 -24
  24. package/cli/process-option.js +0 -75
  25. package/index.js +0 -344
  26. package/src/code-formatter.js +0 -114
  27. package/src/code-gen-process.js +0 -569
  28. package/src/commands/generate-templates/configuration.js +0 -31
  29. package/src/commands/generate-templates/index.js +0 -10
  30. package/src/commands/generate-templates/templates-gen-process.js +0 -205
  31. package/src/component-type-name-resolver.js +0 -42
  32. package/src/configuration.js +0 -445
  33. package/src/constants.js +0 -77
  34. package/src/index.js +0 -16
  35. package/src/schema-components-map.js +0 -76
  36. package/src/schema-parser/base-schema-parsers/array.js +0 -41
  37. package/src/schema-parser/base-schema-parsers/complex.js +0 -49
  38. package/src/schema-parser/base-schema-parsers/discriminator.js +0 -305
  39. package/src/schema-parser/base-schema-parsers/enum.js +0 -156
  40. package/src/schema-parser/base-schema-parsers/object.js +0 -103
  41. package/src/schema-parser/base-schema-parsers/primitive.js +0 -61
  42. package/src/schema-parser/complex-schema-parsers/all-of.js +0 -26
  43. package/src/schema-parser/complex-schema-parsers/any-of.js +0 -27
  44. package/src/schema-parser/complex-schema-parsers/not.js +0 -9
  45. package/src/schema-parser/complex-schema-parsers/one-of.js +0 -27
  46. package/src/schema-parser/mono-schema-parser.js +0 -46
  47. package/src/schema-parser/schema-formatters.js +0 -164
  48. package/src/schema-parser/schema-parser-fabric.js +0 -130
  49. package/src/schema-parser/schema-parser.js +0 -295
  50. package/src/schema-parser/schema-utils.js +0 -319
  51. package/src/schema-parser/util/enum-key-resolver.js +0 -24
  52. package/src/schema-routes/schema-routes.js +0 -1208
  53. package/src/schema-routes/util/specific-arg-name-resolver.js +0 -24
  54. package/src/schema-walker.js +0 -91
  55. package/src/swagger-schema-resolver.js +0 -195
  56. package/src/templates-worker.js +0 -243
  57. package/src/translators/javascript.js +0 -81
  58. package/src/translators/translator.js +0 -33
  59. package/src/type-name-formatter.js +0 -111
  60. package/src/util/file-system.js +0 -95
  61. package/src/util/id.js +0 -7
  62. package/src/util/internal-case.js +0 -7
  63. package/src/util/logger.js +0 -142
  64. package/src/util/name-resolver.js +0 -103
  65. package/src/util/object-assign.js +0 -17
  66. package/src/util/pascal-case.js +0 -7
  67. package/src/util/random.js +0 -11
  68. package/src/util/request.js +0 -63
  69. package/src/util/sort-by-property.js +0 -15
@@ -1,111 +0,0 @@
1
- import _ from "lodash";
2
-
3
- /**
4
- * @typedef {"enum-key" | "type-name"} FormattingSchemaType
5
- */
6
-
7
- class TypeNameFormatter {
8
- /** @type {Map<string, string>} */
9
- formattedModelNamesMap = new Map();
10
-
11
- /** @type {CodeGenConfig} */
12
- config;
13
-
14
- /** @type {Logger} */
15
- logger;
16
-
17
- constructor({ config, logger }) {
18
- this.config = config;
19
- this.logger = logger;
20
- }
21
-
22
- /**
23
- * @param name
24
- * @param options {{ type?: FormattingSchemaType }}
25
- * @return {string}
26
- */
27
- format = (name, options) => {
28
- options = options || {};
29
-
30
- /**
31
- * @type {FormattingSchemaType}
32
- */
33
- const schemaType = options.type || "type-name";
34
-
35
- const typePrefix =
36
- schemaType === "enum-key"
37
- ? this.config.enumKeyPrefix
38
- : this.config.typePrefix;
39
- const typeSuffix =
40
- schemaType === "enum-key"
41
- ? this.config.enumKeySuffix
42
- : this.config.typeSuffix;
43
-
44
- const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
45
-
46
- if (typeof name !== "string") {
47
- this.logger.warn("wrong name of the model name", name);
48
- return name;
49
- }
50
-
51
- // constant names like LEFT_ARROW, RIGHT_FORWARD, ETC_KEY, _KEY_NUM_
52
- if (/^([A-Z_]{1,})$/g.test(name)) {
53
- return _.compact([typePrefix, name, typeSuffix]).join("_");
54
- }
55
-
56
- if (this.formattedModelNamesMap.has(hashKey)) {
57
- return this.formattedModelNamesMap.get(hashKey);
58
- }
59
-
60
- const fixedModelName = this.fixModelName(name, { type: schemaType });
61
-
62
- const formattedName = _.replace(
63
- _.startCase(`${typePrefix}_${fixedModelName}_${typeSuffix}`),
64
- /\s/g,
65
- "",
66
- );
67
- const formattedResultName =
68
- this.config.hooks.onFormatTypeName(formattedName, name, schemaType) ||
69
- formattedName;
70
-
71
- this.formattedModelNamesMap.set(hashKey, formattedResultName);
72
-
73
- return formattedResultName;
74
- };
75
-
76
- isValidName = (name) => /^([A-Za-z$_]{1,})$/g.test(name);
77
-
78
- /**
79
- * @param name
80
- * @param options {{ type?: FormattingSchemaType }}
81
- * @return {string}
82
- */
83
- fixModelName = (name, options) => {
84
- const { type } = options || {};
85
-
86
- if (!this.isValidName(name)) {
87
- if (!/^[a-zA-Z_$]/g.test(name)) {
88
- const fixPrefix =
89
- type === "enum-key"
90
- ? this.config.fixInvalidEnumKeyPrefix
91
- : this.config.fixInvalidTypeNamePrefix;
92
- name = `${fixPrefix} ${name}`;
93
- }
94
-
95
- // specific replaces for TSOA 3.x
96
- if (name.includes("."))
97
- name = name
98
- .replace(/Exclude_keyof[A-Za-z]+/g, () => "ExcludeKeys")
99
- .replace(/%22~AND~%22/g, "And")
100
- .replace(/%22~OR~%22/g, "Or")
101
- .replace(/(\.?%22)|\./g, "_")
102
- .replace(/__+$/, "");
103
-
104
- if (name.includes("-")) name = _.startCase(name).replace(/ /g, "");
105
- }
106
-
107
- return name;
108
- };
109
- }
110
-
111
- export { TypeNameFormatter };
@@ -1,95 +0,0 @@
1
- import fs from "node:fs";
2
- import { dirname, resolve } from "node:path";
3
- import url from "node:url";
4
- import _ from "lodash";
5
- import { Logger } from "./logger.js";
6
-
7
- const FILE_PREFIX = `/* eslint-disable */
8
- /* tslint:disable */
9
- /*
10
- * ---------------------------------------------------------------
11
- * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
12
- * ## ##
13
- * ## AUTHOR: acacode ##
14
- * ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
15
- * ---------------------------------------------------------------
16
- */
17
-
18
- `;
19
-
20
- class FileSystem {
21
- /** @type {Logger} */
22
- logger;
23
-
24
- constructor({ logger = new Logger("file-system") } = {}) {
25
- this.logger = logger;
26
- }
27
-
28
- getFileContent = (path) => {
29
- return fs.readFileSync(path, { encoding: "utf8" });
30
- };
31
-
32
- readDir = (path) => {
33
- return fs.readdirSync(path);
34
- };
35
-
36
- pathIsDir = (path) => {
37
- if (!path) return false;
38
-
39
- try {
40
- const stat = fs.statSync(path);
41
- return stat.isDirectory();
42
- } catch (e) {
43
- return false;
44
- }
45
- };
46
-
47
- cropExtension = (fileName) => {
48
- const fileNameParts = _.split(fileName, ".");
49
-
50
- if (fileNameParts.length > 1) {
51
- fileNameParts.pop();
52
- }
53
-
54
- return fileNameParts.join(".");
55
- };
56
-
57
- removeDir = (path) => {
58
- try {
59
- if (typeof fs.rmSync === "function") {
60
- fs.rmSync(path, { recursive: true });
61
- } else {
62
- fs.rmdirSync(path, { recursive: true });
63
- }
64
- } catch (e) {
65
- this.logger.debug("failed to remove dir", e);
66
- }
67
- };
68
-
69
- createDir = (path) => {
70
- try {
71
- fs.mkdirSync(path, { recursive: true });
72
- } catch (e) {
73
- this.logger.debug("failed to create dir", e);
74
- }
75
- };
76
-
77
- cleanDir = (path) => {
78
- this.removeDir(path);
79
- this.createDir(path);
80
- };
81
-
82
- pathIsExist = (path) => {
83
- return !!path && fs.existsSync(path);
84
- };
85
-
86
- createFile = ({ path, fileName, content, withPrefix }) => {
87
- const __dirname = dirname(url.fileURLToPath(import.meta.url));
88
- const absolutePath = resolve(__dirname, path, `./${fileName}`);
89
- const fileContent = `${withPrefix ? FILE_PREFIX : ""}${content}`;
90
-
91
- return fs.writeFileSync(absolutePath, fileContent, _.noop);
92
- };
93
- }
94
-
95
- export { FileSystem };
package/src/util/id.js DELETED
@@ -1,7 +0,0 @@
1
- import { customAlphabet } from "nanoid";
2
-
3
- const ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
4
-
5
- const generateId = customAlphabet(ALPHABET, 12);
6
-
7
- export { generateId };
@@ -1,7 +0,0 @@
1
- import _ from "lodash";
2
-
3
- function internalCase(value) {
4
- return _.camelCase(_.lowerCase(value));
5
- }
6
-
7
- export { internalCase };
@@ -1,142 +0,0 @@
1
- import _ from "lodash";
2
- import { emojify } from "node-emoji";
3
-
4
- class Logger {
5
- firstLog = true;
6
- /**
7
- * @type {CodeGenConfig}
8
- */
9
- config;
10
-
11
- constructor({ config }) {
12
- this.config = config;
13
- }
14
-
15
- createLogMessage = ({ type, emojiName, messages }) => {
16
- if (this.config.silent) return;
17
-
18
- const emoji = emojify(emojiName);
19
-
20
- if (this.firstLog) {
21
- this.firstLog = false;
22
- this.log(
23
- `swagger-typescript-api(${this.config.version}),${
24
- process.env.npm_config_user_agent || `nodejs(${process.version})`
25
- },debug mode ${this.config.debug ? "ENABLED" : "DISABLED"}`,
26
- );
27
- }
28
-
29
- if (type === "debug" || this.config.debug) {
30
- const trace = new Error().stack
31
- .split("\n")
32
- .splice(3)
33
- .filter(
34
- (line) =>
35
- !line.includes("swagger-typescript-api\\node_modules") &&
36
- !line.includes("swagger-typescript-api/node_modules"),
37
- )
38
- .slice(0, 10);
39
- const logFn = console[type] || console.log;
40
- logFn(`${emoji} [${type}]`, new Date().toISOString());
41
- if (this.config.debugExtras && Array.isArray(this.config.debugExtras)) {
42
- logFn(`[${this.config.debugExtras.join(" ")}]`);
43
- }
44
- logFn(
45
- "[message]",
46
- ..._.map(messages, (message) =>
47
- _.startsWith(message, "\n")
48
- ? `\n ${message.replace(/\n/, "")}`
49
- : message,
50
- ),
51
- );
52
- logFn(trace.join("\n") + "\n---");
53
- return;
54
- }
55
-
56
- console[type](
57
- emoji,
58
- " ",
59
- ..._.map(messages, (message) =>
60
- _.startsWith(message, "\n")
61
- ? `\n${emoji} ${message.replace(/\n/, "")}`
62
- : message,
63
- ),
64
- );
65
- };
66
-
67
- /**
68
- *
69
- * @param messages {any[]}
70
- */
71
- log = (...messages) =>
72
- this.createLogMessage({
73
- type: "log",
74
- emojiName: ":sparkles:",
75
- messages,
76
- });
77
-
78
- /**
79
- *
80
- * @param messages {any[]}
81
- * @return {void}
82
- */
83
- event = (...messages) =>
84
- this.createLogMessage({
85
- type: "log",
86
- emojiName: ":star:",
87
- messages,
88
- });
89
-
90
- /**
91
- *
92
- * @param messages {any[]}
93
- * @return {void}
94
- */
95
- success = (...messages) =>
96
- this.createLogMessage({
97
- type: "log",
98
- emojiName: ":white_check_mark:",
99
- messages,
100
- });
101
-
102
- /**
103
- *
104
- * @param messages {any[]}
105
- * @return {void}
106
- */
107
- warn = (...messages) =>
108
- this.createLogMessage({
109
- type: "warn",
110
- emojiName: ":exclamation:",
111
- messages,
112
- });
113
-
114
- /**
115
- *
116
- * @param messages {any[]}
117
- * @return {void}
118
- */
119
- error = (...messages) =>
120
- this.createLogMessage({
121
- type: "error",
122
- emojiName: ":no_entry:",
123
- messages,
124
- });
125
-
126
- /**
127
- *
128
- * @param messages {any[]}
129
- * @return {void}
130
- */
131
- debug = (...messages) => {
132
- if (!this.config.debug) return;
133
-
134
- this.createLogMessage({
135
- type: "debug",
136
- emojiName: ":black_large_square:",
137
- messages,
138
- });
139
- };
140
- }
141
-
142
- export { Logger };
@@ -1,103 +0,0 @@
1
- import _ from "lodash";
2
-
3
- class NameResolver {
4
- reservedNames = [];
5
- getFallbackName = null;
6
-
7
- /** @type {CodeGenConfig} */
8
- config;
9
- /** @type {Logger} */
10
- logger;
11
-
12
- /**
13
- * @param {CodeGenConfig} config;
14
- * @param {Logger} logger;
15
- * @param {string[]} reservedNames
16
- */
17
- constructor(config, logger, reservedNames, getFallbackName) {
18
- this.config = config;
19
- this.logger = logger;
20
- this.getFallbackName = getFallbackName;
21
- this.reserve(reservedNames);
22
- }
23
-
24
- /**
25
- * @param {string[]} names
26
- */
27
- reserve(names) {
28
- const fixedNames = _.uniq(_.compact(names));
29
- for (const name of fixedNames) {
30
- if (this.reservedNames.indexOf(name) === -1) {
31
- this.reservedNames.push(name);
32
- }
33
- }
34
- }
35
-
36
- unreserve(names) {
37
- this.reservedNames.filter(
38
- (reservedName) => !names.some((name) => name === reservedName),
39
- );
40
- }
41
-
42
- isReserved(name) {
43
- return _.some(this.reservedNames, (reservedName) => reservedName === name);
44
- }
45
-
46
- /**
47
- *
48
- * @param {(string[])} variants
49
- * @param {(reserved: string[]) => string)} [resolver]
50
- * @param {any} [extras]
51
- * @returns {string | null}
52
- */
53
- resolve(variants, resolver, extras, shouldReserve = true) {
54
- if (typeof resolver === "function") {
55
- let usageName = null;
56
- while (usageName === null) {
57
- const variant = resolver(variants, extras);
58
-
59
- if (variant === undefined) {
60
- this.logger.warn(
61
- "unable to resolve name. current reserved names: ",
62
- this.reservedNames,
63
- );
64
- return null;
65
- }
66
- if (!shouldReserve || !this.isReserved(variant)) {
67
- usageName = variant;
68
- }
69
- }
70
-
71
- shouldReserve && this.reserve([usageName]);
72
- return usageName;
73
- } else if (Array.isArray(variants)) {
74
- let usageName = null;
75
- const uniqVariants = _.uniq(_.compact(variants));
76
-
77
- _.forEach(uniqVariants, (variant) => {
78
- if (!usageName && (!shouldReserve || !this.isReserved(variant))) {
79
- usageName = variant;
80
- }
81
- });
82
-
83
- if (usageName) {
84
- shouldReserve && this.reserve([usageName]);
85
- return usageName;
86
- }
87
-
88
- this.logger.debug(
89
- "trying to resolve name with using fallback name generator using variants",
90
- variants,
91
- );
92
- return this.resolve(variants, this.getFallbackName, extras);
93
- }
94
-
95
- this.logger.debug(
96
- "problem with reserving names. current reserved names: ",
97
- this.reservedNames,
98
- );
99
- return null;
100
- }
101
- }
102
-
103
- export { NameResolver };
@@ -1,17 +0,0 @@
1
- import _ from "lodash";
2
-
3
- const objectAssign = (target, updaterFn) => {
4
- if (!updaterFn) return;
5
- const update =
6
- typeof updaterFn === "function" ? updaterFn(target) : updaterFn;
7
- const undefinedKeys = _.map(
8
- update,
9
- (value, key) => value === undefined && key,
10
- ).filter(Boolean);
11
- Object.assign(target, _.merge(target, update));
12
- undefinedKeys.forEach((key) => {
13
- target[key] = undefined;
14
- });
15
- };
16
-
17
- export { objectAssign };
@@ -1,7 +0,0 @@
1
- import _ from "lodash";
2
-
3
- function pascalCase(value) {
4
- return _.upperFirst(_.camelCase(value));
5
- }
6
-
7
- export { pascalCase };
@@ -1,11 +0,0 @@
1
- const getRandomFloat = (min = 0, max = 1) => {
2
- return Math.random() * (max - min) + min;
3
- };
4
-
5
- const getRandomInt = (min = 0, max = 1) => {
6
- if (min === max) return min;
7
-
8
- return Math.round(getRandomFloat(min, max));
9
- };
10
-
11
- export { getRandomInt, getRandomFloat };
@@ -1,63 +0,0 @@
1
- import _ from "lodash";
2
-
3
- class Request {
4
- /**
5
- * @type {CodeGenConfig}
6
- */
7
- config;
8
- /**
9
- * @type {Logger}
10
- */
11
- logger;
12
-
13
- constructor(config, logger) {
14
- this.config = config;
15
- this.logger = logger;
16
- }
17
-
18
- /**
19
- * @param url {string}
20
- * @param disableStrictSSL
21
- * @param authToken
22
- * @param options {Partial<RequestInit>}
23
- * @return {Promise<string>}
24
- */
25
- async download({ url, disableStrictSSL, authToken, ...options }) {
26
- /**
27
- * @type {Partial<RequestInit>}
28
- */
29
- const requestOptions = {};
30
-
31
- if (disableStrictSSL && !_.startsWith(url, "http://")) {
32
- const undiciGlobalDispatcher =
33
- global[Symbol.for("undici.globalDispatcher.1")];
34
- if (!undiciGlobalDispatcher) {
35
- throw new Error("Could not find the global Undici dispatcher");
36
- }
37
- const newDispatcher = new undiciGlobalDispatcher.constructor({
38
- connect: {
39
- rejectUnauthorized: false,
40
- },
41
- });
42
- global[unidiciGlobalDispatcherSymbol] = newDispatcher;
43
- }
44
- if (authToken) {
45
- requestOptions.headers = {
46
- Authorization: authToken,
47
- };
48
- }
49
-
50
- _.merge(requestOptions, options, this.config.requestOptions);
51
-
52
- try {
53
- const response = await fetch(url, requestOptions);
54
- return await response.text();
55
- } catch (error) {
56
- const message = `error while fetching data from URL "${url}"`;
57
- this.logger.error(message, "response" in error ? error.response : error);
58
- return message;
59
- }
60
- }
61
- }
62
-
63
- export { Request };
@@ -1,15 +0,0 @@
1
- /**
2
- * @param propertyName {string}
3
- * @returns {(o1: Record<string, any>, o2: Record<string, any>) => 1 | -1 | 0}
4
- */
5
- const sortByProperty = (propertyName) => (o1, o2) => {
6
- if (o1[propertyName] > o2[propertyName]) {
7
- return 1;
8
- }
9
- if (o1[propertyName] < o2[propertyName]) {
10
- return -1;
11
- }
12
- return 0;
13
- };
14
-
15
- export { sortByProperty };