@sebspark/openapi-typegen 1.8.1 → 1.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +37 -66
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -49,10 +49,8 @@ var document = ({ title, description }) => {
|
|
|
49
49
|
if (title || description) {
|
|
50
50
|
const tokens = [];
|
|
51
51
|
tokens.push("/**");
|
|
52
|
-
if (title)
|
|
53
|
-
|
|
54
|
-
if (description)
|
|
55
|
-
tokens.push(` * ${description}`);
|
|
52
|
+
if (title) tokens.push(` * ${title}`);
|
|
53
|
+
if (description) tokens.push(` * ${description}`);
|
|
56
54
|
tokens.push(" */\n");
|
|
57
55
|
return tokens.join("\n");
|
|
58
56
|
}
|
|
@@ -68,14 +66,11 @@ var documentServerPath = (path, responses) => documentPath(path, responses);
|
|
|
68
66
|
var documentPath = (path, responses, argsBefore = [], argsAfter = []) => {
|
|
69
67
|
const tokens = [];
|
|
70
68
|
tokens.push("/**");
|
|
71
|
-
if (path.title)
|
|
72
|
-
|
|
73
|
-
if (path.description)
|
|
74
|
-
tokens.push(` * ${path.description}`);
|
|
69
|
+
if (path.title) tokens.push(` * ${path.title}`);
|
|
70
|
+
if (path.description) tokens.push(` * ${path.description}`);
|
|
75
71
|
tokens.push(" *");
|
|
76
72
|
tokens.push(...argsBefore);
|
|
77
|
-
if (path.args)
|
|
78
|
-
tokens.push(...documentArgs(path.args));
|
|
73
|
+
if (path.args) tokens.push(...documentArgs(path.args));
|
|
79
74
|
tokens.push(...argsAfter);
|
|
80
75
|
tokens.push(` * @returns {Promise<${responses}>}`);
|
|
81
76
|
tokens.push(" */");
|
|
@@ -99,8 +94,7 @@ var documentArgs = (args) => {
|
|
|
99
94
|
};
|
|
100
95
|
var buildPath = (path, property) => rxProperVariable.test(property) ? `${path}.${property}` : `${path}["${property}"]`;
|
|
101
96
|
var requestArgs = (args, name, title) => {
|
|
102
|
-
if (!args)
|
|
103
|
-
return [];
|
|
97
|
+
if (!args) return [];
|
|
104
98
|
const tokens = [];
|
|
105
99
|
const type = (args.allOf || []).map((e) => e.type).join(AND) || "Object";
|
|
106
100
|
tokens.push(
|
|
@@ -138,12 +132,9 @@ var param = (name, type, optional = false, title = "", description = "") => {
|
|
|
138
132
|
);
|
|
139
133
|
if (optional || title || description) {
|
|
140
134
|
tokens.push(" -");
|
|
141
|
-
if (optional)
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
tokens.push(` ${title}`);
|
|
145
|
-
if (description)
|
|
146
|
-
tokens.push(` ${description}`);
|
|
135
|
+
if (optional) tokens.push(" Optional.");
|
|
136
|
+
if (title) tokens.push(` ${title}`);
|
|
137
|
+
if (description) tokens.push(` ${description}`);
|
|
147
138
|
}
|
|
148
139
|
return tokens.join("");
|
|
149
140
|
};
|
|
@@ -195,8 +186,7 @@ var rxProperVariable = /^[a-zA-Z_<>$][a-zA-Z0-9_<>$]*$/;
|
|
|
195
186
|
var isValidName = (name) => {
|
|
196
187
|
const namingConventionRegex = /^([A-Z_]\w*)([a-z_]\w*)(<([a-z_]\w*(,\s*)?)+>)?$/;
|
|
197
188
|
const hasCapitalLetterRegex = /[A-Z]/;
|
|
198
|
-
if (!namingConventionRegex.test(name))
|
|
199
|
-
return false;
|
|
189
|
+
if (!namingConventionRegex.test(name)) return false;
|
|
200
190
|
if (!hasCapitalLetterRegex.test(name)) {
|
|
201
191
|
return false;
|
|
202
192
|
}
|
|
@@ -277,11 +267,9 @@ var generateHeader = (header) => {
|
|
|
277
267
|
};
|
|
278
268
|
var generateResponseBody = (type, optional = true) => {
|
|
279
269
|
const customType = type.type;
|
|
280
|
-
if (customType)
|
|
281
|
-
return typeName(customType);
|
|
270
|
+
if (customType) return typeName(customType);
|
|
282
271
|
const body = type;
|
|
283
|
-
if (!body.data && !body.headers)
|
|
284
|
-
return "undefined";
|
|
272
|
+
if (!body.data && !body.headers) return "undefined";
|
|
285
273
|
const tokens = [];
|
|
286
274
|
tokens.push(preamble(body));
|
|
287
275
|
tokens.push("APIResponse<");
|
|
@@ -335,8 +323,7 @@ var generateHeaders = (headers) => {
|
|
|
335
323
|
return `{${tokens.join(", ")}}`;
|
|
336
324
|
};
|
|
337
325
|
var serializeValue = (value) => {
|
|
338
|
-
if (typeof value === "string")
|
|
339
|
-
return `'${value}'`;
|
|
326
|
+
if (typeof value === "string") return `'${value}'`;
|
|
340
327
|
return value;
|
|
341
328
|
};
|
|
342
329
|
|
|
@@ -359,16 +346,14 @@ var generateArgs = (args, isServer) => {
|
|
|
359
346
|
);
|
|
360
347
|
}
|
|
361
348
|
}
|
|
362
|
-
if (!tokens.length)
|
|
363
|
-
return "";
|
|
349
|
+
if (!tokens.length) return "";
|
|
364
350
|
const optional = argsOptional(args);
|
|
365
351
|
return `args${optional ? "?" : ""}: ${isServer ? "Req & " : ""}{ ${tokens.join(", ")} }, `;
|
|
366
352
|
}
|
|
367
353
|
return "";
|
|
368
354
|
};
|
|
369
355
|
var wrapArgs = (args, wrap) => {
|
|
370
|
-
if (!wrap)
|
|
371
|
-
return args;
|
|
356
|
+
if (!wrap) return args;
|
|
372
357
|
return `LowerCaseHeaders<${args}>`;
|
|
373
358
|
};
|
|
374
359
|
var argsOptional = (args) => (
|
|
@@ -418,8 +403,7 @@ var generateServer = (name, paths) => {
|
|
|
418
403
|
};
|
|
419
404
|
var groupPathsByUrl = (paths) => paths.reduce(
|
|
420
405
|
(group, path) => {
|
|
421
|
-
if (!group[path.url])
|
|
422
|
-
group[path.url] = [];
|
|
406
|
+
if (!group[path.url]) group[path.url] = [];
|
|
423
407
|
group[path.url].push(path);
|
|
424
408
|
return group;
|
|
425
409
|
},
|
|
@@ -447,7 +431,7 @@ var generate = (name, doc) => `
|
|
|
447
431
|
* This file was auto-generated.
|
|
448
432
|
* Do not make direct changes to the file.
|
|
449
433
|
*/
|
|
450
|
-
|
|
434
|
+
|
|
451
435
|
import type {
|
|
452
436
|
APIResponse,
|
|
453
437
|
APIServerDefinition,
|
|
@@ -458,18 +442,18 @@ var generate = (name, doc) => `
|
|
|
458
442
|
RequestOptions,
|
|
459
443
|
Serialized,
|
|
460
444
|
} from '@sebspark/openapi-core'
|
|
461
|
-
import type { Request } from 'express'
|
|
462
|
-
|
|
463
|
-
type Req = Pick<
|
|
464
|
-
|
|
445
|
+
import type { Request as ExpressRequest } from 'express'
|
|
446
|
+
|
|
447
|
+
type Req = Pick<ExpressRequest, 'url' | 'baseUrl' | 'cookies' | 'hostname'>
|
|
448
|
+
|
|
465
449
|
/* tslint:disable */
|
|
466
450
|
/* eslint-disable */
|
|
467
|
-
|
|
451
|
+
|
|
468
452
|
${generateComponents(doc.components)}
|
|
469
453
|
|
|
470
|
-
${generateServer(name, doc.paths)}
|
|
454
|
+
${doc.paths.length ? generateServer(name, doc.paths) : ""}
|
|
471
455
|
|
|
472
|
-
${generateClient(name, doc.paths)}
|
|
456
|
+
${doc.paths.length ? generateClient(name, doc.paths) : ""}
|
|
473
457
|
|
|
474
458
|
`;
|
|
475
459
|
var generateComponents = (components) => {
|
|
@@ -541,10 +525,8 @@ var findRef = (components, ref) => {
|
|
|
541
525
|
};
|
|
542
526
|
var parseDocumentation = (source) => {
|
|
543
527
|
const documented = {};
|
|
544
|
-
if (source.title)
|
|
545
|
-
|
|
546
|
-
if (source.description)
|
|
547
|
-
documented.description = source.description;
|
|
528
|
+
if (source.title) documented.title = source.title;
|
|
529
|
+
if (source.description) documented.description = source.description;
|
|
548
530
|
return documented;
|
|
549
531
|
};
|
|
550
532
|
|
|
@@ -614,8 +596,7 @@ var parseObjectSchema = (name, schema) => {
|
|
|
614
596
|
return type;
|
|
615
597
|
};
|
|
616
598
|
var parseArraySchema = (name, schema) => {
|
|
617
|
-
if (schema.type !== "array")
|
|
618
|
-
throw new Error("Not an array");
|
|
599
|
+
if (schema.type !== "array") throw new Error("Not an array");
|
|
619
600
|
return {
|
|
620
601
|
name,
|
|
621
602
|
type: "array",
|
|
@@ -755,8 +736,7 @@ var joinArg = (arg1, arg2) => {
|
|
|
755
736
|
arg.oneOf = (arg1.oneOf || []).concat(arg2.oneOf || []);
|
|
756
737
|
if (arg1.description || arg2.description)
|
|
757
738
|
arg.description = arg1.description || arg2.description;
|
|
758
|
-
if (arg1.title || arg2.title)
|
|
759
|
-
arg.title = arg1.title || arg2.title;
|
|
739
|
+
if (arg1.title || arg2.title) arg.title = arg1.title || arg2.title;
|
|
760
740
|
return arg;
|
|
761
741
|
};
|
|
762
742
|
var parseSecurity = (security = [], components = {}) => {
|
|
@@ -769,8 +749,7 @@ var parseSecurity = (security = [], components = {}) => {
|
|
|
769
749
|
);
|
|
770
750
|
const arg = args.header || createArgs({ ...parseDocumentation(param2) });
|
|
771
751
|
arg.optional = false;
|
|
772
|
-
if (!arg.allOf)
|
|
773
|
-
arg.allOf = [];
|
|
752
|
+
if (!arg.allOf) arg.allOf = [];
|
|
774
753
|
arg.allOf.push({ type: parseRef(name) });
|
|
775
754
|
args.header = arg;
|
|
776
755
|
}
|
|
@@ -788,8 +767,7 @@ var parseParameters2 = (parameters = [], components = {}) => {
|
|
|
788
767
|
const param2 = findRef(components, ref);
|
|
789
768
|
const arg = args[param2.in] || createArgs({ ...parseDocumentation(param2) });
|
|
790
769
|
arg.optional = arg.optional && !param2.required;
|
|
791
|
-
if (!arg.allOf)
|
|
792
|
-
arg.allOf = [];
|
|
770
|
+
if (!arg.allOf) arg.allOf = [];
|
|
793
771
|
arg.allOf.push({ type: parseRef(ref) });
|
|
794
772
|
args[param2.in] = arg;
|
|
795
773
|
break;
|
|
@@ -826,8 +804,7 @@ var parseParameters2 = (parameters = [], components = {}) => {
|
|
|
826
804
|
var parseRequestBody = (requestBody, components = {}) => {
|
|
827
805
|
var _a, _b;
|
|
828
806
|
const args = {};
|
|
829
|
-
if (!requestBody)
|
|
830
|
-
return args;
|
|
807
|
+
if (!requestBody) return args;
|
|
831
808
|
const ref = requestBody.$ref;
|
|
832
809
|
if (ref) {
|
|
833
810
|
const refBody = findRef(components, ref);
|
|
@@ -875,14 +852,11 @@ var parseResponseBodies = (responses = {}) => {
|
|
|
875
852
|
var parseResponseBody = (name, response) => {
|
|
876
853
|
var _a, _b;
|
|
877
854
|
const ref = response.$ref;
|
|
878
|
-
if (ref)
|
|
879
|
-
return { type: parseRef(ref) };
|
|
855
|
+
if (ref) return { type: parseRef(ref) };
|
|
880
856
|
const responseObject = response;
|
|
881
857
|
const body = {};
|
|
882
|
-
if (name)
|
|
883
|
-
|
|
884
|
-
if (responseObject.description)
|
|
885
|
-
body.description = responseObject.description;
|
|
858
|
+
if (name) body.name = name;
|
|
859
|
+
if (responseObject.description) body.description = responseObject.description;
|
|
886
860
|
if ((_b = (_a = responseObject.content) == null ? void 0 : _a["application/json"]) == null ? void 0 : _b.schema) {
|
|
887
861
|
const schema = responseObject.content["application/json"].schema;
|
|
888
862
|
body.data = parseSchema(void 0, schema);
|
|
@@ -898,8 +872,7 @@ var parseResponseBody = (name, response) => {
|
|
|
898
872
|
type: { type: parseRef(ref2) },
|
|
899
873
|
...parseDocumentation(header)
|
|
900
874
|
});
|
|
901
|
-
else
|
|
902
|
-
body.headers.push(parseHeader(headerName, header));
|
|
875
|
+
else body.headers.push(parseHeader(headerName, header));
|
|
903
876
|
}
|
|
904
877
|
}
|
|
905
878
|
return body;
|
|
@@ -1033,16 +1006,14 @@ var generateTypescript = async (name, doc) => {
|
|
|
1033
1006
|
var generate2 = async (input, output) => {
|
|
1034
1007
|
const docs = await readDocs(input);
|
|
1035
1008
|
const generated = await generateDocs(docs);
|
|
1036
|
-
if (!output)
|
|
1037
|
-
return generated.map((d) => d.ts).join("\n\n");
|
|
1009
|
+
if (!output) return generated.map((d) => d.ts).join("\n\n");
|
|
1038
1010
|
await saveDocs(output, generated);
|
|
1039
1011
|
};
|
|
1040
1012
|
var readDocs = async (input) => {
|
|
1041
1013
|
const path = (0, import_node_path.resolve)(input);
|
|
1042
1014
|
const stats = await (0, import_promises.stat)(path);
|
|
1043
1015
|
const filePaths = [];
|
|
1044
|
-
if (stats.isFile())
|
|
1045
|
-
filePaths.push(path);
|
|
1016
|
+
if (stats.isFile()) filePaths.push(path);
|
|
1046
1017
|
if (stats.isDirectory()) {
|
|
1047
1018
|
const files = await (0, import_promises.readdir)(path);
|
|
1048
1019
|
filePaths.push(...files.map((f) => (0, import_node_path.resolve)(path, f)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/openapi-typegen",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"build": "tsup-node src/index.ts src/cli.ts --format cjs --dts",
|
|
16
16
|
"dev": "tsc --watch --noEmit",
|
|
17
17
|
"lint": "biome check .",
|
|
18
|
-
"test": "vitest --passWithNoTests --coverage",
|
|
18
|
+
"test": "vitest run --passWithNoTests --coverage",
|
|
19
19
|
"typecheck": "vitest --typecheck.only --passWithNoTests"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"boxen": "<6",
|
|
28
28
|
"chalk": "<5",
|
|
29
29
|
"change-case": "4.1.2",
|
|
30
|
-
"prettier": "3.2
|
|
31
|
-
"yaml": "2.4.
|
|
30
|
+
"prettier": "3.3.2",
|
|
31
|
+
"yaml": "2.4.3",
|
|
32
32
|
"yargs": "17.7.2",
|
|
33
33
|
"yarn": "1.22.22"
|
|
34
34
|
}
|