@sdk-it/core 0.12.10 → 0.12.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/dist/index 10.js +0 -844
- package/dist/index 12.js +0 -732
- package/dist/index 13.js +0 -844
- package/dist/index 14.js +0 -853
- package/dist/index 15.js +0 -732
- package/dist/index 16.js +0 -0
- package/dist/index 17.js +0 -853
- package/dist/index 2.js +0 -844
- package/dist/index 3.js +0 -732
- package/dist/index 4.js +0 -732
- package/dist/index 5.js +0 -732
- package/dist/index 6.js +0 -853
- package/dist/index 7.js +0 -0
- package/dist/index 8.js +0 -844
- package/dist/index 9.js +0 -732
- package/dist/index.d 10.ts +0 -14
- package/dist/index.d 11.ts +0 -8
- package/dist/index.d 13.ts +0 -8
- package/dist/index.d 15.ts +0 -8
- package/dist/index.d 16.ts +0 -14
- package/dist/index.d 17.ts +0 -14
- package/dist/index.d 18.ts +0 -8
- package/dist/index.d 2.ts +0 -14
- package/dist/index.d 3.ts +0 -8
- package/dist/index.d 4.ts +0 -8
- package/dist/index.d 5.ts +0 -14
- package/dist/index.d 6.ts +0 -14
- package/dist/index.d 7.ts +0 -8
- package/dist/index.d 8.ts +0 -8
- package/dist/index.d 9.ts +0 -14
- package/dist/index.d.ts 10.map +0 -1
- package/dist/index.d.ts 11.map +0 -1
- package/dist/index.d.ts 12.map +0 -1
- package/dist/index.d.ts 13.map +0 -1
- package/dist/index.d.ts 14.map +0 -1
- package/dist/index.d.ts 15.map +0 -1
- package/dist/index.d.ts 16.map +0 -1
- package/dist/index.d.ts 17.map +0 -1
- package/dist/index.d.ts 2.map +0 -1
- package/dist/index.d.ts 3.map +0 -1
- package/dist/index.d.ts 4.map +0 -1
- package/dist/index.d.ts 5.map +0 -1
- package/dist/index.d.ts 6.map +0 -1
- package/dist/index.d.ts 7.map +0 -1
- package/dist/index.d.ts 8.map +0 -1
- package/dist/index.js 10.map +0 -7
- package/dist/index.js 11.map +0 -7
- package/dist/index.js 12.map +0 -7
- package/dist/index.js 13.map +0 -7
- package/dist/index.js 14.map +0 -7
- package/dist/index.js 15.map +0 -7
- package/dist/index.js 16.map +0 -7
- package/dist/index.js 2.map +0 -7
- package/dist/index.js 3.map +0 -7
- package/dist/index.js 4.map +0 -7
- package/dist/index.js 5.map +0 -7
- package/dist/index.js 6.map +0 -7
- package/dist/index.js 7.map +0 -7
- package/dist/index.js 8.map +0 -7
- package/dist/index.js 9.map +0 -7
package/dist/index 6.js
DELETED
|
@@ -1,853 +0,0 @@
|
|
|
1
|
-
// packages/core/src/lib/deriver.ts
|
|
2
|
-
import ts, { TypeFlags, symbolName } from "typescript";
|
|
3
|
-
var deriveSymbol = Symbol.for("serialize");
|
|
4
|
-
var $types = Symbol.for("types");
|
|
5
|
-
var defaults = {
|
|
6
|
-
Readable: "any",
|
|
7
|
-
ReadableStream: "any",
|
|
8
|
-
DateConstructor: "string",
|
|
9
|
-
ArrayBufferConstructor: "any",
|
|
10
|
-
SharedArrayBufferConstructor: "any",
|
|
11
|
-
Int8ArrayConstructor: "any",
|
|
12
|
-
Uint8Array: "any"
|
|
13
|
-
};
|
|
14
|
-
var TypeDeriver = class {
|
|
15
|
-
collector = {};
|
|
16
|
-
checker;
|
|
17
|
-
constructor(checker) {
|
|
18
|
-
this.checker = checker;
|
|
19
|
-
}
|
|
20
|
-
serializeType(type) {
|
|
21
|
-
const indexType = type.getStringIndexType();
|
|
22
|
-
if (indexType) {
|
|
23
|
-
return {
|
|
24
|
-
[deriveSymbol]: true,
|
|
25
|
-
kind: "record",
|
|
26
|
-
optional: false,
|
|
27
|
-
[$types]: [this.serializeType(indexType)]
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
if (type.flags & TypeFlags.Any) {
|
|
31
|
-
return {
|
|
32
|
-
[deriveSymbol]: true,
|
|
33
|
-
optional: false,
|
|
34
|
-
[$types]: []
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
if (type.isStringLiteral()) {
|
|
38
|
-
return {
|
|
39
|
-
[deriveSymbol]: true,
|
|
40
|
-
optional: false,
|
|
41
|
-
kind: "literal",
|
|
42
|
-
value: type.value,
|
|
43
|
-
[$types]: ["string"]
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
if (type.isNumberLiteral()) {
|
|
47
|
-
return {
|
|
48
|
-
[deriveSymbol]: true,
|
|
49
|
-
optional: false,
|
|
50
|
-
kind: "literal",
|
|
51
|
-
value: type.value,
|
|
52
|
-
[$types]: ["number"]
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (type.flags & TypeFlags.TemplateLiteral) {
|
|
56
|
-
return {
|
|
57
|
-
[deriveSymbol]: true,
|
|
58
|
-
optional: false,
|
|
59
|
-
[$types]: ["string"]
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
if (type.flags & TypeFlags.String) {
|
|
63
|
-
return {
|
|
64
|
-
[deriveSymbol]: true,
|
|
65
|
-
optional: false,
|
|
66
|
-
[$types]: ["string"]
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
if (type.flags & TypeFlags.Number) {
|
|
70
|
-
return {
|
|
71
|
-
[deriveSymbol]: true,
|
|
72
|
-
optional: false,
|
|
73
|
-
[$types]: ["number"]
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
if (type.flags & ts.TypeFlags.Boolean) {
|
|
77
|
-
return {
|
|
78
|
-
[deriveSymbol]: true,
|
|
79
|
-
optional: false,
|
|
80
|
-
[$types]: ["boolean"]
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
if (type.flags & TypeFlags.Null) {
|
|
84
|
-
return {
|
|
85
|
-
[deriveSymbol]: true,
|
|
86
|
-
optional: true,
|
|
87
|
-
[$types]: ["null"]
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
if (type.isIntersection()) {
|
|
91
|
-
let optional;
|
|
92
|
-
const types = [];
|
|
93
|
-
for (const unionType of type.types) {
|
|
94
|
-
if (optional === void 0) {
|
|
95
|
-
optional = (unionType.flags & ts.TypeFlags.Undefined) !== 0;
|
|
96
|
-
if (optional) {
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
types.push(this.serializeType(unionType));
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
[deriveSymbol]: true,
|
|
104
|
-
kind: "intersection",
|
|
105
|
-
optional,
|
|
106
|
-
[$types]: types
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
if (type.isUnion()) {
|
|
110
|
-
let optional;
|
|
111
|
-
const types = [];
|
|
112
|
-
for (const unionType of type.types) {
|
|
113
|
-
if (optional === void 0) {
|
|
114
|
-
optional = (unionType.flags & ts.TypeFlags.Undefined) !== 0;
|
|
115
|
-
if (optional) {
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
types.push(this.serializeType(unionType));
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
[deriveSymbol]: true,
|
|
123
|
-
kind: "union",
|
|
124
|
-
optional,
|
|
125
|
-
[$types]: types
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
if (this.checker.isArrayLikeType(type)) {
|
|
129
|
-
const [argType] = this.checker.getTypeArguments(type);
|
|
130
|
-
if (!argType) {
|
|
131
|
-
const typeName = type.symbol?.getName() || "<unknown>";
|
|
132
|
-
console.warn(
|
|
133
|
-
`Could not find generic type argument for array type ${typeName}`
|
|
134
|
-
);
|
|
135
|
-
return {
|
|
136
|
-
[deriveSymbol]: true,
|
|
137
|
-
optional: false,
|
|
138
|
-
kind: "array",
|
|
139
|
-
[$types]: ["any"]
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
const typeSymbol = argType.getSymbol();
|
|
143
|
-
if (!typeSymbol) {
|
|
144
|
-
return {
|
|
145
|
-
[deriveSymbol]: true,
|
|
146
|
-
optional: false,
|
|
147
|
-
kind: "array",
|
|
148
|
-
[$types]: [this.serializeType(argType)]
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
if (typeSymbol.valueDeclaration) {
|
|
152
|
-
return {
|
|
153
|
-
kind: "array",
|
|
154
|
-
[deriveSymbol]: true,
|
|
155
|
-
[$types]: [this.serializeNode(typeSymbol.valueDeclaration)]
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
const maybeDeclaration = typeSymbol.declarations?.[0];
|
|
159
|
-
if (maybeDeclaration) {
|
|
160
|
-
if (ts.isMappedTypeNode(maybeDeclaration)) {
|
|
161
|
-
const resolvedType = this.checker.getPropertiesOfType(argType).reduce((acc, prop) => {
|
|
162
|
-
const propType = this.checker.getTypeOfSymbol(prop);
|
|
163
|
-
acc[prop.name] = this.serializeType(propType);
|
|
164
|
-
return acc;
|
|
165
|
-
}, {});
|
|
166
|
-
return {
|
|
167
|
-
kind: "array",
|
|
168
|
-
optional: false,
|
|
169
|
-
[deriveSymbol]: true,
|
|
170
|
-
[$types]: [resolvedType]
|
|
171
|
-
};
|
|
172
|
-
} else {
|
|
173
|
-
return {
|
|
174
|
-
kind: "array",
|
|
175
|
-
...this.serializeNode(maybeDeclaration)
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return {
|
|
180
|
-
kind: "array",
|
|
181
|
-
optional: false,
|
|
182
|
-
[deriveSymbol]: true,
|
|
183
|
-
[$types]: ["any"]
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
if (type.isClass()) {
|
|
187
|
-
const declaration = type.symbol?.valueDeclaration;
|
|
188
|
-
if (!declaration) {
|
|
189
|
-
return {
|
|
190
|
-
[deriveSymbol]: true,
|
|
191
|
-
optional: false,
|
|
192
|
-
[$types]: [type.symbol.getName()]
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
return this.serializeNode(declaration);
|
|
196
|
-
}
|
|
197
|
-
if (isInterfaceType(type)) {
|
|
198
|
-
const valueDeclaration = type.symbol.valueDeclaration ?? type.symbol.declarations?.[0];
|
|
199
|
-
if (!valueDeclaration) {
|
|
200
|
-
return {
|
|
201
|
-
[deriveSymbol]: true,
|
|
202
|
-
optional: false,
|
|
203
|
-
[$types]: [type.symbol.getName()]
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
return this.serializeNode(valueDeclaration);
|
|
207
|
-
}
|
|
208
|
-
if (type.flags & TypeFlags.Object) {
|
|
209
|
-
if (defaults[symbolName(type.symbol)]) {
|
|
210
|
-
return {
|
|
211
|
-
[deriveSymbol]: true,
|
|
212
|
-
optional: false,
|
|
213
|
-
[$types]: [defaults[type.symbol.name]]
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
const properties = this.checker.getPropertiesOfType(type);
|
|
217
|
-
if (properties.length > 0) {
|
|
218
|
-
const serializedProps = {};
|
|
219
|
-
for (const prop of properties) {
|
|
220
|
-
const propAssingment = (prop.getDeclarations() ?? []).find(
|
|
221
|
-
(it) => ts.isPropertyAssignment(it)
|
|
222
|
-
);
|
|
223
|
-
if (propAssingment) {
|
|
224
|
-
const type2 = this.checker.getTypeAtLocation(
|
|
225
|
-
propAssingment.initializer
|
|
226
|
-
);
|
|
227
|
-
serializedProps[prop.name] = this.serializeType(type2);
|
|
228
|
-
}
|
|
229
|
-
if ((prop.getDeclarations() ?? []).find(
|
|
230
|
-
(it) => ts.isPropertySignature(it)
|
|
231
|
-
)) {
|
|
232
|
-
const propType = this.checker.getTypeOfSymbol(prop);
|
|
233
|
-
serializedProps[prop.name] = this.serializeType(propType);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return {
|
|
237
|
-
[deriveSymbol]: true,
|
|
238
|
-
kind: "object",
|
|
239
|
-
optional: false,
|
|
240
|
-
[$types]: [serializedProps]
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
const declaration = type.symbol.valueDeclaration ?? type.symbol.declarations?.[0];
|
|
244
|
-
if (!declaration) {
|
|
245
|
-
return {
|
|
246
|
-
[deriveSymbol]: true,
|
|
247
|
-
optional: false,
|
|
248
|
-
[$types]: [type.symbol.getName()]
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
return this.serializeNode(declaration);
|
|
252
|
-
}
|
|
253
|
-
console.warn(`Unhandled type: ${type.flags} ${ts.TypeFlags[type.flags]}`);
|
|
254
|
-
return {
|
|
255
|
-
[deriveSymbol]: true,
|
|
256
|
-
optional: false,
|
|
257
|
-
[$types]: [
|
|
258
|
-
this.checker.typeToString(
|
|
259
|
-
type,
|
|
260
|
-
void 0,
|
|
261
|
-
ts.TypeFormatFlags.NoTruncation
|
|
262
|
-
)
|
|
263
|
-
]
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
serializeNode(node) {
|
|
267
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
268
|
-
const symbolType = this.checker.getTypeAtLocation(node);
|
|
269
|
-
const props = {};
|
|
270
|
-
for (const symbol of symbolType.getProperties()) {
|
|
271
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
272
|
-
props[symbol.name] = this.serializeType(type);
|
|
273
|
-
}
|
|
274
|
-
for (const prop of node.properties) {
|
|
275
|
-
if (ts.isPropertyAssignment(prop)) {
|
|
276
|
-
const type = this.checker.getTypeAtLocation(prop.initializer);
|
|
277
|
-
props[prop.name.getText()] = this.serializeType(type);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return props;
|
|
281
|
-
}
|
|
282
|
-
if (ts.isPropertyAccessExpression(node)) {
|
|
283
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
284
|
-
if (!symbol) {
|
|
285
|
-
console.warn(`No symbol found for ${node.name.getText()}`);
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
289
|
-
return this.serializeType(type);
|
|
290
|
-
}
|
|
291
|
-
if (ts.isPropertySignature(node)) {
|
|
292
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
293
|
-
if (!symbol) {
|
|
294
|
-
console.warn(`No symbol found for ${node.name.getText()}`);
|
|
295
|
-
return null;
|
|
296
|
-
}
|
|
297
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
298
|
-
return this.serializeType(type);
|
|
299
|
-
}
|
|
300
|
-
if (ts.isPropertyDeclaration(node)) {
|
|
301
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
302
|
-
if (!symbol) {
|
|
303
|
-
console.warn(`No symbol found for ${node.name.getText()}`);
|
|
304
|
-
return null;
|
|
305
|
-
}
|
|
306
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
307
|
-
return this.serializeType(type);
|
|
308
|
-
}
|
|
309
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
310
|
-
if (!node.name?.text) {
|
|
311
|
-
throw new Error("Interface has no name");
|
|
312
|
-
}
|
|
313
|
-
if (defaults[node.name.text]) {
|
|
314
|
-
return {
|
|
315
|
-
[deriveSymbol]: true,
|
|
316
|
-
optional: false,
|
|
317
|
-
[$types]: [defaults[node.name.text]]
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
if (!this.collector[node.name.text]) {
|
|
321
|
-
this.collector[node.name.text] = {};
|
|
322
|
-
const members = {};
|
|
323
|
-
for (const member of node.members.filter(ts.isPropertySignature)) {
|
|
324
|
-
members[member.name.getText()] = this.serializeNode(member);
|
|
325
|
-
}
|
|
326
|
-
this.collector[node.name.text] = members;
|
|
327
|
-
}
|
|
328
|
-
return {
|
|
329
|
-
[deriveSymbol]: true,
|
|
330
|
-
optional: false,
|
|
331
|
-
[$types]: [`#/components/schemas/${node.name.text}`]
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
if (ts.isClassDeclaration(node)) {
|
|
335
|
-
if (!node.name?.text) {
|
|
336
|
-
throw new Error("Class has no name");
|
|
337
|
-
}
|
|
338
|
-
if (defaults[node.name.text]) {
|
|
339
|
-
return {
|
|
340
|
-
[deriveSymbol]: true,
|
|
341
|
-
optional: false,
|
|
342
|
-
[$types]: [defaults[node.name.text]]
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
if (!this.collector[node.name.text]) {
|
|
346
|
-
this.collector[node.name.text] = {};
|
|
347
|
-
const members = {};
|
|
348
|
-
for (const member of node.members.filter(ts.isPropertyDeclaration)) {
|
|
349
|
-
members[member.name.getText()] = this.serializeNode(member);
|
|
350
|
-
}
|
|
351
|
-
this.collector[node.name.text] = members;
|
|
352
|
-
}
|
|
353
|
-
return {
|
|
354
|
-
[deriveSymbol]: true,
|
|
355
|
-
optional: false,
|
|
356
|
-
[$types]: [`#/components/schemas/${node.name.text}`],
|
|
357
|
-
$ref: `#/components/schemas/${node.name.text}`
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
if (ts.isVariableDeclaration(node)) {
|
|
361
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
362
|
-
if (!symbol) {
|
|
363
|
-
console.warn(`No symbol found for ${node.name.getText()}`);
|
|
364
|
-
return null;
|
|
365
|
-
}
|
|
366
|
-
if (!node.type) {
|
|
367
|
-
console.warn(`No type found for ${node.name.getText()}`);
|
|
368
|
-
return "any";
|
|
369
|
-
}
|
|
370
|
-
const type = this.checker.getTypeFromTypeNode(node.type);
|
|
371
|
-
return this.serializeType(type);
|
|
372
|
-
}
|
|
373
|
-
if (ts.isIdentifier(node)) {
|
|
374
|
-
const symbol = this.checker.getSymbolAtLocation(node);
|
|
375
|
-
if (!symbol) {
|
|
376
|
-
console.warn(`Identifer: No symbol found for ${node.getText()}`);
|
|
377
|
-
return null;
|
|
378
|
-
}
|
|
379
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
380
|
-
return this.serializeType(type);
|
|
381
|
-
}
|
|
382
|
-
if (ts.isAwaitExpression(node)) {
|
|
383
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
384
|
-
return this.serializeType(type);
|
|
385
|
-
}
|
|
386
|
-
if (ts.isCallExpression(node)) {
|
|
387
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
388
|
-
return this.serializeType(type);
|
|
389
|
-
}
|
|
390
|
-
if (ts.isAsExpression(node)) {
|
|
391
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
392
|
-
return this.serializeType(type);
|
|
393
|
-
}
|
|
394
|
-
if (ts.isTypeLiteralNode(node)) {
|
|
395
|
-
const symbolType = this.checker.getTypeAtLocation(node);
|
|
396
|
-
const props = {};
|
|
397
|
-
for (const symbol of symbolType.getProperties()) {
|
|
398
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
399
|
-
props[symbol.name] = this.serializeType(type);
|
|
400
|
-
}
|
|
401
|
-
return {
|
|
402
|
-
[deriveSymbol]: true,
|
|
403
|
-
optional: false,
|
|
404
|
-
[$types]: [props]
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
if (node.kind === ts.SyntaxKind.NullKeyword) {
|
|
408
|
-
return {
|
|
409
|
-
[deriveSymbol]: true,
|
|
410
|
-
optional: true,
|
|
411
|
-
[$types]: ["null"]
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
if (node.kind === ts.SyntaxKind.BooleanKeyword) {
|
|
415
|
-
return {
|
|
416
|
-
[deriveSymbol]: true,
|
|
417
|
-
optional: false,
|
|
418
|
-
[$types]: ["boolean"]
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
if (node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
422
|
-
return {
|
|
423
|
-
[deriveSymbol]: true,
|
|
424
|
-
optional: false,
|
|
425
|
-
kind: "literal",
|
|
426
|
-
value: true,
|
|
427
|
-
[$types]: ["boolean"]
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
431
|
-
return {
|
|
432
|
-
[deriveSymbol]: true,
|
|
433
|
-
optional: false,
|
|
434
|
-
kind: "literal",
|
|
435
|
-
value: false,
|
|
436
|
-
[$types]: ["boolean"]
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
440
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
441
|
-
return this.serializeType(type);
|
|
442
|
-
}
|
|
443
|
-
console.warn(`Unhandled node: ${ts.SyntaxKind[node.kind]} ${node.flags}`);
|
|
444
|
-
return {
|
|
445
|
-
[deriveSymbol]: true,
|
|
446
|
-
optional: false,
|
|
447
|
-
[$types]: ["any"]
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
function isInterfaceType(type) {
|
|
452
|
-
if (type.isClassOrInterface()) {
|
|
453
|
-
return !!(type.symbol.flags & ts.SymbolFlags.Interface);
|
|
454
|
-
}
|
|
455
|
-
return false;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// packages/core/src/lib/file-system.ts
|
|
459
|
-
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
460
|
-
import { dirname, isAbsolute, join } from "node:path";
|
|
461
|
-
async function getFile(filePath) {
|
|
462
|
-
if (await exist(filePath)) {
|
|
463
|
-
return readFile(filePath, "utf-8");
|
|
464
|
-
}
|
|
465
|
-
return null;
|
|
466
|
-
}
|
|
467
|
-
async function exist(file) {
|
|
468
|
-
return stat(file).then(() => true).catch(() => false);
|
|
469
|
-
}
|
|
470
|
-
async function readFolder(path) {
|
|
471
|
-
if (await exist(path)) {
|
|
472
|
-
return readdir(path);
|
|
473
|
-
}
|
|
474
|
-
return [];
|
|
475
|
-
}
|
|
476
|
-
async function writeFiles(dir, contents) {
|
|
477
|
-
await Promise.all(
|
|
478
|
-
Object.entries(contents).map(async ([file, content]) => {
|
|
479
|
-
if (content === null) {
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
const filePath = isAbsolute(file) ? file : join(dir, file);
|
|
483
|
-
await mkdir(dirname(filePath), { recursive: true });
|
|
484
|
-
if (typeof content === "string") {
|
|
485
|
-
await writeFile(filePath, content, "utf-8");
|
|
486
|
-
} else {
|
|
487
|
-
if (content.ignoreIfExists) {
|
|
488
|
-
if (!await exist(filePath)) {
|
|
489
|
-
await writeFile(filePath, content.content, "utf-8");
|
|
490
|
-
}
|
|
491
|
-
} else {
|
|
492
|
-
await writeFile(filePath, content.content, "utf-8");
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
})
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
|
-
async function getFolderExports(folder, extensions = ["ts"], ignore = () => false) {
|
|
499
|
-
const files = await readdir(folder, { withFileTypes: true });
|
|
500
|
-
const exports = [];
|
|
501
|
-
for (const file of files) {
|
|
502
|
-
if (ignore(file)) {
|
|
503
|
-
continue;
|
|
504
|
-
}
|
|
505
|
-
if (file.isDirectory()) {
|
|
506
|
-
exports.push(`export * from './${file.name}/index.ts';`);
|
|
507
|
-
} else if (file.name !== "index.ts" && extensions.includes(getExt(file.name))) {
|
|
508
|
-
exports.push(`export * from './${file.name}';`);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
return exports.join("\n");
|
|
512
|
-
}
|
|
513
|
-
var getExt = (fileName) => {
|
|
514
|
-
if (!fileName) {
|
|
515
|
-
return "";
|
|
516
|
-
}
|
|
517
|
-
const lastDot = fileName.lastIndexOf(".");
|
|
518
|
-
if (lastDot === -1) {
|
|
519
|
-
return "";
|
|
520
|
-
}
|
|
521
|
-
const ext = fileName.slice(lastDot + 1).split("/").filter(Boolean).join("");
|
|
522
|
-
if (ext === fileName) {
|
|
523
|
-
return "";
|
|
524
|
-
}
|
|
525
|
-
return ext || "txt";
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
// packages/core/src/lib/paths.ts
|
|
529
|
-
var methods = [
|
|
530
|
-
"get",
|
|
531
|
-
"post",
|
|
532
|
-
"put",
|
|
533
|
-
"patch",
|
|
534
|
-
"delete",
|
|
535
|
-
"trace",
|
|
536
|
-
"head"
|
|
537
|
-
];
|
|
538
|
-
var semanticSourceToOpenAPI = {
|
|
539
|
-
queries: "query",
|
|
540
|
-
query: "query",
|
|
541
|
-
headers: "header",
|
|
542
|
-
params: "path"
|
|
543
|
-
};
|
|
544
|
-
var Paths = class {
|
|
545
|
-
#commonZodImport;
|
|
546
|
-
#onOperation;
|
|
547
|
-
#operations = [];
|
|
548
|
-
constructor(config) {
|
|
549
|
-
this.#commonZodImport = config.commonZodImport;
|
|
550
|
-
this.#onOperation = config.onOperation;
|
|
551
|
-
}
|
|
552
|
-
addPath(name, path, method, selectors, responses, sourceFile, tags, description) {
|
|
553
|
-
const responsesObject = this.#responseItemToResponses(responses);
|
|
554
|
-
this.#operations.push({
|
|
555
|
-
name,
|
|
556
|
-
path: this.#tunePath(path),
|
|
557
|
-
sourceFile,
|
|
558
|
-
method,
|
|
559
|
-
selectors,
|
|
560
|
-
responses: responsesObject,
|
|
561
|
-
tags,
|
|
562
|
-
description
|
|
563
|
-
});
|
|
564
|
-
return this;
|
|
565
|
-
}
|
|
566
|
-
#responseItemToResponses(responses) {
|
|
567
|
-
const responsesObject = {};
|
|
568
|
-
for (const item of responses) {
|
|
569
|
-
const ct = item.contentType;
|
|
570
|
-
const schema = item.response ? toSchema(item.response) : {};
|
|
571
|
-
if (!responsesObject[item.statusCode]) {
|
|
572
|
-
responsesObject[item.statusCode] = {
|
|
573
|
-
description: `Response for ${item.statusCode}`,
|
|
574
|
-
content: ct !== "empty" ? {
|
|
575
|
-
[ct]: ct === "application/octet-stream" ? { schema: { type: "string", format: "binary" } } : { schema }
|
|
576
|
-
} : void 0,
|
|
577
|
-
headers: item.headers.length ? item.headers.reduce((acc, current) => {
|
|
578
|
-
const headers = typeof current === "string" ? { [current]: [] } : current;
|
|
579
|
-
return Object.entries(headers).reduce(
|
|
580
|
-
(subAcc, [key, value]) => {
|
|
581
|
-
const header = {
|
|
582
|
-
[key]: {
|
|
583
|
-
schema: {
|
|
584
|
-
type: "string",
|
|
585
|
-
enum: value.length ? value : void 0
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
return { ...subAcc, ...header };
|
|
590
|
-
},
|
|
591
|
-
acc
|
|
592
|
-
);
|
|
593
|
-
}, {}) : void 0
|
|
594
|
-
};
|
|
595
|
-
} else {
|
|
596
|
-
if (!responsesObject[item.statusCode].content[ct]) {
|
|
597
|
-
responsesObject[item.statusCode].content[ct] = { schema };
|
|
598
|
-
} else {
|
|
599
|
-
const existing = responsesObject[item.statusCode].content[ct].schema;
|
|
600
|
-
if (existing.oneOf) {
|
|
601
|
-
if (!existing.oneOf.find(
|
|
602
|
-
(it) => JSON.stringify(it) === JSON.stringify(schema)
|
|
603
|
-
)) {
|
|
604
|
-
existing.oneOf.push(schema);
|
|
605
|
-
}
|
|
606
|
-
} else if (JSON.stringify(existing) !== JSON.stringify(schema)) {
|
|
607
|
-
responsesObject[item.statusCode].content[ct].schema = {
|
|
608
|
-
oneOf: [existing, schema]
|
|
609
|
-
};
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
return responsesObject;
|
|
615
|
-
}
|
|
616
|
-
async #selectosToParameters(selectors) {
|
|
617
|
-
const parameters = [];
|
|
618
|
-
const bodySchemaProps = {};
|
|
619
|
-
for (const selector of selectors) {
|
|
620
|
-
if (selector.source === "body") {
|
|
621
|
-
bodySchemaProps[selector.name] = {
|
|
622
|
-
required: selector.required,
|
|
623
|
-
schema: await evalZod(selector.against, this.#commonZodImport)
|
|
624
|
-
};
|
|
625
|
-
continue;
|
|
626
|
-
}
|
|
627
|
-
const parameter = {
|
|
628
|
-
in: semanticSourceToOpenAPI[selector.source],
|
|
629
|
-
name: selector.name,
|
|
630
|
-
required: selector.required,
|
|
631
|
-
schema: await evalZod(selector.against, this.#commonZodImport)
|
|
632
|
-
};
|
|
633
|
-
parameters.push(parameter);
|
|
634
|
-
}
|
|
635
|
-
return { parameters, bodySchemaProps };
|
|
636
|
-
}
|
|
637
|
-
async getPaths() {
|
|
638
|
-
const operations = {};
|
|
639
|
-
for (const operation of this.#operations) {
|
|
640
|
-
const { path, method, selectors } = operation;
|
|
641
|
-
const { parameters, bodySchemaProps } = await this.#selectosToParameters(selectors);
|
|
642
|
-
const bodySchema = {};
|
|
643
|
-
const required = [];
|
|
644
|
-
for (const [key, value] of Object.entries(bodySchemaProps)) {
|
|
645
|
-
if (value.required) {
|
|
646
|
-
required.push(key);
|
|
647
|
-
}
|
|
648
|
-
bodySchema[key] = value.schema;
|
|
649
|
-
}
|
|
650
|
-
const operationObject = {
|
|
651
|
-
operationId: operation.name,
|
|
652
|
-
parameters,
|
|
653
|
-
tags: operation.tags,
|
|
654
|
-
description: operation.description,
|
|
655
|
-
requestBody: Object.keys(bodySchema).length ? {
|
|
656
|
-
content: {
|
|
657
|
-
"application/json": {
|
|
658
|
-
schema: {
|
|
659
|
-
required: required.length ? required : void 0,
|
|
660
|
-
type: "object",
|
|
661
|
-
properties: bodySchema
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
} : void 0,
|
|
666
|
-
responses: Object.keys(operation.responses).length === 0 ? void 0 : operation.responses
|
|
667
|
-
};
|
|
668
|
-
if (!operations[path]) {
|
|
669
|
-
operations[path] = {};
|
|
670
|
-
}
|
|
671
|
-
operations[path][method] = operationObject;
|
|
672
|
-
if (this.#onOperation) {
|
|
673
|
-
const paths = this.#onOperation?.(
|
|
674
|
-
operation.sourceFile,
|
|
675
|
-
method,
|
|
676
|
-
path,
|
|
677
|
-
operationObject
|
|
678
|
-
);
|
|
679
|
-
Object.assign(operations, paths ?? {});
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
return operations;
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* Converts Express/Node.js style path parameters (/path/:param) to OpenAPI style (/path/{param})
|
|
686
|
-
*/
|
|
687
|
-
#tunePath(path) {
|
|
688
|
-
return path.replace(/:([^/]+)/g, "{$1}");
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
async function evalZod(schema, commonZodImport) {
|
|
692
|
-
const lines = [
|
|
693
|
-
`import { createRequire } from "node:module";`,
|
|
694
|
-
`const filename = "${import.meta.url}";`,
|
|
695
|
-
`const require = createRequire(filename);`,
|
|
696
|
-
`const z = require("zod");`,
|
|
697
|
-
commonZodImport ? `const commonZod = require('${commonZodImport}');` : "",
|
|
698
|
-
`const {zodToJsonSchema} = require('zod-to-json-schema');`,
|
|
699
|
-
`const schema = ${schema.replace(".optional()", "")};`,
|
|
700
|
-
`const jsonSchema = zodToJsonSchema(schema, {
|
|
701
|
-
$refStrategy: 'root',
|
|
702
|
-
basePath: ['#', 'components', 'schemas']
|
|
703
|
-
});`,
|
|
704
|
-
`export default jsonSchema;`
|
|
705
|
-
];
|
|
706
|
-
const base64 = Buffer.from(lines.join("\n")).toString("base64");
|
|
707
|
-
return import(`data:text/javascript;base64,${base64}`).then((mod) => mod.default).then(({ $schema, ...result }) => result);
|
|
708
|
-
}
|
|
709
|
-
function toSchema(data) {
|
|
710
|
-
if (data === null || data === void 0) {
|
|
711
|
-
return { type: "any" };
|
|
712
|
-
} else if (typeof data === "string") {
|
|
713
|
-
const isRef = data.startsWith("#");
|
|
714
|
-
if (isRef) {
|
|
715
|
-
return { $ref: data };
|
|
716
|
-
}
|
|
717
|
-
return { type: data };
|
|
718
|
-
} else if (data.kind === "literal") {
|
|
719
|
-
return { enum: [data.value], type: data[$types][0] };
|
|
720
|
-
} else if (data.kind === "record") {
|
|
721
|
-
return {
|
|
722
|
-
type: "object",
|
|
723
|
-
additionalProperties: toSchema(data[$types][0])
|
|
724
|
-
};
|
|
725
|
-
} else if (data.kind === "array") {
|
|
726
|
-
const items = data[$types].map(toSchema);
|
|
727
|
-
return { type: "array", items: data[$types].length ? items[0] : {} };
|
|
728
|
-
} else if (data.kind === "union") {
|
|
729
|
-
return { anyOf: data[$types].map(toSchema) };
|
|
730
|
-
} else if (data.kind === "intersection") {
|
|
731
|
-
return { allOf: data[$types].map(toSchema) };
|
|
732
|
-
} else if ($types in data) {
|
|
733
|
-
return data[$types].map(toSchema)[0] ?? {};
|
|
734
|
-
} else {
|
|
735
|
-
const props = {};
|
|
736
|
-
const required = [];
|
|
737
|
-
for (const [key, value] of Object.entries(data)) {
|
|
738
|
-
props[key] = toSchema(value);
|
|
739
|
-
if (!value.optional) {
|
|
740
|
-
required.push(key);
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
return {
|
|
744
|
-
type: "object",
|
|
745
|
-
properties: props,
|
|
746
|
-
required,
|
|
747
|
-
additionalProperties: false
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
function isHttpMethod(name) {
|
|
752
|
-
return ["get", "post", "put", "delete", "patch"].includes(name);
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
// packages/core/src/lib/program.ts
|
|
756
|
-
import debug from "debug";
|
|
757
|
-
import { dirname as dirname2, join as join2 } from "node:path";
|
|
758
|
-
import ts2 from "typescript";
|
|
759
|
-
var logger = debug("january:client");
|
|
760
|
-
function parseTsConfig(tsconfigPath) {
|
|
761
|
-
logger(`Using TypeScript version: ${ts2.version}`);
|
|
762
|
-
const configContent = ts2.readConfigFile(tsconfigPath, ts2.sys.readFile);
|
|
763
|
-
if (configContent.error) {
|
|
764
|
-
console.error(
|
|
765
|
-
`Failed to read tsconfig file:`,
|
|
766
|
-
ts2.formatDiagnosticsWithColorAndContext([configContent.error], {
|
|
767
|
-
getCanonicalFileName: (path) => path,
|
|
768
|
-
getCurrentDirectory: ts2.sys.getCurrentDirectory,
|
|
769
|
-
getNewLine: () => ts2.sys.newLine
|
|
770
|
-
})
|
|
771
|
-
);
|
|
772
|
-
throw new Error("Failed to parse tsconfig.json");
|
|
773
|
-
}
|
|
774
|
-
const parsed = ts2.parseJsonConfigFileContent(
|
|
775
|
-
configContent.config,
|
|
776
|
-
ts2.sys,
|
|
777
|
-
dirname2(tsconfigPath)
|
|
778
|
-
);
|
|
779
|
-
if (parsed.errors.length > 0) {
|
|
780
|
-
console.error(
|
|
781
|
-
`Errors found in tsconfig.json:`,
|
|
782
|
-
ts2.formatDiagnosticsWithColorAndContext(parsed.errors, {
|
|
783
|
-
getCanonicalFileName: (path) => path,
|
|
784
|
-
getCurrentDirectory: ts2.sys.getCurrentDirectory,
|
|
785
|
-
getNewLine: () => ts2.sys.newLine
|
|
786
|
-
})
|
|
787
|
-
);
|
|
788
|
-
throw new Error("Failed to parse tsconfig.json");
|
|
789
|
-
}
|
|
790
|
-
return parsed;
|
|
791
|
-
}
|
|
792
|
-
function getProgram(tsconfigPath) {
|
|
793
|
-
const tsConfigParseResult = parseTsConfig(tsconfigPath);
|
|
794
|
-
logger(`Parsing tsconfig`);
|
|
795
|
-
return ts2.createProgram({
|
|
796
|
-
options: {
|
|
797
|
-
...tsConfigParseResult.options,
|
|
798
|
-
noEmit: true,
|
|
799
|
-
incremental: true,
|
|
800
|
-
tsBuildInfoFile: join2(dirname2(tsconfigPath), "./.tsbuildinfo")
|
|
801
|
-
// not working atm
|
|
802
|
-
},
|
|
803
|
-
rootNames: tsConfigParseResult.fileNames,
|
|
804
|
-
projectReferences: tsConfigParseResult.projectReferences,
|
|
805
|
-
configFileParsingDiagnostics: tsConfigParseResult.errors
|
|
806
|
-
});
|
|
807
|
-
}
|
|
808
|
-
function getPropertyAssignment(node, name) {
|
|
809
|
-
if (ts2.isObjectLiteralExpression(node)) {
|
|
810
|
-
return node.properties.filter((prop) => ts2.isPropertyAssignment(prop)).find((prop) => prop.name.getText() === name);
|
|
811
|
-
}
|
|
812
|
-
return void 0;
|
|
813
|
-
}
|
|
814
|
-
function isCallExpression(node, name) {
|
|
815
|
-
return ts2.isCallExpression(node) && node.expression && ts2.isIdentifier(node.expression) && node.expression.text === name;
|
|
816
|
-
}
|
|
817
|
-
function isInterfaceType2(type) {
|
|
818
|
-
if (type.isClassOrInterface()) {
|
|
819
|
-
return !!(type.symbol.flags & ts2.SymbolFlags.Interface);
|
|
820
|
-
}
|
|
821
|
-
return false;
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
// packages/core/src/index.ts
|
|
825
|
-
function removeDuplicates(data, accessor) {
|
|
826
|
-
return [...new Map(data.map((x) => [accessor(x), x])).values()];
|
|
827
|
-
}
|
|
828
|
-
function toLitObject(obj, accessor = (value) => value) {
|
|
829
|
-
return `{${Object.keys(obj).map((key) => `${key}: ${accessor(obj[key])}`).join(", ")}}`;
|
|
830
|
-
}
|
|
831
|
-
export {
|
|
832
|
-
$types,
|
|
833
|
-
Paths,
|
|
834
|
-
TypeDeriver,
|
|
835
|
-
deriveSymbol,
|
|
836
|
-
exist,
|
|
837
|
-
getExt,
|
|
838
|
-
getFile,
|
|
839
|
-
getFolderExports,
|
|
840
|
-
getProgram,
|
|
841
|
-
getPropertyAssignment,
|
|
842
|
-
isCallExpression,
|
|
843
|
-
isHttpMethod,
|
|
844
|
-
isInterfaceType2 as isInterfaceType,
|
|
845
|
-
methods,
|
|
846
|
-
parseTsConfig,
|
|
847
|
-
readFolder,
|
|
848
|
-
removeDuplicates,
|
|
849
|
-
toLitObject,
|
|
850
|
-
toSchema,
|
|
851
|
-
writeFiles
|
|
852
|
-
};
|
|
853
|
-
//# sourceMappingURL=index.js.map
|