@keq-request/cli 5.0.0-alpha.29 → 5.0.0-alpha.31
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 +21 -0
- package/dist/chunk-7HHTHYRA.cjs +2895 -0
- package/dist/chunk-7HHTHYRA.cjs.map +1 -0
- package/dist/chunk-DZ2SC5HL.js +2895 -0
- package/dist/chunk-DZ2SC5HL.js.map +1 -0
- package/dist/chunk-HN7ZZFKC.js +19 -0
- package/dist/chunk-HN7ZZFKC.js.map +1 -0
- package/dist/chunk-JX74ZR2O.cjs +670 -0
- package/dist/chunk-JX74ZR2O.cjs.map +1 -0
- package/dist/chunk-OC36G3PF.cjs +19 -0
- package/dist/chunk-OC36G3PF.cjs.map +1 -0
- package/dist/chunk-XRZLO2PB.js +670 -0
- package/dist/chunk-XRZLO2PB.js.map +1 -0
- package/dist/cli.cjs +171 -2757
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +161 -2724
- package/dist/cli.js.map +1 -1
- package/dist/compiler/compiler.d.ts +2 -0
- package/dist/compiler/compiler.d.ts.map +1 -1
- package/dist/index.cjs +8 -2777
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +15 -2738
- package/dist/index.js.map +1 -1
- package/dist/plugins/body-fallback/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/body-fallback/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/body-fallback/index.d.ts.map +1 -1
- package/dist/plugins/chinese-to-pinyin/chinese-to-pinyin.plugin.d.ts.map +1 -1
- package/dist/plugins/chinese-to-pinyin/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/chinese-to-pinyin/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/download-http-file/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/download-http-file/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/download-http-file/download-http-file.plugin.d.ts.map +1 -1
- package/dist/plugins/download-local-file/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/download-local-file/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/download-local-file/download-local-file.plugin.d.ts.map +1 -1
- package/dist/plugins/eslint/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/eslint/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/eslint/eslint.plugin.d.ts.map +1 -1
- package/dist/plugins/generate-declaration/constants/metadata-storage.d.ts +2 -1
- package/dist/plugins/generate-declaration/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/generate-declaration/generate-declaration.plugin.d.ts.map +1 -1
- package/dist/plugins/generate-micro-function/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/generate-micro-function/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/generate-micro-function/generate-micro-function.plugin.d.ts.map +1 -1
- package/dist/plugins/generate-nestjs-module/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/generate-nestjs-module/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/generate-nestjs-module/generate-nestjs-module.d.ts.map +1 -1
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/prettier/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/prettier/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/prettier/prettier.plugin.d.ts.map +1 -1
- package/dist/plugins/shaking/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/shaking/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/shaking/shaking.plugin.d.ts.map +1 -1
- package/dist/plugins/terminal-select/constants/metadata-storage.d.ts +1 -0
- package/dist/plugins/terminal-select/constants/metadata-storage.d.ts.map +1 -1
- package/dist/plugins/terminal-select/terminal-select.plugin.d.ts.map +1 -1
- package/dist/plugins/use-valibot/constants/index.d.ts +2 -0
- package/dist/plugins/use-valibot/constants/index.d.ts.map +1 -0
- package/dist/plugins/use-valibot/constants/metadata-storage.d.ts +6 -0
- package/dist/plugins/use-valibot/constants/metadata-storage.d.ts.map +1 -0
- package/dist/plugins/use-valibot/index.d.ts +3 -0
- package/dist/plugins/use-valibot/index.d.ts.map +1 -0
- package/dist/plugins/use-valibot/use-valibot.plugin.d.ts +12 -0
- package/dist/plugins/use-valibot/use-valibot.plugin.d.ts.map +1 -0
- package/dist/plugins.cjs +3 -2532
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.js +31 -2500
- package/dist/plugins.js.map +1 -1
- package/dist/transformers/json-schema/json-schema.transformer.d.ts +2 -0
- package/dist/transformers/json-schema/json-schema.transformer.d.ts.map +1 -1
- package/dist/transformers/json-schema/valibot.renderer.d.ts +26 -0
- package/dist/transformers/json-schema/valibot.renderer.d.ts.map +1 -0
- package/dist/transformers/schema-definition/schema-definition.transformer.d.ts +5 -0
- package/dist/transformers/schema-definition/schema-definition.transformer.d.ts.map +1 -1
- package/dist/translators.cjs +8 -2047
- package/dist/translators.cjs.map +1 -1
- package/dist/translators.js +7 -2008
- package/dist/translators.js.map +1 -1
- package/dist/utils/scan-generated-files.d.ts +15 -0
- package/dist/utils/scan-generated-files.d.ts.map +1 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,2745 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Compiler
|
|
3
|
+
} from "./chunk-XRZLO2PB.js";
|
|
4
|
+
import "./chunk-HN7ZZFKC.js";
|
|
5
|
+
import {
|
|
6
|
+
ApiDocumentV3_1,
|
|
7
|
+
Artifact,
|
|
8
|
+
Asset,
|
|
9
|
+
FileNamingStyle,
|
|
10
|
+
ModuleDefinition,
|
|
11
|
+
OperationDefinition,
|
|
12
|
+
QsArrayFormat,
|
|
13
|
+
SchemaDefinition
|
|
14
|
+
} from "./chunk-DZ2SC5HL.js";
|
|
15
|
+
|
|
1
16
|
// src/define-config.ts
|
|
2
17
|
function defineConfig(config) {
|
|
3
18
|
return config;
|
|
4
19
|
}
|
|
5
|
-
|
|
6
|
-
// src/constants/file-naming-style.ts
|
|
7
|
-
var FileNamingStyle = /* @__PURE__ */ ((FileNamingStyle2) => {
|
|
8
|
-
FileNamingStyle2["camelCase"] = "camelCase";
|
|
9
|
-
FileNamingStyle2["capitalCase"] = "capitalCase";
|
|
10
|
-
FileNamingStyle2["constantCase"] = "constantCase";
|
|
11
|
-
FileNamingStyle2["dotCase"] = "dotCase";
|
|
12
|
-
FileNamingStyle2["headerCase"] = "headerCase";
|
|
13
|
-
FileNamingStyle2["noCase"] = "noCase";
|
|
14
|
-
FileNamingStyle2["paramCase"] = "paramCase";
|
|
15
|
-
FileNamingStyle2["pascalCase"] = "pascalCase";
|
|
16
|
-
FileNamingStyle2["pathCase"] = "pathCase";
|
|
17
|
-
FileNamingStyle2["sentenceCase"] = "sentenceCase";
|
|
18
|
-
FileNamingStyle2["snakeCase"] = "snakeCase";
|
|
19
|
-
return FileNamingStyle2;
|
|
20
|
-
})(FileNamingStyle || {});
|
|
21
|
-
|
|
22
|
-
// src/constants/qs-array-format.ts
|
|
23
|
-
var QsArrayFormat = /* @__PURE__ */ ((QsArrayFormat2) => {
|
|
24
|
-
QsArrayFormat2["brackets"] = "brackets";
|
|
25
|
-
QsArrayFormat2["indices"] = "indices";
|
|
26
|
-
QsArrayFormat2["repeat"] = "repeat";
|
|
27
|
-
QsArrayFormat2["comma"] = "comma";
|
|
28
|
-
QsArrayFormat2["space"] = "space";
|
|
29
|
-
QsArrayFormat2["pipe"] = "pipe";
|
|
30
|
-
return QsArrayFormat2;
|
|
31
|
-
})(QsArrayFormat || {});
|
|
32
|
-
|
|
33
|
-
// src/constants/supported-methods.ts
|
|
34
|
-
var SupportedMethods = [
|
|
35
|
-
"get",
|
|
36
|
-
"post",
|
|
37
|
-
"put",
|
|
38
|
-
"delete",
|
|
39
|
-
"patch",
|
|
40
|
-
"head",
|
|
41
|
-
"options"
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
// src/compiler/compiler.ts
|
|
45
|
-
import { Listr } from "listr2";
|
|
46
|
-
import { AsyncParallelHook, AsyncSeriesBailHook, AsyncSeriesHook, SyncHook } from "tapable";
|
|
47
|
-
|
|
48
|
-
// src/compiler/tasks/setup/index.ts
|
|
49
|
-
import fs4 from "fs-extra";
|
|
50
|
-
import path10 from "path";
|
|
51
|
-
import { cosmiconfig } from "cosmiconfig";
|
|
52
|
-
|
|
53
|
-
// src/utils/ignore-matcher.ts
|
|
54
|
-
import * as R from "ramda";
|
|
55
|
-
import fs from "fs-extra";
|
|
56
|
-
var IgnoreMatcher = class _IgnoreMatcher {
|
|
57
|
-
rules;
|
|
58
|
-
constructor(rules) {
|
|
59
|
-
this.rules = rules;
|
|
60
|
-
}
|
|
61
|
-
static async read(filepath) {
|
|
62
|
-
let content = await fs.readFile(filepath, "utf-8");
|
|
63
|
-
content = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/#.*$/gm, "").replace(/\n+/g, "\n").trim();
|
|
64
|
-
let rules = content.split("\n").map((line) => line.trim()).filter((line) => line.length > 0).map((line) => {
|
|
65
|
-
const matched = line.match(/^(!)?\s*([^\s/]+)\s+([^\s/]+)\s+([^\s]+)$/);
|
|
66
|
-
if (!matched) throw new Error(`Invalid ignore rule: "${line}"`);
|
|
67
|
-
const [, flag, moduleName, operationMethod, operationPathname] = matched;
|
|
68
|
-
return {
|
|
69
|
-
persist: true,
|
|
70
|
-
ignore: !flag,
|
|
71
|
-
moduleName,
|
|
72
|
-
operationMethod: operationMethod.toLowerCase(),
|
|
73
|
-
operationPathname
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
rules = R.sortBy(
|
|
77
|
-
R.prop("ignore"),
|
|
78
|
-
rules
|
|
79
|
-
);
|
|
80
|
-
rules = R.uniqWith(
|
|
81
|
-
(a, b) => a.moduleName === b.moduleName && a.operationMethod === b.operationMethod && a.operationPathname === b.operationPathname,
|
|
82
|
-
rules
|
|
83
|
-
);
|
|
84
|
-
return new _IgnoreMatcher(rules);
|
|
85
|
-
}
|
|
86
|
-
async write(filepath) {
|
|
87
|
-
const ruleGroups = R.compose(
|
|
88
|
-
R.groupBy((rule) => rule.moduleName),
|
|
89
|
-
R.reverse,
|
|
90
|
-
R.uniqWith(
|
|
91
|
-
(a, b) => a.moduleName === b.moduleName && a.operationMethod === b.operationMethod && a.operationPathname === b.operationPathname
|
|
92
|
-
),
|
|
93
|
-
R.filter((rule) => rule.persist)
|
|
94
|
-
)(this.rules);
|
|
95
|
-
const content = Object.entries(ruleGroups).sort((a, b) => {
|
|
96
|
-
const aModuleName = a[0];
|
|
97
|
-
const bModuleName = b[0];
|
|
98
|
-
if (aModuleName === "*") return -1;
|
|
99
|
-
if (bModuleName === "*") return 1;
|
|
100
|
-
return aModuleName.localeCompare(bModuleName);
|
|
101
|
-
}).map(([, rules]) => (rules || []).sort((a, b) => {
|
|
102
|
-
if (a.ignore !== b.ignore) return a.ignore ? -1 : 1;
|
|
103
|
-
if (a.operationMethod === "*") return -1;
|
|
104
|
-
if (b.operationMethod === "*") return 1;
|
|
105
|
-
if (a.operationPathname === "*") return -1;
|
|
106
|
-
if (b.operationPathname === "*") return 1;
|
|
107
|
-
if (a.operationMethod !== b.operationMethod) return a.operationMethod.localeCompare(b.operationMethod);
|
|
108
|
-
if (a.operationPathname !== b.operationPathname) return a.operationPathname.localeCompare(b.operationPathname);
|
|
109
|
-
return 0;
|
|
110
|
-
}).map((rule) => `${rule.ignore ? "" : "! "}${rule.moduleName} ${rule.operationMethod.toUpperCase()} ${rule.operationPathname}`).join("\n")).join("\n\n");
|
|
111
|
-
await fs.writeFile(filepath, content, "utf-8");
|
|
112
|
-
}
|
|
113
|
-
append(rule) {
|
|
114
|
-
this.rules.unshift(rule);
|
|
115
|
-
}
|
|
116
|
-
// if operation is ignored, return true
|
|
117
|
-
isOperationIgnored(operationDefinition) {
|
|
118
|
-
const moduleName = operationDefinition.module.name;
|
|
119
|
-
const operationMethod = operationDefinition.method.toLowerCase();
|
|
120
|
-
const operationPathname = operationDefinition.pathname;
|
|
121
|
-
for (const rule of this.rules) {
|
|
122
|
-
if (rule.moduleName !== "*" && rule.moduleName !== moduleName) continue;
|
|
123
|
-
if (rule.operationMethod !== "*" && rule.operationMethod !== operationMethod) continue;
|
|
124
|
-
if (rule.operationPathname !== "*" && rule.operationPathname !== operationPathname) continue;
|
|
125
|
-
return rule.ignore;
|
|
126
|
-
}
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
isModuleIgnored(moduleDefinition) {
|
|
130
|
-
const moduleName = moduleDefinition.name;
|
|
131
|
-
for (const rule of this.rules) {
|
|
132
|
-
if (!rule.ignore) {
|
|
133
|
-
if (rule.moduleName === "*" || rule.moduleName === moduleName) return false;
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
if (rule.operationMethod !== "*") continue;
|
|
137
|
-
if (rule.operationPathname !== "*") continue;
|
|
138
|
-
if (rule.moduleName === "*" || rule.moduleName === moduleName) return true;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
// src/compiler/tasks/setup/utils/find-nearest-package-json.ts
|
|
145
|
-
import fs2 from "fs";
|
|
146
|
-
import path from "path";
|
|
147
|
-
function findNearestPackageJson(startDir = process.cwd()) {
|
|
148
|
-
let dir = startDir;
|
|
149
|
-
while (true) {
|
|
150
|
-
const pkgPath = path.join(dir, "package.json");
|
|
151
|
-
if (fs2.existsSync(pkgPath)) {
|
|
152
|
-
const json = JSON.parse(fs2.readFileSync(pkgPath, "utf8"));
|
|
153
|
-
return { json, path: pkgPath };
|
|
154
|
-
}
|
|
155
|
-
const parent = path.dirname(dir);
|
|
156
|
-
if (parent === dir) break;
|
|
157
|
-
dir = parent;
|
|
158
|
-
}
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/compiler/tasks/setup/utils/get-project-module-system.ts
|
|
163
|
-
function getProjectModuleSystem(pkgInfo) {
|
|
164
|
-
if (!pkgInfo?.json) return "cjs";
|
|
165
|
-
const { json } = pkgInfo;
|
|
166
|
-
if (json.type === "module") return "esm";
|
|
167
|
-
return "cjs";
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// src/compiler/tasks/setup/utils/parse-runtime-config.ts
|
|
171
|
-
import { Value } from "@sinclair/typebox/value";
|
|
172
|
-
|
|
173
|
-
// src/types/address.ts
|
|
174
|
-
import { Type } from "@sinclair/typebox";
|
|
175
|
-
var Address = Type.Object({
|
|
176
|
-
url: Type.String(),
|
|
177
|
-
headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })),
|
|
178
|
-
encoding: Type.Optional(
|
|
179
|
-
Type.Union([
|
|
180
|
-
Type.Literal("utf8"),
|
|
181
|
-
Type.Literal("ascii")
|
|
182
|
-
], { default: "utf8" })
|
|
183
|
-
)
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
// src/types/runtime-config.ts
|
|
187
|
-
import * as R2 from "ramda";
|
|
188
|
-
import { Type as Type2 } from "@sinclair/typebox";
|
|
189
|
-
|
|
190
|
-
// src/utils/is-valid-url.ts
|
|
191
|
-
var URL_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/(?:[^\s@]+@)?[^\s/:?#]*(?::\d+)?(?:\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#[^\s]*)?$/;
|
|
192
|
-
function isValidURL(url) {
|
|
193
|
-
return URL_REGEX.test(url);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// src/types/runtime-config.ts
|
|
197
|
-
var Modules = Type2.Transform(
|
|
198
|
-
Type2.Record(
|
|
199
|
-
Type2.String(),
|
|
200
|
-
Type2.Union([Type2.String(), Address])
|
|
201
|
-
)
|
|
202
|
-
).Decode((value) => {
|
|
203
|
-
const keys2 = Object.keys(value);
|
|
204
|
-
for (const key of keys2) {
|
|
205
|
-
if (!/^[A-Za-z_][A-Za-z0-9_$]*$/.test(key)) {
|
|
206
|
-
throw new Error(`Module name "${key}" is not valid. It must start with a letter or underscore, and can only contain letters, numbers, and underscores.`);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
const keysGroupByLowerCase = R2.groupBy(R2.toLower, keys2);
|
|
210
|
-
for (const groupKey in keysGroupByLowerCase) {
|
|
211
|
-
const keys3 = keysGroupByLowerCase[groupKey] || [];
|
|
212
|
-
if (keys3.length > 1) {
|
|
213
|
-
throw new Error(`Module names ${keys3.map((name) => `"${name}"`).join(", ")} are case-insensitively duplicated.`);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
for (const key in value) {
|
|
217
|
-
const url = typeof value[key] === "string" ? value[key] : value[key].url;
|
|
218
|
-
if (isValidURL(url)) continue;
|
|
219
|
-
throw new Error(`The ${JSON.stringify(url)} of module "${key}" is not a valid URL.`);
|
|
220
|
-
}
|
|
221
|
-
return R2.map(
|
|
222
|
-
(item) => typeof item !== "string" ? item : { url: item, headers: {}, encoding: "utf8" },
|
|
223
|
-
value
|
|
224
|
-
);
|
|
225
|
-
}).Encode((value) => value);
|
|
226
|
-
var RawConfig = Type2.Object({
|
|
227
|
-
/**
|
|
228
|
-
* Whether to generate ES Module code
|
|
229
|
-
*
|
|
230
|
-
* If not specified, the module system will be inferred from the nearest package.json "type" field
|
|
231
|
-
* or defaults to "cjs" if no package.json is found.
|
|
232
|
-
*/
|
|
233
|
-
esm: Type2.Optional(Type2.Boolean({ default: false })),
|
|
234
|
-
/**
|
|
235
|
-
* Output directory for generated files
|
|
236
|
-
*/
|
|
237
|
-
outdir: Type2.String({ default: `${process.cwd()}/api` }),
|
|
238
|
-
/**
|
|
239
|
-
* File naming style for generated files
|
|
240
|
-
*/
|
|
241
|
-
fileNamingStyle: Type2.Enum(FileNamingStyle, { default: "snakeCase" /* snakeCase */ }),
|
|
242
|
-
/**
|
|
243
|
-
* OpenAPI/Swagger document sources
|
|
244
|
-
*
|
|
245
|
-
* A map of module names to their document URLs or Address objects.
|
|
246
|
-
* The module name must be a valid JavaScript identifier.
|
|
247
|
-
*/
|
|
248
|
-
modules: Modules,
|
|
249
|
-
/**
|
|
250
|
-
* Enable debug mode to output detailed logs during compilation
|
|
251
|
-
*/
|
|
252
|
-
debug: Type2.Optional(Type2.Boolean({ default: false })),
|
|
253
|
-
/**
|
|
254
|
-
* Whether to tolerate wrong openapi/swagger structure
|
|
255
|
-
*/
|
|
256
|
-
tolerant: Type2.Optional(Type2.Boolean({ default: false })),
|
|
257
|
-
/**
|
|
258
|
-
* Translators to transform generated code
|
|
259
|
-
*
|
|
260
|
-
* Used to customize the code generation process for different frameworks or patterns.
|
|
261
|
-
* Can be a single translator or an array of translators.
|
|
262
|
-
*/
|
|
263
|
-
translators: Type2.Optional(
|
|
264
|
-
Type2.Transform(
|
|
265
|
-
Type2.Union([
|
|
266
|
-
Type2.Unsafe(Type2.Any()),
|
|
267
|
-
Type2.Array(Type2.Unsafe(Type2.Any()))
|
|
268
|
-
], { default: [] })
|
|
269
|
-
).Decode((value) => {
|
|
270
|
-
if (value === void 0) return [];
|
|
271
|
-
if (Array.isArray(value)) return value;
|
|
272
|
-
return [value];
|
|
273
|
-
}).Encode((value) => value)
|
|
274
|
-
),
|
|
275
|
-
/**
|
|
276
|
-
* Plugins to extend code generation functionality
|
|
277
|
-
*
|
|
278
|
-
* An array of plugin instances that can hook into various stages of the compilation process,
|
|
279
|
-
* such as code transformation, formatting, linting, or custom file generation.
|
|
280
|
-
*/
|
|
281
|
-
plugins: Type2.Optional(Type2.Array(Type2.Unsafe(Type2.Any()), { default: [] }))
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
// src/compiler/tasks/setup/utils/parse-runtime-config.ts
|
|
285
|
-
function parseRuntimeConfig(data) {
|
|
286
|
-
try {
|
|
287
|
-
const originalPlugins = typeof data === "object" && data !== null && "plugins" in data ? data.plugins : void 0;
|
|
288
|
-
const originTranslators = typeof data === "object" && data !== null && "translators" in data ? data.translators : void 0;
|
|
289
|
-
const parsed = Value.Parse(RawConfig, data);
|
|
290
|
-
if (originalPlugins !== void 0) {
|
|
291
|
-
parsed.plugins = originalPlugins;
|
|
292
|
-
}
|
|
293
|
-
if (originTranslators !== void 0) {
|
|
294
|
-
parsed.translators = originTranslators;
|
|
295
|
-
}
|
|
296
|
-
return parsed;
|
|
297
|
-
} catch (error) {
|
|
298
|
-
if (error instanceof Error) {
|
|
299
|
-
error.message = `Invalid Config: ${error.message}`;
|
|
300
|
-
}
|
|
301
|
-
throw error;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// src/plugins/eslint/eslint.plugin.ts
|
|
306
|
-
import { ESLint } from "eslint";
|
|
307
|
-
|
|
308
|
-
// src/plugins/prettier/prettier.plugin.ts
|
|
309
|
-
import { exec } from "child_process";
|
|
310
|
-
import { promisify } from "util";
|
|
311
|
-
var execAsync = promisify(exec);
|
|
312
|
-
|
|
313
|
-
// src/plugins/body-fallback/index.ts
|
|
314
|
-
import * as R21 from "ramda";
|
|
315
|
-
|
|
316
|
-
// src/utils/json-schema-utils/is-array.ts
|
|
317
|
-
function isArray(schema) {
|
|
318
|
-
return schema.type === "array";
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// src/utils/json-schema-utils/is-mixed.ts
|
|
322
|
-
function isMixed(schema) {
|
|
323
|
-
return Array.isArray(schema.type);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// src/utils/json-schema-utils/is-non-array.ts
|
|
327
|
-
function isNonArray(schema) {
|
|
328
|
-
return typeof schema.type === "string" && schema.type !== "array";
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// src/utils/json-schema-utils/is-ref.ts
|
|
332
|
-
function isRef(schema) {
|
|
333
|
-
return "$ref" in schema;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// src/utils/json-schema-utils/index.ts
|
|
337
|
-
var JsonSchemaUtils = class {
|
|
338
|
-
static isRef = isRef;
|
|
339
|
-
static isArray = isArray;
|
|
340
|
-
static isNonArray = isNonArray;
|
|
341
|
-
static isMixed = isMixed;
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
// src/plugins/generate-micro-function/generate-micro-function.plugin.ts
|
|
345
|
-
import { AsyncSeriesWaterfallHook as AsyncSeriesWaterfallHook2 } from "tapable";
|
|
346
|
-
|
|
347
|
-
// src/plugins/generate-micro-function/constants/metadata-storage.ts
|
|
348
|
-
var MetadataStorage3 = /* @__PURE__ */ new WeakMap();
|
|
349
|
-
|
|
350
|
-
// src/plugins/generate-micro-function/generators/micro_function/micro-function.generator.ts
|
|
351
|
-
import * as R20 from "ramda";
|
|
352
|
-
import * as path8 from "path";
|
|
353
|
-
import * as changeCase6 from "change-case";
|
|
354
|
-
|
|
355
|
-
// src/models/artifact.ts
|
|
356
|
-
import * as path2 from "path";
|
|
357
|
-
|
|
358
|
-
// src/models/anchor.ts
|
|
359
|
-
var AnchorBlock = class {
|
|
360
|
-
constructor(artifact) {
|
|
361
|
-
this.artifact = artifact;
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Append content to the end of the anchor block.
|
|
365
|
-
*/
|
|
366
|
-
append(anchorName, content) {
|
|
367
|
-
const lines = this.artifact.content.split("\n");
|
|
368
|
-
const anchor = `@anchor:${anchorName}:end`;
|
|
369
|
-
const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
|
|
370
|
-
if (anchorIndex === -1) {
|
|
371
|
-
throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
|
|
372
|
-
}
|
|
373
|
-
lines.splice(anchorIndex, 0, content);
|
|
374
|
-
this.artifact.content = lines.join("\n");
|
|
375
|
-
}
|
|
376
|
-
prepend(anchorName, content) {
|
|
377
|
-
const lines = this.artifact.content.split("\n");
|
|
378
|
-
const anchor = `@anchor:${anchorName}:start`;
|
|
379
|
-
const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
|
|
380
|
-
if (anchorIndex === -1) {
|
|
381
|
-
throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
|
|
382
|
-
}
|
|
383
|
-
lines.splice(anchorIndex + 1, 0, content);
|
|
384
|
-
this.artifact.content = lines.join("\n");
|
|
385
|
-
}
|
|
386
|
-
replace(anchorName, content) {
|
|
387
|
-
const lines = this.artifact.content.split("\n");
|
|
388
|
-
const startIndex = lines.findIndex((line) => line.includes(`/* @anchor:${anchorName}:start */`));
|
|
389
|
-
const endIndex = lines.findIndex((line) => line.includes(`/* @anchor:${anchorName}:end */`));
|
|
390
|
-
if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) {
|
|
391
|
-
throw new Error(`"@anchor:${anchorName}:start" or "@anchor:${anchorName}:end" not found in artifact "${this.artifact.filepath}".`);
|
|
392
|
-
}
|
|
393
|
-
lines.splice(startIndex + 1, endIndex - startIndex - 1, content);
|
|
394
|
-
this.artifact.content = lines.join("\n");
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
var Anchor = class {
|
|
398
|
-
constructor(artifact) {
|
|
399
|
-
this.artifact = artifact;
|
|
400
|
-
this.block = new AnchorBlock(artifact);
|
|
401
|
-
}
|
|
402
|
-
block;
|
|
403
|
-
append(anchorName, content) {
|
|
404
|
-
const lines = this.artifact.content.split("\n");
|
|
405
|
-
const anchor = `@anchor:${anchorName}`;
|
|
406
|
-
const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
|
|
407
|
-
if (anchorIndex === -1) {
|
|
408
|
-
throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
|
|
409
|
-
}
|
|
410
|
-
lines.splice(anchorIndex + 1, 0, content);
|
|
411
|
-
this.artifact.content = lines.join("\n");
|
|
412
|
-
}
|
|
413
|
-
prepend(anchorName, content) {
|
|
414
|
-
const lines = this.artifact.content.split("\n");
|
|
415
|
-
const anchor = `@anchor:${anchorName}`;
|
|
416
|
-
const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
|
|
417
|
-
if (anchorIndex === -1) {
|
|
418
|
-
throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
|
|
419
|
-
}
|
|
420
|
-
lines.splice(anchorIndex, 0, content);
|
|
421
|
-
this.artifact.content = lines.join("\n");
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
// src/models/artifact.ts
|
|
426
|
-
var HeaderComment = [
|
|
427
|
-
"// Code generated by keq-cli. DO NOT EDIT.",
|
|
428
|
-
"// Code generated by keq-cli. DO NOT EDIT.",
|
|
429
|
-
"// Code generated by keq-cli. DO NOT EDIT.",
|
|
430
|
-
"",
|
|
431
|
-
""
|
|
432
|
-
].join("\n");
|
|
433
|
-
var Artifact = class {
|
|
434
|
-
id;
|
|
435
|
-
filepath;
|
|
436
|
-
content;
|
|
437
|
-
extensionName;
|
|
438
|
-
anchor = new Anchor(this);
|
|
439
|
-
constructor(options) {
|
|
440
|
-
this.id = options.id;
|
|
441
|
-
this.filepath = options.filepath;
|
|
442
|
-
this.extensionName = options.extensionName || path2.extname(options.filepath);
|
|
443
|
-
this.content = options.content;
|
|
444
|
-
}
|
|
445
|
-
get dirname() {
|
|
446
|
-
return path2.dirname(this.filepath);
|
|
447
|
-
}
|
|
448
|
-
renderer() {
|
|
449
|
-
return `${HeaderComment}${this.content}`;
|
|
450
|
-
}
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
// src/models/api-document_v3_1.ts
|
|
454
|
-
import * as R5 from "ramda";
|
|
455
|
-
|
|
456
|
-
// src/models/module-definition.ts
|
|
457
|
-
var ModuleDefinition = class _ModuleDefinition {
|
|
458
|
-
name;
|
|
459
|
-
address;
|
|
460
|
-
constructor(name, address) {
|
|
461
|
-
this.name = name;
|
|
462
|
-
if (typeof address === "string") {
|
|
463
|
-
this.address = { url: address, headers: {}, encoding: "utf8" };
|
|
464
|
-
} else {
|
|
465
|
-
this.address = address;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
static unknown() {
|
|
469
|
-
return new _ModuleDefinition("", { url: "", headers: {}, encoding: "utf8" });
|
|
470
|
-
}
|
|
471
|
-
};
|
|
472
|
-
|
|
473
|
-
// src/models/operation-definition.ts
|
|
474
|
-
import * as R4 from "ramda";
|
|
475
|
-
|
|
476
|
-
// src/utils/is-keywords.ts
|
|
477
|
-
var keywords = [
|
|
478
|
-
"break",
|
|
479
|
-
"case",
|
|
480
|
-
"catch",
|
|
481
|
-
"continue",
|
|
482
|
-
"default",
|
|
483
|
-
"delete",
|
|
484
|
-
"do",
|
|
485
|
-
"else",
|
|
486
|
-
"finally",
|
|
487
|
-
"for",
|
|
488
|
-
"function",
|
|
489
|
-
"if",
|
|
490
|
-
"in",
|
|
491
|
-
"instanceof",
|
|
492
|
-
"new",
|
|
493
|
-
"return",
|
|
494
|
-
"switch",
|
|
495
|
-
"this",
|
|
496
|
-
"throw",
|
|
497
|
-
"try",
|
|
498
|
-
"typeof",
|
|
499
|
-
"var",
|
|
500
|
-
"void",
|
|
501
|
-
"while",
|
|
502
|
-
"with"
|
|
503
|
-
];
|
|
504
|
-
function isKeywords(str) {
|
|
505
|
-
return keywords.includes(str);
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
// src/utils/is-reserved-word.ts
|
|
509
|
-
var reservedWords = [
|
|
510
|
-
"abstract",
|
|
511
|
-
"boolean",
|
|
512
|
-
"byte",
|
|
513
|
-
"char",
|
|
514
|
-
"class",
|
|
515
|
-
"const",
|
|
516
|
-
"debugger",
|
|
517
|
-
"double",
|
|
518
|
-
"enum",
|
|
519
|
-
"export",
|
|
520
|
-
"extends",
|
|
521
|
-
"final",
|
|
522
|
-
"float",
|
|
523
|
-
"goto",
|
|
524
|
-
"implements",
|
|
525
|
-
"import",
|
|
526
|
-
"int",
|
|
527
|
-
"interface",
|
|
528
|
-
"long",
|
|
529
|
-
"native",
|
|
530
|
-
"package",
|
|
531
|
-
"private",
|
|
532
|
-
"protected",
|
|
533
|
-
"public",
|
|
534
|
-
"short",
|
|
535
|
-
"static",
|
|
536
|
-
"super",
|
|
537
|
-
"synchronized",
|
|
538
|
-
"throws",
|
|
539
|
-
"transient",
|
|
540
|
-
"volatile"
|
|
541
|
-
];
|
|
542
|
-
function isReservedWord(str) {
|
|
543
|
-
return reservedWords.includes(str);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// src/models/operation-definition.ts
|
|
547
|
-
import { JSONPath as JSONPath2 } from "jsonpath-plus";
|
|
548
|
-
|
|
549
|
-
// src/models/schema-definition.ts
|
|
550
|
-
import * as R3 from "ramda";
|
|
551
|
-
import { JSONPath } from "jsonpath-plus";
|
|
552
|
-
var SchemaDefinition = class _SchemaDefinition {
|
|
553
|
-
name;
|
|
554
|
-
schema;
|
|
555
|
-
module;
|
|
556
|
-
document;
|
|
557
|
-
get id() {
|
|
558
|
-
return `${this.module.address}#/components/schemas/${this.name}`;
|
|
559
|
-
}
|
|
560
|
-
constructor(args) {
|
|
561
|
-
this.module = args.module;
|
|
562
|
-
this.name = args.name;
|
|
563
|
-
this.schema = args.schema;
|
|
564
|
-
this.document = args.document;
|
|
565
|
-
}
|
|
566
|
-
getDependencies() {
|
|
567
|
-
const refs = R3.uniq(JSONPath({
|
|
568
|
-
path: "$..*['$ref']",
|
|
569
|
-
json: this.schema
|
|
570
|
-
}));
|
|
571
|
-
return refs.filter((ref) => typeof ref === "string" && !!ref.trim()).map((ref) => {
|
|
572
|
-
const definition = this.document.dereference(ref);
|
|
573
|
-
if (definition) return definition;
|
|
574
|
-
return _SchemaDefinition.unknown();
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
static unknown() {
|
|
578
|
-
return new _SchemaDefinition({
|
|
579
|
-
name: "",
|
|
580
|
-
schema: {},
|
|
581
|
-
module: ModuleDefinition.unknown(),
|
|
582
|
-
document: ApiDocumentV3_1.unknown()
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
static isUnknown(definition) {
|
|
586
|
-
return definition.name === "";
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
// src/models/operation-definition.ts
|
|
591
|
-
var OperationDefinition = class {
|
|
592
|
-
module;
|
|
593
|
-
operationId;
|
|
594
|
-
method;
|
|
595
|
-
pathname;
|
|
596
|
-
operation;
|
|
597
|
-
document;
|
|
598
|
-
get id() {
|
|
599
|
-
return `${this.module.address}#/paths/${this.pathname}/${this.method}`;
|
|
600
|
-
}
|
|
601
|
-
constructor(args) {
|
|
602
|
-
this.module = args.module;
|
|
603
|
-
this.method = args.method.toLowerCase();
|
|
604
|
-
this.pathname = args.pathname;
|
|
605
|
-
this.document = args.document;
|
|
606
|
-
this.operationId = this.formatOperationId(args.method, args.pathname, args.operation);
|
|
607
|
-
this.operation = {
|
|
608
|
-
...args.operation,
|
|
609
|
-
operationId: this.operationId
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
formatOperationId(method, pathname, operation) {
|
|
613
|
-
const operationId = operation.operationId;
|
|
614
|
-
if (operationId && operationId !== "index" && !isKeywords(operationId) && !isReservedWord(operationId)) {
|
|
615
|
-
return operationId;
|
|
616
|
-
}
|
|
617
|
-
return `${method}_${pathname}`.replace(/\//g, "_").replace(/-/g, "_").replace(/:/g, "$$").replace(/{(.+)}/, "$$$1");
|
|
618
|
-
}
|
|
619
|
-
getDependencies() {
|
|
620
|
-
const refs = R4.uniq([
|
|
621
|
-
...JSONPath2({
|
|
622
|
-
path: "$.requestBody.content..schema..$ref",
|
|
623
|
-
json: this.operation
|
|
624
|
-
}),
|
|
625
|
-
...JSONPath2({
|
|
626
|
-
path: "$.responses..content..schema..$ref",
|
|
627
|
-
json: this.operation
|
|
628
|
-
}),
|
|
629
|
-
...JSONPath2({
|
|
630
|
-
path: "$.parameters..schema..$ref",
|
|
631
|
-
json: this.operation
|
|
632
|
-
})
|
|
633
|
-
]);
|
|
634
|
-
const dependencies = refs.filter((ref) => typeof ref === "string" && ref).map((ref) => {
|
|
635
|
-
const schemaDefinition = this.document.dereference(ref);
|
|
636
|
-
if (schemaDefinition) return schemaDefinition;
|
|
637
|
-
return SchemaDefinition.unknown();
|
|
638
|
-
});
|
|
639
|
-
return dependencies;
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
// src/utils/logger.ts
|
|
644
|
-
import chalk from "chalk";
|
|
645
|
-
var logger = {
|
|
646
|
-
log: (str) => console.log(chalk.green(str)),
|
|
647
|
-
warn: (str) => console.warn(chalk.yellow(str)),
|
|
648
|
-
error: (str) => console.error(chalk.red(str))
|
|
649
|
-
};
|
|
650
|
-
|
|
651
|
-
// src/models/api-document_v3_1.ts
|
|
652
|
-
var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
|
|
653
|
-
module;
|
|
654
|
-
specification;
|
|
655
|
-
constructor(specification, module) {
|
|
656
|
-
this.module = module;
|
|
657
|
-
this.specification = specification;
|
|
658
|
-
}
|
|
659
|
-
get schemas() {
|
|
660
|
-
const module = this.module;
|
|
661
|
-
return Object.entries(this.specification.components?.schemas || {}).map(([name, schema]) => new SchemaDefinition({
|
|
662
|
-
name,
|
|
663
|
-
schema,
|
|
664
|
-
module,
|
|
665
|
-
document: this
|
|
666
|
-
}));
|
|
667
|
-
}
|
|
668
|
-
get operations() {
|
|
669
|
-
const module = this.module;
|
|
670
|
-
return Object.entries(this.specification.paths || {}).flatMap(([pathname, pathItem]) => Object.entries(pathItem || {}).filter(([method]) => SupportedMethods.includes(method.toLowerCase())).map(([method, operation]) => new OperationDefinition({
|
|
671
|
-
method,
|
|
672
|
-
pathname,
|
|
673
|
-
operation,
|
|
674
|
-
module,
|
|
675
|
-
document: this
|
|
676
|
-
})));
|
|
677
|
-
}
|
|
678
|
-
isEmpty() {
|
|
679
|
-
return R5.isEmpty(this.specification.paths);
|
|
680
|
-
}
|
|
681
|
-
dereference($ref) {
|
|
682
|
-
if ($ref.startsWith("#/")) {
|
|
683
|
-
return this.schemas.find((schema) => schema.id.endsWith($ref));
|
|
684
|
-
}
|
|
685
|
-
logger.warn(`The $ref(${$ref}) is not defined in ${this.module.name} openapi/swagger.`);
|
|
686
|
-
}
|
|
687
|
-
isRefDefined($ref) {
|
|
688
|
-
if ($ref.startsWith("#/")) {
|
|
689
|
-
const path12 = $ref.replace("#/", "").split("/");
|
|
690
|
-
return R5.path(path12, this.specification) !== void 0;
|
|
691
|
-
}
|
|
692
|
-
return false;
|
|
693
|
-
}
|
|
694
|
-
static unknown() {
|
|
695
|
-
return new _ApiDocumentV3_1({}, ModuleDefinition.unknown());
|
|
696
|
-
}
|
|
697
|
-
};
|
|
698
|
-
|
|
699
|
-
// src/models/asset.ts
|
|
700
|
-
var Asset = class {
|
|
701
|
-
path;
|
|
702
|
-
constructor(path12) {
|
|
703
|
-
this.path = path12;
|
|
704
|
-
}
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
// src/plugins/generate-declaration/generate-declaration.plugin.ts
|
|
708
|
-
import { AsyncSeriesWaterfallHook } from "tapable";
|
|
709
|
-
|
|
710
|
-
// src/plugins/generate-declaration/generators/operation-declaration/operation.generator.ts
|
|
711
|
-
import * as path7 from "path";
|
|
712
|
-
import * as changeCase5 from "change-case";
|
|
713
|
-
|
|
714
|
-
// src/transformers/json-schema/comment.renderer.ts
|
|
715
|
-
var CommentRenderer = class {
|
|
716
|
-
constructor(schema) {
|
|
717
|
-
this.schema = schema;
|
|
718
|
-
}
|
|
719
|
-
render() {
|
|
720
|
-
const schema = this.schema;
|
|
721
|
-
const lines = ["/**"];
|
|
722
|
-
if (schema.description) {
|
|
723
|
-
const description = schema.description.replace("*/", "*\\/");
|
|
724
|
-
lines.push(...description.split("\n").map((line) => ` * ${line}`));
|
|
725
|
-
}
|
|
726
|
-
if (schema.deprecated) {
|
|
727
|
-
lines.push(" * @deprecated");
|
|
728
|
-
}
|
|
729
|
-
if (schema.readOnly) {
|
|
730
|
-
lines.push(" * @readonly");
|
|
731
|
-
}
|
|
732
|
-
if (schema.format) {
|
|
733
|
-
lines.push(` * @format ${schema.format}`);
|
|
734
|
-
}
|
|
735
|
-
lines.push(" */");
|
|
736
|
-
if (lines.length === 2) return "";
|
|
737
|
-
return lines.join("\n");
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
|
|
741
|
-
// src/transformers/json-schema/declaration.renderer.ts
|
|
742
|
-
import * as R7 from "ramda";
|
|
743
|
-
|
|
744
|
-
// src/utils/indent.ts
|
|
745
|
-
function indent(space, text) {
|
|
746
|
-
if (text === "") return "";
|
|
747
|
-
const indentation = " ".repeat(space);
|
|
748
|
-
return text.split("\n").map((line) => line ? `${indentation}${line}` : line).join("\n");
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
// src/transformers/json-schema/reference.transformer.ts
|
|
752
|
-
import * as R6 from "ramda";
|
|
753
|
-
var ReferenceTransformer = class {
|
|
754
|
-
static toDeclaration(schema, alias2 = R6.identity) {
|
|
755
|
-
if (!schema.$ref || !schema.$ref.startsWith("#")) return `unknown /* ${schema.$ref.replace("*/", "*\\/")} */`;
|
|
756
|
-
const parts = schema.$ref.split("/");
|
|
757
|
-
return alias2(parts[parts.length - 1]) || "unknown";
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
|
-
|
|
761
|
-
// src/transformers/json-schema/declaration.renderer.ts
|
|
762
|
-
var DeclarationRenderer = class {
|
|
763
|
-
constructor(schema, options = {}) {
|
|
764
|
-
this.schema = schema;
|
|
765
|
-
this.options = options;
|
|
766
|
-
}
|
|
767
|
-
render() {
|
|
768
|
-
return this.renderSchema(this.schema);
|
|
769
|
-
}
|
|
770
|
-
renderSchema(schema) {
|
|
771
|
-
if (typeof schema === "boolean") return "unknown";
|
|
772
|
-
if (JsonSchemaUtils.isRef(schema)) return this.renderReference(schema);
|
|
773
|
-
if (JsonSchemaUtils.isMixed(schema)) return this.renderMixed(schema);
|
|
774
|
-
if (JsonSchemaUtils.isArray(schema)) return this.renderArray(schema);
|
|
775
|
-
if (schema.type === "object") return this.renderObject(schema);
|
|
776
|
-
if (schema.enum) return this.renderEnum(schema);
|
|
777
|
-
if (schema.oneOf) return this.renderOneOf(schema);
|
|
778
|
-
if (schema.anyOf) return this.renderAnyOf(schema);
|
|
779
|
-
if (schema.allOf) return this.renderAllOf(schema);
|
|
780
|
-
if (schema.type === "string") return this.renderString(schema);
|
|
781
|
-
if (schema.type === "number") return this.renderNumber(schema);
|
|
782
|
-
if (schema.type === "boolean") return this.renderBoolean(schema);
|
|
783
|
-
if (schema.type === "null") return this.renderNull(schema);
|
|
784
|
-
if (schema.type === "integer") return this.renderInteger(schema);
|
|
785
|
-
return "unknown";
|
|
786
|
-
}
|
|
787
|
-
renderMixed(schema) {
|
|
788
|
-
if (Array.isArray(schema.type)) {
|
|
789
|
-
schema.type.map((type) => ({ ...schema, type })).map((schema2) => this.renderSchema(schema2)).join(" | ");
|
|
790
|
-
}
|
|
791
|
-
return "unknown";
|
|
792
|
-
}
|
|
793
|
-
renderReference(schema) {
|
|
794
|
-
if (!this.options.referenceTransformer) {
|
|
795
|
-
return ReferenceTransformer.toDeclaration(schema);
|
|
796
|
-
}
|
|
797
|
-
return this.options.referenceTransformer(schema);
|
|
798
|
-
}
|
|
799
|
-
renderArray(schema) {
|
|
800
|
-
if (schema.items && Array.isArray(schema.items)) {
|
|
801
|
-
const items = schema.items.map((s) => this.renderSchema(s)).join(", ");
|
|
802
|
-
return `[${items}]`;
|
|
803
|
-
}
|
|
804
|
-
if (schema.items && typeof schema.items === "object") {
|
|
805
|
-
return `${this.renderSchema(schema.items)}[]`;
|
|
806
|
-
}
|
|
807
|
-
return "any[]";
|
|
808
|
-
}
|
|
809
|
-
renderObject(schema) {
|
|
810
|
-
if ((!schema.properties || R7.isEmpty(schema.properties)) && (!schema.additionalProperties || R7.isEmpty(schema.additionalProperties))) {
|
|
811
|
-
return "object";
|
|
812
|
-
}
|
|
813
|
-
const $properties = Object.entries(schema.properties || {}).map(([propertyName, propertySchema]) => {
|
|
814
|
-
let $comment = new CommentRenderer(propertySchema).render();
|
|
815
|
-
if ($comment) $comment += "\n";
|
|
816
|
-
const $key = `"${propertyName}"${schema.required?.includes(propertyName) ? "" : "?"}`;
|
|
817
|
-
const $value = this.renderSchema(propertySchema);
|
|
818
|
-
return indent(2, `${$comment}${$key}: ${$value}`);
|
|
819
|
-
});
|
|
820
|
-
if (schema.additionalProperties) {
|
|
821
|
-
const $value = schema.additionalProperties === true ? "any" : this.renderSchema(schema.additionalProperties);
|
|
822
|
-
$properties.push(indent(2, `[key: string]: ${$value}`));
|
|
823
|
-
}
|
|
824
|
-
return [
|
|
825
|
-
"{",
|
|
826
|
-
...$properties,
|
|
827
|
-
"}"
|
|
828
|
-
].join("\n");
|
|
829
|
-
}
|
|
830
|
-
renderOneOf(schema) {
|
|
831
|
-
if (!schema.oneOf) return "unknown";
|
|
832
|
-
return schema.oneOf.map((s) => this.renderSchema(s)).join(" | ");
|
|
833
|
-
}
|
|
834
|
-
renderAnyOf(schema) {
|
|
835
|
-
if (!schema.anyOf) return "unknown";
|
|
836
|
-
return schema.anyOf.map((s) => this.renderSchema(s)).join(" | ");
|
|
837
|
-
}
|
|
838
|
-
renderAllOf(schema) {
|
|
839
|
-
if (!schema.allOf) return "unknown";
|
|
840
|
-
return schema.allOf.map((s) => this.renderSchema(s)).join(" & ");
|
|
841
|
-
}
|
|
842
|
-
renderEnum(schema) {
|
|
843
|
-
if (!schema.enum) return "unknown";
|
|
844
|
-
return schema.enum.map((v) => JSON.stringify(v)).join(" | ");
|
|
845
|
-
}
|
|
846
|
-
renderString(schema) {
|
|
847
|
-
if (schema.contentMediaType === "application/octet-stream") return "Blob | Buffer";
|
|
848
|
-
if (schema.format === "binary") return "Blob | Buffer";
|
|
849
|
-
return "string";
|
|
850
|
-
}
|
|
851
|
-
renderNumber(schema) {
|
|
852
|
-
return "number";
|
|
853
|
-
}
|
|
854
|
-
renderBoolean(schema) {
|
|
855
|
-
return "boolean";
|
|
856
|
-
}
|
|
857
|
-
renderNull(schema) {
|
|
858
|
-
return "null";
|
|
859
|
-
}
|
|
860
|
-
renderInteger(schema) {
|
|
861
|
-
return "number";
|
|
862
|
-
}
|
|
863
|
-
};
|
|
864
|
-
|
|
865
|
-
// src/transformers/json-schema/json-schema.transformer.ts
|
|
866
|
-
var JsonSchemaTransformer = class {
|
|
867
|
-
static toComment(schema) {
|
|
868
|
-
return new CommentRenderer(schema).render();
|
|
869
|
-
}
|
|
870
|
-
static toDeclaration(schema, options) {
|
|
871
|
-
return new DeclarationRenderer(schema, options).render();
|
|
872
|
-
}
|
|
873
|
-
};
|
|
874
|
-
|
|
875
|
-
// src/transformers/schema-definition/schema-definition.transformer.ts
|
|
876
|
-
import * as R8 from "ramda";
|
|
877
|
-
var SchemaDefinitionTransformer = class {
|
|
878
|
-
static toDeclaration(schemaDefinition, options) {
|
|
879
|
-
const dependencies = schemaDefinition.getDependencies();
|
|
880
|
-
let $dependencies = dependencies.filter((dep) => !SchemaDefinition.isUnknown(dep)).map((dep) => {
|
|
881
|
-
const filepath = options.getDependentSchemaDefinitionFilepath(dep);
|
|
882
|
-
return `import type { ${dep.name} } from "${filepath}"`;
|
|
883
|
-
}).map((str) => str.replace(/ from "(\.\.?\/.+?)(\.ts|\.mts|\.cts|\.js|\.cjs|\.mjs)?"/, options.esm ? ' from "$1.js"' : ' from "$1"')).join("\n");
|
|
884
|
-
if ($dependencies) $dependencies += "\n";
|
|
885
|
-
let $comment = JsonSchemaTransformer.toComment(schemaDefinition.schema);
|
|
886
|
-
if ($comment) $comment += "\n";
|
|
887
|
-
if (typeof schemaDefinition.schema === "boolean") {
|
|
888
|
-
return [
|
|
889
|
-
"/* @anchor:file:start */",
|
|
890
|
-
"",
|
|
891
|
-
$dependencies,
|
|
892
|
-
$comment || void 0,
|
|
893
|
-
`type ${schemaDefinition.name} = unknown`,
|
|
894
|
-
"",
|
|
895
|
-
"/* @anchor:file:end */"
|
|
896
|
-
].filter(R8.isNotNil).join("\n");
|
|
897
|
-
}
|
|
898
|
-
if (JsonSchemaUtils.isNonArray(schemaDefinition.schema) && schemaDefinition.schema.type === "object") {
|
|
899
|
-
const $schema = JsonSchemaTransformer.toDeclaration(schemaDefinition.schema);
|
|
900
|
-
const $declaration = $schema.startsWith("{") ? `export interface ${schemaDefinition.name} ${$schema}` : `export type ${schemaDefinition.name} = ${$schema}`;
|
|
901
|
-
return [
|
|
902
|
-
"/* @anchor:file:start */",
|
|
903
|
-
"",
|
|
904
|
-
$dependencies,
|
|
905
|
-
$comment || void 0,
|
|
906
|
-
$declaration,
|
|
907
|
-
"",
|
|
908
|
-
"/* @anchor:file:end */"
|
|
909
|
-
].filter(R8.isNotNil).join("\n");
|
|
910
|
-
}
|
|
911
|
-
return [
|
|
912
|
-
"/* @anchor:file:start */",
|
|
913
|
-
"",
|
|
914
|
-
$dependencies,
|
|
915
|
-
$comment || void 0,
|
|
916
|
-
`export type ${schemaDefinition.name} = ${JsonSchemaTransformer.toDeclaration(schemaDefinition.schema)}`,
|
|
917
|
-
"",
|
|
918
|
-
"/* @anchor:file:end */"
|
|
919
|
-
].filter(R8.isNotNil).join("\n");
|
|
920
|
-
}
|
|
921
|
-
};
|
|
922
|
-
|
|
923
|
-
// src/transformers/operation-definition/comment.renderer.ts
|
|
924
|
-
import * as R9 from "ramda";
|
|
925
|
-
var CommentRenderer2 = class {
|
|
926
|
-
constructor(operationDefinition) {
|
|
927
|
-
this.operationDefinition = operationDefinition;
|
|
928
|
-
}
|
|
929
|
-
render() {
|
|
930
|
-
const operation = this.operationDefinition.operation;
|
|
931
|
-
if (!operation.summary && !operation.description) return "";
|
|
932
|
-
const lines = ["/**"];
|
|
933
|
-
if (operation.summary && typeof operation.summary === "string") {
|
|
934
|
-
lines.push(` * ${R9.trim(operation.summary)}`);
|
|
935
|
-
lines.push(" *");
|
|
936
|
-
}
|
|
937
|
-
if (operation.description && typeof operation.description === "string") {
|
|
938
|
-
lines.push(` * @description ${R9.trim(operation.description)}`);
|
|
939
|
-
}
|
|
940
|
-
lines.push(" */");
|
|
941
|
-
return lines.join("\n");
|
|
942
|
-
}
|
|
943
|
-
};
|
|
944
|
-
|
|
945
|
-
// src/transformers/operation-definition/declaration.renderer.ts
|
|
946
|
-
import * as R14 from "ramda";
|
|
947
|
-
|
|
948
|
-
// src/transformers/operation-definition/utils/type-name-factory.ts
|
|
949
|
-
import * as changeCase from "change-case";
|
|
950
|
-
function typeNameFactory(operationDefinition) {
|
|
951
|
-
const pascalCaseOperationId = changeCase.pascalCase(operationDefinition.operationId);
|
|
952
|
-
return (name) => `${pascalCaseOperationId}${name}`;
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
// src/utils/openapi-utils/dereference.ts
|
|
956
|
-
function dereference($ref, swagger) {
|
|
957
|
-
let value;
|
|
958
|
-
for (const key of $ref.split("/")) {
|
|
959
|
-
if (key === "#") {
|
|
960
|
-
value = swagger;
|
|
961
|
-
} else {
|
|
962
|
-
value = value[key];
|
|
963
|
-
}
|
|
964
|
-
if (!value) break;
|
|
965
|
-
}
|
|
966
|
-
return value;
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
// src/utils/openapi-utils/dereference-operation.ts
|
|
970
|
-
import * as R12 from "ramda";
|
|
971
|
-
import jsonpointer2 from "jsonpointer";
|
|
972
|
-
import { JSONPath as JSONPath4 } from "jsonpath-plus";
|
|
973
|
-
|
|
974
|
-
// src/utils/openapi-utils/remove-undefined-ref.ts
|
|
975
|
-
import * as R11 from "ramda";
|
|
976
|
-
import jsonpointer from "jsonpointer";
|
|
977
|
-
import { JSONPath as JSONPath3 } from "jsonpath-plus";
|
|
978
|
-
|
|
979
|
-
// src/utils/openapi-utils/is-ref-defined.ts
|
|
980
|
-
import * as R10 from "ramda";
|
|
981
|
-
function isRefDefined($ref, swagger) {
|
|
982
|
-
if ($ref.startsWith("#/")) {
|
|
983
|
-
const path12 = $ref.replace("#/", "").split("/");
|
|
984
|
-
return R10.path(path12, swagger) !== void 0;
|
|
985
|
-
}
|
|
986
|
-
return false;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
// src/utils/openapi-utils/remove-undefined-ref.ts
|
|
990
|
-
function removeUndefinedRef(swagger) {
|
|
991
|
-
const shadow = R11.clone(swagger);
|
|
992
|
-
const matches = JSONPath3({
|
|
993
|
-
path: "$..*['$ref']^",
|
|
994
|
-
json: swagger,
|
|
995
|
-
resultType: "all"
|
|
996
|
-
});
|
|
997
|
-
for (const match of matches) {
|
|
998
|
-
if (match.value.$ref && !isRefDefined(match.value.$ref, swagger)) {
|
|
999
|
-
jsonpointer.set(shadow, match.pointer, R11.omit(["$ref"], match.value));
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
return shadow;
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
// src/utils/openapi-utils/dereference-operation.ts
|
|
1006
|
-
function dereferencePathObject(swagger) {
|
|
1007
|
-
const matches = JSONPath4({
|
|
1008
|
-
path: "$.paths.*.$ref^",
|
|
1009
|
-
json: swagger,
|
|
1010
|
-
resultType: "all"
|
|
1011
|
-
});
|
|
1012
|
-
for (const match of matches) {
|
|
1013
|
-
const value = dereference(match.value.$ref, swagger);
|
|
1014
|
-
jsonpointer2.set(swagger, match.pointer, value);
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
function dereferenceRequestBody(swagger) {
|
|
1018
|
-
const matches = JSONPath4({
|
|
1019
|
-
path: "$.paths.*.*.requestBody.$ref^",
|
|
1020
|
-
json: swagger,
|
|
1021
|
-
resultType: "all"
|
|
1022
|
-
});
|
|
1023
|
-
for (const match of matches) {
|
|
1024
|
-
const value = dereference(match.value.$ref, swagger);
|
|
1025
|
-
jsonpointer2.set(swagger, match.pointer, value);
|
|
1026
|
-
}
|
|
1027
|
-
}
|
|
1028
|
-
function dereferenceResponses(swagger) {
|
|
1029
|
-
const matches = [
|
|
1030
|
-
...JSONPath4({
|
|
1031
|
-
path: "$.paths.*.*.responses.*.$ref^",
|
|
1032
|
-
json: swagger,
|
|
1033
|
-
resultType: "all"
|
|
1034
|
-
}),
|
|
1035
|
-
...JSONPath4({
|
|
1036
|
-
path: "$.paths.*.*.responses.*.headers.*.$ref^",
|
|
1037
|
-
json: swagger,
|
|
1038
|
-
resultType: "all"
|
|
1039
|
-
})
|
|
1040
|
-
];
|
|
1041
|
-
for (const match of matches) {
|
|
1042
|
-
const value = dereference(match.value.$ref, swagger);
|
|
1043
|
-
jsonpointer2.set(swagger, match.pointer, value);
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
function dereferenceParameters(swagger) {
|
|
1047
|
-
const matches = JSONPath4({
|
|
1048
|
-
path: "$.paths.*.*.parameters.*.$ref^",
|
|
1049
|
-
json: swagger,
|
|
1050
|
-
resultType: "all"
|
|
1051
|
-
});
|
|
1052
|
-
for (const match of matches) {
|
|
1053
|
-
const value = dereference(match.value.$ref, swagger);
|
|
1054
|
-
jsonpointer2.set(swagger, match.pointer, value);
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
function dereferenceOperation(swagger) {
|
|
1058
|
-
const shadow = R12.clone(swagger);
|
|
1059
|
-
dereferencePathObject(shadow);
|
|
1060
|
-
dereferenceRequestBody(shadow);
|
|
1061
|
-
dereferenceResponses(shadow);
|
|
1062
|
-
dereferenceParameters(shadow);
|
|
1063
|
-
return removeUndefinedRef(shadow);
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
// src/utils/openapi-utils/dereference-deep.ts
|
|
1067
|
-
function dereferenceDeep($ref, swagger) {
|
|
1068
|
-
const stack = [$ref];
|
|
1069
|
-
let value;
|
|
1070
|
-
while (true) {
|
|
1071
|
-
const last2 = stack[stack.length - 1];
|
|
1072
|
-
value = dereference(last2, swagger);
|
|
1073
|
-
if (JsonSchemaUtils.isRef(value)) {
|
|
1074
|
-
if (!stack.includes(value.$ref)) {
|
|
1075
|
-
stack.push(value.$ref);
|
|
1076
|
-
continue;
|
|
1077
|
-
} else {
|
|
1078
|
-
throw new Error(`Circular reference detected: ${stack.join(" -> ")} -> ${value.$ref}`);
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
break;
|
|
1082
|
-
}
|
|
1083
|
-
return value;
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
// src/utils/openapi-utils/to-3_1.ts
|
|
1087
|
-
import { upgrade } from "@scalar/openapi-parser";
|
|
1088
|
-
function To3_1(openapi) {
|
|
1089
|
-
const { specification } = upgrade(openapi);
|
|
1090
|
-
return specification;
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
// src/utils/openapi-utils/map-operation.ts
|
|
1094
|
-
import * as R13 from "ramda";
|
|
1095
|
-
function mapOperation(specification, mapper) {
|
|
1096
|
-
const shadow = R13.clone(specification);
|
|
1097
|
-
for (const [pathname, pathItem] of Object.entries(shadow.paths || {})) {
|
|
1098
|
-
for (const m in pathItem) {
|
|
1099
|
-
const method = m.toLowerCase();
|
|
1100
|
-
if (!SupportedMethods.includes(method)) continue;
|
|
1101
|
-
if (typeof pathItem[m] !== "object" || Array.isArray(pathItem[m]) || pathItem[m] === null) continue;
|
|
1102
|
-
const operation = pathItem[m];
|
|
1103
|
-
pathItem[m] = mapper(method, pathname, operation);
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
return shadow;
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
// src/utils/openapi-utils/map-parameter.ts
|
|
1110
|
-
function mapParameter(specification, mapper) {
|
|
1111
|
-
return mapOperation(specification, (method, pathname, operation) => {
|
|
1112
|
-
if (!operation.parameters) return operation;
|
|
1113
|
-
operation.parameters = operation.parameters.map((parameter) => mapper(method, pathname, operation, parameter));
|
|
1114
|
-
return operation;
|
|
1115
|
-
});
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
// src/utils/openapi-utils/index.ts
|
|
1119
|
-
var OpenapiUtils = class {
|
|
1120
|
-
static isRefDefined = isRefDefined;
|
|
1121
|
-
static dereference = dereference;
|
|
1122
|
-
static dereferenceDeep = dereferenceDeep;
|
|
1123
|
-
static removeUndefinedRef = removeUndefinedRef;
|
|
1124
|
-
static dereferenceOperation = dereferenceOperation;
|
|
1125
|
-
static mapOperation = mapOperation;
|
|
1126
|
-
static mapParameter = mapParameter;
|
|
1127
|
-
// static updateOperationId = updateOperationId
|
|
1128
|
-
static to3_1 = To3_1;
|
|
1129
|
-
};
|
|
1130
|
-
|
|
1131
|
-
// src/transformers/operation-definition/declaration.renderer.ts
|
|
1132
|
-
var alias = (name) => `${name}Schema`;
|
|
1133
|
-
var DeclarationRenderer2 = class {
|
|
1134
|
-
constructor(operationDefinition, options) {
|
|
1135
|
-
this.operationDefinition = operationDefinition;
|
|
1136
|
-
this.options = options;
|
|
1137
|
-
this.typeName = typeNameFactory(operationDefinition);
|
|
1138
|
-
}
|
|
1139
|
-
typeName;
|
|
1140
|
-
renderResponseBodies(operation, options) {
|
|
1141
|
-
if (!operation.responses || R14.isEmpty(operation.responses)) {
|
|
1142
|
-
return `export interface ${this.typeName("ResponseBodies")} {}`;
|
|
1143
|
-
}
|
|
1144
|
-
const $responses = Object.entries(operation.responses).map(([statusCode, response]) => {
|
|
1145
|
-
if (!JsonSchemaUtils.isRef(response)) {
|
|
1146
|
-
const $value = Object.entries(response.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schema]) => {
|
|
1147
|
-
if (mediaType.includes("text/event-stream")) return "ReadableStream<ServerSentEvent>";
|
|
1148
|
-
if (mediaType.includes("multipart/form-data")) return "FormData";
|
|
1149
|
-
if (!schema) return "unknown";
|
|
1150
|
-
return JsonSchemaTransformer.toDeclaration(schema, options);
|
|
1151
|
-
}).join(" | ");
|
|
1152
|
-
return indent(2, `${statusCode}: ${$value || "void"}`);
|
|
1153
|
-
}
|
|
1154
|
-
}).join("\n");
|
|
1155
|
-
return [
|
|
1156
|
-
`export interface ${this.typeName("ResponseBodies")} {`,
|
|
1157
|
-
$responses,
|
|
1158
|
-
"}"
|
|
1159
|
-
].join("\n");
|
|
1160
|
-
}
|
|
1161
|
-
renderRequestBodies(operation, options) {
|
|
1162
|
-
let $requestBodies = `export interface ${this.typeName("RequestBodies")} {}`;
|
|
1163
|
-
if (operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody)) {
|
|
1164
|
-
const $mediaTypes = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schema]) => {
|
|
1165
|
-
if (!schema) return `${JSON.stringify(mediaType)}: unknown`;
|
|
1166
|
-
return `${JSON.stringify(mediaType)}: ${JsonSchemaTransformer.toDeclaration(schema, options)}`;
|
|
1167
|
-
}).map((pair) => indent(2, pair));
|
|
1168
|
-
$requestBodies = [
|
|
1169
|
-
`export interface ${this.typeName("RequestBodies")} {`,
|
|
1170
|
-
...$mediaTypes,
|
|
1171
|
-
"}"
|
|
1172
|
-
].join("\n");
|
|
1173
|
-
}
|
|
1174
|
-
return $requestBodies;
|
|
1175
|
-
}
|
|
1176
|
-
renderParameterBodies(operationDefinition, options) {
|
|
1177
|
-
const { operation } = operationDefinition;
|
|
1178
|
-
let parameterBodies = "";
|
|
1179
|
-
if (operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody)) {
|
|
1180
|
-
const $mediaTypes = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schemaOrRef]) => {
|
|
1181
|
-
if (!schemaOrRef) return `${JSON.stringify(mediaType)}: unknown`;
|
|
1182
|
-
const schema = JsonSchemaUtils.isRef(schemaOrRef) ? OpenapiUtils.dereferenceDeep(schemaOrRef.$ref, operationDefinition.document.specification) : schemaOrRef;
|
|
1183
|
-
if (schema.type === "object" || schema.properties) {
|
|
1184
|
-
return `${JSON.stringify(mediaType)}: ${JsonSchemaTransformer.toDeclaration(schemaOrRef, options)} & { [key: string]: any }`;
|
|
1185
|
-
}
|
|
1186
|
-
return `${JSON.stringify(mediaType)}: { [key: string]: any }`;
|
|
1187
|
-
}).map((pair) => indent(2, pair));
|
|
1188
|
-
parameterBodies = [
|
|
1189
|
-
`interface ${this.typeName("ParameterBodies")} {`,
|
|
1190
|
-
...$mediaTypes,
|
|
1191
|
-
"}",
|
|
1192
|
-
""
|
|
1193
|
-
].join("\n");
|
|
1194
|
-
}
|
|
1195
|
-
return parameterBodies;
|
|
1196
|
-
}
|
|
1197
|
-
renderRequestParameters(operation, options) {
|
|
1198
|
-
const mediaTypes = operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody) ? Object.keys(operation.requestBody.content || {}) : [];
|
|
1199
|
-
const base = `${this.typeName("RequestQuery")} & ${this.typeName("RouteParameters")} & ${this.typeName("RequestHeaders")}`;
|
|
1200
|
-
if (mediaTypes.length === 1) {
|
|
1201
|
-
return `export type ${this.typeName("RequestParameters")} = ${base} & ${this.typeName("RequestBodies")}[${JSON.stringify(mediaTypes[0])}]`;
|
|
1202
|
-
}
|
|
1203
|
-
if (mediaTypes.length > 1) {
|
|
1204
|
-
const unions = mediaTypes.map((mediaType) => `(${base} & ${this.typeName("RequestBodies")}[${JSON.stringify(mediaType)}] & { "content-type": ${JSON.stringify(mediaType)} })`).join("\n| ");
|
|
1205
|
-
return `export type ${this.typeName("RequestParameters")} = ${unions}`;
|
|
1206
|
-
}
|
|
1207
|
-
return `export type ${this.typeName("RequestParameters")} = ${base}`;
|
|
1208
|
-
}
|
|
1209
|
-
rendererParameters(name, parameters, options) {
|
|
1210
|
-
if (parameters.length === 0) {
|
|
1211
|
-
return `export type ${name} = {}`;
|
|
1212
|
-
}
|
|
1213
|
-
const $parameters = parameters.map((parameter) => {
|
|
1214
|
-
const parameterName = `"${parameter.name}"`;
|
|
1215
|
-
const $key = parameter.required ? parameterName : `${parameterName}?`;
|
|
1216
|
-
const $value = JsonSchemaTransformer.toDeclaration(parameter.schema || { type: "any" }, options);
|
|
1217
|
-
return indent(2, `${$key}: ${$value}`);
|
|
1218
|
-
}).join("\n");
|
|
1219
|
-
return [
|
|
1220
|
-
`export type ${name} = {`,
|
|
1221
|
-
$parameters,
|
|
1222
|
-
"}"
|
|
1223
|
-
].filter(Boolean).join("\n");
|
|
1224
|
-
}
|
|
1225
|
-
renderDependencies() {
|
|
1226
|
-
const schemaDefinitions = this.operationDefinition.getDependencies().filter((schemaDefinition) => !SchemaDefinition.isUnknown(schemaDefinition));
|
|
1227
|
-
const $schemaDefinitions = schemaDefinitions.map((schemaDefinition) => {
|
|
1228
|
-
const filepath = this.options.getDependentSchemaDefinitionFilepath(schemaDefinition);
|
|
1229
|
-
const schemaName = schemaDefinition.name;
|
|
1230
|
-
return `import type { ${schemaName} as ${alias(schemaName)} } from "${filepath}"`;
|
|
1231
|
-
}).map((str) => str.replace(/ from "(\.\.?\/.+?)(\.ts|\.mts|\.cts|\.js|\.cjs|\.mjs)?"/, this.options.esm ? ' from "$1.js"' : ' from "$1"'));
|
|
1232
|
-
return [
|
|
1233
|
-
'import type { KeqOperation, KeqPathParameterInit, KeqQueryInit, ServerSentEvent } from "keq"',
|
|
1234
|
-
...$schemaDefinitions
|
|
1235
|
-
].join("\n");
|
|
1236
|
-
}
|
|
1237
|
-
render() {
|
|
1238
|
-
const { operation } = this.operationDefinition;
|
|
1239
|
-
if (!operation.responses) return "";
|
|
1240
|
-
const jsonSchemaDeclarationRendererOptions = {
|
|
1241
|
-
referenceTransformer: (schema) => {
|
|
1242
|
-
return ReferenceTransformer.toDeclaration(schema, alias);
|
|
1243
|
-
}
|
|
1244
|
-
};
|
|
1245
|
-
const $dependencies = this.renderDependencies();
|
|
1246
|
-
const $responseBodies = this.renderResponseBodies(operation, jsonSchemaDeclarationRendererOptions);
|
|
1247
|
-
const $requestBodies = this.renderRequestBodies(operation, jsonSchemaDeclarationRendererOptions);
|
|
1248
|
-
const $parameterBodies = this.renderParameterBodies(this.operationDefinition, jsonSchemaDeclarationRendererOptions);
|
|
1249
|
-
const $requestParameters = this.renderRequestParameters(operation, jsonSchemaDeclarationRendererOptions);
|
|
1250
|
-
const $requestQuery = this.rendererParameters(
|
|
1251
|
-
`${this.typeName("RequestQuery")}`,
|
|
1252
|
-
operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p) && p.in === "query") || [],
|
|
1253
|
-
jsonSchemaDeclarationRendererOptions
|
|
1254
|
-
);
|
|
1255
|
-
const $routeParameters = this.rendererParameters(
|
|
1256
|
-
`${this.typeName("RouteParameters")}`,
|
|
1257
|
-
operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p) && p.in === "path") || [],
|
|
1258
|
-
jsonSchemaDeclarationRendererOptions
|
|
1259
|
-
);
|
|
1260
|
-
const $requestHeaders = this.rendererParameters(
|
|
1261
|
-
`${this.typeName("RequestHeaders")}`,
|
|
1262
|
-
operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p) && p.in === "header") || [],
|
|
1263
|
-
jsonSchemaDeclarationRendererOptions
|
|
1264
|
-
);
|
|
1265
|
-
return [
|
|
1266
|
-
"/* @anchor:file:start */",
|
|
1267
|
-
"",
|
|
1268
|
-
$dependencies,
|
|
1269
|
-
"",
|
|
1270
|
-
$responseBodies,
|
|
1271
|
-
"",
|
|
1272
|
-
$requestBodies,
|
|
1273
|
-
"",
|
|
1274
|
-
$requestQuery,
|
|
1275
|
-
"",
|
|
1276
|
-
$routeParameters,
|
|
1277
|
-
"",
|
|
1278
|
-
$requestHeaders,
|
|
1279
|
-
"",
|
|
1280
|
-
$parameterBodies || void 0,
|
|
1281
|
-
$requestParameters,
|
|
1282
|
-
"",
|
|
1283
|
-
`export interface ${this.typeName("Operation")}<STATUS extends keyof ${this.typeName("ResponseBodies")}, CONTENT_TYPE extends ${$parameterBodies ? `keyof ${this.typeName("ParameterBodies")}` : "string"} > extends KeqOperation {`,
|
|
1284
|
-
` requestParams: ${this.typeName("RouteParameters")} & { [key: string]: KeqPathParameterInit }`,
|
|
1285
|
-
` requestQuery: ${this.typeName("RequestQuery")} & { [key: string]: KeqQueryInit }`,
|
|
1286
|
-
` requestHeaders: ${this.typeName("RequestHeaders")} & { [key: string]: string | number }`,
|
|
1287
|
-
` requestBody: ${$parameterBodies ? `${this.typeName("ParameterBodies")}[CONTENT_TYPE] | ` : "object | "}BodyInit`,
|
|
1288
|
-
` responseBody: ${this.typeName("ResponseBodies")}[STATUS]`,
|
|
1289
|
-
"}",
|
|
1290
|
-
"",
|
|
1291
|
-
"/* @anchor:file:end */"
|
|
1292
|
-
].filter(R14.isNotNil).join("\n");
|
|
1293
|
-
}
|
|
1294
|
-
};
|
|
1295
|
-
|
|
1296
|
-
// src/transformers/operation-definition/micro-function.renderer.ts
|
|
1297
|
-
import * as R16 from "ramda";
|
|
1298
|
-
|
|
1299
|
-
// src/exception.ts
|
|
1300
|
-
import { CustomError } from "ts-custom-error";
|
|
1301
|
-
import wrap from "word-wrap";
|
|
1302
|
-
var Exception = class extends CustomError {
|
|
1303
|
-
constructor(module, message) {
|
|
1304
|
-
const moduleName = typeof module === "string" ? module : module.name;
|
|
1305
|
-
const msg = wrap(message, { width: 60, indent: " " });
|
|
1306
|
-
super(`${moduleName} module failed to compile:
|
|
1307
|
-
${msg}`);
|
|
1308
|
-
Object.defineProperty(this, "name", { value: "KeqCLI_Exception" });
|
|
1309
|
-
}
|
|
1310
|
-
};
|
|
1311
|
-
|
|
1312
|
-
// src/transformers/operation-definition/typescript-snippet.ts
|
|
1313
|
-
import * as R15 from "ramda";
|
|
1314
|
-
var OperationDefinitionSnippet = class {
|
|
1315
|
-
constructor(operationDefinition, options) {
|
|
1316
|
-
this.operationDefinition = operationDefinition;
|
|
1317
|
-
this.options = options;
|
|
1318
|
-
this.typeName = typeNameFactory(operationDefinition);
|
|
1319
|
-
}
|
|
1320
|
-
typeName;
|
|
1321
|
-
getQsParameters(parameter) {
|
|
1322
|
-
let arrayFormat;
|
|
1323
|
-
let allowDots;
|
|
1324
|
-
let indices;
|
|
1325
|
-
const style = parameter.style || "form";
|
|
1326
|
-
const explode = parameter.explode ?? true;
|
|
1327
|
-
if ("x-qs-array-format" in parameter) {
|
|
1328
|
-
arrayFormat = parameter["x-qs-array-format"];
|
|
1329
|
-
} else {
|
|
1330
|
-
if (style === "deepObject") {
|
|
1331
|
-
arrayFormat = "brackets" /* brackets */;
|
|
1332
|
-
} else if (explode) {
|
|
1333
|
-
arrayFormat = "repeat" /* repeat */;
|
|
1334
|
-
} else {
|
|
1335
|
-
if (style === "form") {
|
|
1336
|
-
arrayFormat = "comma" /* comma */;
|
|
1337
|
-
} else if (style === "spaceDelimited") {
|
|
1338
|
-
arrayFormat = "space" /* space */;
|
|
1339
|
-
} else if (style === "pipeDelimited") {
|
|
1340
|
-
arrayFormat = "pipe" /* pipe */;
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
if ("x-qs-allow-dots" in parameter) {
|
|
1345
|
-
allowDots = Boolean(parameter["x-qs-allow-dots"]);
|
|
1346
|
-
}
|
|
1347
|
-
if ("x-qs-indices" in parameter) {
|
|
1348
|
-
indices = Boolean(parameter["x-qs-indices"]);
|
|
1349
|
-
}
|
|
1350
|
-
if (arrayFormat || allowDots !== void 0 || indices !== void 0) {
|
|
1351
|
-
return {
|
|
1352
|
-
...arrayFormat ? { arrayFormat } : {},
|
|
1353
|
-
...allowDots !== void 0 ? { allowDots } : {},
|
|
1354
|
-
...indices !== void 0 ? { indices } : {}
|
|
1355
|
-
};
|
|
1356
|
-
}
|
|
1357
|
-
return void 0;
|
|
1358
|
-
}
|
|
1359
|
-
// render: "if (args && 'Authorization' in args) req.header('Authorization', args['Authorization'])"
|
|
1360
|
-
renderRequestHeaders() {
|
|
1361
|
-
const { operation } = this.operationDefinition;
|
|
1362
|
-
const $headers = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "header").map((p) => `if (args && ${JSON.stringify(p.name)} in args) req.header(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).join("\n");
|
|
1363
|
-
return $headers;
|
|
1364
|
-
}
|
|
1365
|
-
// render: "if (args && 'id' in args) req.query('id', args['id'], options)"
|
|
1366
|
-
renderRequestQuery() {
|
|
1367
|
-
const { operation } = this.operationDefinition;
|
|
1368
|
-
const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
|
|
1369
|
-
const option = this.getQsParameters(p);
|
|
1370
|
-
const $option = !option || R15.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
|
|
1371
|
-
return `if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
|
|
1372
|
-
}).join("\n");
|
|
1373
|
-
return $query;
|
|
1374
|
-
}
|
|
1375
|
-
// render: "if (args && 'id' in args) req.params('id', args['id'])"
|
|
1376
|
-
renderRequestPathParameters() {
|
|
1377
|
-
const { operation } = this.operationDefinition;
|
|
1378
|
-
const $pathParameters = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "path").map((p) => `if (args && ${JSON.stringify(p.name)} in args) req.params(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).join("\n");
|
|
1379
|
-
return $pathParameters;
|
|
1380
|
-
}
|
|
1381
|
-
getRequestMediaTypes() {
|
|
1382
|
-
const { operation } = this.operationDefinition;
|
|
1383
|
-
const requestBodyContent = operation.requestBody?.content || {};
|
|
1384
|
-
return Object.keys(requestBodyContent);
|
|
1385
|
-
}
|
|
1386
|
-
// render "req.type("application/json")"
|
|
1387
|
-
renderMediaType() {
|
|
1388
|
-
const mediaTypes = this.getRequestMediaTypes();
|
|
1389
|
-
if (mediaTypes.length === 1 && !mediaTypes[0].endsWith("/*")) {
|
|
1390
|
-
return `req.type("${mediaTypes[0]}")
|
|
1391
|
-
`;
|
|
1392
|
-
} else if (mediaTypes.some((mediaType) => mediaType === "*/*")) {
|
|
1393
|
-
} else if (mediaTypes.some((mediaType) => mediaType.endsWith("/*"))) {
|
|
1394
|
-
return 'if(args?.["content-type"]) req.type(args["content-type"])\n';
|
|
1395
|
-
} else if (mediaTypes.length > 1) {
|
|
1396
|
-
return 'if(args?.["content-type"]) req.type(args["content-type"])\n';
|
|
1397
|
-
}
|
|
1398
|
-
return "";
|
|
1399
|
-
}
|
|
1400
|
-
// render body
|
|
1401
|
-
requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType) {
|
|
1402
|
-
try {
|
|
1403
|
-
const $propertyName = JSON.stringify(propertyName);
|
|
1404
|
-
const schema = JsonSchemaUtils.isRef(propertySchema) ? OpenapiUtils.dereferenceDeep(propertySchema.$ref, this.operationDefinition.document.specification) : propertySchema;
|
|
1405
|
-
if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
|
|
1406
|
-
return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
|
|
1407
|
-
} else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
|
|
1408
|
-
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
|
|
1409
|
-
} else if (schema.type === "number" || schema.type === "integer") {
|
|
1410
|
-
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]))`;
|
|
1411
|
-
}
|
|
1412
|
-
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]) /* type is non-string in schema; triggers type coercion here */)`;
|
|
1413
|
-
} catch (err) {
|
|
1414
|
-
return this.renderErrorComment(err, mediaType);
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
requestBodyPropertyRenderer(propertyName, propertySchema, mediaType) {
|
|
1418
|
-
if (mediaType === "application/json") {
|
|
1419
|
-
const $propertyName = JSON.stringify(propertyName);
|
|
1420
|
-
return `if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
|
|
1421
|
-
} else if (mediaType === "multipart/form-data") {
|
|
1422
|
-
return this.requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType);
|
|
1423
|
-
} else {
|
|
1424
|
-
throw new Error(`Unsupported media type: ${mediaType}`);
|
|
1425
|
-
}
|
|
1426
|
-
}
|
|
1427
|
-
renderRequestBody() {
|
|
1428
|
-
const { operation } = this.operationDefinition;
|
|
1429
|
-
const requestBodyContent = operation.requestBody?.content || {};
|
|
1430
|
-
const $requestBody = Object.entries(requestBodyContent).map(([mediaType, mediaTypeObject]) => {
|
|
1431
|
-
if (!mediaTypeObject.schema) return;
|
|
1432
|
-
try {
|
|
1433
|
-
const schema = JsonSchemaUtils.isRef(mediaTypeObject.schema) ? OpenapiUtils.dereferenceDeep(mediaTypeObject.schema.$ref, this.operationDefinition.document.specification) : mediaTypeObject.schema;
|
|
1434
|
-
if (schema.type !== "object") return;
|
|
1435
|
-
const properties = schema.properties || {};
|
|
1436
|
-
return Object.entries(properties).map(([propertyName, propertySchema]) => this.requestBodyPropertyRenderer(propertyName, propertySchema, mediaType)).join("\n");
|
|
1437
|
-
} catch (err) {
|
|
1438
|
-
return this.renderErrorComment(err, mediaType);
|
|
1439
|
-
}
|
|
1440
|
-
}).filter(R15.isNotNil).join("\n");
|
|
1441
|
-
return $requestBody;
|
|
1442
|
-
}
|
|
1443
|
-
renderErrorComment(err, mediaType) {
|
|
1444
|
-
const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
|
|
1445
|
-
return [
|
|
1446
|
-
"/**",
|
|
1447
|
-
` * Unable to dereference schema for media type ${mediaType}`,
|
|
1448
|
-
$err,
|
|
1449
|
-
" */"
|
|
1450
|
-
].join("\n");
|
|
1451
|
-
}
|
|
1452
|
-
};
|
|
1453
|
-
|
|
1454
|
-
// src/transformers/operation-definition/micro-function.renderer.ts
|
|
1455
|
-
var OperationDefinitionMicroFunctionRenderer = class {
|
|
1456
|
-
constructor(operationDefinition, options) {
|
|
1457
|
-
this.operationDefinition = operationDefinition;
|
|
1458
|
-
this.options = options;
|
|
1459
|
-
this.typeName = typeNameFactory(operationDefinition);
|
|
1460
|
-
this.helper = new OperationDefinitionSnippet(operationDefinition, options);
|
|
1461
|
-
}
|
|
1462
|
-
helper;
|
|
1463
|
-
typeName;
|
|
1464
|
-
render() {
|
|
1465
|
-
const { operation, operationId, method, pathname } = this.operationDefinition;
|
|
1466
|
-
if (!operation.responses) return "";
|
|
1467
|
-
const $dependencies = this.renderDependencies();
|
|
1468
|
-
const $comment = new CommentRenderer2(this.operationDefinition).render();
|
|
1469
|
-
const moduleName = this.operationDefinition.module.name;
|
|
1470
|
-
const $method = method.toLowerCase();
|
|
1471
|
-
const $queryParameters = this.helper.renderRequestQuery();
|
|
1472
|
-
const $headerParameters = this.helper.renderRequestHeaders();
|
|
1473
|
-
const $pathParameters = this.helper.renderRequestPathParameters();
|
|
1474
|
-
const $mediaType = this.helper.renderMediaType();
|
|
1475
|
-
const $requestBody = this.helper.renderRequestBody();
|
|
1476
|
-
const $operationDeclaration = this.renderOperationDeclaration();
|
|
1477
|
-
return [
|
|
1478
|
-
"/* @anchor:file:start */",
|
|
1479
|
-
"",
|
|
1480
|
-
$dependencies,
|
|
1481
|
-
"",
|
|
1482
|
-
`const moduleName = "${moduleName}"`,
|
|
1483
|
-
`const method = "${method}"`,
|
|
1484
|
-
`const pathname = "${pathname}"`,
|
|
1485
|
-
"",
|
|
1486
|
-
$comment || void 0,
|
|
1487
|
-
`export ${$operationDeclaration} {`,
|
|
1488
|
-
` const req = request.${$method}<${this.typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
|
|
1489
|
-
" .option('module', { name: moduleName, pathname, method })",
|
|
1490
|
-
"",
|
|
1491
|
-
$mediaType ? indent(2, $mediaType) : void 0,
|
|
1492
|
-
" /* @anchor:query:start */",
|
|
1493
|
-
$queryParameters ? indent(2, $queryParameters) : void 0,
|
|
1494
|
-
" /* @anchor:query:end */",
|
|
1495
|
-
"",
|
|
1496
|
-
" /* @anchor:headers:start */",
|
|
1497
|
-
$headerParameters ? indent(2, $headerParameters) : void 0,
|
|
1498
|
-
" /* @anchor:headers:end */",
|
|
1499
|
-
"",
|
|
1500
|
-
" /* @anchor:path-parameters:start */",
|
|
1501
|
-
$pathParameters ? indent(2, $pathParameters) : void 0,
|
|
1502
|
-
" /* @anchor:path-parameters:end */",
|
|
1503
|
-
"",
|
|
1504
|
-
" /* @anchor:body:start */",
|
|
1505
|
-
$requestBody ? indent(2, $requestBody) : void 0,
|
|
1506
|
-
" /* @anchor:body:end */",
|
|
1507
|
-
"",
|
|
1508
|
-
" /* @anchor:operation-return */",
|
|
1509
|
-
` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
|
|
1510
|
-
"}",
|
|
1511
|
-
"",
|
|
1512
|
-
`${operationId}.pathname = pathname`,
|
|
1513
|
-
`${operationId}.method = method`,
|
|
1514
|
-
"/* @anchor:file:end */"
|
|
1515
|
-
].filter(R16.isNotNil).join("\n");
|
|
1516
|
-
}
|
|
1517
|
-
renderDependencies() {
|
|
1518
|
-
const declarationFilepath = this.options.getOperationDefinitionDeclarationFilepath(this.operationDefinition);
|
|
1519
|
-
const requestFilepath = this.options.getRequestFilepath();
|
|
1520
|
-
return [
|
|
1521
|
-
'import { Keq } from "keq"',
|
|
1522
|
-
`import { request } from "${requestFilepath}"`,
|
|
1523
|
-
`import type { ${this.typeName("Operation")}, ${this.typeName("ResponseBodies")}, ${this.typeName("RequestParameters")} } from "${declarationFilepath}"`,
|
|
1524
|
-
`export type { ${this.typeName("RequestQuery")}, ${this.typeName("RequestHeaders")}, ${this.typeName("RequestBodies")} } from "${declarationFilepath}"`
|
|
1525
|
-
].map((str) => str.replace(/ from "(\.\.?\/.+?)(\.ts|\.mts|\.cts|\.js|\.cjs|\.mjs)?"/, this.options.esm ? ' from "$1.js"' : ' from "$1"')).join("\n");
|
|
1526
|
-
}
|
|
1527
|
-
renderOperationDeclaration() {
|
|
1528
|
-
const { operationId } = this.operationDefinition;
|
|
1529
|
-
const mediaTypes = this.helper.getRequestMediaTypes();
|
|
1530
|
-
if (mediaTypes.length === 0) {
|
|
1531
|
-
return `function ${operationId}<STATUS extends keyof ${this.typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${this.typeName("RequestParameters")}): Keq<${this.typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1532
|
-
} else if (mediaTypes.length === 1) {
|
|
1533
|
-
return `function ${operationId}<STATUS extends keyof ${this.typeName("ResponseBodies")}, CONTENT_TYPE extends ${JSON.stringify(mediaTypes[0])} = ${JSON.stringify(mediaTypes[0])}>(args?: ${this.typeName("RequestParameters")}): Keq<${this.typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1534
|
-
} else if (mediaTypes.length > 1) {
|
|
1535
|
-
return `function ${operationId}<STATUS extends keyof ${this.typeName("ResponseBodies")}, CONTENT_TYPE extends ${this.typeName("RequestParameters")}["content-type"]>(args?: Extract<${this.typeName("RequestParameters")}, { "content-type": CONTENT_TYPE }>): Keq<${this.typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1536
|
-
}
|
|
1537
|
-
throw new Exception(this.operationDefinition.module, "[operationDeclarationRenderer] Unreachable");
|
|
1538
|
-
}
|
|
1539
|
-
};
|
|
1540
|
-
|
|
1541
|
-
// src/transformers/operation-definition/nestjs-method.renderer.ts
|
|
1542
|
-
import * as R17 from "ramda";
|
|
1543
|
-
var OperationDefinitionNestjsMethodRenderer = class {
|
|
1544
|
-
constructor(operationDefinition, options) {
|
|
1545
|
-
this.operationDefinition = operationDefinition;
|
|
1546
|
-
this.options = options;
|
|
1547
|
-
this.typeName = typeNameFactory(operationDefinition);
|
|
1548
|
-
this.helper = new OperationDefinitionSnippet(operationDefinition, options);
|
|
1549
|
-
}
|
|
1550
|
-
helper;
|
|
1551
|
-
typeName;
|
|
1552
|
-
render() {
|
|
1553
|
-
const pathname = this.operationDefinition.pathname;
|
|
1554
|
-
const $comment = new CommentRenderer2(this.operationDefinition).render();
|
|
1555
|
-
const $method = this.operationDefinition.method.toLowerCase();
|
|
1556
|
-
const $mediaType = this.helper.renderMediaType();
|
|
1557
|
-
const $operationDeclaration = this.renderOperationDeclaration(this.operationDefinition);
|
|
1558
|
-
const $queryParameters = this.helper.renderRequestQuery();
|
|
1559
|
-
const $headerParameters = this.helper.renderRequestHeaders();
|
|
1560
|
-
const $pathParameters = this.helper.renderRequestPathParameters();
|
|
1561
|
-
const $requestBody = this.helper.renderRequestBody();
|
|
1562
|
-
return [
|
|
1563
|
-
$comment,
|
|
1564
|
-
`${$operationDeclaration} {`,
|
|
1565
|
-
` const req = this.request.${$method}<${this.typeName("ResponseBodies")}[STATUS]>(${JSON.stringify(pathname)})`,
|
|
1566
|
-
" .option(",
|
|
1567
|
-
' "module",',
|
|
1568
|
-
" {",
|
|
1569
|
-
` name: ${JSON.stringify(this.operationDefinition.module.name)},`,
|
|
1570
|
-
` pathname: ${JSON.stringify(pathname)},`,
|
|
1571
|
-
` method: ${JSON.stringify($method)},`,
|
|
1572
|
-
" },",
|
|
1573
|
-
" )",
|
|
1574
|
-
"",
|
|
1575
|
-
$mediaType ? indent(2, $mediaType) : void 0,
|
|
1576
|
-
"",
|
|
1577
|
-
" /* @anchor:query:start */",
|
|
1578
|
-
$queryParameters ? indent(2, $queryParameters) : void 0,
|
|
1579
|
-
" /* @anchor:query:end */",
|
|
1580
|
-
"",
|
|
1581
|
-
" /* @anchor:headers:start */",
|
|
1582
|
-
$headerParameters ? indent(2, $headerParameters) : void 0,
|
|
1583
|
-
" /* @anchor:headers:end */",
|
|
1584
|
-
"",
|
|
1585
|
-
" /* @anchor:path-parameters:start */",
|
|
1586
|
-
$pathParameters ? indent(2, $pathParameters) : void 0,
|
|
1587
|
-
" /* @anchor:path-parameters:end */",
|
|
1588
|
-
"",
|
|
1589
|
-
" /* @anchor:body:start */",
|
|
1590
|
-
$requestBody ? indent(2, $requestBody) : void 0,
|
|
1591
|
-
" /* @anchor:body:end */",
|
|
1592
|
-
"",
|
|
1593
|
-
" return req",
|
|
1594
|
-
"}"
|
|
1595
|
-
].filter(R17.isNotNil).join("\n");
|
|
1596
|
-
}
|
|
1597
|
-
renderOperationDeclaration(operationDefinition) {
|
|
1598
|
-
const { operationId } = operationDefinition;
|
|
1599
|
-
const typeName = typeNameFactory(operationDefinition);
|
|
1600
|
-
const mediaTypes = this.helper.getRequestMediaTypes();
|
|
1601
|
-
if (mediaTypes.length === 0) {
|
|
1602
|
-
return `${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${typeName("RequestParameters")}): Keq<${typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1603
|
-
} else if (mediaTypes.length === 1) {
|
|
1604
|
-
return `${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends ${JSON.stringify(mediaTypes[0])} = ${JSON.stringify(mediaTypes[0])}>(args?: ${typeName("RequestParameters")}): Keq<${typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1605
|
-
} else if (mediaTypes.length > 1) {
|
|
1606
|
-
return `${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends ${typeName("RequestParameters")}["content-type"]>(args?: Extract<${typeName("RequestParameters")}, { "content-type": CONTENT_TYPE }>): Keq<${typeName("Operation")}<STATUS, CONTENT_TYPE>>`;
|
|
1607
|
-
}
|
|
1608
|
-
throw new Exception(operationDefinition.module, "[operationDeclarationRenderer] Unreachable");
|
|
1609
|
-
}
|
|
1610
|
-
};
|
|
1611
|
-
|
|
1612
|
-
// src/transformers/operation-definition/operation-definition.transformer.ts
|
|
1613
|
-
var OperationDefinitionTransformer = class {
|
|
1614
|
-
static toDeclaration(operationDefinition, options) {
|
|
1615
|
-
return new DeclarationRenderer2(operationDefinition, options).render();
|
|
1616
|
-
}
|
|
1617
|
-
static toMicroFunction(operationDefinition, options) {
|
|
1618
|
-
return new OperationDefinitionMicroFunctionRenderer(operationDefinition, options).render();
|
|
1619
|
-
}
|
|
1620
|
-
static toNestjsMethod(operationDefinition, options) {
|
|
1621
|
-
return new OperationDefinitionNestjsMethodRenderer(operationDefinition, options).render();
|
|
1622
|
-
}
|
|
1623
|
-
static toComment(operationDefinition) {
|
|
1624
|
-
return new CommentRenderer2(operationDefinition).render();
|
|
1625
|
-
}
|
|
1626
|
-
};
|
|
1627
|
-
|
|
1628
|
-
// src/transformers/entrypoint/entrypoint.transformer.ts
|
|
1629
|
-
import * as path5 from "path";
|
|
1630
|
-
var EntrypointTransformer = class {
|
|
1631
|
-
static toTypescript(exports, options) {
|
|
1632
|
-
const $exports = exports.map((exportArtifact) => {
|
|
1633
|
-
const relativePath = path5.relative(
|
|
1634
|
-
options.dirpath,
|
|
1635
|
-
exportArtifact.filepath
|
|
1636
|
-
).replace(/(\.ts|\.mts|\.cts|\.js|\.cjs|\.mjs)?$/, options.esm ? ".js" : "");
|
|
1637
|
-
return relativePath.startsWith(".") ? `export * from '${relativePath}'` : `export * from './${relativePath}'`;
|
|
1638
|
-
});
|
|
1639
|
-
return [
|
|
1640
|
-
"/* @anchor:file:start */",
|
|
1641
|
-
"",
|
|
1642
|
-
...$exports,
|
|
1643
|
-
"",
|
|
1644
|
-
"/* @anchor:file:end */"
|
|
1645
|
-
].join("\n");
|
|
1646
|
-
}
|
|
1647
|
-
};
|
|
1648
|
-
|
|
1649
|
-
// src/transformers/api-document/nestjs-client.renderer.ts
|
|
1650
|
-
import * as R18 from "ramda";
|
|
1651
|
-
import * as changeCase2 from "change-case";
|
|
1652
|
-
|
|
1653
|
-
// src/transformers/api-document/nestjs-module.renderer.ts
|
|
1654
|
-
import * as changeCase3 from "change-case";
|
|
1655
|
-
|
|
1656
|
-
// src/plugins/generate-declaration/generators/schema-declaration/schema.generator.ts
|
|
1657
|
-
import * as R19 from "ramda";
|
|
1658
|
-
import * as path6 from "path";
|
|
1659
|
-
import * as changeCase4 from "change-case";
|
|
1660
|
-
|
|
1661
|
-
// src/plugins/generate-declaration/constants/metadata-storage.ts
|
|
1662
|
-
var MetadataStorage4 = /* @__PURE__ */ new WeakMap();
|
|
1663
|
-
|
|
1664
|
-
// src/plugins/generate-declaration/generators/schema-declaration/schema.generator.ts
|
|
1665
|
-
var SCHEMA_GENERATOR = "schemaGenerator";
|
|
1666
|
-
var SchemaDeclarationGenerator = class _SchemaDeclarationGenerator {
|
|
1667
|
-
async compile(compiler, task) {
|
|
1668
|
-
const context = compiler.context;
|
|
1669
|
-
const metadata = MetadataStorage4.get(compiler);
|
|
1670
|
-
const rc = context.rc;
|
|
1671
|
-
const documents = context.documents;
|
|
1672
|
-
const schemaDefinitions = documents.flatMap((document) => document.schemas);
|
|
1673
|
-
const artifactMap = new Map(
|
|
1674
|
-
await Promise.all(
|
|
1675
|
-
schemaDefinitions.map(async (schemaDefinition) => [
|
|
1676
|
-
schemaDefinition,
|
|
1677
|
-
await metadata.hooks.afterSchemaDeclarationArtifactGenerated.promise(
|
|
1678
|
-
this.generateSchemaDefinitionsArtifact(schemaDefinition, rc),
|
|
1679
|
-
schemaDefinition,
|
|
1680
|
-
task
|
|
1681
|
-
)
|
|
1682
|
-
])
|
|
1683
|
-
)
|
|
1684
|
-
);
|
|
1685
|
-
const entrypoints = R19.collectBy(
|
|
1686
|
-
(schemaDefinition) => schemaDefinition.module.name,
|
|
1687
|
-
schemaDefinitions
|
|
1688
|
-
).map((schemaDefinitions2) => [
|
|
1689
|
-
schemaDefinitions2[0].module,
|
|
1690
|
-
schemaDefinitions2.map((schemaDefinition) => artifactMap.get(schemaDefinition)).filter((artifact) => Boolean(artifact))
|
|
1691
|
-
]).map(([moduleDefinition, artifacts]) => this.generateEntrypointArtifact(
|
|
1692
|
-
moduleDefinition,
|
|
1693
|
-
artifacts,
|
|
1694
|
-
rc
|
|
1695
|
-
));
|
|
1696
|
-
return [...artifactMap.values(), ...entrypoints];
|
|
1697
|
-
}
|
|
1698
|
-
generateSchemaDefinitionsArtifact(schemaDefinition, rc) {
|
|
1699
|
-
const filepath = _SchemaDeclarationGenerator.getSchemaDefinitionArtifactFilepath(schemaDefinition, rc.fileNamingStyle);
|
|
1700
|
-
const dirpath = path6.dirname(filepath);
|
|
1701
|
-
const artifact = new Artifact({
|
|
1702
|
-
id: _SchemaDeclarationGenerator.getSchemaDefinitionArtifactId(schemaDefinition),
|
|
1703
|
-
filepath,
|
|
1704
|
-
content: SchemaDefinitionTransformer.toDeclaration(
|
|
1705
|
-
schemaDefinition,
|
|
1706
|
-
{
|
|
1707
|
-
esm: rc.esm,
|
|
1708
|
-
getDependentSchemaDefinitionFilepath(dependentSchemaDefinition) {
|
|
1709
|
-
const relativePath = path6.relative(
|
|
1710
|
-
dirpath,
|
|
1711
|
-
_SchemaDeclarationGenerator.getSchemaDefinitionArtifactFilepath(dependentSchemaDefinition, rc.fileNamingStyle)
|
|
1712
|
-
);
|
|
1713
|
-
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
),
|
|
1717
|
-
extensionName: ".schema.ts"
|
|
1718
|
-
});
|
|
1719
|
-
return artifact;
|
|
1720
|
-
}
|
|
1721
|
-
generateEntrypointArtifact(moduleDefinition, exports, rc) {
|
|
1722
|
-
const filepath = _SchemaDeclarationGenerator.getEntrypointArtifactFilepath(moduleDefinition, rc.fileNamingStyle);
|
|
1723
|
-
const dirpath = path6.dirname(filepath);
|
|
1724
|
-
const artifact = new Artifact({
|
|
1725
|
-
id: _SchemaDeclarationGenerator.getEntrypointArtifactId(moduleDefinition),
|
|
1726
|
-
filepath,
|
|
1727
|
-
content: EntrypointTransformer.toTypescript(exports, { dirpath })
|
|
1728
|
-
});
|
|
1729
|
-
return artifact;
|
|
1730
|
-
}
|
|
1731
|
-
static getEntrypointArtifactFilepath(moduleDefinition, fileNamingStyle) {
|
|
1732
|
-
return [
|
|
1733
|
-
".",
|
|
1734
|
-
changeCase4[fileNamingStyle](moduleDefinition.name),
|
|
1735
|
-
"types",
|
|
1736
|
-
"components",
|
|
1737
|
-
"schemas",
|
|
1738
|
-
"index.ts"
|
|
1739
|
-
].join("/");
|
|
1740
|
-
}
|
|
1741
|
-
static getEntrypointArtifactId(moduleDefinition) {
|
|
1742
|
-
return `${moduleDefinition.address.url}/components/schemas/entrypoint?generator=${SCHEMA_GENERATOR}`;
|
|
1743
|
-
}
|
|
1744
|
-
static getSchemaDefinitionArtifactFilepath(schemaDefinition, fileNamingStyle) {
|
|
1745
|
-
const filename = `${changeCase4[fileNamingStyle](schemaDefinition.name)}.schema.ts`;
|
|
1746
|
-
return [
|
|
1747
|
-
".",
|
|
1748
|
-
changeCase4[fileNamingStyle](schemaDefinition.module.name),
|
|
1749
|
-
"types",
|
|
1750
|
-
"components",
|
|
1751
|
-
"schemas",
|
|
1752
|
-
filename
|
|
1753
|
-
].join("/");
|
|
1754
|
-
}
|
|
1755
|
-
static getSchemaDefinitionArtifactId(schemaDefinition) {
|
|
1756
|
-
return `${schemaDefinition.id}?generator=${SCHEMA_GENERATOR}`;
|
|
1757
|
-
}
|
|
1758
|
-
};
|
|
1759
|
-
|
|
1760
|
-
// src/plugins/generate-declaration/generators/operation-declaration/operation.generator.ts
|
|
1761
|
-
var OPERATION_GENERATOR = "operationDeclarationGenerator";
|
|
1762
|
-
var OperationDeclarationGenerator = class _OperationDeclarationGenerator {
|
|
1763
|
-
async compile(compiler, task) {
|
|
1764
|
-
const context = compiler.context;
|
|
1765
|
-
const metadata = MetadataStorage4.get(compiler);
|
|
1766
|
-
const rc = context.rc;
|
|
1767
|
-
const documents = context.documents;
|
|
1768
|
-
const operationDefinitions = documents.flatMap((document) => document.operations);
|
|
1769
|
-
const artifactMap = new Map(
|
|
1770
|
-
await Promise.all(
|
|
1771
|
-
operationDefinitions.map(async (operationDefinition) => [
|
|
1772
|
-
operationDefinition,
|
|
1773
|
-
await metadata.hooks.afterOperationDeclarationArtifactGenerated.promise(
|
|
1774
|
-
this.generateOperationDefinitionArtifact(operationDefinition, rc),
|
|
1775
|
-
operationDefinition,
|
|
1776
|
-
task
|
|
1777
|
-
)
|
|
1778
|
-
])
|
|
1779
|
-
)
|
|
1780
|
-
);
|
|
1781
|
-
return Array.from(artifactMap.values());
|
|
1782
|
-
}
|
|
1783
|
-
generateOperationDefinitionArtifact(operationDefinition, rc) {
|
|
1784
|
-
const filepath = _OperationDeclarationGenerator.getOperationDefinitionArtifactFilepath(operationDefinition, rc.fileNamingStyle);
|
|
1785
|
-
const dirpath = path7.dirname(filepath);
|
|
1786
|
-
const artifact = new Artifact({
|
|
1787
|
-
id: _OperationDeclarationGenerator.getOperationDefinitionArtifactId(operationDefinition),
|
|
1788
|
-
filepath,
|
|
1789
|
-
content: OperationDefinitionTransformer.toDeclaration(
|
|
1790
|
-
operationDefinition,
|
|
1791
|
-
{
|
|
1792
|
-
esm: rc.esm,
|
|
1793
|
-
getDependentSchemaDefinitionFilepath(dependentSchemaDefinition) {
|
|
1794
|
-
const relativePath = path7.relative(
|
|
1795
|
-
dirpath,
|
|
1796
|
-
SchemaDeclarationGenerator.getSchemaDefinitionArtifactFilepath(dependentSchemaDefinition, rc.fileNamingStyle)
|
|
1797
|
-
);
|
|
1798
|
-
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
),
|
|
1802
|
-
extensionName: ".type.ts"
|
|
1803
|
-
});
|
|
1804
|
-
return artifact;
|
|
1805
|
-
}
|
|
1806
|
-
generateEntrypointArtifact(moduleDefinition, exports, rc) {
|
|
1807
|
-
const filepath = _OperationDeclarationGenerator.getEntrypointArtifactFilepath(moduleDefinition, rc.fileNamingStyle);
|
|
1808
|
-
const dirpath = path7.dirname(filepath);
|
|
1809
|
-
const artifact = new Artifact({
|
|
1810
|
-
id: _OperationDeclarationGenerator.getEntrypointArtifactId(moduleDefinition),
|
|
1811
|
-
filepath,
|
|
1812
|
-
content: EntrypointTransformer.toTypescript(exports, { dirpath })
|
|
1813
|
-
});
|
|
1814
|
-
return artifact;
|
|
1815
|
-
}
|
|
1816
|
-
static getOperationDefinitionArtifactFilepath(operationDefinition, fileNamingStyle) {
|
|
1817
|
-
const filename = `${changeCase5[fileNamingStyle](operationDefinition.operationId)}.type.ts`;
|
|
1818
|
-
const filepath = [
|
|
1819
|
-
".",
|
|
1820
|
-
changeCase5[fileNamingStyle](operationDefinition.module.name),
|
|
1821
|
-
"types",
|
|
1822
|
-
"operations",
|
|
1823
|
-
filename
|
|
1824
|
-
].join("/");
|
|
1825
|
-
return filepath;
|
|
1826
|
-
}
|
|
1827
|
-
static getOperationDefinitionArtifactId(operationDefinition) {
|
|
1828
|
-
return `${operationDefinition.id}?generator=${OPERATION_GENERATOR}`;
|
|
1829
|
-
}
|
|
1830
|
-
static getEntrypointArtifactFilepath(moduleDefinition, fileNamingStyle) {
|
|
1831
|
-
return [
|
|
1832
|
-
".",
|
|
1833
|
-
changeCase5[fileNamingStyle](moduleDefinition.name),
|
|
1834
|
-
"types",
|
|
1835
|
-
"operations",
|
|
1836
|
-
"index.ts"
|
|
1837
|
-
].join("/");
|
|
1838
|
-
}
|
|
1839
|
-
static getEntrypointArtifactId(moduleDefinition) {
|
|
1840
|
-
return `${moduleDefinition.address.url}/paths/entrypoint?generator=${OPERATION_GENERATOR}`;
|
|
1841
|
-
}
|
|
1842
|
-
};
|
|
1843
|
-
|
|
1844
|
-
// src/plugins/generate-declaration/generate-declaration.plugin.ts
|
|
1845
|
-
var DECLARATION_GENERATOR = "declarationGenerator";
|
|
1846
|
-
var GenerateDeclarationPlugin = class _GenerateDeclarationPlugin {
|
|
1847
|
-
name = DECLARATION_GENERATOR;
|
|
1848
|
-
operationGenerator = new OperationDeclarationGenerator();
|
|
1849
|
-
schemaGenerator = new SchemaDeclarationGenerator();
|
|
1850
|
-
constructor() {
|
|
1851
|
-
}
|
|
1852
|
-
apply(compiler) {
|
|
1853
|
-
if (MetadataStorage4.has(compiler)) return;
|
|
1854
|
-
_GenerateDeclarationPlugin.register(compiler);
|
|
1855
|
-
compiler.hooks.compile.tapPromise(_GenerateDeclarationPlugin.name, async (task) => {
|
|
1856
|
-
compiler.context.artifacts?.push(
|
|
1857
|
-
...await this.schemaGenerator.compile(compiler, task),
|
|
1858
|
-
...await this.operationGenerator.compile(compiler, task)
|
|
1859
|
-
);
|
|
1860
|
-
});
|
|
1861
|
-
}
|
|
1862
|
-
static register(compiler) {
|
|
1863
|
-
if (!MetadataStorage4.has(compiler)) {
|
|
1864
|
-
MetadataStorage4.set(compiler, {
|
|
1865
|
-
hooks: {
|
|
1866
|
-
afterEntrypointArtifactGenerated: new AsyncSeriesWaterfallHook(["artifact", "task"]),
|
|
1867
|
-
afterSchemaDeclarationArtifactGenerated: new AsyncSeriesWaterfallHook(["artifact", "schemaDefinition", "task"]),
|
|
1868
|
-
afterOperationDeclarationArtifactGenerated: new AsyncSeriesWaterfallHook(["artifact", "operationDefinition", "task"])
|
|
1869
|
-
}
|
|
1870
|
-
});
|
|
1871
|
-
}
|
|
1872
|
-
return MetadataStorage4.get(compiler);
|
|
1873
|
-
}
|
|
1874
|
-
static of(compiler) {
|
|
1875
|
-
return this.register(compiler);
|
|
1876
|
-
}
|
|
1877
|
-
};
|
|
1878
|
-
|
|
1879
|
-
// src/plugins/generate-micro-function/generators/request/request.generator.ts
|
|
1880
|
-
var MICRO_FUNCTION_REQUEST_GENERATOR = "microFunctionRequestGenerator";
|
|
1881
|
-
var RequestGenerator = class _RequestGenerator {
|
|
1882
|
-
generateRequestArtifact() {
|
|
1883
|
-
const content = [
|
|
1884
|
-
"/* @anchor:file:start */",
|
|
1885
|
-
"",
|
|
1886
|
-
'import { KeqRequest } from "keq"',
|
|
1887
|
-
"",
|
|
1888
|
-
"/* @anchor:request-declaration */",
|
|
1889
|
-
"export const request = new KeqRequest()",
|
|
1890
|
-
"",
|
|
1891
|
-
"/* @anchor:file:end */"
|
|
1892
|
-
].join("\n");
|
|
1893
|
-
return new Artifact({
|
|
1894
|
-
id: _RequestGenerator.getRequestArtifactId(),
|
|
1895
|
-
filepath: _RequestGenerator.getRequestArtifactFilepath(),
|
|
1896
|
-
content
|
|
1897
|
-
});
|
|
1898
|
-
}
|
|
1899
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
1900
|
-
async compile(compiler, task) {
|
|
1901
|
-
return [
|
|
1902
|
-
this.generateRequestArtifact()
|
|
1903
|
-
];
|
|
1904
|
-
}
|
|
1905
|
-
static getRequestArtifactFilepath() {
|
|
1906
|
-
return "./request.ts";
|
|
1907
|
-
}
|
|
1908
|
-
static getRequestArtifactId() {
|
|
1909
|
-
return `request?generator=${MICRO_FUNCTION_REQUEST_GENERATOR}`;
|
|
1910
|
-
}
|
|
1911
|
-
};
|
|
1912
|
-
|
|
1913
|
-
// src/plugins/generate-micro-function/generators/micro_function/micro-function.generator.ts
|
|
1914
|
-
var MICRO_FUNCTION_GENERATOR = "microFunctionGenerator";
|
|
1915
|
-
var MicroFunctionGenerator = class _MicroFunctionGenerator {
|
|
1916
|
-
async compile(compiler, task) {
|
|
1917
|
-
const metadata = MetadataStorage3.get(compiler);
|
|
1918
|
-
const context = compiler.context;
|
|
1919
|
-
const rc = context.rc;
|
|
1920
|
-
const documents = context.documents;
|
|
1921
|
-
const operationDefinitions = documents.flatMap((document) => document.operations);
|
|
1922
|
-
const artifactMap = new Map(
|
|
1923
|
-
await Promise.all(
|
|
1924
|
-
operationDefinitions.map(async (operationDefinition) => [
|
|
1925
|
-
operationDefinition,
|
|
1926
|
-
await metadata.hooks.afterMicroFunctionArtifactGenerated.promise(
|
|
1927
|
-
this.generateOperationDefinitionArtifact(operationDefinition, rc),
|
|
1928
|
-
operationDefinition,
|
|
1929
|
-
task
|
|
1930
|
-
)
|
|
1931
|
-
])
|
|
1932
|
-
)
|
|
1933
|
-
);
|
|
1934
|
-
const entrypoints = await Promise.all(
|
|
1935
|
-
R20.collectBy(
|
|
1936
|
-
(operationDefinition) => operationDefinition.module.name,
|
|
1937
|
-
operationDefinitions
|
|
1938
|
-
).map((operationDefinitions2) => [
|
|
1939
|
-
operationDefinitions2[0].module,
|
|
1940
|
-
operationDefinitions2.map((operationDefinition) => artifactMap.get(operationDefinition)).filter((artifact) => Boolean(artifact))
|
|
1941
|
-
]).map(async ([moduleDefinition, artifacts]) => await metadata.hooks.afterEntrypointArtifactGenerated.promise(
|
|
1942
|
-
this.generateEntrypointArtifact(
|
|
1943
|
-
moduleDefinition,
|
|
1944
|
-
artifacts,
|
|
1945
|
-
rc
|
|
1946
|
-
),
|
|
1947
|
-
task
|
|
1948
|
-
))
|
|
1949
|
-
);
|
|
1950
|
-
return [...entrypoints, ...artifactMap.values()];
|
|
1951
|
-
}
|
|
1952
|
-
generateOperationDefinitionArtifact(operationDefinition, rc) {
|
|
1953
|
-
const filepath = _MicroFunctionGenerator.getOperationDefinitionArtifactFilepath(operationDefinition, rc.fileNamingStyle);
|
|
1954
|
-
const dirpath = path8.dirname(filepath);
|
|
1955
|
-
const artifact = new Artifact({
|
|
1956
|
-
id: _MicroFunctionGenerator.getOperationDefinitionArtifactId(operationDefinition),
|
|
1957
|
-
filepath,
|
|
1958
|
-
content: OperationDefinitionTransformer.toMicroFunction(operationDefinition, {
|
|
1959
|
-
esm: rc.esm,
|
|
1960
|
-
getOperationDefinitionDeclarationFilepath(operationDefinition2) {
|
|
1961
|
-
const relativePath = path8.relative(
|
|
1962
|
-
dirpath,
|
|
1963
|
-
OperationDeclarationGenerator.getOperationDefinitionArtifactFilepath(operationDefinition2, rc.fileNamingStyle)
|
|
1964
|
-
);
|
|
1965
|
-
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
1966
|
-
},
|
|
1967
|
-
getRequestFilepath() {
|
|
1968
|
-
const relativePath = path8.relative(
|
|
1969
|
-
dirpath,
|
|
1970
|
-
RequestGenerator.getRequestArtifactFilepath()
|
|
1971
|
-
);
|
|
1972
|
-
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
1973
|
-
}
|
|
1974
|
-
}),
|
|
1975
|
-
extensionName: ".type.ts"
|
|
1976
|
-
});
|
|
1977
|
-
return artifact;
|
|
1978
|
-
}
|
|
1979
|
-
generateEntrypointArtifact(moduleDefinition, exports, rc) {
|
|
1980
|
-
const filepath = _MicroFunctionGenerator.getEntrypointArtifactFilepath(moduleDefinition, rc.fileNamingStyle);
|
|
1981
|
-
const dirpath = filepath.substring(0, filepath.lastIndexOf("/"));
|
|
1982
|
-
const artifact = new Artifact({
|
|
1983
|
-
id: _MicroFunctionGenerator.getEntrypointArtifactId(moduleDefinition),
|
|
1984
|
-
filepath,
|
|
1985
|
-
content: EntrypointTransformer.toTypescript(exports, { dirpath })
|
|
1986
|
-
});
|
|
1987
|
-
return artifact;
|
|
1988
|
-
}
|
|
1989
|
-
static getOperationDefinitionArtifactFilepath(operationDefinition, fileNamingStyle) {
|
|
1990
|
-
const filename = `${changeCase6[fileNamingStyle](operationDefinition.operationId)}.fn.ts`;
|
|
1991
|
-
const filepath = [
|
|
1992
|
-
".",
|
|
1993
|
-
changeCase6[fileNamingStyle](operationDefinition.module.name),
|
|
1994
|
-
"operations",
|
|
1995
|
-
filename
|
|
1996
|
-
].join("/");
|
|
1997
|
-
return filepath;
|
|
1998
|
-
}
|
|
1999
|
-
static getOperationDefinitionArtifactId(operationDefinition) {
|
|
2000
|
-
return `${operationDefinition.id}?generator=${MICRO_FUNCTION_GENERATOR}`;
|
|
2001
|
-
}
|
|
2002
|
-
static getEntrypointArtifactFilepath(moduleDefinition, fileNamingStyle) {
|
|
2003
|
-
return [
|
|
2004
|
-
".",
|
|
2005
|
-
changeCase6[fileNamingStyle](moduleDefinition.name),
|
|
2006
|
-
"operations",
|
|
2007
|
-
"index.ts"
|
|
2008
|
-
].join("/");
|
|
2009
|
-
}
|
|
2010
|
-
static getEntrypointArtifactId(moduleDefinition) {
|
|
2011
|
-
return `${moduleDefinition.address}/paths/entrypoint?generator=${MICRO_FUNCTION_GENERATOR}`;
|
|
2012
|
-
}
|
|
2013
|
-
};
|
|
2014
|
-
|
|
2015
|
-
// src/plugins/generate-micro-function/generate-micro-function.plugin.ts
|
|
2016
|
-
var GenerateMicroFunctionPlugin = class _GenerateMicroFunctionPlugin {
|
|
2017
|
-
microFunctionGenerator = new MicroFunctionGenerator();
|
|
2018
|
-
requestGenerator = new RequestGenerator();
|
|
2019
|
-
apply(compiler) {
|
|
2020
|
-
if (MetadataStorage3.has(compiler)) return;
|
|
2021
|
-
_GenerateMicroFunctionPlugin.register(compiler);
|
|
2022
|
-
compiler.hooks.compile.tapPromise(_GenerateMicroFunctionPlugin.name, async (task) => {
|
|
2023
|
-
const artifacts = [
|
|
2024
|
-
...await this.requestGenerator.compile(compiler, task),
|
|
2025
|
-
...await this.microFunctionGenerator.compile(compiler, task)
|
|
2026
|
-
];
|
|
2027
|
-
compiler.context.artifacts.push(...artifacts);
|
|
2028
|
-
});
|
|
2029
|
-
}
|
|
2030
|
-
static register(compiler) {
|
|
2031
|
-
if (!MetadataStorage3.has(compiler)) {
|
|
2032
|
-
MetadataStorage3.set(compiler, {
|
|
2033
|
-
hooks: {
|
|
2034
|
-
afterEntrypointArtifactGenerated: new AsyncSeriesWaterfallHook2(["artifact", "task"]),
|
|
2035
|
-
afterMicroFunctionArtifactGenerated: new AsyncSeriesWaterfallHook2(["artifact", "operationDefinition", "task"])
|
|
2036
|
-
}
|
|
2037
|
-
});
|
|
2038
|
-
}
|
|
2039
|
-
return MetadataStorage3.get(compiler);
|
|
2040
|
-
}
|
|
2041
|
-
static of(compiler) {
|
|
2042
|
-
return this.register(compiler);
|
|
2043
|
-
}
|
|
2044
|
-
};
|
|
2045
|
-
|
|
2046
|
-
// src/plugins/generate-nestjs-module/generate-nestjs-module.ts
|
|
2047
|
-
import { AsyncSeriesWaterfallHook as AsyncSeriesWaterfallHook3 } from "tapable";
|
|
2048
|
-
|
|
2049
|
-
// src/plugins/generate-nestjs-module/generators/nestjs-module.generator.ts
|
|
2050
|
-
import * as changeCase7 from "change-case";
|
|
2051
|
-
|
|
2052
|
-
// src/plugins/chinese-to-pinyin/chinese-to-pinyin.plugin.ts
|
|
2053
|
-
import { fixSwagger } from "swagger-fix";
|
|
2054
|
-
|
|
2055
|
-
// src/plugins/download-http-file/constants/metadata-storage.ts
|
|
2056
|
-
var MetadataStorage8 = /* @__PURE__ */ new WeakMap();
|
|
2057
|
-
|
|
2058
|
-
// src/plugins/download-http-file/download-http-file.plugin.ts
|
|
2059
|
-
var DownloadHttpFilePlugin = class _DownloadHttpFilePlugin {
|
|
2060
|
-
apply(compiler) {
|
|
2061
|
-
if (MetadataStorage8.has(compiler)) return;
|
|
2062
|
-
_DownloadHttpFilePlugin.register(compiler);
|
|
2063
|
-
compiler.hooks.download.tapPromise(_DownloadHttpFilePlugin.name, async (address, task) => {
|
|
2064
|
-
const { url } = address;
|
|
2065
|
-
if (!url.startsWith("http://") && !url.startsWith("https://")) return void 0;
|
|
2066
|
-
const content = await this.download(address);
|
|
2067
|
-
const spec = this.deserialize(content);
|
|
2068
|
-
return JSON.stringify(spec);
|
|
2069
|
-
});
|
|
2070
|
-
}
|
|
2071
|
-
async download(address) {
|
|
2072
|
-
const { url, headers } = address;
|
|
2073
|
-
try {
|
|
2074
|
-
const res = await fetch(url, { headers });
|
|
2075
|
-
if (res.status >= 400) throw new Error(`failed with status code ${res.status}`);
|
|
2076
|
-
return await res.text();
|
|
2077
|
-
} catch (e) {
|
|
2078
|
-
if (e instanceof Error) {
|
|
2079
|
-
e.message = `Unable get the openapi/swagger file from ${url}: ${e.message}`;
|
|
2080
|
-
}
|
|
2081
|
-
throw e;
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
deserialize(content) {
|
|
2085
|
-
const json = JSON.parse(content);
|
|
2086
|
-
const spec = OpenapiUtils.to3_1(json);
|
|
2087
|
-
return spec;
|
|
2088
|
-
}
|
|
2089
|
-
static register(compiler) {
|
|
2090
|
-
if (!MetadataStorage8.has(compiler)) {
|
|
2091
|
-
MetadataStorage8.set(compiler, {
|
|
2092
|
-
hooks: {}
|
|
2093
|
-
});
|
|
2094
|
-
}
|
|
2095
|
-
return MetadataStorage8.get(compiler);
|
|
2096
|
-
}
|
|
2097
|
-
static of(compiler) {
|
|
2098
|
-
return this.register(compiler);
|
|
2099
|
-
}
|
|
2100
|
-
};
|
|
2101
|
-
|
|
2102
|
-
// src/plugins/download-local-file/download-local-file.plugin.ts
|
|
2103
|
-
import * as path9 from "path";
|
|
2104
|
-
import * as fs3 from "fs/promises";
|
|
2105
|
-
import * as yaml from "js-yaml";
|
|
2106
|
-
import { fileURLToPath } from "url";
|
|
2107
|
-
|
|
2108
|
-
// src/plugins/download-local-file/constants/metadata-storage.ts
|
|
2109
|
-
var MetadataStorage9 = /* @__PURE__ */ new WeakMap();
|
|
2110
|
-
|
|
2111
|
-
// src/plugins/download-local-file/download-local-file.plugin.ts
|
|
2112
|
-
var DownloadLocalFilePlugin = class _DownloadLocalFilePlugin {
|
|
2113
|
-
apply(compiler) {
|
|
2114
|
-
if (MetadataStorage9.has(compiler)) return;
|
|
2115
|
-
_DownloadLocalFilePlugin.register(compiler);
|
|
2116
|
-
compiler.hooks.download.tapPromise(_DownloadLocalFilePlugin.name, async (address, task) => {
|
|
2117
|
-
const { url, encoding } = address;
|
|
2118
|
-
if (!url.startsWith("file://")) return void 0;
|
|
2119
|
-
const filepath = fileURLToPath(url);
|
|
2120
|
-
const fileExt = path9.extname(filepath);
|
|
2121
|
-
const content = await fs3.readFile(filepath, encoding);
|
|
2122
|
-
const str = typeof content === "string" ? content : content.toString(encoding);
|
|
2123
|
-
if ([".yml", ".yaml"].includes(fileExt)) {
|
|
2124
|
-
const value = yaml.load(str);
|
|
2125
|
-
return JSON.stringify(OpenapiUtils.to3_1(value));
|
|
2126
|
-
} else if (fileExt === ".json") {
|
|
2127
|
-
return JSON.stringify(OpenapiUtils.to3_1(JSON.parse(str)));
|
|
2128
|
-
}
|
|
2129
|
-
});
|
|
2130
|
-
}
|
|
2131
|
-
static register(compiler) {
|
|
2132
|
-
if (!MetadataStorage9.has(compiler)) {
|
|
2133
|
-
MetadataStorage9.set(compiler, {
|
|
2134
|
-
hooks: {}
|
|
2135
|
-
});
|
|
2136
|
-
}
|
|
2137
|
-
return MetadataStorage9.get(compiler);
|
|
2138
|
-
}
|
|
2139
|
-
static of(compiler) {
|
|
2140
|
-
return this.register(compiler);
|
|
2141
|
-
}
|
|
2142
|
-
};
|
|
2143
|
-
|
|
2144
|
-
// src/plugins/shaking/shaking.plugin.ts
|
|
2145
|
-
import { openapiShakingSync } from "@opendoc/openapi-shaking";
|
|
2146
|
-
|
|
2147
|
-
// src/plugins/shaking/constants/metadata-storage.ts
|
|
2148
|
-
var MetadataStorage10 = /* @__PURE__ */ new WeakMap();
|
|
2149
|
-
|
|
2150
|
-
// src/plugins/shaking/shaking.plugin.ts
|
|
2151
|
-
var ShakingPlugin = class _ShakingPlugin {
|
|
2152
|
-
apply(compiler) {
|
|
2153
|
-
if (MetadataStorage10.has(compiler)) return;
|
|
2154
|
-
_ShakingPlugin.register(compiler);
|
|
2155
|
-
compiler.hooks.beforeCompile.tap(_ShakingPlugin.name, (task) => {
|
|
2156
|
-
const matcher = compiler.context.matcher;
|
|
2157
|
-
const documents = compiler.context.documents;
|
|
2158
|
-
compiler.context.documents = documents.map((document) => this.shaking(compiler, document)).filter((document) => !document.isEmpty()).filter((document) => !matcher.isModuleIgnored(document.module));
|
|
2159
|
-
});
|
|
2160
|
-
}
|
|
2161
|
-
shaking(compiler, document) {
|
|
2162
|
-
const rc = compiler.context.rc;
|
|
2163
|
-
const matcher = compiler.context.matcher;
|
|
2164
|
-
const isAccepted = (pathname, method, operation) => {
|
|
2165
|
-
if (!SupportedMethods.includes(method)) return false;
|
|
2166
|
-
return !matcher.isOperationIgnored(new OperationDefinition({
|
|
2167
|
-
method,
|
|
2168
|
-
pathname,
|
|
2169
|
-
operation,
|
|
2170
|
-
module: document.module,
|
|
2171
|
-
document
|
|
2172
|
-
}));
|
|
2173
|
-
};
|
|
2174
|
-
const sharkedSwagger = openapiShakingSync(
|
|
2175
|
-
document.specification,
|
|
2176
|
-
isAccepted,
|
|
2177
|
-
{ tolerant: rc.tolerant }
|
|
2178
|
-
);
|
|
2179
|
-
return new ApiDocumentV3_1(
|
|
2180
|
-
sharkedSwagger,
|
|
2181
|
-
new ModuleDefinition(
|
|
2182
|
-
document.module.name,
|
|
2183
|
-
{
|
|
2184
|
-
url: `memory://${document.module.name}.v3_1.sharked.json`,
|
|
2185
|
-
headers: {},
|
|
2186
|
-
encoding: "utf8"
|
|
2187
|
-
}
|
|
2188
|
-
)
|
|
2189
|
-
);
|
|
2190
|
-
}
|
|
2191
|
-
static register(compiler) {
|
|
2192
|
-
if (!MetadataStorage10.has(compiler)) {
|
|
2193
|
-
MetadataStorage10.set(compiler, {
|
|
2194
|
-
hooks: {}
|
|
2195
|
-
});
|
|
2196
|
-
}
|
|
2197
|
-
return MetadataStorage10.get(compiler);
|
|
2198
|
-
}
|
|
2199
|
-
static of(compiler) {
|
|
2200
|
-
return this.register(compiler);
|
|
2201
|
-
}
|
|
2202
|
-
};
|
|
2203
|
-
|
|
2204
|
-
// src/plugins/terminal-select/utils/select-operation-definitions.ts
|
|
2205
|
-
import { select } from "inquirer-select-pro";
|
|
2206
|
-
import { ListrInquirerPromptAdapter } from "@listr2/prompt-adapter-inquirer";
|
|
2207
|
-
async function selectOperationDefinitions(task, operationDefinitions) {
|
|
2208
|
-
const selectedOperationDefinitions = await task.prompt(ListrInquirerPromptAdapter).run(
|
|
2209
|
-
select,
|
|
2210
|
-
{
|
|
2211
|
-
message: "Select Pathname",
|
|
2212
|
-
defaultValue: [],
|
|
2213
|
-
options: (input) => {
|
|
2214
|
-
const items = operationDefinitions.map((op) => ({ name: `${op.module.name} ${op.method.toUpperCase()} ${op.pathname}`, value: op }));
|
|
2215
|
-
if (!input) return items;
|
|
2216
|
-
const keys2 = input.trim().toLowerCase().split(/\s+/);
|
|
2217
|
-
return items.filter((i) => {
|
|
2218
|
-
const name = i.name.toLowerCase();
|
|
2219
|
-
return keys2.every((q) => name.includes(q));
|
|
2220
|
-
});
|
|
2221
|
-
}
|
|
2222
|
-
}
|
|
2223
|
-
);
|
|
2224
|
-
return selectedOperationDefinitions;
|
|
2225
|
-
}
|
|
2226
|
-
|
|
2227
|
-
// src/plugins/terminal-select/constants/metadata-storage.ts
|
|
2228
|
-
var MetadataStorage11 = /* @__PURE__ */ new WeakMap();
|
|
2229
|
-
|
|
2230
|
-
// src/plugins/terminal-select/terminal-select.plugin.ts
|
|
2231
|
-
var TerminalSelectPlugin = class _TerminalSelectPlugin {
|
|
2232
|
-
constructor(options) {
|
|
2233
|
-
this.options = options;
|
|
2234
|
-
}
|
|
2235
|
-
apply(compiler) {
|
|
2236
|
-
if (MetadataStorage11.has(compiler)) return;
|
|
2237
|
-
_TerminalSelectPlugin.register(compiler);
|
|
2238
|
-
compiler.hooks.afterDownload.tapPromise(_TerminalSelectPlugin.name, async (task) => {
|
|
2239
|
-
const context = compiler.context;
|
|
2240
|
-
const matcher = context.matcher;
|
|
2241
|
-
const documents = context.documents;
|
|
2242
|
-
const operationDefinitions = documents.flatMap((document) => document.operations);
|
|
2243
|
-
const selectedOperationDefinitions = await selectOperationDefinitions(task, operationDefinitions);
|
|
2244
|
-
if (this.options.clear) {
|
|
2245
|
-
matcher.append({
|
|
2246
|
-
persist: false,
|
|
2247
|
-
ignore: true,
|
|
2248
|
-
moduleName: "*",
|
|
2249
|
-
operationMethod: "*",
|
|
2250
|
-
operationPathname: "*"
|
|
2251
|
-
});
|
|
2252
|
-
}
|
|
2253
|
-
for (const op of selectedOperationDefinitions) {
|
|
2254
|
-
matcher.append({
|
|
2255
|
-
persist: !!this.options.persist,
|
|
2256
|
-
ignore: this.options.mode === "add",
|
|
2257
|
-
moduleName: op.module.name,
|
|
2258
|
-
operationMethod: op.method,
|
|
2259
|
-
operationPathname: op.pathname
|
|
2260
|
-
});
|
|
2261
|
-
}
|
|
2262
|
-
});
|
|
2263
|
-
}
|
|
2264
|
-
static register(compiler) {
|
|
2265
|
-
if (!MetadataStorage11.has(compiler)) {
|
|
2266
|
-
MetadataStorage11.set(compiler, {
|
|
2267
|
-
hooks: {}
|
|
2268
|
-
});
|
|
2269
|
-
}
|
|
2270
|
-
return MetadataStorage11.get(compiler);
|
|
2271
|
-
}
|
|
2272
|
-
static of(compiler) {
|
|
2273
|
-
return this.register(compiler);
|
|
2274
|
-
}
|
|
2275
|
-
};
|
|
2276
|
-
|
|
2277
|
-
// src/plugins/initialize/initialize.plugin.ts
|
|
2278
|
-
import * as R22 from "ramda";
|
|
2279
|
-
var InitializePlugin = class _InitializePlugin {
|
|
2280
|
-
constructor(options) {
|
|
2281
|
-
this.options = options;
|
|
2282
|
-
}
|
|
2283
|
-
apply(compiler) {
|
|
2284
|
-
compiler.hooks.setup.tap(_InitializePlugin.name, (task) => {
|
|
2285
|
-
const plugins = [
|
|
2286
|
-
new DownloadHttpFilePlugin(),
|
|
2287
|
-
new DownloadLocalFilePlugin()
|
|
2288
|
-
];
|
|
2289
|
-
if (this.options.build) {
|
|
2290
|
-
plugins.push(new ShakingPlugin());
|
|
2291
|
-
}
|
|
2292
|
-
if (this.options.interactive) {
|
|
2293
|
-
plugins.push(
|
|
2294
|
-
new TerminalSelectPlugin(
|
|
2295
|
-
typeof this.options.interactive === "object" ? this.options.interactive : { mode: "except" }
|
|
2296
|
-
)
|
|
2297
|
-
);
|
|
2298
|
-
}
|
|
2299
|
-
const rc = compiler.context.rc;
|
|
2300
|
-
plugins.push(
|
|
2301
|
-
...R22.unnest(
|
|
2302
|
-
(rc.translators || []).map((translator) => {
|
|
2303
|
-
const plugins2 = translator.apply();
|
|
2304
|
-
return plugins2;
|
|
2305
|
-
})
|
|
2306
|
-
)
|
|
2307
|
-
);
|
|
2308
|
-
if (rc.plugins && rc.plugins.length) {
|
|
2309
|
-
plugins.push(...rc.plugins);
|
|
2310
|
-
}
|
|
2311
|
-
for (const plugin of plugins) {
|
|
2312
|
-
plugin.apply(compiler);
|
|
2313
|
-
}
|
|
2314
|
-
});
|
|
2315
|
-
if (this.options.includes && this.options.includes.length) {
|
|
2316
|
-
const modulesIncludes = this.options.includes;
|
|
2317
|
-
compiler.hooks.afterSetup.tap(_InitializePlugin.name, (task) => {
|
|
2318
|
-
const rc = compiler.context.rc;
|
|
2319
|
-
const matcher = compiler.context.matcher;
|
|
2320
|
-
const notExistModules = modulesIncludes.filter((moduleName) => !(moduleName in rc.modules));
|
|
2321
|
-
if (notExistModules.length) {
|
|
2322
|
-
throw new Error(`Cannot find module(s) ${notExistModules.join(", ")} in config file.`);
|
|
2323
|
-
}
|
|
2324
|
-
const ignoredModules = R22.difference(R22.keys(rc.modules), modulesIncludes);
|
|
2325
|
-
for (const moduleName of ignoredModules) {
|
|
2326
|
-
matcher.append({
|
|
2327
|
-
persist: false,
|
|
2328
|
-
ignore: true,
|
|
2329
|
-
moduleName,
|
|
2330
|
-
operationMethod: "*",
|
|
2331
|
-
operationPathname: "*"
|
|
2332
|
-
});
|
|
2333
|
-
}
|
|
2334
|
-
});
|
|
2335
|
-
}
|
|
2336
|
-
}
|
|
2337
|
-
};
|
|
2338
|
-
|
|
2339
|
-
// src/plugins/overwrite-query-options/overwrite-query-options.plugin.ts
|
|
2340
|
-
var QsArrayFormatUnion = Object.values(QsArrayFormat);
|
|
2341
|
-
|
|
2342
|
-
// src/translators/micro-function.translator.ts
|
|
2343
|
-
var MicroFunctionTranslator = class {
|
|
2344
|
-
apply() {
|
|
2345
|
-
return [
|
|
2346
|
-
new GenerateDeclarationPlugin(),
|
|
2347
|
-
new GenerateMicroFunctionPlugin()
|
|
2348
|
-
];
|
|
2349
|
-
}
|
|
2350
|
-
};
|
|
2351
|
-
|
|
2352
|
-
// src/compiler/tasks/setup/index.ts
|
|
2353
|
-
var explore = cosmiconfig("keq");
|
|
2354
|
-
function main(compiler, options) {
|
|
2355
|
-
return {
|
|
2356
|
-
task: async (context, task) => {
|
|
2357
|
-
const result = options?.config ? await explore.load(options.config) : await explore.search();
|
|
2358
|
-
if (!result || "isEmpty" in result && result.isEmpty) {
|
|
2359
|
-
throw new Error("Cannot find config file.");
|
|
2360
|
-
}
|
|
2361
|
-
const rc = parseRuntimeConfig(result.config);
|
|
2362
|
-
if (options?.debug) {
|
|
2363
|
-
await fs4.ensureDir(".keq");
|
|
2364
|
-
rc.debug = true;
|
|
2365
|
-
}
|
|
2366
|
-
rc.tolerant = Boolean(rc.tolerant);
|
|
2367
|
-
if (!rc.translators || !rc.translators.length) {
|
|
2368
|
-
rc.translators = [new MicroFunctionTranslator()];
|
|
2369
|
-
}
|
|
2370
|
-
const packageJsonInfo = findNearestPackageJson();
|
|
2371
|
-
if (packageJsonInfo) {
|
|
2372
|
-
const moduleSystem = getProjectModuleSystem(packageJsonInfo);
|
|
2373
|
-
rc.esm = moduleSystem === "esm";
|
|
2374
|
-
}
|
|
2375
|
-
context.rc = rc;
|
|
2376
|
-
let matcher = new IgnoreMatcher([]);
|
|
2377
|
-
if (result.filepath) {
|
|
2378
|
-
const ignoreFilepath = path10.resolve(path10.dirname(result.filepath), ".keqignore");
|
|
2379
|
-
if (await fs4.exists(ignoreFilepath)) {
|
|
2380
|
-
matcher = await IgnoreMatcher.read(ignoreFilepath);
|
|
2381
|
-
}
|
|
2382
|
-
}
|
|
2383
|
-
const ignoreRules = options.ignore === false ? [] : options.ignore?.rules || [];
|
|
2384
|
-
for (const rule of ignoreRules) {
|
|
2385
|
-
matcher.append({
|
|
2386
|
-
persist: !!rule.persist,
|
|
2387
|
-
ignore: rule.ignore,
|
|
2388
|
-
moduleName: rule.moduleName,
|
|
2389
|
-
operationMethod: rule.operationMethod,
|
|
2390
|
-
operationPathname: rule.operationPathname
|
|
2391
|
-
});
|
|
2392
|
-
}
|
|
2393
|
-
context.matcher = matcher;
|
|
2394
|
-
await compiler.hooks.setup.promise(task);
|
|
2395
|
-
}
|
|
2396
|
-
};
|
|
2397
|
-
}
|
|
2398
|
-
function createSetupTask(compiler, options) {
|
|
2399
|
-
return {
|
|
2400
|
-
title: "Setup",
|
|
2401
|
-
enabled: options?.enabled,
|
|
2402
|
-
skip: options?.skip,
|
|
2403
|
-
task: (context, task) => task.newListr(
|
|
2404
|
-
[
|
|
2405
|
-
main(compiler, options),
|
|
2406
|
-
{
|
|
2407
|
-
task: (context2, task2) => compiler.hooks.afterSetup.promise(task2)
|
|
2408
|
-
}
|
|
2409
|
-
],
|
|
2410
|
-
{
|
|
2411
|
-
concurrent: false
|
|
2412
|
-
}
|
|
2413
|
-
)
|
|
2414
|
-
};
|
|
2415
|
-
}
|
|
2416
|
-
|
|
2417
|
-
// src/compiler/tasks/download/index.ts
|
|
2418
|
-
import { PRESET_TIMER } from "listr2";
|
|
2419
|
-
import { validate } from "@scalar/openapi-parser";
|
|
2420
|
-
function main2(compiler, options) {
|
|
2421
|
-
return {
|
|
2422
|
-
task: (context, task) => {
|
|
2423
|
-
if (!context.rc || !context.matcher) {
|
|
2424
|
-
throw new Error("Please run setup task first.");
|
|
2425
|
-
}
|
|
2426
|
-
const rc = context.rc;
|
|
2427
|
-
const matcher = context.matcher;
|
|
2428
|
-
context.documents = [];
|
|
2429
|
-
return task.newListr(
|
|
2430
|
-
Object.entries(rc.modules).map(([moduleName, address]) => new ModuleDefinition(moduleName, address)).map((moduleDefinition) => ({
|
|
2431
|
-
title: moduleDefinition.name,
|
|
2432
|
-
task: async (ctx, task2) => {
|
|
2433
|
-
if (options?.skipIgnoredModules && matcher.isModuleIgnored(moduleDefinition)) {
|
|
2434
|
-
task2.skip(`(${moduleDefinition.name}) is ignored`);
|
|
2435
|
-
return;
|
|
2436
|
-
}
|
|
2437
|
-
task2.output = `Downloaded from ${moduleDefinition.address.url}`;
|
|
2438
|
-
const content = await compiler.hooks.download.promise(moduleDefinition.address, moduleDefinition, task2);
|
|
2439
|
-
if (!content) {
|
|
2440
|
-
throw new Exception(moduleDefinition, `Cannot download document from ${moduleDefinition.address.url}`);
|
|
2441
|
-
}
|
|
2442
|
-
const spec = JSON.parse(content);
|
|
2443
|
-
const { valid, errors } = await validate(spec);
|
|
2444
|
-
if (!valid) {
|
|
2445
|
-
const message = `${moduleDefinition.name} module openapi/swagger file does not conform to the openapi specifications or have grammatical errors, which may cause unexpected errors:
|
|
2446
|
-
${errors?.map((e) => ` - ${e.message}`).join("\n")}`;
|
|
2447
|
-
task2.output = message;
|
|
2448
|
-
}
|
|
2449
|
-
OpenapiUtils.dereferenceOperation(spec);
|
|
2450
|
-
const document = new ApiDocumentV3_1(
|
|
2451
|
-
spec,
|
|
2452
|
-
moduleDefinition
|
|
2453
|
-
);
|
|
2454
|
-
ctx.documents?.push(document);
|
|
2455
|
-
}
|
|
2456
|
-
})),
|
|
2457
|
-
{
|
|
2458
|
-
concurrent: true,
|
|
2459
|
-
exitOnError: false,
|
|
2460
|
-
collectErrors: "minimal",
|
|
2461
|
-
rendererOptions: {
|
|
2462
|
-
collapseSubtasks: false,
|
|
2463
|
-
// collapseSkips: false,
|
|
2464
|
-
suffixSkips: true,
|
|
2465
|
-
timer: PRESET_TIMER
|
|
2466
|
-
}
|
|
2467
|
-
}
|
|
2468
|
-
);
|
|
2469
|
-
}
|
|
2470
|
-
};
|
|
2471
|
-
}
|
|
2472
|
-
function createDownloadTask(compiler, options) {
|
|
2473
|
-
return {
|
|
2474
|
-
title: "Download",
|
|
2475
|
-
enabled: options?.enabled,
|
|
2476
|
-
skip: options?.skip,
|
|
2477
|
-
task: (_, task) => task.newListr(
|
|
2478
|
-
[
|
|
2479
|
-
main2(compiler, options),
|
|
2480
|
-
{
|
|
2481
|
-
task: (context, task2) => compiler.hooks.afterDownload.promise(task2)
|
|
2482
|
-
}
|
|
2483
|
-
],
|
|
2484
|
-
{
|
|
2485
|
-
concurrent: false
|
|
2486
|
-
}
|
|
2487
|
-
)
|
|
2488
|
-
};
|
|
2489
|
-
}
|
|
2490
|
-
|
|
2491
|
-
// src/compiler/tasks/persist/index.ts
|
|
2492
|
-
import * as path11 from "path";
|
|
2493
|
-
import fs5 from "fs-extra";
|
|
2494
|
-
function createPersistArtifactTask() {
|
|
2495
|
-
return {
|
|
2496
|
-
title: "Write files",
|
|
2497
|
-
task: async (context, task) => {
|
|
2498
|
-
if (!context.rc) throw new Error("Please run setup task first.");
|
|
2499
|
-
if (!context.artifacts || context.artifacts.length === 0) {
|
|
2500
|
-
task.skip("No compiled artifacts to persist.");
|
|
2501
|
-
return;
|
|
2502
|
-
}
|
|
2503
|
-
const rc = context.rc;
|
|
2504
|
-
const artifacts = context.artifacts;
|
|
2505
|
-
const total = artifacts.length;
|
|
2506
|
-
let completed = 0;
|
|
2507
|
-
const files = await Promise.all(artifacts.map(async (artifact) => {
|
|
2508
|
-
const realpath = `./${path11.join(rc.outdir, artifact.filepath)}`;
|
|
2509
|
-
await fs5.ensureFile(realpath);
|
|
2510
|
-
await fs5.writeFile(realpath, artifact.renderer());
|
|
2511
|
-
completed += 1;
|
|
2512
|
-
task.output = `Persisted ${completed}/${total} files`;
|
|
2513
|
-
return new Asset(path11.resolve(realpath));
|
|
2514
|
-
}));
|
|
2515
|
-
context.assets = files;
|
|
2516
|
-
}
|
|
2517
|
-
};
|
|
2518
|
-
}
|
|
2519
|
-
function createPersistIgnoreTask() {
|
|
2520
|
-
return {
|
|
2521
|
-
title: "Update .keqignore",
|
|
2522
|
-
task: async (context, task) => {
|
|
2523
|
-
if (!context.matcher) throw new Error("Please run setup task first.");
|
|
2524
|
-
const matcher = context.matcher;
|
|
2525
|
-
await matcher.write(".keqignore");
|
|
2526
|
-
}
|
|
2527
|
-
};
|
|
2528
|
-
}
|
|
2529
|
-
function main3() {
|
|
2530
|
-
return {
|
|
2531
|
-
task: (context, task) => task.newListr(
|
|
2532
|
-
[
|
|
2533
|
-
createPersistArtifactTask(),
|
|
2534
|
-
createPersistIgnoreTask()
|
|
2535
|
-
],
|
|
2536
|
-
{
|
|
2537
|
-
concurrent: true,
|
|
2538
|
-
rendererOptions: {
|
|
2539
|
-
collapseSubtasks: true
|
|
2540
|
-
}
|
|
2541
|
-
}
|
|
2542
|
-
)
|
|
2543
|
-
};
|
|
2544
|
-
}
|
|
2545
|
-
function createPersistTask(compiler, options) {
|
|
2546
|
-
return {
|
|
2547
|
-
title: "Persist",
|
|
2548
|
-
enabled: options?.enabled,
|
|
2549
|
-
skip: options?.skip,
|
|
2550
|
-
task: (context, task) => task.newListr(
|
|
2551
|
-
[
|
|
2552
|
-
main3(),
|
|
2553
|
-
{
|
|
2554
|
-
task: (context2, task2) => compiler.hooks.afterPersist.promise(task2)
|
|
2555
|
-
}
|
|
2556
|
-
],
|
|
2557
|
-
{
|
|
2558
|
-
concurrent: false
|
|
2559
|
-
}
|
|
2560
|
-
)
|
|
2561
|
-
};
|
|
2562
|
-
}
|
|
2563
|
-
|
|
2564
|
-
// src/compiler/tasks/compile/index.ts
|
|
2565
|
-
function main4(compiler) {
|
|
2566
|
-
return {
|
|
2567
|
-
task: async (context, task) => {
|
|
2568
|
-
if (!context.rc) throw new Error("Please run setup task first.");
|
|
2569
|
-
if (!context.documents) throw new Error("Please run shaking task first.");
|
|
2570
|
-
context.artifacts = [];
|
|
2571
|
-
await compiler.hooks.compile.promise(task);
|
|
2572
|
-
}
|
|
2573
|
-
};
|
|
2574
|
-
}
|
|
2575
|
-
function createCompileTask(compiler, options) {
|
|
2576
|
-
return {
|
|
2577
|
-
title: "Compile",
|
|
2578
|
-
enabled: options?.enabled,
|
|
2579
|
-
skip: options?.skip,
|
|
2580
|
-
task: (context, task) => task.newListr(
|
|
2581
|
-
[
|
|
2582
|
-
{
|
|
2583
|
-
task: (context2, task2) => compiler.hooks.beforeCompile.promise(task2)
|
|
2584
|
-
},
|
|
2585
|
-
main4(compiler),
|
|
2586
|
-
{
|
|
2587
|
-
task: (context2, task2) => compiler.hooks.afterCompile.promise(task2)
|
|
2588
|
-
}
|
|
2589
|
-
],
|
|
2590
|
-
{
|
|
2591
|
-
concurrent: false
|
|
2592
|
-
}
|
|
2593
|
-
)
|
|
2594
|
-
};
|
|
2595
|
-
}
|
|
2596
|
-
|
|
2597
|
-
// src/compiler/intercepter/print-information.ts
|
|
2598
|
-
function proxyTaskWrapper(pluginName, task) {
|
|
2599
|
-
return new Proxy(task, {
|
|
2600
|
-
set(target, prop2, value) {
|
|
2601
|
-
if (prop2 !== "output") {
|
|
2602
|
-
return Reflect.set(target, prop2, value);
|
|
2603
|
-
}
|
|
2604
|
-
target.output = `[Plugin: ${pluginName}] ${value}`;
|
|
2605
|
-
return true;
|
|
2606
|
-
}
|
|
2607
|
-
});
|
|
2608
|
-
}
|
|
2609
|
-
function printInformation(taskIndex) {
|
|
2610
|
-
return {
|
|
2611
|
-
register: (tap) => {
|
|
2612
|
-
const fn = tap.fn;
|
|
2613
|
-
if (tap.type === "promise") {
|
|
2614
|
-
tap.fn = (...args) => {
|
|
2615
|
-
const task = args[taskIndex];
|
|
2616
|
-
const proxyTask = proxyTaskWrapper(tap.name, task);
|
|
2617
|
-
args[taskIndex] = proxyTask;
|
|
2618
|
-
proxyTask.output = "Processing...";
|
|
2619
|
-
return fn(...args);
|
|
2620
|
-
};
|
|
2621
|
-
}
|
|
2622
|
-
if (tap.type === "sync") {
|
|
2623
|
-
tap.fn = (...args) => {
|
|
2624
|
-
const task = args[taskIndex];
|
|
2625
|
-
const proxyTask = proxyTaskWrapper(tap.name, task);
|
|
2626
|
-
args[taskIndex] = proxyTask;
|
|
2627
|
-
proxyTask.output = "Processing...";
|
|
2628
|
-
return fn(...args);
|
|
2629
|
-
};
|
|
2630
|
-
}
|
|
2631
|
-
if (tap.type === "async") {
|
|
2632
|
-
tap.fn = (...args) => {
|
|
2633
|
-
const task = args[taskIndex];
|
|
2634
|
-
const proxyTask = proxyTaskWrapper(tap.name, task);
|
|
2635
|
-
args[taskIndex] = proxyTask;
|
|
2636
|
-
proxyTask.output = "Processing...";
|
|
2637
|
-
return fn(...args);
|
|
2638
|
-
};
|
|
2639
|
-
}
|
|
2640
|
-
return tap;
|
|
2641
|
-
}
|
|
2642
|
-
};
|
|
2643
|
-
}
|
|
2644
|
-
|
|
2645
|
-
// src/compiler/intercepter/perfect-error-message.ts
|
|
2646
|
-
import * as R23 from "ramda";
|
|
2647
|
-
function perfectErrorMessage() {
|
|
2648
|
-
return {
|
|
2649
|
-
register: (tap) => {
|
|
2650
|
-
const fn = tap.fn;
|
|
2651
|
-
function prefix(err) {
|
|
2652
|
-
if (err instanceof Error) {
|
|
2653
|
-
err.message = `[Plugin: ${tap.name}] ${err.message}`;
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
if (tap.type === "promise") {
|
|
2657
|
-
tap.fn = async (...args) => {
|
|
2658
|
-
try {
|
|
2659
|
-
return await fn(...args);
|
|
2660
|
-
} catch (err) {
|
|
2661
|
-
prefix(err);
|
|
2662
|
-
throw err;
|
|
2663
|
-
}
|
|
2664
|
-
};
|
|
2665
|
-
}
|
|
2666
|
-
if (tap.type === "sync") {
|
|
2667
|
-
tap.fn = (...args) => {
|
|
2668
|
-
try {
|
|
2669
|
-
return fn(...args);
|
|
2670
|
-
} catch (err) {
|
|
2671
|
-
prefix(err);
|
|
2672
|
-
throw err;
|
|
2673
|
-
}
|
|
2674
|
-
};
|
|
2675
|
-
}
|
|
2676
|
-
if (tap.type === "async") {
|
|
2677
|
-
tap.fn = (...args) => {
|
|
2678
|
-
const callback = R23.last(args);
|
|
2679
|
-
return fn(...R23.init(args), (err, result) => {
|
|
2680
|
-
prefix(err);
|
|
2681
|
-
return callback(err, result);
|
|
2682
|
-
});
|
|
2683
|
-
};
|
|
2684
|
-
}
|
|
2685
|
-
return tap;
|
|
2686
|
-
}
|
|
2687
|
-
};
|
|
2688
|
-
}
|
|
2689
|
-
|
|
2690
|
-
// src/compiler/compiler.ts
|
|
2691
|
-
var Compiler = class {
|
|
2692
|
-
constructor(options) {
|
|
2693
|
-
this.options = options;
|
|
2694
|
-
for (const hook of Object.values(this.hooks)) {
|
|
2695
|
-
hook.intercept(perfectErrorMessage());
|
|
2696
|
-
}
|
|
2697
|
-
this.hooks.afterSetup.intercept(printInformation(0));
|
|
2698
|
-
this.hooks.afterPersist.intercept(printInformation(0));
|
|
2699
|
-
new InitializePlugin({
|
|
2700
|
-
build: options.build,
|
|
2701
|
-
interactive: options.interactive,
|
|
2702
|
-
includes: options.includes
|
|
2703
|
-
}).apply(this);
|
|
2704
|
-
}
|
|
2705
|
-
context = {};
|
|
2706
|
-
hooks = {
|
|
2707
|
-
setup: new AsyncParallelHook(["task"]),
|
|
2708
|
-
afterSetup: new AsyncSeriesHook(["task"]),
|
|
2709
|
-
beforeDownload: new AsyncSeriesHook(["task"]),
|
|
2710
|
-
download: new AsyncSeriesBailHook(["address", "moduleDefinition", "task"]),
|
|
2711
|
-
afterDownload: new AsyncSeriesHook(["task"]),
|
|
2712
|
-
beforeCompile: new AsyncSeriesHook(["task"]),
|
|
2713
|
-
compile: new AsyncParallelHook(["task"]),
|
|
2714
|
-
afterCompile: new AsyncSeriesHook(["task"]),
|
|
2715
|
-
beforePersist: new AsyncSeriesHook(["task"]),
|
|
2716
|
-
persist: new AsyncParallelHook(["task"]),
|
|
2717
|
-
afterPersist: new AsyncSeriesHook(["task"]),
|
|
2718
|
-
done: new SyncHook()
|
|
2719
|
-
};
|
|
2720
|
-
async run() {
|
|
2721
|
-
const options = this.options;
|
|
2722
|
-
const tasks = new Listr(
|
|
2723
|
-
[
|
|
2724
|
-
createSetupTask(this, options),
|
|
2725
|
-
createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
|
|
2726
|
-
createCompileTask(this, { enabled: !!options.build }),
|
|
2727
|
-
createPersistTask(this)
|
|
2728
|
-
],
|
|
2729
|
-
{
|
|
2730
|
-
concurrent: false,
|
|
2731
|
-
renderer: "default",
|
|
2732
|
-
rendererOptions: {
|
|
2733
|
-
suffixSkips: true,
|
|
2734
|
-
collapseSubtasks: false,
|
|
2735
|
-
collapseErrors: false
|
|
2736
|
-
}
|
|
2737
|
-
}
|
|
2738
|
-
);
|
|
2739
|
-
await tasks.run(this.context);
|
|
2740
|
-
await this.hooks.done.promise();
|
|
2741
|
-
}
|
|
2742
|
-
};
|
|
2743
20
|
export {
|
|
2744
21
|
ApiDocumentV3_1,
|
|
2745
22
|
Artifact,
|