|
1
|
import ts from "typescript" |
|
2
|
import { OpenApiSchema } from "../../types/index.js" |
|
3
|
|
|
4
|
type SchemaMap = Record< |
|
5
|
string, |
|
6
|
OpenApiSchema | ((type: ts.Type) => OpenApiSchema) |
|
7
|
> |
|
8
|
|
|
9
|
/** |
|
10
|
* This class has predefined OAS schemas for some types. It's used to bypass |
|
11
|
* the logic of creating a schema for certain types. |
|
12
|
*/ |
|
13
|
class SchemaFactory { |
|
14
|
private checker: ts.TypeChecker |
|
15
|
/** |
|
16
|
* The pre-defined schemas. |
|
17
|
*/ |
|
18
|
private schemas: SchemaMap = { |
|
19
|
$and: { |
|
20
|
type: "array", |
|
21
|
description: |
|
22
|
"Join query parameters with an AND condition. Each object's content is the same type as the expected query parameters.", |
|
23
|
items: { |
|
24
|
type: "object", |
|
25
|
}, |
|
26
|
}, |
|
27
|
$or: { |
|
28
|
type: "array", |
|
29
|
description: |
|
30
|
"Join query parameters with an OR condition. Each object's content is the same type as the expected query parameters.", |
|
31
|
items: { |
|
32
|
type: "object", |
|
33
|
}, |
|
34
|
}, |
|
35
|
BigNumberInput: { |
|
36
|
type: "string", |
|
37
|
}, |
|
38
|
BigNumber: { |
|
39
|
type: "string", |
|
40
|
}, |
|
41
|
IBigNumber: { |
|
42
|
type: "number", |
|
43
|
}, |
|
44
|
BigNumberValue: { |
|
45
|
type: "number", |
|
46
|
}, |
|
47
|
difference_due: { |
|
48
|
type: "number", |
|
49
|
}, |
|
50
|
refund_amount: { |
|
51
|
type: "number", |
|
52
|
}, |
|
53
|
File: { |
|
54
|
type: "object", |
|
55
|
description: "A File to upload.", |
|
56
|
externalDocs: { |
|
57
|
url: "https://developer.mozilla.org/en-US/docs/Web/API/File", |
|
58
|
description: "Learn more about the File API", |
|
59
|
}, |
|
60
|
}, |
|
61
|
FileList: { |
|
62
|
type: "array", |
|
63
|
description: "list of files to upload.", |
|
64
|
items: { |
|
65
|
type: "object", |
|
66
|
description: "A File to upload.", |
|
67
|
externalDocs: { |
|
68
|
url: "https://developer.mozilla.org/en-US/docs/Web/API/File", |
|
69
|
description: "Learn more about the File API", |
|
70
|
}, |
|
71
|
}, |
|
72
|
}, |
|
73
|
} |
|
74
|
|
|
75
|
/** |
|
76
|
* Schemas used only for query types |
|
77
|
*/ |
|
78
|
private schemasForQuery: SchemaMap = { |
|
79
|
expand: { |
|
80
|
type: "string", |
|
81
|
title: "expand", |
|
82
|
description: |
|
83
|
"Comma-separated relations that should be expanded in the returned data.", |
|
84
|
}, |
|
85
|
fields: { |
|
86
|
type: "string", |
|
87
|
title: "fields", |
|
88
|
description: |
|
89
|
"Comma-separated fields that should be included in the returned data. If a field is prefixed with `+` it will be added to the default fields, using `-` will remove it from the default fields. Without prefix it will replace the entire default fields.", |
|
90
|
externalDocs: { |
|
91
|
url: "#select-fields-and-relations", |
|
92
|
}, |
|
93
|
}, |
|
94
|
offset: { |
|
95
|
type: "number", |
|
96
|
title: "offset", |
|
97
|
description: "The number of items to skip when retrieving a list.", |
|
98
|
externalDocs: { |
|
99
|
url: "#pagination", |
|
100
|
}, |
|
101
|
}, |
|
102
|
limit: { |
|
103
|
type: "number", |
|
104
|
title: "limit", |
|
105
|
description: "Limit the number of items returned in the list.", |
|
106
|
externalDocs: { |
|
107
|
url: "#pagination", |
|
108
|
}, |
|
109
|
}, |
|
110
|
order: { |
|
111
|
type: "string", |
|
112
|
title: "order", |
|
113
|
description: |
|
114
|
"The field to sort the data by. By default, the sort order is ascending. To change the order to descending, prefix the field name with `-`.", |
|
115
|
externalDocs: { |
|
116
|
url: "#pagination", |
|
117
|
}, |
|
118
|
}, |
|
119
|
metadata: { |
|
120
|
type: "object", |
|
121
|
title: "metadata", |
|
122
|
description: "Holds custom key-value pairs.", |
|
123
|
externalDocs: { |
|
124
|
url: "https://docs.medusajs.com/api/store/manage-metadata", |
|
125
|
description: "Learn how to manage metadata", |
|
126
|
}, |
|
127
|
}, |
|
128
|
OperatorMap: (type) => { |
|
129
|
if (!("typeArguments" in type || "aliasTypeArguments" in type)) { |
|
130
|
return {} |
|
131
|
} |
|
132
|
const typeRef = type as ts.TypeReference |
|
133
|
const typeArgs = typeRef.typeArguments || typeRef.aliasTypeArguments |
|
134
|
if (!typeArgs || typeArgs.length === 0) { |
|
135
|
return {} |
|
136
|
} |
|
137
|
const typeStr = this.checker.typeToString(typeArgs[0]) |
|
138
|
return { |
|
139
|
type: "object", |
|
140
|
properties: { |
|
141
|
$and: JSON.parse(JSON.stringify(this.schemas["$and"])), |
|
142
|
$or: JSON.parse(JSON.stringify(this.schemas["$or"])), |
|
143
|
$eq: { |
|
144
|
oneOf: [ |
|
145
|
{ |
|
146
|
type: typeStr, |
|
147
|
title: "$eq", |
|
148
|
description: "Filter by exact value.", |
|
149
|
}, |
|
150
|
{ |
|
151
|
type: "array", |
|
152
|
title: "$eq", |
|
153
|
description: "Filter by exact value.", |
|
154
|
items: { |
|
155
|
type: typeStr, |
|
156
|
}, |
|
157
|
}, |
|
158
|
], |
|
159
|
}, |
|
160
|
$ne: { |
|
161
|
type: typeStr, |
|
162
|
title: "$ne", |
|
163
|
description: "Filter by not equal to the given value.", |
|
164
|
}, |
|
165
|
$in: { |
|
166
|
type: "array", |
|
167
|
title: "$in", |
|
168
|
description: "Filter by values included in the given array.", |
|
169
|
items: { |
|
170
|
type: typeStr, |
|
171
|
}, |
|
172
|
}, |
|
173
|
$nin: { |
|
174
|
type: "array", |
|
175
|
title: "$nin", |
|
176
|
description: "Filter by values not included in the given array.", |
|
177
|
items: { |
|
178
|
type: typeStr, |
|
179
|
}, |
|
180
|
}, |
|
181
|
$not: { |
|
182
|
oneOf: [ |
|
183
|
{ |
|
184
|
type: typeStr, |
|
185
|
title: "$not", |
|
186
|
description: "Filter by not equal to the given value.", |
|
187
|
}, |
|
188
|
{ |
|
189
|
type: "object", |
|
190
|
title: "$not", |
|
191
|
description: |
|
192
|
"Filter by values not matching the conditions in this parameter.", |
|
193
|
}, |
|
194
|
{ |
|
195
|
type: "array", |
|
196
|
title: "$not", |
|
197
|
description: |
|
198
|
"Filter by values not matching the conditions in this parameter.", |
|
199
|
items: { |
|
200
|
type: typeStr, |
|
201
|
}, |
|
202
|
}, |
|
203
|
], |
|
204
|
}, |
|
205
|
$gt: { |
|
206
|
type: typeStr, |
|
207
|
title: "$gt", |
|
208
|
description: "Filter by values greater than the given value.", |
|
209
|
}, |
|
210
|
$gte: { |
|
211
|
type: typeStr, |
|
212
|
title: "$gte", |
|
213
|
description: |
|
214
|
"Filter by values greater than or equal to the given value.", |
|
215
|
}, |
|
216
|
$lt: { |
|
217
|
type: typeStr, |
|
218
|
title: "$lt", |
|
219
|
description: "Filter by values less than the given value.", |
|
220
|
}, |
|
221
|
$lte: { |
|
222
|
type: typeStr, |
|
223
|
title: "$lte", |
|
224
|
description: |
|
225
|
"Filter by values less than or equal to the given value.", |
|
226
|
}, |
|
227
|
$like: { |
|
228
|
type: typeStr, |
|
229
|
title: "$like", |
|
230
|
description: "Apply a `like` filter. Useful for strings only.", |
|
231
|
}, |
|
232
|
$re: { |
|
233
|
type: typeStr, |
|
234
|
title: "$re", |
|
235
|
description: "Apply a regex filter. Useful for strings only.", |
|
236
|
}, |
|
237
|
$ilike: { |
|
238
|
type: typeStr, |
|
239
|
title: "$ilike", |
|
240
|
description: |
|
241
|
"Apply a case-insensitive `like` filter. Useful for strings only.", |
|
242
|
}, |
|
243
|
$fulltext: { |
|
244
|
type: typeStr, |
|
245
|
title: "$fulltext", |
|
246
|
description: "Filter to apply on full-text properties.", |
|
247
|
}, |
|
248
|
$overlap: { |
|
249
|
type: "array", |
|
250
|
title: "$overlap", |
|
251
|
description: |
|
252
|
"Filter to apply on array properties to find overlapping values.", |
|
253
|
items: { |
|
254
|
type: typeStr, |
|
255
|
}, |
|
256
|
}, |
|
257
|
$contains: { |
|
258
|
type: "array", |
|
259
|
title: "$contains", |
|
260
|
description: |
|
261
|
"Filter to apply on array properties to find contained values.", |
|
262
|
items: { |
|
263
|
type: typeStr, |
|
264
|
}, |
|
265
|
}, |
|
266
|
$contained: { |
|
267
|
type: "array", |
|
268
|
title: "$contained", |
|
269
|
description: |
|
270
|
"Filter to apply on array properties to find contained values.", |
|
271
|
items: { |
|
272
|
type: typeStr, |
|
273
|
}, |
|
274
|
}, |
|
275
|
$exists: { |
|
276
|
type: "boolean", |
|
277
|
title: "$exists", |
|
278
|
description: "Filter by whether a value exists or not.", |
|
279
|
}, |
|
280
|
}, |
|
281
|
} as OpenApiSchema |
|
282
|
}, |
|
283
|
} |
|
284
|
|
|
285
|
/** |
|
286
|
* Schemas used only for response types. |
|
287
|
*/ |
|
288
|
private schemasForResponse: SchemaMap = { |
|
289
|
created_at: { |
|
290
|
type: "string", |
|
291
|
format: "date-time", |
|
292
|
}, |
|
293
|
updated_at: { |
|
294
|
type: "string", |
|
295
|
format: "date-time", |
|
296
|
}, |
|
297
|
deleted_at: { |
|
298
|
type: "string", |
|
299
|
format: "date-time", |
|
300
|
}, |
|
301
|
} |
|
302
|
|
|
303
|
constructor({ checker }: { checker: ts.TypeChecker }) { |
|
304
|
this.checker = checker |
|
305
|
} |
|
306
|
|
|
307
|
/** |
|
308
|
* Try to retrieve the pre-defined schema of a type name. |
|
309
|
* |
|
310
|
* @param name - the name of the type. |
|
311
|
* @param additionalData - Additional data to pass along/override in the predefined schema. For example, a description. |
|
312
|
* @returns The schema, if found. |
|
313
|
*/ |
|
314
|
public tryGetSchema({ |
|
315
|
name, |
|
316
|
additionalData, |
|
317
|
context = "all", |
|
318
|
type, |
|
319
|
}: { |
|
320
|
name: string |
|
321
|
additionalData?: Partial<OpenApiSchema> |
|
322
|
context?: "request" | "query" | "response" | "all" |
|
323
|
type?: ts.Type |
|
324
|
}): OpenApiSchema | undefined { |
|
325
|
const schemasFactory = |
|
326
|
context === "response" |
|
327
|
? this.mergeSchemas(this.schemasForResponse, this.schemas) |
|
328
|
: context === "query" |
|
329
|
? this.mergeSchemas(this.schemasForQuery, this.schemas) |
|
330
|
: this.mergeSchemas(this.schemas) |
|
331
|
const key = Object.hasOwn(schemasFactory, name) |
|
332
|
? name |
|
333
|
: additionalData?.title || "" |
|
334
|
if (!Object.hasOwn(schemasFactory, key)) { |
|
335
|
return |
|
336
|
} |
|
337
|
|
|
338
|
let schema: OpenApiSchema | undefined |
|
339
|
|
|
340
|
if (typeof schemasFactory[key] === "function") { |
|
341
|
if (!type) { |
|
342
|
return |
|
343
|
} |
|
344
|
|
|
345
|
schema = schemasFactory[key](type) |
|
346
|
} else { |
|
347
|
schema = Object.assign({}, schemasFactory[key]) |
|
348
|
} |
|
349
|
|
|
350
|
if (additionalData) { |
|
351
|
schema = Object.assign(schema, { |
|
352
|
...additionalData, |
|
353
|
// keep the description |
|
354
|
description: schema.description || additionalData.description, |
|
355
|
}) |
|
356
|
} |
|
357
|
|
|
358
|
return schema |
|
359
|
} |
|
360
|
|
|
361
|
private mergeSchemas(...schemas: SchemaMap[]): SchemaMap { |
|
362
|
return schemas.reduce((merged, schema) => { |
|
363
|
Object.entries(schema).forEach(([key, value]) => { |
|
364
|
merged[key] = |
|
365
|
typeof value === "function" |
|
366
|
? value |
|
367
|
: JSON.parse(JSON.stringify(value)) |
|
368
|
}) |
|
369
|
return merged |
|
370
|
}, {} as SchemaMap) |
|
371
|
} |
|
372
|
} |
|
373
|
|
|
374
|
export default SchemaFactory |