|
1
|
import ts from "typescript" |
|
2
|
import { |
|
3
|
API_ROUTE_PARAM_REGEX, |
|
4
|
DOCBLOCK_NEW_LINE, |
|
5
|
SUMMARY_PLACEHOLDER, |
|
6
|
} from "../../constants.js" |
|
7
|
import pluralize from "pluralize" |
|
8
|
import { |
|
9
|
camelToTitle, |
|
10
|
camelToWords, |
|
11
|
kebabToTitle, |
|
12
|
snakeToWords, |
|
13
|
wordsToKebab, |
|
14
|
} from "utils" |
|
15
|
import { normalizeName } from "../../utils/str-formatting.js" |
|
16
|
|
|
17
|
const singular = pluralize.singular |
|
18
|
|
|
19
|
type TemplateOptions = { |
|
20
|
pluralIndicatorStr?: string |
|
21
|
parentName?: string |
|
22
|
rawParentName?: string |
|
23
|
returnTypeName?: string |
|
24
|
} |
|
25
|
|
|
26
|
type KnowledgeBase = { |
|
27
|
startsWith?: string |
|
28
|
endsWith?: string |
|
29
|
exact?: string |
|
30
|
pattern?: RegExp |
|
31
|
template: |
|
32
|
| string |
|
33
|
| ((str: string, options?: TemplateOptions) => string | undefined) |
|
34
|
kind?: ts.SyntaxKind[] |
|
35
|
} |
|
36
|
|
|
37
|
export type RetrieveOptions = { |
|
38
|
/** |
|
39
|
* A name that can be of a function, type, etc... |
|
40
|
*/ |
|
41
|
str: string |
|
42
|
/** |
|
43
|
* Options to pass to the `template` function of a |
|
44
|
* knowledge base item. |
|
45
|
*/ |
|
46
|
templateOptions?: TemplateOptions |
|
47
|
/** |
|
48
|
* The kind of the associated node. |
|
49
|
*/ |
|
50
|
kind?: ts.SyntaxKind |
|
51
|
} |
|
52
|
|
|
53
|
type RetrieveSymbolOptions = Omit<RetrieveOptions, "str"> & { |
|
54
|
/** |
|
55
|
* The symbol to retrieve the item from the knowledge base. |
|
56
|
*/ |
|
57
|
symbol: ts.Symbol |
|
58
|
} |
|
59
|
|
|
60
|
/** |
|
61
|
* A class that holds common Medusa patterns and acts as a knowledge base for possible summaries/examples/general templates. |
|
62
|
*/ |
|
63
|
class KnowledgeBaseFactory { |
|
64
|
private TYPE_PLACEHOLDER = `{type name}` |
|
65
|
private summaryKnowledgeBase: KnowledgeBase[] = [ |
|
66
|
{ |
|
67
|
startsWith: "FindConfig", |
|
68
|
template: (str) => { |
|
69
|
const typeArgs = str |
|
70
|
.replace("FindConfig<", "") |
|
71
|
.replace(/>$/, "") |
|
72
|
.split(",") |
|
73
|
.map((part) => camelToWords(normalizeName(part.trim()))) |
|
74
|
const typeName = |
|
75
|
typeArgs.length > 0 && typeArgs[0].length > 0 |
|
76
|
? typeArgs[0] |
|
77
|
: this.TYPE_PLACEHOLDER |
|
78
|
return `The configurations determining how the ${typeName} is retrieved. Its properties, such as \`select\` or \`relations\`, accept the ${DOCBLOCK_NEW_LINE}attributes or relations associated with a ${typeName}.` |
|
79
|
}, |
|
80
|
}, |
|
81
|
{ |
|
82
|
startsWith: "Filterable", |
|
83
|
endsWith: "Props", |
|
84
|
template: (str) => { |
|
85
|
return `The filters to apply on the retrieved ${camelToWords( |
|
86
|
normalizeName(str) |
|
87
|
)}s.` |
|
88
|
}, |
|
89
|
}, |
|
90
|
{ |
|
91
|
startsWith: "Create", |
|
92
|
endsWith: "DTO", |
|
93
|
template: (str, options) => { |
|
94
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
95
|
return `The ${camelToWords(normalizeName(str))}${ |
|
96
|
isPlural ? "s" : "" |
|
97
|
} to be created.` |
|
98
|
}, |
|
99
|
}, |
|
100
|
{ |
|
101
|
startsWith: "Update", |
|
102
|
endsWith: "DTO", |
|
103
|
template: (str, options) => { |
|
104
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
105
|
return `The attributes to update in the ${camelToWords( |
|
106
|
normalizeName(str) |
|
107
|
)}${isPlural ? "s" : ""}.` |
|
108
|
}, |
|
109
|
}, |
|
110
|
{ |
|
111
|
endsWith: "UpdatableFields", |
|
112
|
template: (str, options) => { |
|
113
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
114
|
return `The attributes to update in the ${camelToWords( |
|
115
|
normalizeName(str) |
|
116
|
)}${isPlural ? "s" : ""}.` |
|
117
|
}, |
|
118
|
}, |
|
119
|
{ |
|
120
|
startsWith: "Upsert", |
|
121
|
endsWith: "DTO", |
|
122
|
template: (str, options) => { |
|
123
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
124
|
return `The attributes in the ${camelToWords(normalizeName(str))}${ |
|
125
|
isPlural ? "s" : "" |
|
126
|
} to be created or updated.` |
|
127
|
}, |
|
128
|
}, |
|
129
|
{ |
|
130
|
startsWith: "RestoreReturn", |
|
131
|
template: (_str, options) => { |
|
132
|
return this.replaceTypePlaceholder( |
|
133
|
`Configurations determining which relations to restore along with each of the ${this.TYPE_PLACEHOLDER}. You can pass to its \`returnLinkableKeys\` ${DOCBLOCK_NEW_LINE}property any of the ${this.TYPE_PLACEHOLDER}'s relation attribute names, such as \`{type relation name}\`.`, |
|
134
|
options |
|
135
|
) |
|
136
|
}, |
|
137
|
}, |
|
138
|
{ |
|
139
|
endsWith: "DTO", |
|
140
|
template: (str, options) => { |
|
141
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
142
|
return `The ${camelToWords(normalizeName(str))}${ |
|
143
|
isPlural ? "s" : "" |
|
144
|
} details.` |
|
145
|
}, |
|
146
|
}, |
|
147
|
{ |
|
148
|
endsWith: "_id", |
|
149
|
template: (str: string): string => { |
|
150
|
const formatted = str.replace(/_id$/, "").split("_").join(" ") |
|
151
|
|
|
152
|
return `The associated ${formatted}'s ID.` |
|
153
|
}, |
|
154
|
kind: [ts.SyntaxKind.PropertySignature], |
|
155
|
}, |
|
156
|
{ |
|
157
|
endsWith: "Id", |
|
158
|
template: (str: string): string => { |
|
159
|
const formatted = camelToWords(str.replace(/Id$/, "")) |
|
160
|
|
|
161
|
return `The ${formatted}'s ID.` |
|
162
|
}, |
|
163
|
kind: [ |
|
164
|
ts.SyntaxKind.PropertySignature, |
|
165
|
ts.SyntaxKind.PropertyDeclaration, |
|
166
|
ts.SyntaxKind.Parameter, |
|
167
|
], |
|
168
|
}, |
|
169
|
{ |
|
170
|
exact: "id", |
|
171
|
template: (str, options) => { |
|
172
|
if (options?.rawParentName?.startsWith("Filterable")) { |
|
173
|
return `The IDs to filter the ${options?.parentName || `{name}`}s by.` |
|
174
|
} |
|
175
|
const parentName = options?.parentName |
|
176
|
? options.parentName |
|
177
|
: options?.rawParentName |
|
178
|
? camelToWords(normalizeName(options.rawParentName)) |
|
179
|
: `{name}` |
|
180
|
return `The ID of the ${parentName}.` |
|
181
|
}, |
|
182
|
kind: [ts.SyntaxKind.PropertySignature], |
|
183
|
}, |
|
184
|
{ |
|
185
|
exact: "ids", |
|
186
|
template: (str, options) => { |
|
187
|
if (options?.rawParentName?.startsWith("Filterable")) { |
|
188
|
return `The IDs to filter the ${options?.parentName || `{name}`} by.` |
|
189
|
} |
|
190
|
const parentName = options?.parentName |
|
191
|
? options.parentName |
|
192
|
: options?.rawParentName |
|
193
|
? camelToWords(normalizeName(options.rawParentName)) |
|
194
|
: `{name}` |
|
195
|
return `The IDs of the ${parentName}.` |
|
196
|
}, |
|
197
|
kind: [ts.SyntaxKind.PropertySignature], |
|
198
|
}, |
|
199
|
{ |
|
200
|
exact: "metadata", |
|
201
|
template: "Holds custom data in key-value pairs.", |
|
202
|
kind: [ts.SyntaxKind.PropertySignature], |
|
203
|
}, |
|
204
|
{ |
|
205
|
exact: "customHeaders", |
|
206
|
template: "Custom headers to attach to the request.", |
|
207
|
}, |
|
208
|
{ |
|
209
|
startsWith: "I", |
|
210
|
endsWith: "ModuleService", |
|
211
|
template: (str) => { |
|
212
|
const normalizedStr = camelToTitle(normalizeName(str)) |
|
213
|
|
|
214
|
return `The main service interface for the ${normalizedStr} Module.` |
|
215
|
}, |
|
216
|
}, |
|
217
|
{ |
|
218
|
exact: "$eq", |
|
219
|
template: () => { |
|
220
|
return `Find records whose property exactly matches this value.` |
|
221
|
}, |
|
222
|
kind: [ts.SyntaxKind.PropertySignature], |
|
223
|
}, |
|
224
|
{ |
|
225
|
exact: "$ne", |
|
226
|
template: () => { |
|
227
|
return `Find records whose property doesn't matches this value.` |
|
228
|
}, |
|
229
|
kind: [ts.SyntaxKind.PropertySignature], |
|
230
|
}, |
|
231
|
{ |
|
232
|
exact: "$in", |
|
233
|
template: () => { |
|
234
|
return `Find records whose property is within the specified values.` |
|
235
|
}, |
|
236
|
kind: [ts.SyntaxKind.PropertySignature], |
|
237
|
}, |
|
238
|
{ |
|
239
|
exact: "$nin", |
|
240
|
template: () => { |
|
241
|
return `Find records whose property isn't within the specified values.` |
|
242
|
}, |
|
243
|
kind: [ts.SyntaxKind.PropertySignature], |
|
244
|
}, |
|
245
|
{ |
|
246
|
exact: "$like", |
|
247
|
template: () => { |
|
248
|
return `Find records whose property satisfies this like filter. For example, \`My%\`.` |
|
249
|
}, |
|
250
|
kind: [ts.SyntaxKind.PropertySignature], |
|
251
|
}, |
|
252
|
{ |
|
253
|
exact: "$ilike", |
|
254
|
template: () => { |
|
255
|
return `Find records whose property satisfies this [ilike filter](https://www.postgresql.org/docs/current/functions-matching.html). For example, \`My%\`.` |
|
256
|
}, |
|
257
|
kind: [ts.SyntaxKind.PropertySignature], |
|
258
|
}, |
|
259
|
{ |
|
260
|
exact: "$re", |
|
261
|
template: () => { |
|
262
|
return `Find records whose property matches this regular expression pattern.` |
|
263
|
}, |
|
264
|
kind: [ts.SyntaxKind.PropertySignature], |
|
265
|
}, |
|
266
|
{ |
|
267
|
exact: "$contains", |
|
268
|
template: () => { |
|
269
|
return `Find records whose property is an array that has one or more items.` |
|
270
|
}, |
|
271
|
kind: [ts.SyntaxKind.PropertySignature], |
|
272
|
}, |
|
273
|
{ |
|
274
|
exact: "$gt", |
|
275
|
template: () => { |
|
276
|
return `Find records whose property's value is greater than the specified number or date.` |
|
277
|
}, |
|
278
|
kind: [ts.SyntaxKind.PropertySignature], |
|
279
|
}, |
|
280
|
{ |
|
281
|
exact: "$gte", |
|
282
|
template: () => { |
|
283
|
return `Find records whose property's value is greater than or equal to the specified number or date.` |
|
284
|
}, |
|
285
|
kind: [ts.SyntaxKind.PropertySignature], |
|
286
|
}, |
|
287
|
{ |
|
288
|
exact: "$lt", |
|
289
|
template: () => { |
|
290
|
return `Find records whose property's value is less than the specified number or date.` |
|
291
|
}, |
|
292
|
kind: [ts.SyntaxKind.PropertySignature], |
|
293
|
}, |
|
294
|
{ |
|
295
|
exact: "$lte", |
|
296
|
template: () => { |
|
297
|
return `Find records whose property's value is less than or equal to the specified number or date.` |
|
298
|
}, |
|
299
|
kind: [ts.SyntaxKind.PropertySignature], |
|
300
|
}, |
|
301
|
] |
|
302
|
private functionSummaryKnowledgeBase: KnowledgeBase[] = [ |
|
303
|
{ |
|
304
|
startsWith: "listAndCount", |
|
305
|
template: (str) => { |
|
306
|
const { pluralName } = this.getPluralConfigForFunction({ |
|
307
|
str, |
|
308
|
replacement: "listAndCount", |
|
309
|
}) |
|
310
|
return `retrieves a paginated list of ${pluralName} along with the total count of available ${pluralName} satisfying the provided filters.` |
|
311
|
}, |
|
312
|
}, |
|
313
|
{ |
|
314
|
startsWith: "list", |
|
315
|
template: (str) => { |
|
316
|
const { pluralName } = this.getPluralConfigForFunction({ |
|
317
|
str, |
|
318
|
replacement: "list", |
|
319
|
}) |
|
320
|
return `retrieves a paginated list of ${pluralName} based on optional filters and configuration.` |
|
321
|
}, |
|
322
|
}, |
|
323
|
{ |
|
324
|
startsWith: "retrieve", |
|
325
|
template: (str) => { |
|
326
|
const { singularName } = this.getPluralConfigForFunction({ |
|
327
|
str, |
|
328
|
replacement: "retrieve", |
|
329
|
}) |
|
330
|
return `retrieves a ${singularName} by its ID.` |
|
331
|
}, |
|
332
|
}, |
|
333
|
{ |
|
334
|
startsWith: "create", |
|
335
|
template: (str, options) => { |
|
336
|
const { article, isPlural, singularName, pluralName } = |
|
337
|
this.getPluralConfigForFunction({ |
|
338
|
str, |
|
339
|
replacement: "create", |
|
340
|
options, |
|
341
|
}) |
|
342
|
return `creates${article} ${isPlural ? pluralName : singularName}.` |
|
343
|
}, |
|
344
|
}, |
|
345
|
{ |
|
346
|
startsWith: "delete", |
|
347
|
template: (str, options) => { |
|
348
|
const { article, isPlural, pronoun, singularName, pluralName } = |
|
349
|
this.getPluralConfigForFunction({ |
|
350
|
str, |
|
351
|
replacement: "delete", |
|
352
|
options, |
|
353
|
}) |
|
354
|
return `deletes${article} ${ |
|
355
|
isPlural ? pluralName : singularName |
|
356
|
} by ${pronoun} ID${isPlural ? "s" : ""}.` |
|
357
|
}, |
|
358
|
}, |
|
359
|
{ |
|
360
|
startsWith: "update", |
|
361
|
template: (str, options) => { |
|
362
|
const { article, isPlural, singularName, pluralName } = |
|
363
|
this.getPluralConfigForFunction({ |
|
364
|
str, |
|
365
|
replacement: "update", |
|
366
|
options, |
|
367
|
}) |
|
368
|
return `updates${article} existing ${ |
|
369
|
isPlural ? pluralName : singularName |
|
370
|
}.` |
|
371
|
}, |
|
372
|
}, |
|
373
|
{ |
|
374
|
startsWith: "softDelete", |
|
375
|
template: (str, options) => { |
|
376
|
const { article, pronoun, isPlural, singularName, pluralName } = |
|
377
|
this.getPluralConfigForFunction({ |
|
378
|
str, |
|
379
|
replacement: "softDelete", |
|
380
|
options, |
|
381
|
}) |
|
382
|
return `soft deletes${article} ${ |
|
383
|
isPlural ? pluralName : singularName |
|
384
|
} by ${pronoun} IDs.` |
|
385
|
}, |
|
386
|
}, |
|
387
|
{ |
|
388
|
startsWith: "restore", |
|
389
|
template: (str, options) => { |
|
390
|
const { article, pronoun, isPlural, singularName, pluralName } = |
|
391
|
this.getPluralConfigForFunction({ |
|
392
|
str, |
|
393
|
replacement: "restore", |
|
394
|
options, |
|
395
|
}) |
|
396
|
return `restores${article} soft deleted ${ |
|
397
|
isPlural ? pluralName : singularName |
|
398
|
} by ${pronoun} ID${isPlural ? "s" : ""}.` |
|
399
|
}, |
|
400
|
}, |
|
401
|
{ |
|
402
|
startsWith: "upsert", |
|
403
|
template: (str, options) => { |
|
404
|
const { article, isPlural, singularName, pluralName } = |
|
405
|
this.getPluralConfigForFunction({ |
|
406
|
str, |
|
407
|
replacement: "upsert", |
|
408
|
options, |
|
409
|
}) |
|
410
|
return `updates or creates${article} ${ |
|
411
|
isPlural ? pluralName : singularName |
|
412
|
} if ${isPlural ? "they don't" : "it doesn't"} exist.` |
|
413
|
}, |
|
414
|
}, |
|
415
|
] |
|
416
|
private functionReturnKnowledgeBase: KnowledgeBase[] = [ |
|
417
|
{ |
|
418
|
startsWith: "listAndCount", |
|
419
|
template: (str, options) => { |
|
420
|
const { isPlural, singularName, pluralName } = |
|
421
|
this.getPluralConfigForFunction({ |
|
422
|
str, |
|
423
|
replacement: "listAndCount", |
|
424
|
options, |
|
425
|
}) |
|
426
|
return `The list of ${ |
|
427
|
isPlural ? pluralName : singularName |
|
428
|
} along with their total count.` |
|
429
|
}, |
|
430
|
}, |
|
431
|
{ |
|
432
|
startsWith: "list", |
|
433
|
template: (str, options) => { |
|
434
|
const { isPlural, singularName, pluralName } = |
|
435
|
this.getPluralConfigForFunction({ |
|
436
|
str, |
|
437
|
replacement: "list", |
|
438
|
options, |
|
439
|
}) |
|
440
|
return `The list of ${isPlural ? pluralName : singularName}.` |
|
441
|
}, |
|
442
|
}, |
|
443
|
{ |
|
444
|
startsWith: "retrieve", |
|
445
|
template: (str, options) => { |
|
446
|
const { isPlural, singularName, pluralName } = |
|
447
|
this.getPluralConfigForFunction({ |
|
448
|
str, |
|
449
|
replacement: "retrieve", |
|
450
|
options, |
|
451
|
}) |
|
452
|
return `The retrieved ${isPlural ? pluralName : singularName}.` |
|
453
|
}, |
|
454
|
}, |
|
455
|
{ |
|
456
|
startsWith: "create", |
|
457
|
template: (str, options) => { |
|
458
|
const { isPlural, singularName, pluralName } = |
|
459
|
this.getPluralConfigForFunction({ |
|
460
|
str, |
|
461
|
replacement: "create", |
|
462
|
options, |
|
463
|
}) |
|
464
|
return `The created ${isPlural ? pluralName : singularName}.` |
|
465
|
}, |
|
466
|
}, |
|
467
|
{ |
|
468
|
startsWith: "update", |
|
469
|
template: (str, options) => { |
|
470
|
const { isPlural, singularName, pluralName } = |
|
471
|
this.getPluralConfigForFunction({ |
|
472
|
str, |
|
473
|
replacement: "update", |
|
474
|
options, |
|
475
|
}) |
|
476
|
return `The updated ${isPlural ? pluralName : singularName}.` |
|
477
|
}, |
|
478
|
}, |
|
479
|
{ |
|
480
|
startsWith: "upsert", |
|
481
|
template: (str, options) => { |
|
482
|
const { isPlural, singularName, pluralName } = |
|
483
|
this.getPluralConfigForFunction({ |
|
484
|
str, |
|
485
|
replacement: "upsert", |
|
486
|
options, |
|
487
|
}) |
|
488
|
return `The created or updated ${isPlural ? pluralName : singularName}.` |
|
489
|
}, |
|
490
|
}, |
|
491
|
{ |
|
492
|
startsWith: "softDelete", |
|
493
|
template: (str, options) => { |
|
494
|
const { isPlural, singularName, pluralName } = |
|
495
|
this.getPluralConfigForFunction({ |
|
496
|
str, |
|
497
|
replacement: "softDelete", |
|
498
|
options, |
|
499
|
}) |
|
500
|
return `An object whose keys are of the format \`{camel_case_data_model}_id\` and values are arrays of IDs of soft-deleted ${ |
|
501
|
isPlural ? pluralName : singularName |
|
502
|
}` |
|
503
|
}, |
|
504
|
}, |
|
505
|
{ |
|
506
|
startsWith: "restore", |
|
507
|
template: (str, options) => { |
|
508
|
const { isPlural, singularName, pluralName } = |
|
509
|
this.getPluralConfigForFunction({ |
|
510
|
str, |
|
511
|
replacement: "restore", |
|
512
|
options, |
|
513
|
}) |
|
514
|
return `An object whose keys are of the format \`{camel_case_data_model}_id\` and values are arrays of IDs of restored ${ |
|
515
|
isPlural ? pluralName : singularName |
|
516
|
}` |
|
517
|
}, |
|
518
|
}, |
|
519
|
] |
|
520
|
private oasDescriptionKnowledgeBase: KnowledgeBase[] = [ |
|
521
|
{ |
|
522
|
exact: "additional_data", |
|
523
|
template: |
|
524
|
"Pass additional custom data to the API route. This data is passed to the underlying workflow under the `additional_data` parameter.", |
|
525
|
}, |
|
526
|
{ |
|
527
|
exact: "object", |
|
528
|
template: (_, options) => { |
|
529
|
if (!options?.rawParentName?.includes("DeleteResponse")) { |
|
530
|
return |
|
531
|
} |
|
532
|
|
|
533
|
return "The name of the deleted object." |
|
534
|
}, |
|
535
|
}, |
|
536
|
{ |
|
537
|
exact: "deleted", |
|
538
|
template: (_, options) => { |
|
539
|
if ( |
|
540
|
!options?.rawParentName?.includes("DeleteResponse") || |
|
541
|
!options.parentName |
|
542
|
) { |
|
543
|
return |
|
544
|
} |
|
545
|
|
|
546
|
return `Whether the ${singular(options.parentName)} was deleted.` |
|
547
|
}, |
|
548
|
}, |
|
549
|
{ |
|
550
|
exact: "$eq", |
|
551
|
template: `Filter by an exact match.`, |
|
552
|
}, |
|
553
|
{ |
|
554
|
exact: "$ne", |
|
555
|
template: `Filter by values not equal to this parameter.`, |
|
556
|
}, |
|
557
|
{ |
|
558
|
exact: "$in", |
|
559
|
template: `Filter by values in this array.`, |
|
560
|
}, |
|
561
|
{ |
|
562
|
exact: "$nin", |
|
563
|
template: `Filter by values not in this array.`, |
|
564
|
}, |
|
565
|
{ |
|
566
|
exact: "$not", |
|
567
|
template: `Filter by values not matching the conditions in this parameter.`, |
|
568
|
}, |
|
569
|
{ |
|
570
|
exact: "$gt", |
|
571
|
template: `Filter by values greater than this parameter. Useful for numbers and dates only.`, |
|
572
|
}, |
|
573
|
{ |
|
574
|
exact: "$gte", |
|
575
|
template: `Filter by values greater than or equal to this parameter. Useful for numbers and dates only.`, |
|
576
|
}, |
|
577
|
{ |
|
578
|
exact: "$lt", |
|
579
|
template: `Filter by values less than this parameter. Useful for numbers and dates only.`, |
|
580
|
}, |
|
581
|
{ |
|
582
|
exact: "$lte", |
|
583
|
template: `Filter by values less than or equal to this parameter. Useful for numbers and dates only.`, |
|
584
|
}, |
|
585
|
{ |
|
586
|
exact: "$like", |
|
587
|
template: "Apply a `like` filter. Useful for strings only.", |
|
588
|
}, |
|
589
|
{ |
|
590
|
exact: "$re", |
|
591
|
template: "Apply a regex filter. Useful for strings only.", |
|
592
|
}, |
|
593
|
{ |
|
594
|
exact: "$ilike", |
|
595
|
template: |
|
596
|
"Apply a case-insensitive `like` filter. Useful for strings only.", |
|
597
|
}, |
|
598
|
{ |
|
599
|
exact: "$fulltext", |
|
600
|
template: "Filter to apply on full-text properties.", |
|
601
|
}, |
|
602
|
{ |
|
603
|
exact: "$overlap", |
|
604
|
template: |
|
605
|
"Filter arrays that have overlapping values with this parameter.", |
|
606
|
}, |
|
607
|
{ |
|
608
|
exact: "$contains", |
|
609
|
template: |
|
610
|
"Filter arrays that contain some of the values of this parameter.", |
|
611
|
}, |
|
612
|
{ |
|
613
|
exact: "$contained", |
|
614
|
template: "Filter arrays that contain all values of this parameter.", |
|
615
|
}, |
|
616
|
{ |
|
617
|
exact: "$exists", |
|
618
|
template: |
|
619
|
"Filter by whether a value for this parameter exists (not `null`).", |
|
620
|
}, |
|
621
|
{ |
|
622
|
pattern: /.*/, |
|
623
|
template(str, options) { |
|
624
|
if (!options?.parentName) { |
|
625
|
return |
|
626
|
} |
|
627
|
|
|
628
|
const formattedName = str === "id" ? "ID" : snakeToWords(str) |
|
629
|
const formattedParentName = pluralize.singular( |
|
630
|
snakeToWords(options.parentName) |
|
631
|
) |
|
632
|
|
|
633
|
if (formattedName === formattedParentName) { |
|
634
|
return `The ${formattedParentName}'s details.` |
|
635
|
} |
|
636
|
|
|
637
|
return `The ${formattedParentName}'s ${formattedName}.` |
|
638
|
}, |
|
639
|
}, |
|
640
|
] |
|
641
|
|
|
642
|
/** |
|
643
|
* Tries to find in a specified knowledge base a template relevant to the specified name. |
|
644
|
* |
|
645
|
* @returns {string | undefined} The matching knowledge base template, if found. |
|
646
|
*/ |
|
647
|
private tryToFindInKnowledgeBase({ |
|
648
|
str, |
|
649
|
knowledgeBase, |
|
650
|
templateOptions, |
|
651
|
kind, |
|
652
|
}: RetrieveOptions & { |
|
653
|
/** |
|
654
|
* A knowledge base to search in. |
|
655
|
*/ |
|
656
|
knowledgeBase: KnowledgeBase[] |
|
657
|
}): string | undefined { |
|
658
|
const foundItem = knowledgeBase.find((item) => { |
|
659
|
if (item.exact) { |
|
660
|
return str === item.exact |
|
661
|
} |
|
662
|
|
|
663
|
if (item.pattern) { |
|
664
|
return item.pattern.test(str) |
|
665
|
} |
|
666
|
|
|
667
|
if (item.kind?.length && (!kind || !item.kind.includes(kind))) { |
|
668
|
return false |
|
669
|
} |
|
670
|
|
|
671
|
if (item.startsWith && item.endsWith) { |
|
672
|
return str.startsWith(item.startsWith) && str.endsWith(item.endsWith) |
|
673
|
} |
|
674
|
|
|
675
|
if (item.startsWith) { |
|
676
|
return str.startsWith(item.startsWith) |
|
677
|
} |
|
678
|
|
|
679
|
return item.endsWith ? str.endsWith(item.endsWith) : false |
|
680
|
}) |
|
681
|
|
|
682
|
if (!foundItem) { |
|
683
|
return |
|
684
|
} |
|
685
|
|
|
686
|
return typeof foundItem.template === "string" |
|
687
|
? foundItem?.template |
|
688
|
: foundItem?.template(str, templateOptions) |
|
689
|
} |
|
690
|
|
|
691
|
/** |
|
692
|
* This method replaces uses of {@link TYPE_PLACEHOLDER} with the normalized parent name, if provided. |
|
693
|
* |
|
694
|
* @param str - The string to normalize |
|
695
|
* @param options - The template options |
|
696
|
* @returns The normalized string |
|
697
|
*/ |
|
698
|
private replaceTypePlaceholder( |
|
699
|
str: string, |
|
700
|
options?: TemplateOptions |
|
701
|
): string { |
|
702
|
const typeName = options?.rawParentName |
|
703
|
? camelToWords(normalizeName(options.rawParentName)) |
|
704
|
: this.TYPE_PLACEHOLDER |
|
705
|
|
|
706
|
return str.replaceAll(this.TYPE_PLACEHOLDER, typeName) |
|
707
|
} |
|
708
|
|
|
709
|
/** |
|
710
|
* This method retrieves plural configuration for Medusa service's functions. |
|
711
|
* |
|
712
|
* @param param0 - The function's input. |
|
713
|
* @returns The plural configurations. |
|
714
|
*/ |
|
715
|
private getPluralConfigForFunction({ |
|
716
|
str, |
|
717
|
replacement, |
|
718
|
options, |
|
719
|
}: { |
|
720
|
str: string |
|
721
|
replacement: string |
|
722
|
options?: TemplateOptions |
|
723
|
}): { |
|
724
|
isPlural: boolean |
|
725
|
typeName: string |
|
726
|
singularName: string |
|
727
|
pluralName: string |
|
728
|
article: string |
|
729
|
pronoun: string |
|
730
|
} { |
|
731
|
const isPlural = this.isTypePlural(options?.pluralIndicatorStr) |
|
732
|
const typeName = camelToWords(str.replace(replacement, "")) |
|
733
|
const singularName = singular(typeName) |
|
734
|
const pluralName = pluralize(typeName) |
|
735
|
const article = isPlural ? "" : " a" |
|
736
|
const pronoun = isPlural ? "their" : "its" |
|
737
|
|
|
738
|
return { |
|
739
|
isPlural, |
|
740
|
typeName, |
|
741
|
singularName, |
|
742
|
pluralName, |
|
743
|
article, |
|
744
|
pronoun, |
|
745
|
} |
|
746
|
} |
|
747
|
|
|
748
|
/** |
|
749
|
* Checks whether a type should be handled as a plural. Typically used with {@link TemplateOptions.pluralIndicatorStr}. |
|
750
|
* |
|
751
|
* @param str - The type string to check. |
|
752
|
* @returns Whether the type is handled as a plural. |
|
753
|
*/ |
|
754
|
private isTypePlural(str: string | undefined): boolean { |
|
755
|
return str?.endsWith("[]") || false |
|
756
|
} |
|
757
|
|
|
758
|
/** |
|
759
|
* Tries to retrieve the summary template of a specified type from the {@link summaryKnowledgeBase}. |
|
760
|
* |
|
761
|
* @returns {string | undefined} The matching knowledge base template, if found. |
|
762
|
*/ |
|
763
|
tryToGetSummary({ str, ...options }: RetrieveOptions): string | undefined { |
|
764
|
const normalizedTypeStr = str.replaceAll("[]", "") |
|
765
|
return this.tryToFindInKnowledgeBase({ |
|
766
|
...options, |
|
767
|
str: normalizedTypeStr, |
|
768
|
knowledgeBase: this.summaryKnowledgeBase, |
|
769
|
templateOptions: { |
|
770
|
pluralIndicatorStr: str, |
|
771
|
...options.templateOptions, |
|
772
|
}, |
|
773
|
}) |
|
774
|
} |
|
775
|
|
|
776
|
/** |
|
777
|
* Tries to retrieve the summary template of a function's symbol from the {@link functionSummaryKnowledgeBase}. |
|
778
|
* |
|
779
|
* @returns {string | undefined} The matching knowledge base template, if found. |
|
780
|
*/ |
|
781
|
tryToGetFunctionSummary({ |
|
782
|
symbol, |
|
783
|
...options |
|
784
|
}: RetrieveSymbolOptions): string | undefined { |
|
785
|
return this.tryToFindInKnowledgeBase({ |
|
786
|
...options, |
|
787
|
str: symbol.getName(), |
|
788
|
knowledgeBase: this.functionSummaryKnowledgeBase, |
|
789
|
}) |
|
790
|
} |
|
791
|
|
|
792
|
/** |
|
793
|
* Tries to retrieve the return template of a function's symbol from the {@link functionReturnKnowledgeBase}. |
|
794
|
* |
|
795
|
* @returns {string | undefined} The matching knowledge base template, if found. |
|
796
|
*/ |
|
797
|
tryToGetFunctionReturns({ |
|
798
|
symbol, |
|
799
|
...options |
|
800
|
}: RetrieveSymbolOptions): string | undefined { |
|
801
|
return this.tryToFindInKnowledgeBase({ |
|
802
|
...options, |
|
803
|
str: symbol.getName(), |
|
804
|
knowledgeBase: this.functionReturnKnowledgeBase, |
|
805
|
}) |
|
806
|
} |
|
807
|
|
|
808
|
/** |
|
809
|
* Tries to retrieve the description template of an OAS property from the {@link oasDescriptionKnowledgeBase}. |
|
810
|
* |
|
811
|
* @returns {string | undefined} The matching knowledgebase template, if found. |
|
812
|
*/ |
|
813
|
tryToGetOasSchemaDescription({ |
|
814
|
str, |
|
815
|
...options |
|
816
|
}: RetrieveOptions): string | undefined { |
|
817
|
const normalizedTypeStr = str.replaceAll("[]", "") |
|
818
|
return this.tryToFindInKnowledgeBase({ |
|
819
|
...options, |
|
820
|
str: normalizedTypeStr, |
|
821
|
knowledgeBase: this.oasDescriptionKnowledgeBase, |
|
822
|
}) |
|
823
|
} |
|
824
|
/** |
|
825
|
* Retrieve the summary and description of the OAS. |
|
826
|
* |
|
827
|
* @param param0 - The OAS operation's details. |
|
828
|
* @returns The summary and description. |
|
829
|
*/ |
|
830
|
tryToGetOasMethodSummaryAndDescription({ |
|
831
|
oasPath, |
|
832
|
httpMethod, |
|
833
|
tag, |
|
834
|
}: { |
|
835
|
/** |
|
836
|
* The OAS path. |
|
837
|
*/ |
|
838
|
oasPath: string |
|
839
|
/** |
|
840
|
* The HTTP method name. |
|
841
|
*/ |
|
842
|
httpMethod: string |
|
843
|
/** |
|
844
|
* The OAS tag name. |
|
845
|
*/ |
|
846
|
tag: string |
|
847
|
}): { |
|
848
|
/** |
|
849
|
* The OAS's summary |
|
850
|
*/ |
|
851
|
summary: string |
|
852
|
/** |
|
853
|
* The OAS's description. |
|
854
|
*/ |
|
855
|
description: string |
|
856
|
} { |
|
857
|
// reset regex manually |
|
858
|
API_ROUTE_PARAM_REGEX.lastIndex = 0 |
|
859
|
const result = { |
|
860
|
summary: SUMMARY_PLACEHOLDER, |
|
861
|
description: SUMMARY_PLACEHOLDER, |
|
862
|
} |
|
863
|
// retrieve different variations of the tag to include in the summary/description |
|
864
|
const lowerTag = tag.toLowerCase() |
|
865
|
const singularLowerTag = pluralize.singular(lowerTag) |
|
866
|
const singularTag = pluralize.singular(tag) |
|
867
|
|
|
868
|
// check if the OAS operation is performed on a single entity or |
|
869
|
// general entities. If the operation has a path parameter, then it's |
|
870
|
// considered for a single entity. |
|
871
|
const isForSingleEntity = API_ROUTE_PARAM_REGEX.test(oasPath) |
|
872
|
|
|
873
|
if (isForSingleEntity) { |
|
874
|
// Check whether the OAS operation is applied on a different entity. |
|
875
|
// If the OAS path ends with /batch or a different entity |
|
876
|
// name than the tag name, then it's performed on an entity other than the |
|
877
|
// main entity (the one indicated by the tag), so the summary/description vary |
|
878
|
// slightly. |
|
879
|
const splitOasPath = oasPath |
|
880
|
.replaceAll(API_ROUTE_PARAM_REGEX, "") |
|
881
|
.replace(/\/(batch)*$/, "") |
|
882
|
.split("/") |
|
883
|
const isBulk = oasPath.endsWith("/batch") |
|
884
|
const isOperationOnDifferentEntity = |
|
885
|
wordsToKebab(tag) !== splitOasPath[splitOasPath.length - 1] |
|
886
|
|
|
887
|
if (isBulk || isOperationOnDifferentEntity) { |
|
888
|
// if the operation is a bulk operation and it ends with a path parameter (after removing the `/batch` part) |
|
889
|
// then the tag name is the targeted entity. Else, it's the last part of the OAS path (after removing the `/batch` part). |
|
890
|
const endingEntityName = |
|
891
|
isBulk && |
|
892
|
API_ROUTE_PARAM_REGEX.test(splitOasPath[splitOasPath.length - 1]) |
|
893
|
? tag |
|
894
|
: kebabToTitle(splitOasPath[splitOasPath.length - 1]) |
|
895
|
// retrieve different formatted versions of the entity name for the summary/description |
|
896
|
const pluralEndingEntityName = pluralize.plural(endingEntityName) |
|
897
|
const singularEndingEntityName = pluralize.singular(endingEntityName) |
|
898
|
const lowerEndingEntityName = pluralEndingEntityName.toLowerCase() |
|
899
|
const singularLowerEndingEntityName = |
|
900
|
pluralize.singular(endingEntityName) |
|
901
|
|
|
902
|
// set the summary/description based on the HTTP method |
|
903
|
if (httpMethod === "get") { |
|
904
|
result.summary = `List ${pluralEndingEntityName}` |
|
905
|
result.description = `Retrieve a list of ${lowerEndingEntityName} in a ${singularLowerTag}. The ${lowerEndingEntityName} can be filtered by fields like FILTER FIELDS. The ${lowerEndingEntityName} can also be paginated.` |
|
906
|
} else if (httpMethod === "post") { |
|
907
|
result.summary = `Add ${ |
|
908
|
isBulk ? pluralEndingEntityName : singularEndingEntityName |
|
909
|
} to ${singularTag}` |
|
910
|
result.description = isBulk |
|
911
|
? `Add a list of ${lowerEndingEntityName} to a ${singularLowerTag}.` |
|
912
|
: `Add a ${singularLowerEndingEntityName} to a ${singularLowerTag}` |
|
913
|
} else { |
|
914
|
result.summary = `Remove ${ |
|
915
|
isBulk ? pluralEndingEntityName : singularEndingEntityName |
|
916
|
} from ${singularTag}` |
|
917
|
result.description = isBulk |
|
918
|
? `Remove a list of ${lowerEndingEntityName} from a ${singularLowerTag}.` |
|
919
|
: `Remove a ${singularLowerEndingEntityName} from a ${singularLowerTag}.` |
|
920
|
} |
|
921
|
} else { |
|
922
|
// the OAS operation is applied on a single entity that is the main entity (denoted by the tag). |
|
923
|
// retrieve the summary/description based on the HTTP method. |
|
924
|
if (httpMethod === "get") { |
|
925
|
result.summary = `Get a ${singularTag}` |
|
926
|
result.description = `Retrieve a ${singularLowerTag} by its ID. You can expand the ${singularLowerTag}'s relations or select the fields that should be returned.` |
|
927
|
} else if (httpMethod === "post") { |
|
928
|
result.summary = `Update a ${singularTag}` |
|
929
|
result.description = `Update a ${singularLowerTag}'s details.` |
|
930
|
} else { |
|
931
|
result.summary = `Delete a ${singularTag}` |
|
932
|
result.description = `Delete a ${singularLowerTag}.` |
|
933
|
} |
|
934
|
} |
|
935
|
} else { |
|
936
|
// the OAS operation is applied on all entities of the tag in general. |
|
937
|
// retrieve the summary/description based on the HTTP method. |
|
938
|
if (httpMethod === "get") { |
|
939
|
result.summary = `List ${tag}` |
|
940
|
result.description = `Retrieve a list of ${lowerTag}. The ${lowerTag} can be filtered by fields such as \`id\`. The ${lowerTag} can also be sorted or paginated.` |
|
941
|
} else if (httpMethod === "post") { |
|
942
|
result.summary = `Create ${singularTag}` |
|
943
|
result.description = `Create a ${singularLowerTag}.` |
|
944
|
} else { |
|
945
|
result.summary = `Delete ${tag}` |
|
946
|
result.description = `Delete ${tag}` |
|
947
|
} |
|
948
|
} |
|
949
|
|
|
950
|
return result |
|
951
|
} |
|
952
|
|
|
953
|
/** |
|
954
|
* Retrieve summary and description for a property of an object, interface, or type. |
|
955
|
* |
|
956
|
* @param param0 - The property's details. |
|
957
|
* @returns The property's summary. |
|
958
|
*/ |
|
959
|
tryToGetObjectPropertySummary({ |
|
960
|
retrieveOptions, |
|
961
|
propertyDetails: { isClassOrInterface, isBoolean, classOrInterfaceName }, |
|
962
|
}: { |
|
963
|
/** |
|
964
|
* The options used to retrieve the summary from the knowledge base, if available. |
|
965
|
*/ |
|
966
|
retrieveOptions: RetrieveOptions |
|
967
|
/** |
|
968
|
* The details of the property. |
|
969
|
*/ |
|
970
|
propertyDetails: { |
|
971
|
/** |
|
972
|
* Whether the property's value is a class or interface. This applies to all |
|
973
|
* object-like types. |
|
974
|
* |
|
975
|
* If `true`, the property is considered to represent a relationship to another |
|
976
|
* class / interface. |
|
977
|
*/ |
|
978
|
isClassOrInterface: boolean |
|
979
|
/** |
|
980
|
* Whether the property's value is a boolean |
|
981
|
*/ |
|
982
|
isBoolean: boolean |
|
983
|
/** |
|
984
|
* The name of the class / interface this property's value is associated to. |
|
985
|
* This is only used if {@link isClassOrInterface} is `true`. |
|
986
|
*/ |
|
987
|
classOrInterfaceName?: string |
|
988
|
} |
|
989
|
}): string { |
|
990
|
let summary = this.tryToGetSummary(retrieveOptions) |
|
991
|
|
|
992
|
if (summary) { |
|
993
|
return summary |
|
994
|
} |
|
995
|
|
|
996
|
if (isClassOrInterface) { |
|
997
|
summary = `The associated ${classOrInterfaceName}.` |
|
998
|
} else if (isBoolean) { |
|
999
|
summary = `Whether the ${retrieveOptions.templateOptions |
|
1000
|
?.parentName} ${snakeToWords(retrieveOptions.str)}.` |
|
1001
|
} else { |
|
1002
|
summary = `The ${snakeToWords( |
|
1003
|
retrieveOptions.str |
|
1004
|
)} of the ${retrieveOptions.templateOptions?.parentName}` |
|
1005
|
} |
|
1006
|
|
|
1007
|
return summary |
|
1008
|
} |
|
1009
|
} |
|
1010
|
|
|
1011
|
export default KnowledgeBaseFactory |