swagger-typescript-api 13.0.0-experimental-1 → 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.
- package/LICENSE +21 -21
- package/README.md +22 -12
- package/cli/constants.js +3 -3
- package/cli/execute.js +52 -31
- package/cli/index.d.ts +1 -2
- package/cli/index.js +18 -17
- package/cli/operations/display-help.js +51 -29
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +28 -20
- package/index.d.ts +113 -8
- package/index.js +158 -135
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +357 -259
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -2
- package/src/commands/generate-templates/templates-gen-process.js +62 -35
- package/src/component-type-name-resolver.js +44 -0
- package/src/configuration.js +167 -95
- package/src/constants.js +28 -22
- package/src/index.js +3 -4
- package/src/schema-components-map.js +39 -23
- package/src/schema-parser/base-schema-parsers/array.js +43 -0
- package/src/schema-parser/base-schema-parsers/complex.js +51 -0
- package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
- package/src/schema-parser/base-schema-parsers/enum.js +158 -0
- package/src/schema-parser/base-schema-parsers/object.js +105 -0
- package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
- package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
- package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
- package/src/schema-parser/complex-schema-parsers/not.js +9 -0
- package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
- package/src/schema-parser/mono-schema-parser.js +48 -0
- package/src/schema-parser/schema-formatters.js +69 -60
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +208 -427
- package/src/schema-parser/schema-utils.js +123 -58
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/schema-routes/schema-routes.js +1225 -0
- package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
- package/src/schema-walker.js +93 -0
- package/src/swagger-schema-resolver.js +61 -28
- package/src/templates-worker.js +240 -0
- package/src/translators/javascript.js +83 -0
- package/src/translators/translator.js +35 -0
- package/src/type-name-formatter.js +33 -18
- package/src/util/file-system.js +30 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +46 -20
- package/src/util/name-resolver.js +52 -60
- package/src/util/object-assign.js +7 -3
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +5 -5
- package/src/util/sort-by-property.js +17 -0
- package/templates/README.md +17 -17
- package/templates/base/README.md +7 -7
- package/templates/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -27
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +3 -3
- package/templates/base/http-clients/axios-http-client.ejs +139 -138
- package/templates/base/http-clients/fetch-http-client.ejs +224 -224
- package/templates/base/interface-data-contract.ejs +10 -10
- package/templates/base/object-field-jsdoc.ejs +28 -28
- package/templates/base/route-docs.ejs +30 -30
- package/templates/base/route-name.ejs +42 -42
- package/templates/base/route-type.ejs +22 -21
- package/templates/base/type-data-contract.ejs +15 -15
- package/templates/default/README.md +6 -6
- package/templates/default/api.ejs +69 -68
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -32
- package/templates/modular/README.md +6 -6
- package/templates/modular/api.ejs +28 -28
- package/templates/modular/procedure-call.ejs +100 -100
- package/templates/modular/route-types.ejs +18 -18
- package/src/schema-parser/schema-processor.js +0 -79
- package/src/schema-parser/schema-routes.js +0 -950
- package/src/templates.js +0 -182
- package/src/translators/JavaScript.js +0 -60
package/src/templates.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
const { resolve } = require("path");
|
|
2
|
-
const _ = require("lodash");
|
|
3
|
-
const Eta = require("eta");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
|
|
6
|
-
class Templates {
|
|
7
|
-
/**
|
|
8
|
-
* @type {CodeGenConfig}
|
|
9
|
-
*/
|
|
10
|
-
config;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @type {Logger}
|
|
14
|
-
*/
|
|
15
|
-
logger;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @type {FileSystem}
|
|
19
|
-
*/
|
|
20
|
-
fileSystem;
|
|
21
|
-
|
|
22
|
-
getRenderTemplateData;
|
|
23
|
-
|
|
24
|
-
constructor(config, logger, fileSystem, getRenderTemplateData) {
|
|
25
|
-
this.config = config;
|
|
26
|
-
this.logger = logger;
|
|
27
|
-
this.fileSystem = fileSystem;
|
|
28
|
-
this.getRenderTemplateData = getRenderTemplateData;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
getTemplatePaths = ({ templates, modular }) => {
|
|
32
|
-
const baseTemplatesPath = resolve(__dirname, "../templates/base");
|
|
33
|
-
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
|
|
34
|
-
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
|
|
35
|
-
const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
|
|
36
|
-
const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
/** `templates/base` */
|
|
40
|
-
base: baseTemplatesPath,
|
|
41
|
-
/** `templates/default` */
|
|
42
|
-
default: defaultTemplatesPath,
|
|
43
|
-
/** `templates/modular` */
|
|
44
|
-
modular: modularTemplatesPath,
|
|
45
|
-
/** usage path if `--templates` option is not set */
|
|
46
|
-
original: originalTemplatesPath,
|
|
47
|
-
/** custom path to templates (`--templates`) */
|
|
48
|
-
custom: customTemplatesPath,
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
cropExtension = (path) =>
|
|
53
|
-
this.config.templateExtensions.reduce((path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path), path);
|
|
54
|
-
|
|
55
|
-
getTemplateFullPath = (path, fileName) => {
|
|
56
|
-
const raw = resolve(path, "./", this.cropExtension(fileName));
|
|
57
|
-
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
|
|
58
|
-
|
|
59
|
-
return pathVariants.find((variant) => !!this.fileSystem.pathIsExist(variant));
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
requireFnFromTemplate = (packageOrPath) => {
|
|
63
|
-
const isPath = _.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
|
|
64
|
-
|
|
65
|
-
if (isPath) {
|
|
66
|
-
return require(path.resolve(this.config.templates, packageOrPath));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return require(packageOrPath);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
getTemplate = ({ fileName, name, path }) => {
|
|
73
|
-
const { templatePaths } = this.config;
|
|
74
|
-
|
|
75
|
-
if (path) {
|
|
76
|
-
return this.fileSystem.getFileContent(path);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!fileName) return "";
|
|
80
|
-
|
|
81
|
-
const customFullPath = this.getTemplateFullPath(templatePaths.custom, fileName);
|
|
82
|
-
let fileContent = customFullPath && this.fileSystem.getFileContent(customFullPath);
|
|
83
|
-
|
|
84
|
-
if (fileContent) {
|
|
85
|
-
this.logger.log(`"${_.lowerCase(name)}" template found in "${templatePaths.custom}"`);
|
|
86
|
-
return fileContent;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const baseFullPath = this.getTemplateFullPath(templatePaths.base, fileName);
|
|
90
|
-
|
|
91
|
-
if (baseFullPath) {
|
|
92
|
-
fileContent = this.fileSystem.getFileContent(baseFullPath);
|
|
93
|
-
} else {
|
|
94
|
-
this.logger.warn(
|
|
95
|
-
`"${_.lowerCase(name)}" template not found in "${templatePaths.custom}"`,
|
|
96
|
-
`\nCode generator will use the default template`,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const originalFullPath = this.getTemplateFullPath(templatePaths.original, fileName);
|
|
101
|
-
|
|
102
|
-
if (originalFullPath) {
|
|
103
|
-
fileContent = this.fileSystem.getFileContent(originalFullPath);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return fileContent;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
getTemplates = ({ templatePaths }) => {
|
|
110
|
-
this.logger.log(`try to read templates from directory "${templatePaths.custom}"`);
|
|
111
|
-
|
|
112
|
-
return _.reduce(
|
|
113
|
-
this.config.templateInfos,
|
|
114
|
-
(acc, { fileName, name }) => ({
|
|
115
|
-
...acc,
|
|
116
|
-
[name]: this.getTemplate({ fileName, name }),
|
|
117
|
-
}),
|
|
118
|
-
{},
|
|
119
|
-
);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
findTemplateWithExt = (path) => {
|
|
123
|
-
const raw = this.cropExtension(path);
|
|
124
|
-
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
|
|
125
|
-
return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
getTemplateContent = (path) => {
|
|
129
|
-
const foundTemplatePathKey = _.keys(this.config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
|
|
130
|
-
|
|
131
|
-
const rawPath = resolve(
|
|
132
|
-
_.replace(path, `@${foundTemplatePathKey}`, this.config.templatePaths[foundTemplatePathKey]),
|
|
133
|
-
);
|
|
134
|
-
const fixedPath = this.findTemplateWithExt(rawPath);
|
|
135
|
-
|
|
136
|
-
if (fixedPath) {
|
|
137
|
-
return this.fileSystem.getFileContent(fixedPath);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const customPath = this.findTemplateWithExt(resolve(this.config.templatePaths.custom, path));
|
|
141
|
-
|
|
142
|
-
if (customPath) {
|
|
143
|
-
return this.fileSystem.getFileContent(customPath);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const originalPath = this.findTemplateWithExt(resolve(this.config.templatePaths.original, path));
|
|
147
|
-
|
|
148
|
-
if (originalPath) {
|
|
149
|
-
return this.fileSystem.getFileContent(originalPath);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return "";
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
renderTemplate = async (template, configuration, options) => {
|
|
156
|
-
if (!template) return "";
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
return await Eta.renderAsync(
|
|
160
|
-
template,
|
|
161
|
-
{
|
|
162
|
-
...this.getRenderTemplateData(),
|
|
163
|
-
...configuration,
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
async: true,
|
|
167
|
-
...(options || {}),
|
|
168
|
-
includeFile: async (path, configuration, options) => {
|
|
169
|
-
return await this.renderTemplate(this.getTemplateContent(path), configuration, options);
|
|
170
|
-
},
|
|
171
|
-
},
|
|
172
|
-
);
|
|
173
|
-
} catch (e) {
|
|
174
|
-
console.error("problem in this templates\n", template);
|
|
175
|
-
throw e;
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
module.exports = {
|
|
181
|
-
Templates,
|
|
182
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const ts = require("typescript");
|
|
2
|
-
|
|
3
|
-
function translate(fileName, content, options) {
|
|
4
|
-
const output = {};
|
|
5
|
-
const host = ts.createCompilerHost(options, true);
|
|
6
|
-
const fileNames = [fileName];
|
|
7
|
-
const originalSourceFileGet = host.getSourceFile.bind(host);
|
|
8
|
-
host.getSourceFile = (sourceFileName, languageVersion, onError, shouldCreateNewSourceFile) => {
|
|
9
|
-
if (sourceFileName !== fileName)
|
|
10
|
-
return originalSourceFileGet(sourceFileName, languageVersion, onError, shouldCreateNewSourceFile);
|
|
11
|
-
|
|
12
|
-
return ts.createSourceFile(sourceFileName, content, languageVersion, true, ts.ScriptKind.External);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
host.writeFile = (fileName, contents) => {
|
|
16
|
-
output[fileName] = contents;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
ts.createProgram(fileNames, options, host).emit();
|
|
20
|
-
|
|
21
|
-
return output;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
module.exports = {
|
|
25
|
-
translate: (fileName, sourceTypeScript) => {
|
|
26
|
-
const translated = translate(fileName, sourceTypeScript, {
|
|
27
|
-
module: "ESNext",
|
|
28
|
-
noImplicitReturns: true,
|
|
29
|
-
alwaysStrict: true,
|
|
30
|
-
target: ts.ScriptTarget.ESNext,
|
|
31
|
-
declaration: true,
|
|
32
|
-
noImplicitAny: false,
|
|
33
|
-
sourceMap: false,
|
|
34
|
-
removeComments: false,
|
|
35
|
-
disableSizeLimit: true,
|
|
36
|
-
esModuleInterop: true,
|
|
37
|
-
emitDecoratorMetadata: true,
|
|
38
|
-
skipLibCheck: true,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const sourceFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Js);
|
|
42
|
-
const declarationFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Dts);
|
|
43
|
-
const sourceContent = translated[sourceFileName];
|
|
44
|
-
const tsImportRows = sourceTypeScript.split("\n").filter((line) => line.startsWith("import "));
|
|
45
|
-
const declarationContent = translated[declarationFileName]
|
|
46
|
-
.split("\n")
|
|
47
|
-
.map((line) => {
|
|
48
|
-
if (line.startsWith("import ")) {
|
|
49
|
-
return tsImportRows.shift();
|
|
50
|
-
}
|
|
51
|
-
return line;
|
|
52
|
-
})
|
|
53
|
-
.join("\n");
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
sourceContent: sourceContent,
|
|
57
|
-
declarationContent: declarationContent,
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
};
|