1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.mergeSchema = exports.checkValidMIMEType = exports.reduceTypes = exports.toTSType = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
6 | const ts = tslib_1.__importStar(require("typescript"));
|
7 | const debug = (0, debug_1.default)('dtsgen');
|
8 | function toTSType(type, debugSource) {
|
9 | switch (type) {
|
10 | case 'any':
|
11 | return ts.SyntaxKind.AnyKeyword;
|
12 | case 'boolean':
|
13 | return ts.SyntaxKind.BooleanKeyword;
|
14 | case 'integer':
|
15 | return ts.SyntaxKind.NumberKeyword;
|
16 | case 'null':
|
17 | return ts.SyntaxKind.NullKeyword;
|
18 | case 'number':
|
19 | return ts.SyntaxKind.NumberKeyword;
|
20 | case 'string':
|
21 | return ts.SyntaxKind.StringKeyword;
|
22 | case 'undefined':
|
23 | return ts.SyntaxKind.UndefinedKeyword;
|
24 | case 'object':
|
25 | case 'array':
|
26 | return undefined;
|
27 | case 'file':
|
28 | return ts.SyntaxKind.UnknownKeyword;
|
29 | default:
|
30 | if (debugSource) {
|
31 | debug(`toTSType: unknown type: ${JSON.stringify(debugSource, null, 2)}`);
|
32 | }
|
33 | throw new Error('unknown type: ' + type);
|
34 | }
|
35 | }
|
36 | exports.toTSType = toTSType;
|
37 | function reduceTypes(types) {
|
38 | if (types.length < 2) {
|
39 | return types;
|
40 | }
|
41 | const set = new Set(types);
|
42 | return Array.from(set.values());
|
43 | }
|
44 | exports.reduceTypes = reduceTypes;
|
45 | function checkValidMIMEType(mime) {
|
46 | var _a;
|
47 | const type = (_a = mime.toLowerCase().split(';')[0]) === null || _a === void 0 ? void 0 : _a.trim();
|
48 | if (type == null) {
|
49 | return false;
|
50 | }
|
51 | if ([
|
52 | 'application/octet-stream',
|
53 | 'application/x-www-form-urlencoded',
|
54 | 'multipart/form-data',
|
55 | 'application/jwt',
|
56 | 'application/vnd.apple.pkpass',
|
57 | ].includes(type)) {
|
58 | return true;
|
59 | }
|
60 | if (type.startsWith('text/') || type.startsWith('image/')) {
|
61 | return true;
|
62 | }
|
63 | return /^application\/(?:[a-z0-9-_.]+\+)?json5?$/.test(type);
|
64 | }
|
65 | exports.checkValidMIMEType = checkValidMIMEType;
|
66 | function mergeSchema(a, b) {
|
67 | if ('$ref' in a || '$ref' in b) {
|
68 | a.$ref = b.$ref || a.$ref;
|
69 | return false;
|
70 | }
|
71 | Object.keys(b).forEach((key) => {
|
72 | var _a, _b;
|
73 | const value = b[key];
|
74 | if (a[key] != null && typeof value !== typeof a[key]) {
|
75 | debug(`mergeSchema warning: type is mismatched, key=${key}`);
|
76 | }
|
77 | if (Array.isArray(value)) {
|
78 | a[key] = ((_a = a[key]) !== null && _a !== void 0 ? _a : []).concat(value);
|
79 | }
|
80 | else if (value != null && typeof value === 'object') {
|
81 | (_b = a[key]) !== null && _b !== void 0 ? _b : (a[key] = {});
|
82 | mergeSchema(a[key], value);
|
83 | }
|
84 | else {
|
85 | a[key] = value;
|
86 | }
|
87 | });
|
88 | return true;
|
89 | }
|
90 | exports.mergeSchema = mergeSchema;
|