@visulima/crud 1.0.4 → 1.0.6
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/CHANGELOG.md +21 -0
- package/dist/chunk-JSOEPRPZ.mjs +2 -0
- package/dist/{chunk-SH6A4KBC.mjs.map → chunk-JSOEPRPZ.mjs.map} +1 -1
- package/dist/chunk-U35WXGEF.js +2 -0
- package/dist/chunk-U35WXGEF.js.map +1 -0
- package/dist/index.js +1 -1120
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1120
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.js +1 -457
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +1 -457
- package/dist/next/index.mjs.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-SH6A4KBC.mjs +0 -28
- package/dist/chunk-ZY3WOLEP.js +0 -28
- package/dist/chunk-ZY3WOLEP.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,1121 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var _chunkZY3WOLEPjs = require('./chunk-ZY3WOLEP.js');
|
|
5
|
-
|
|
6
|
-
// src/adapter/prisma/index.ts
|
|
7
|
-
var _httperrors = require('http-errors'); var _httperrors2 = _interopRequireDefault(_httperrors);
|
|
8
|
-
|
|
9
|
-
// src/adapter/prisma/utils/models-to-route-names.ts
|
|
10
|
-
var modelsToRouteNames = (mappingsMap, models) => {
|
|
11
|
-
const routesMap = {};
|
|
12
|
-
models == null ? void 0 : models.forEach((model) => {
|
|
13
|
-
routesMap[model] = mappingsMap[model].plural;
|
|
14
|
-
});
|
|
15
|
-
return routesMap;
|
|
16
|
-
};
|
|
17
|
-
var models_to_route_names_default = modelsToRouteNames;
|
|
18
|
-
|
|
19
|
-
// src/utils/is-primitive.ts
|
|
20
|
-
var primitiveTypes = /* @__PURE__ */ new Set(["string", "boolean", "number"]);
|
|
21
|
-
var isPrimitive = (value) => primitiveTypes.has(typeof value);
|
|
22
|
-
var is_primitive_default = isPrimitive;
|
|
23
|
-
|
|
24
|
-
// src/adapter/prisma/utils/parse-cursor.ts
|
|
25
|
-
var parsePrismaCursor = (cursor) => {
|
|
26
|
-
const parsed = {};
|
|
27
|
-
Object.keys(cursor).forEach((key) => {
|
|
28
|
-
const value = cursor[key];
|
|
29
|
-
if (is_primitive_default(value)) {
|
|
30
|
-
parsed[key] = value;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
if (Object.keys(parsed).length !== 1) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
"cursor needs to be an object with exactly 1 property with a primitive value"
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return parsed;
|
|
39
|
-
};
|
|
40
|
-
var parse_cursor_default = parsePrismaCursor;
|
|
41
|
-
|
|
42
|
-
// src/adapter/prisma/utils/parse-order-by.ts
|
|
43
|
-
var operatorsAssociation = {
|
|
44
|
-
$asc: "asc",
|
|
45
|
-
$desc: "desc"
|
|
46
|
-
};
|
|
47
|
-
var parsePrismaOrderBy = (orderBy) => {
|
|
48
|
-
const parsed = {};
|
|
49
|
-
Object.keys(orderBy).forEach((key) => {
|
|
50
|
-
const value = orderBy[key];
|
|
51
|
-
parsed[key] = operatorsAssociation[value];
|
|
52
|
-
});
|
|
53
|
-
return parsed;
|
|
54
|
-
};
|
|
55
|
-
var parse_order_by_default = parsePrismaOrderBy;
|
|
56
|
-
|
|
57
|
-
// src/adapter/prisma/utils/parse-recursive.ts
|
|
58
|
-
var parsePrismaRecursiveField = (select, fieldName) => {
|
|
59
|
-
const parsed = {};
|
|
60
|
-
Object.keys(select).forEach((field) => {
|
|
61
|
-
parsed[field] = select[field] === true ? true : {
|
|
62
|
-
[fieldName]: parsePrismaRecursiveField(
|
|
63
|
-
select[field],
|
|
64
|
-
fieldName
|
|
65
|
-
)
|
|
66
|
-
};
|
|
67
|
-
});
|
|
68
|
-
return parsed;
|
|
69
|
-
};
|
|
70
|
-
var parse_recursive_default = parsePrismaRecursiveField;
|
|
71
|
-
|
|
72
|
-
// src/adapter/prisma/utils/parse-where.ts
|
|
73
|
-
var isObject = (a) => a instanceof Object;
|
|
74
|
-
var operatorsAssociation2 = {
|
|
75
|
-
$eq: "equals",
|
|
76
|
-
$neq: "not",
|
|
77
|
-
$cont: "contains",
|
|
78
|
-
$ends: "endsWith",
|
|
79
|
-
$gt: "gt",
|
|
80
|
-
$gte: "gte",
|
|
81
|
-
$in: "in",
|
|
82
|
-
$lt: "lt",
|
|
83
|
-
$lte: "lte",
|
|
84
|
-
$notin: "notIn",
|
|
85
|
-
$starts: "startsWith"
|
|
86
|
-
};
|
|
87
|
-
var isDateString = (value) => /^\d{4}-[01]\d-[0-3]\d(?:T[0-2](?:\d:[0-5]){2}\d(?:\.\d+)?(?:Z|[+-][0-2]\d(?::?[0-5]\d)?)?)?$/g.test(value);
|
|
88
|
-
var getSearchValue = (originalValue) => {
|
|
89
|
-
if (isDateString(originalValue)) {
|
|
90
|
-
return new Date(originalValue);
|
|
91
|
-
}
|
|
92
|
-
if (typeof originalValue === "string" && originalValue === "$isnull") {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
return originalValue;
|
|
96
|
-
};
|
|
97
|
-
var isRelation = (key, manyRelations) => {
|
|
98
|
-
const splitKey = key.split(".");
|
|
99
|
-
splitKey.splice(-1, 1);
|
|
100
|
-
return manyRelations.includes(splitKey.join("."));
|
|
101
|
-
};
|
|
102
|
-
var parseSimpleField = (value) => {
|
|
103
|
-
const operator = Object.keys(value)[0];
|
|
104
|
-
const prismaOperator = operatorsAssociation2[operator];
|
|
105
|
-
if (prismaOperator) {
|
|
106
|
-
return {
|
|
107
|
-
[prismaOperator]: value[operator]
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
return void 0;
|
|
111
|
-
};
|
|
112
|
-
var parseRelation = (value, key, parsed, manyRelations) => {
|
|
113
|
-
var _a;
|
|
114
|
-
const fields = key.split(".").reverse();
|
|
115
|
-
let formatFields = {};
|
|
116
|
-
fields.forEach((field, index) => {
|
|
117
|
-
if (index === 0) {
|
|
118
|
-
basicParse(value, field, formatFields, manyRelations);
|
|
119
|
-
} else {
|
|
120
|
-
formatFields = {
|
|
121
|
-
[field]: {
|
|
122
|
-
some: formatFields
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
const initialFieldKey = fields.reverse()[0];
|
|
128
|
-
const oldParsed = parsed[initialFieldKey];
|
|
129
|
-
parsed[initialFieldKey] = {
|
|
130
|
-
some: {
|
|
131
|
-
...oldParsed == null ? void 0 : oldParsed.some,
|
|
132
|
-
...(_a = formatFields[initialFieldKey]) == null ? void 0 : _a.some
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
var parseObjectCombination = (object, manyRelations) => {
|
|
137
|
-
const parsed = {};
|
|
138
|
-
Object.keys(object).forEach((key) => {
|
|
139
|
-
const value = object[key];
|
|
140
|
-
if (isRelation(key, manyRelations)) {
|
|
141
|
-
parseRelation(value, key, parsed, manyRelations);
|
|
142
|
-
} else if (is_primitive_default(value)) {
|
|
143
|
-
parsed[key] = value;
|
|
144
|
-
} else if (isObject(value)) {
|
|
145
|
-
const fieldResult = parseSimpleField(value);
|
|
146
|
-
if (fieldResult) {
|
|
147
|
-
parsed[key] = fieldResult;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
return parsed;
|
|
152
|
-
};
|
|
153
|
-
var basicParse = (value, key, parsed, manyRelations) => {
|
|
154
|
-
if (is_primitive_default(value)) {
|
|
155
|
-
parsed[key] = getSearchValue(value);
|
|
156
|
-
} else {
|
|
157
|
-
switch (key) {
|
|
158
|
-
case "$or": {
|
|
159
|
-
if (isObject(value)) {
|
|
160
|
-
parsed.OR = parseObjectCombination(value, manyRelations);
|
|
161
|
-
}
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
case "$and": {
|
|
165
|
-
if (isObject(value)) {
|
|
166
|
-
parsed.AND = parseObjectCombination(value, manyRelations);
|
|
167
|
-
}
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
case "$not": {
|
|
171
|
-
if (isObject(value)) {
|
|
172
|
-
parsed.NOT = parseObjectCombination(value, manyRelations);
|
|
173
|
-
}
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
default: {
|
|
177
|
-
parsed[key] = parseSimpleField(value);
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
var parsePrismaWhere = (where, manyRelations) => {
|
|
184
|
-
const parsed = {};
|
|
185
|
-
Object.keys(where).forEach((key) => {
|
|
186
|
-
const value = where[key];
|
|
187
|
-
if (isRelation(key, manyRelations)) {
|
|
188
|
-
parseRelation(value, key, parsed, manyRelations);
|
|
189
|
-
} else {
|
|
190
|
-
basicParse(value, key, parsed, manyRelations);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
return parsed;
|
|
194
|
-
};
|
|
195
|
-
var parse_where_default = parsePrismaWhere;
|
|
196
|
-
|
|
197
|
-
// src/adapter/prisma/index.ts
|
|
198
|
-
var PrismaAdapter = class {
|
|
199
|
-
constructor({
|
|
200
|
-
primaryKey = "id",
|
|
201
|
-
prismaClient,
|
|
202
|
-
manyRelations = {},
|
|
203
|
-
models
|
|
204
|
-
}) {
|
|
205
|
-
this.getPrismaClientModels = async () => {
|
|
206
|
-
var _a;
|
|
207
|
-
if (this.prismaClient._dmmf) {
|
|
208
|
-
this.dmmf = this.prismaClient._dmmf;
|
|
209
|
-
return (_a = this.dmmf) == null ? void 0 : _a.mappingsMap;
|
|
210
|
-
}
|
|
211
|
-
if (this.prismaClient._getDmmf) {
|
|
212
|
-
this.dmmf = await this.prismaClient._getDmmf();
|
|
213
|
-
return this.dmmf.mappingsMap;
|
|
214
|
-
}
|
|
215
|
-
throw new Error("Couldn't get prisma client models");
|
|
216
|
-
};
|
|
217
|
-
this.prismaClient = prismaClient;
|
|
218
|
-
this.primaryKey = primaryKey;
|
|
219
|
-
this.manyRelations = manyRelations;
|
|
220
|
-
this.ctorModels = models;
|
|
221
|
-
}
|
|
222
|
-
async init() {
|
|
223
|
-
const models = this.ctorModels;
|
|
224
|
-
const prismaDmmfModels = await this.getPrismaClientModels();
|
|
225
|
-
if (models !== void 0) {
|
|
226
|
-
models.forEach((model) => {
|
|
227
|
-
if (!Object.keys(prismaDmmfModels).includes(model)) {
|
|
228
|
-
throw new Error(`Model name ${model} is invalid.`);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
this.models = _nullishCoalesce(models, () => ( Object.keys(prismaDmmfModels)));
|
|
233
|
-
}
|
|
234
|
-
async getPaginationData(resourceName, query) {
|
|
235
|
-
const total = await this.getPrismaDelegate(resourceName).count({
|
|
236
|
-
where: query.where,
|
|
237
|
-
distinct: query.distinct
|
|
238
|
-
});
|
|
239
|
-
return {
|
|
240
|
-
total,
|
|
241
|
-
pageCount: Math.ceil(total / query.take),
|
|
242
|
-
page: Math.ceil(query.skip / query.take) + 1
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
handleError(error) {
|
|
246
|
-
console.error(error);
|
|
247
|
-
if (error instanceof Error && error.stack) {
|
|
248
|
-
console.error(error.stack);
|
|
249
|
-
}
|
|
250
|
-
throw error.constructor.name === "PrismaClientKnownRequestError" || error.constructor.name === "PrismaClientValidationError" ? _httperrors2.default.call(void 0, 400, "invalid request, check your server logs for more info") : _httperrors2.default.call(void 0, 500, "an unknown error occured, check your server logs for more info");
|
|
251
|
-
}
|
|
252
|
-
parseQuery(resourceName, query) {
|
|
253
|
-
var _a, _b;
|
|
254
|
-
const parsed = {};
|
|
255
|
-
if (query.select) {
|
|
256
|
-
parsed.select = parse_recursive_default(query.select, "select");
|
|
257
|
-
}
|
|
258
|
-
if (query.include) {
|
|
259
|
-
parsed.include = parse_recursive_default(query.include, "include");
|
|
260
|
-
}
|
|
261
|
-
if ((_a = query.originalQuery) == null ? void 0 : _a.where) {
|
|
262
|
-
parsed.where = parse_where_default(JSON.parse(query.originalQuery.where), _nullishCoalesce(this.manyRelations[resourceName], () => ( [])));
|
|
263
|
-
}
|
|
264
|
-
if (query.orderBy) {
|
|
265
|
-
parsed.orderBy = parse_order_by_default(query.orderBy);
|
|
266
|
-
}
|
|
267
|
-
if (query.limit !== void 0) {
|
|
268
|
-
parsed.take = query.limit;
|
|
269
|
-
}
|
|
270
|
-
if (query.skip !== void 0) {
|
|
271
|
-
parsed.skip = query.skip;
|
|
272
|
-
}
|
|
273
|
-
if ((_b = query.originalQuery) == null ? void 0 : _b.cursor) {
|
|
274
|
-
parsed.cursor = parse_cursor_default(JSON.parse(query.originalQuery.cursor));
|
|
275
|
-
}
|
|
276
|
-
if (query.distinct) {
|
|
277
|
-
parsed.distinct = query.distinct;
|
|
278
|
-
}
|
|
279
|
-
return parsed;
|
|
280
|
-
}
|
|
281
|
-
async getAll(resourceName, query) {
|
|
282
|
-
return await this.getPrismaDelegate(resourceName).findMany({
|
|
283
|
-
select: query.select,
|
|
284
|
-
include: query.include,
|
|
285
|
-
where: query.where,
|
|
286
|
-
orderBy: query.orderBy,
|
|
287
|
-
cursor: query.cursor,
|
|
288
|
-
take: query.take,
|
|
289
|
-
skip: query.skip,
|
|
290
|
-
distinct: query.distinct
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
async getOne(resourceName, resourceId, query) {
|
|
294
|
-
const delegate = this.getPrismaDelegate(resourceName);
|
|
295
|
-
const findFunction = delegate.findUnique || delegate.findOne;
|
|
296
|
-
return findFunction({
|
|
297
|
-
where: {
|
|
298
|
-
[this.primaryKey]: resourceId
|
|
299
|
-
},
|
|
300
|
-
select: query.select,
|
|
301
|
-
include: query.include
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
async create(resourceName, data, query) {
|
|
305
|
-
return this.getPrismaDelegate(resourceName).create({
|
|
306
|
-
data,
|
|
307
|
-
select: query.select,
|
|
308
|
-
include: query.include
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
async update(resourceName, resourceId, data, query) {
|
|
312
|
-
return this.getPrismaDelegate(resourceName).update({
|
|
313
|
-
where: {
|
|
314
|
-
[this.primaryKey]: resourceId
|
|
315
|
-
},
|
|
316
|
-
data,
|
|
317
|
-
select: query.select,
|
|
318
|
-
include: query.include
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
async delete(resourceName, resourceId, query) {
|
|
322
|
-
return this.getPrismaDelegate(resourceName).delete({
|
|
323
|
-
where: {
|
|
324
|
-
[this.primaryKey]: resourceId
|
|
325
|
-
},
|
|
326
|
-
select: query.select,
|
|
327
|
-
include: query.include
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
connect() {
|
|
331
|
-
return this.prismaClient.$connect();
|
|
332
|
-
}
|
|
333
|
-
disconnect() {
|
|
334
|
-
return this.prismaClient.$disconnect();
|
|
335
|
-
}
|
|
336
|
-
get client() {
|
|
337
|
-
return this.prismaClient;
|
|
338
|
-
}
|
|
339
|
-
getModels() {
|
|
340
|
-
return this.models || [];
|
|
341
|
-
}
|
|
342
|
-
getPrismaDelegate(resourceName) {
|
|
343
|
-
return this.prismaClient[`${resourceName.charAt(0).toLowerCase()}${resourceName.slice(1)}`];
|
|
344
|
-
}
|
|
345
|
-
async mapModelsToRouteNames() {
|
|
346
|
-
return models_to_route_names_default(await this.getPrismaClientModels(), this.getModels());
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
// src/swagger/json-schema-parser.ts
|
|
351
|
-
var _prismadmmftransformer = require('@visulima/prisma-dmmf-transformer');
|
|
352
|
-
|
|
353
|
-
// src/swagger/utils/format-schema-ref.ts
|
|
354
|
-
var formatSchemaReference = (schemaName) => `#/components/schemas/${schemaName}`;
|
|
355
|
-
var format_schema_ref_default = formatSchemaReference;
|
|
356
|
-
|
|
357
|
-
// src/swagger/json-schema-parser.ts
|
|
358
|
-
var getJSONSchemaScalar = (fieldType) => {
|
|
359
|
-
switch (fieldType) {
|
|
360
|
-
case "Int":
|
|
361
|
-
case "BigInt": {
|
|
362
|
-
return "integer";
|
|
363
|
-
}
|
|
364
|
-
case "DateTime":
|
|
365
|
-
case "Bytes":
|
|
366
|
-
case "String": {
|
|
367
|
-
return "string";
|
|
368
|
-
}
|
|
369
|
-
case "Float":
|
|
370
|
-
case "Decimal": {
|
|
371
|
-
return "number";
|
|
372
|
-
}
|
|
373
|
-
case "Json": {
|
|
374
|
-
return "object";
|
|
375
|
-
}
|
|
376
|
-
case "Boolean": {
|
|
377
|
-
return "boolean";
|
|
378
|
-
}
|
|
379
|
-
case "Null": {
|
|
380
|
-
return "null";
|
|
381
|
-
}
|
|
382
|
-
default: {
|
|
383
|
-
return "";
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
var PAGINATION_SCHEMA_NAME = "PaginationData";
|
|
388
|
-
var methodsNames = [
|
|
389
|
-
{ methodStart: "createOne", schemaNameStart: "Create" },
|
|
390
|
-
{ methodStart: "updateOne", schemaNameStart: "Update" }
|
|
391
|
-
];
|
|
392
|
-
var PrismaJsonSchemaParser = class {
|
|
393
|
-
constructor(dmmf) {
|
|
394
|
-
this.dmmf = dmmf;
|
|
395
|
-
this.schemaInputTypes = /* @__PURE__ */ new Map();
|
|
396
|
-
}
|
|
397
|
-
parseModels() {
|
|
398
|
-
var _a;
|
|
399
|
-
const modelsDefinitions = _prismadmmftransformer.transformDMMF.call(void 0, this.dmmf).definitions;
|
|
400
|
-
(_a = Object.keys(modelsDefinitions || {})) == null ? void 0 : _a.forEach((definition) => {
|
|
401
|
-
const { properties } = modelsDefinitions[definition];
|
|
402
|
-
Object.keys(properties).forEach((property) => {
|
|
403
|
-
if (Array.isArray(properties[property].type) && properties[property].type.includes("null")) {
|
|
404
|
-
properties[property].type = properties[property].type.filter((type) => type !== "null");
|
|
405
|
-
if (properties[property].type.length === 1) {
|
|
406
|
-
properties[property].type = properties[property].type[0];
|
|
407
|
-
}
|
|
408
|
-
properties[property].nullable = true;
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
return modelsDefinitions;
|
|
413
|
-
}
|
|
414
|
-
parseInputTypes(models) {
|
|
415
|
-
const definitions = models.reduce((accumulator, modelName) => {
|
|
416
|
-
const methods = methodsNames.map((method) => {
|
|
417
|
-
return {
|
|
418
|
-
name: `${method.methodStart}${modelName}`,
|
|
419
|
-
schemaName: `${method.schemaNameStart}${modelName}`
|
|
420
|
-
};
|
|
421
|
-
});
|
|
422
|
-
methods.forEach(({ name: method, schemaName }) => {
|
|
423
|
-
const dataFields = this.dmmf.mutationType.fieldMap[method].args[0].inputTypes[0].type.fields;
|
|
424
|
-
const requiredProperties = [];
|
|
425
|
-
const properties = dataFields.reduce((propertiesAccumulator, field) => {
|
|
426
|
-
if (field.inputTypes[0].kind === "scalar") {
|
|
427
|
-
const schema = _prismadmmftransformer.getJSONSchemaProperty.call(void 0,
|
|
428
|
-
this.dmmf.datamodel,
|
|
429
|
-
{}
|
|
430
|
-
)({
|
|
431
|
-
name: field.name,
|
|
432
|
-
...field.inputTypes[0]
|
|
433
|
-
});
|
|
434
|
-
const { type: schemaType } = schema[1];
|
|
435
|
-
if (schemaType && Array.isArray(schemaType)) {
|
|
436
|
-
if (schemaType.includes("null")) {
|
|
437
|
-
propertiesAccumulator[field.name] = {
|
|
438
|
-
...schemaType,
|
|
439
|
-
type: schemaType.filter((type) => type !== "null"),
|
|
440
|
-
nullable: true
|
|
441
|
-
};
|
|
442
|
-
if (propertiesAccumulator[field.name].type.length === 1) {
|
|
443
|
-
propertiesAccumulator[field.name] = {
|
|
444
|
-
...propertiesAccumulator[field.name],
|
|
445
|
-
type: propertiesAccumulator[field.name].type[0]
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
} else {
|
|
450
|
-
propertiesAccumulator[field.name] = schema[1];
|
|
451
|
-
}
|
|
452
|
-
} else {
|
|
453
|
-
const typeName = this.parseObjectInputType(field.inputTypes[0]);
|
|
454
|
-
propertiesAccumulator[field.name] = {
|
|
455
|
-
...typeName,
|
|
456
|
-
nullable: field.isNullable
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
if (field.isRequired) {
|
|
460
|
-
requiredProperties.push(field.name);
|
|
461
|
-
}
|
|
462
|
-
return propertiesAccumulator;
|
|
463
|
-
}, {});
|
|
464
|
-
accumulator[schemaName] = {
|
|
465
|
-
type: "object",
|
|
466
|
-
xml: {
|
|
467
|
-
name: schemaName
|
|
468
|
-
},
|
|
469
|
-
properties
|
|
470
|
-
};
|
|
471
|
-
if (requiredProperties.length > 0) {
|
|
472
|
-
accumulator[schemaName].required = requiredProperties;
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
return accumulator;
|
|
476
|
-
}, {});
|
|
477
|
-
this.schemaInputTypes.forEach((value, key) => {
|
|
478
|
-
definitions[key] = {
|
|
479
|
-
type: "object",
|
|
480
|
-
xml: {
|
|
481
|
-
name: key
|
|
482
|
-
},
|
|
483
|
-
properties: value
|
|
484
|
-
};
|
|
485
|
-
});
|
|
486
|
-
return definitions;
|
|
487
|
-
}
|
|
488
|
-
formatInputTypeData(inputType) {
|
|
489
|
-
if (inputType.kind === "object") {
|
|
490
|
-
const reference = format_schema_ref_default(inputType.type.name);
|
|
491
|
-
if (inputType.isList) {
|
|
492
|
-
return {
|
|
493
|
-
type: "array",
|
|
494
|
-
xml: {
|
|
495
|
-
name: inputType.type.name,
|
|
496
|
-
wrapped: true
|
|
497
|
-
},
|
|
498
|
-
items: {
|
|
499
|
-
$ref: reference
|
|
500
|
-
}
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
return { $ref: reference };
|
|
504
|
-
}
|
|
505
|
-
const type = getJSONSchemaScalar(inputType.type);
|
|
506
|
-
if (inputType.isList) {
|
|
507
|
-
return {
|
|
508
|
-
type: "array",
|
|
509
|
-
xml: {
|
|
510
|
-
name: inputType.type.name,
|
|
511
|
-
wrapped: true
|
|
512
|
-
},
|
|
513
|
-
items: {
|
|
514
|
-
type
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
return { type };
|
|
519
|
-
}
|
|
520
|
-
parseObjectInputType(fieldType) {
|
|
521
|
-
if (fieldType.kind === "object") {
|
|
522
|
-
if (!this.schemaInputTypes.has(fieldType.type.name)) {
|
|
523
|
-
this.schemaInputTypes.set(fieldType.type.name, {});
|
|
524
|
-
fieldType.type.fields.forEach((field) => {
|
|
525
|
-
let fieldData = {};
|
|
526
|
-
if (field.inputTypes.length > 1) {
|
|
527
|
-
let nullable = false;
|
|
528
|
-
const anyOf = field.inputTypes.map((inputType) => {
|
|
529
|
-
const inputTypeData = this.formatInputTypeData(inputType);
|
|
530
|
-
if (inputTypeData.type === "null") {
|
|
531
|
-
nullable = true;
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
return inputTypeData;
|
|
535
|
-
}).filter(Boolean);
|
|
536
|
-
if (anyOf.length === 1) {
|
|
537
|
-
fieldData = anyOf[0];
|
|
538
|
-
} else {
|
|
539
|
-
fieldData.anyOf = anyOf;
|
|
540
|
-
}
|
|
541
|
-
if (nullable) {
|
|
542
|
-
fieldData.nullable = true;
|
|
543
|
-
}
|
|
544
|
-
} else {
|
|
545
|
-
const inputType = field.inputTypes[0];
|
|
546
|
-
fieldData = this.formatInputTypeData(inputType);
|
|
547
|
-
}
|
|
548
|
-
this.schemaInputTypes.set(fieldType.type.name, {
|
|
549
|
-
...this.schemaInputTypes.get(fieldType.type.name),
|
|
550
|
-
[field.name]: fieldData
|
|
551
|
-
});
|
|
552
|
-
field.inputTypes.forEach((inputType) => {
|
|
553
|
-
if (inputType.kind === "object") {
|
|
554
|
-
this.parseObjectInputType(inputType);
|
|
555
|
-
}
|
|
556
|
-
});
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
return { $ref: format_schema_ref_default(fieldType.type.name) };
|
|
560
|
-
}
|
|
561
|
-
return { type: getJSONSchemaScalar(fieldType.type) };
|
|
562
|
-
}
|
|
563
|
-
getPaginationDataSchema() {
|
|
564
|
-
return {
|
|
565
|
-
[PAGINATION_SCHEMA_NAME]: {
|
|
566
|
-
type: "object",
|
|
567
|
-
xml: {
|
|
568
|
-
name: PAGINATION_SCHEMA_NAME
|
|
569
|
-
},
|
|
570
|
-
properties: {
|
|
571
|
-
total: {
|
|
572
|
-
type: "integer",
|
|
573
|
-
minimum: 0,
|
|
574
|
-
description: "Holds the value for the total number of rows in the database"
|
|
575
|
-
},
|
|
576
|
-
perPage: {
|
|
577
|
-
type: "integer",
|
|
578
|
-
minimum: 0,
|
|
579
|
-
description: "Returns the value for the limit passed to the paginate method"
|
|
580
|
-
},
|
|
581
|
-
page: {
|
|
582
|
-
type: "integer",
|
|
583
|
-
minimum: 1,
|
|
584
|
-
description: "Current page number"
|
|
585
|
-
},
|
|
586
|
-
lastPage: {
|
|
587
|
-
type: "integer",
|
|
588
|
-
minimum: 0,
|
|
589
|
-
description: "Returns the value for the last page by taking the total of rows into account"
|
|
590
|
-
},
|
|
591
|
-
firstPage: {
|
|
592
|
-
type: "integer",
|
|
593
|
-
minimum: 0,
|
|
594
|
-
description: "Returns the number for the first page. It is always 1"
|
|
595
|
-
},
|
|
596
|
-
firstPageUrl: {
|
|
597
|
-
type: "string",
|
|
598
|
-
description: "The URL for the first page"
|
|
599
|
-
},
|
|
600
|
-
lastPageUrl: {
|
|
601
|
-
type: "string",
|
|
602
|
-
description: "The URL for the last page"
|
|
603
|
-
},
|
|
604
|
-
nextPageUrl: {
|
|
605
|
-
type: "string",
|
|
606
|
-
description: "The URL for the next page"
|
|
607
|
-
},
|
|
608
|
-
previousPageUrl: {
|
|
609
|
-
type: "string",
|
|
610
|
-
description: "The URL for the previous page"
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
};
|
|
615
|
-
}
|
|
616
|
-
getExampleModelsSchemas(modelNames, schemas) {
|
|
617
|
-
const referenceToSchema = (reference) => {
|
|
618
|
-
const name = reference.replace("#/components/schemas/", "");
|
|
619
|
-
const model = schemas[name];
|
|
620
|
-
const values = {};
|
|
621
|
-
Object.entries((model == null ? void 0 : model.properties) || {}).forEach(([key, v]) => {
|
|
622
|
-
const type = v.type;
|
|
623
|
-
values[key] = type === "array" ? [arrayItemsToSchema(v.items)] : type;
|
|
624
|
-
});
|
|
625
|
-
return values;
|
|
626
|
-
};
|
|
627
|
-
const objectPropertiesToSchema = (objectProperties) => {
|
|
628
|
-
const values = {};
|
|
629
|
-
Object.entries(objectProperties).forEach(([key, value]) => {
|
|
630
|
-
values[key] = value.$ref === void 0 ? value.type : referenceToSchema(value.$ref);
|
|
631
|
-
});
|
|
632
|
-
return values;
|
|
633
|
-
};
|
|
634
|
-
const arrayItemsToSchema = (items) => {
|
|
635
|
-
const values = {};
|
|
636
|
-
Object.entries(items).forEach(([key, value]) => {
|
|
637
|
-
if (value.items.$ref !== void 0) {
|
|
638
|
-
values[key] = [referenceToSchema(value.items.$ref)];
|
|
639
|
-
} else if (value.type === "array") {
|
|
640
|
-
values[key] = [arrayItemsToSchema(value.items)];
|
|
641
|
-
} else if (value.type === "object") {
|
|
642
|
-
values[key] = objectPropertiesToSchema(value.properties);
|
|
643
|
-
} else {
|
|
644
|
-
values[key] = value.type;
|
|
645
|
-
}
|
|
646
|
-
});
|
|
647
|
-
return values;
|
|
648
|
-
};
|
|
649
|
-
return modelNames.reduce((accumulator, modelName) => {
|
|
650
|
-
const value = {};
|
|
651
|
-
const model = schemas[modelName];
|
|
652
|
-
Object.entries(model.properties).forEach(([key, v]) => {
|
|
653
|
-
const type = v.type;
|
|
654
|
-
if (type === "array") {
|
|
655
|
-
value[key] = [referenceToSchema(v.items.$ref)];
|
|
656
|
-
} else if (type === "object") {
|
|
657
|
-
value[key] = objectPropertiesToSchema(v.properties);
|
|
658
|
-
} else {
|
|
659
|
-
value[key] = type;
|
|
660
|
-
}
|
|
661
|
-
});
|
|
662
|
-
const pagination = this.getPaginationDataSchema()[PAGINATION_SCHEMA_NAME];
|
|
663
|
-
const meta = {};
|
|
664
|
-
Object.entries(pagination.properties).forEach(([key, v]) => {
|
|
665
|
-
meta[key] = v.type;
|
|
666
|
-
});
|
|
667
|
-
return {
|
|
668
|
-
...accumulator,
|
|
669
|
-
[`${modelName}`]: {
|
|
670
|
-
value
|
|
671
|
-
},
|
|
672
|
-
[`${modelName}s`]: {
|
|
673
|
-
value: [value]
|
|
674
|
-
},
|
|
675
|
-
[`${modelName}Page`]: {
|
|
676
|
-
value: {
|
|
677
|
-
data: [value],
|
|
678
|
-
meta
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
};
|
|
682
|
-
}, {});
|
|
683
|
-
}
|
|
684
|
-
getPaginatedModelsSchemas(modelNames) {
|
|
685
|
-
return modelNames.reduce((accumulator, modelName) => {
|
|
686
|
-
return {
|
|
687
|
-
...accumulator,
|
|
688
|
-
[`${modelName}Page`]: {
|
|
689
|
-
type: "object",
|
|
690
|
-
xml: {
|
|
691
|
-
name: `${modelName}Page`
|
|
692
|
-
},
|
|
693
|
-
properties: {
|
|
694
|
-
data: {
|
|
695
|
-
type: "array",
|
|
696
|
-
xml: {
|
|
697
|
-
name: "Data",
|
|
698
|
-
wrapped: true
|
|
699
|
-
},
|
|
700
|
-
items: {
|
|
701
|
-
$ref: format_schema_ref_default(modelName)
|
|
702
|
-
}
|
|
703
|
-
},
|
|
704
|
-
meta: {
|
|
705
|
-
$ref: format_schema_ref_default(PAGINATION_SCHEMA_NAME)
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
}, {});
|
|
711
|
-
}
|
|
712
|
-
};
|
|
713
|
-
var json_schema_parser_default = PrismaJsonSchemaParser;
|
|
714
|
-
|
|
715
|
-
// src/swagger/utils/get-models-accessible-routes.ts
|
|
716
|
-
var getModelsAccessibleRoutes = (modelNames, models, defaultExposeStrategy = "all") => modelNames.reduce((accumulator, modelName) => {
|
|
717
|
-
if (models == null ? void 0 : models[modelName]) {
|
|
718
|
-
return {
|
|
719
|
-
...accumulator,
|
|
720
|
-
[modelName]: _chunkZY3WOLEPjs.get_accessible_routes_default.call(void 0, models[modelName].only, models[modelName].exclude, defaultExposeStrategy)
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
return {
|
|
724
|
-
...accumulator,
|
|
725
|
-
[modelName]: _chunkZY3WOLEPjs.get_accessible_routes_default.call(void 0, void 0, void 0, defaultExposeStrategy)
|
|
726
|
-
};
|
|
727
|
-
}, {});
|
|
728
|
-
var get_models_accessible_routes_default = getModelsAccessibleRoutes;
|
|
729
|
-
|
|
730
|
-
// src/swagger/parameters.ts
|
|
731
|
-
var queryParameters = {
|
|
732
|
-
select: {
|
|
733
|
-
name: "select",
|
|
734
|
-
description: "Fields to select. For nested fields, chain them separated with a dot, eg: user.posts",
|
|
735
|
-
schema: {
|
|
736
|
-
type: "string"
|
|
737
|
-
}
|
|
738
|
-
},
|
|
739
|
-
include: {
|
|
740
|
-
name: "include",
|
|
741
|
-
description: "Include relations, same as select",
|
|
742
|
-
schema: {
|
|
743
|
-
type: "string"
|
|
744
|
-
}
|
|
745
|
-
},
|
|
746
|
-
where: {
|
|
747
|
-
name: "where",
|
|
748
|
-
description: 'Fields to filter. See <a href="https://next-crud.js.org/query-params#where">the docs</a>',
|
|
749
|
-
schema: {
|
|
750
|
-
type: "string"
|
|
751
|
-
}
|
|
752
|
-
},
|
|
753
|
-
orderBy: {
|
|
754
|
-
name: "orderBy",
|
|
755
|
-
description: 'Field on which to order by a direction. See <a href="https://next-crud.js.org/query-params#orderBy">the docs</a>',
|
|
756
|
-
schema: {
|
|
757
|
-
type: "string"
|
|
758
|
-
}
|
|
759
|
-
},
|
|
760
|
-
limit: {
|
|
761
|
-
name: "limit",
|
|
762
|
-
description: "Maximum number of elements to retrieve",
|
|
763
|
-
schema: {
|
|
764
|
-
type: "integer",
|
|
765
|
-
minimum: 0
|
|
766
|
-
}
|
|
767
|
-
},
|
|
768
|
-
skip: {
|
|
769
|
-
name: "skip",
|
|
770
|
-
description: "Number of rows to skip",
|
|
771
|
-
schema: {
|
|
772
|
-
type: "integer",
|
|
773
|
-
minimum: 0
|
|
774
|
-
}
|
|
775
|
-
},
|
|
776
|
-
distinct: {
|
|
777
|
-
name: "distinct",
|
|
778
|
-
description: "Fields to distinctively retrieve",
|
|
779
|
-
schema: {
|
|
780
|
-
type: "string"
|
|
781
|
-
}
|
|
782
|
-
},
|
|
783
|
-
page: {
|
|
784
|
-
name: "page",
|
|
785
|
-
description: "Page number. Use only for pagination.",
|
|
786
|
-
schema: {
|
|
787
|
-
type: "integer",
|
|
788
|
-
minimum: 1
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
var commonQueryParameters = [queryParameters.select, queryParameters.include];
|
|
793
|
-
var listQueryParameters = [
|
|
794
|
-
...commonQueryParameters,
|
|
795
|
-
queryParameters.limit,
|
|
796
|
-
queryParameters.skip,
|
|
797
|
-
queryParameters.where,
|
|
798
|
-
queryParameters.orderBy,
|
|
799
|
-
queryParameters.page,
|
|
800
|
-
queryParameters.distinct
|
|
801
|
-
];
|
|
802
|
-
var getQueryParameters = (routeType, additionalQueryParameters = []) => {
|
|
803
|
-
if (routeType === "READ_ALL" /* READ_ALL */) {
|
|
804
|
-
return [...listQueryParameters, ...additionalQueryParameters];
|
|
805
|
-
}
|
|
806
|
-
return [...commonQueryParameters, ...additionalQueryParameters];
|
|
807
|
-
};
|
|
808
|
-
|
|
809
|
-
// src/swagger/utils/format-example-ref.ts
|
|
810
|
-
var formatExampleReference = (schemaName) => `#/components/examples/${schemaName}`;
|
|
811
|
-
var format_example_ref_default = formatExampleReference;
|
|
812
|
-
|
|
813
|
-
// src/swagger/utils/get-swagger-paths.ts
|
|
814
|
-
var generateContentForSchema = (schemaName, isArray) => {
|
|
815
|
-
if (isArray) {
|
|
816
|
-
return {
|
|
817
|
-
type: "array",
|
|
818
|
-
items: {
|
|
819
|
-
$ref: format_schema_ref_default(schemaName)
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
}
|
|
823
|
-
return {
|
|
824
|
-
$ref: format_schema_ref_default(schemaName)
|
|
825
|
-
};
|
|
826
|
-
};
|
|
827
|
-
var generateSwaggerResponse = (routeType, modelName) => {
|
|
828
|
-
if (routeType === "CREATE" /* CREATE */) {
|
|
829
|
-
return {
|
|
830
|
-
statusCode: 201,
|
|
831
|
-
content: {
|
|
832
|
-
description: `${modelName} created`,
|
|
833
|
-
content: {
|
|
834
|
-
"application/json": {
|
|
835
|
-
schema: generateContentForSchema(modelName),
|
|
836
|
-
example: format_example_ref_default(modelName)
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
if (routeType === "DELETE" /* DELETE */) {
|
|
843
|
-
return {
|
|
844
|
-
statusCode: 200,
|
|
845
|
-
content: {
|
|
846
|
-
description: `${modelName} item deleted`,
|
|
847
|
-
content: {
|
|
848
|
-
"application/json": {
|
|
849
|
-
schema: generateContentForSchema(modelName),
|
|
850
|
-
example: format_example_ref_default(modelName)
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
if (routeType === "READ_ALL" /* READ_ALL */) {
|
|
857
|
-
return {
|
|
858
|
-
statusCode: 200,
|
|
859
|
-
content: {
|
|
860
|
-
description: `${modelName} list retrieved`,
|
|
861
|
-
content: {
|
|
862
|
-
"application/json": {
|
|
863
|
-
schema: {
|
|
864
|
-
oneOf: [generateContentForSchema(modelName, true), generateContentForSchema(`${modelName}Page`, false)]
|
|
865
|
-
},
|
|
866
|
-
examples: {
|
|
867
|
-
Default: {
|
|
868
|
-
$ref: format_example_ref_default(`${modelName}s`)
|
|
869
|
-
},
|
|
870
|
-
Pagination: {
|
|
871
|
-
$ref: format_example_ref_default(`${modelName}Page`)
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
if (routeType === "READ_ONE" /* READ_ONE */) {
|
|
880
|
-
return {
|
|
881
|
-
statusCode: 200,
|
|
882
|
-
content: {
|
|
883
|
-
description: `${modelName} item retrieved`,
|
|
884
|
-
content: {
|
|
885
|
-
"application/json": {
|
|
886
|
-
schema: generateContentForSchema(modelName),
|
|
887
|
-
example: format_example_ref_default(modelName)
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
}
|
|
893
|
-
if (routeType === "UPDATE" /* UPDATE */) {
|
|
894
|
-
return {
|
|
895
|
-
statusCode: 200,
|
|
896
|
-
content: {
|
|
897
|
-
description: `${modelName} item updated`,
|
|
898
|
-
content: {
|
|
899
|
-
"application/json": {
|
|
900
|
-
schema: generateContentForSchema(modelName),
|
|
901
|
-
example: format_example_ref_default(modelName)
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
return void 0;
|
|
908
|
-
};
|
|
909
|
-
var generateRequestBody = (schemaStartName, modelName) => {
|
|
910
|
-
return {
|
|
911
|
-
content: {
|
|
912
|
-
"application/json": {
|
|
913
|
-
schema: {
|
|
914
|
-
$ref: format_schema_ref_default(`${schemaStartName}${modelName}`)
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
};
|
|
919
|
-
};
|
|
920
|
-
var getRouteTypeMethod = (routeType) => {
|
|
921
|
-
switch (routeType) {
|
|
922
|
-
case "CREATE" /* CREATE */: {
|
|
923
|
-
return "post";
|
|
924
|
-
}
|
|
925
|
-
case "READ_ALL" /* READ_ALL */:
|
|
926
|
-
case "READ_ONE" /* READ_ONE */: {
|
|
927
|
-
return "get";
|
|
928
|
-
}
|
|
929
|
-
case "UPDATE" /* UPDATE */: {
|
|
930
|
-
return "put";
|
|
931
|
-
}
|
|
932
|
-
case "DELETE" /* DELETE */: {
|
|
933
|
-
return "delete";
|
|
934
|
-
}
|
|
935
|
-
default: {
|
|
936
|
-
throw new TypeError(`Method for route type ${routeType} was not found.`);
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
};
|
|
940
|
-
var generateSwaggerPathObject = ({
|
|
941
|
-
tag,
|
|
942
|
-
routeTypes,
|
|
943
|
-
modelName,
|
|
944
|
-
modelsConfig,
|
|
945
|
-
hasId
|
|
946
|
-
}) => {
|
|
947
|
-
const methods = {};
|
|
948
|
-
routeTypes.forEach((routeType) => {
|
|
949
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
950
|
-
if (routeTypes.includes(routeType)) {
|
|
951
|
-
const returnType = _nullishCoalesce(_nullishCoalesce(((_d = (_c = (_b = (_a = modelsConfig == null ? void 0 : modelsConfig[modelName]) == null ? void 0 : _a.routeTypes) == null ? void 0 : _b[routeType]) == null ? void 0 : _c.response) == null ? void 0 : _d.name), () => ( ((_f = (_e = modelsConfig == null ? void 0 : modelsConfig[modelName]) == null ? void 0 : _e.type) == null ? void 0 : _f.name))), () => ( modelName));
|
|
952
|
-
const method = getRouteTypeMethod(routeType);
|
|
953
|
-
const response = generateSwaggerResponse(routeType, returnType);
|
|
954
|
-
if (response === void 0) {
|
|
955
|
-
throw new TypeError(`Route type ${routeType}; response config was not found.`);
|
|
956
|
-
}
|
|
957
|
-
methods[method] = {
|
|
958
|
-
tags: [tag],
|
|
959
|
-
summary: (_i = (_h = (_g = modelsConfig == null ? void 0 : modelsConfig[modelName]) == null ? void 0 : _g.routeTypes) == null ? void 0 : _h[routeType]) == null ? void 0 : _i.summary,
|
|
960
|
-
parameters: getQueryParameters(routeType).map((queryParameter) => {
|
|
961
|
-
return { ...queryParameter, in: "query" };
|
|
962
|
-
}),
|
|
963
|
-
responses: {
|
|
964
|
-
[response.statusCode]: response.content,
|
|
965
|
-
...(_l = (_k = (_j = modelsConfig == null ? void 0 : modelsConfig[modelName]) == null ? void 0 : _j.routeTypes) == null ? void 0 : _k[routeType]) == null ? void 0 : _l.responses
|
|
966
|
-
}
|
|
967
|
-
};
|
|
968
|
-
if (hasId) {
|
|
969
|
-
methods[method].parameters.push({
|
|
970
|
-
in: "path",
|
|
971
|
-
name: "id",
|
|
972
|
-
description: `ID of the ${modelName}`,
|
|
973
|
-
required: true,
|
|
974
|
-
schema: {
|
|
975
|
-
type: "string"
|
|
976
|
-
}
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
if (routeType === "UPDATE" /* UPDATE */ || routeType === "CREATE" /* CREATE */) {
|
|
980
|
-
if (routeType === "UPDATE" /* UPDATE */) {
|
|
981
|
-
methods[method].requestBody = generateRequestBody("Update", returnType);
|
|
982
|
-
} else if (routeType === "CREATE" /* CREATE */) {
|
|
983
|
-
methods[method].requestBody = generateRequestBody("Create", returnType);
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
});
|
|
988
|
-
return methods;
|
|
989
|
-
};
|
|
990
|
-
var getSwaggerPaths = ({
|
|
991
|
-
routes,
|
|
992
|
-
models,
|
|
993
|
-
modelsConfig,
|
|
994
|
-
routesMap
|
|
995
|
-
}) => Object.keys(routes).reduce((accumulator, value) => {
|
|
996
|
-
var _a, _b, _c;
|
|
997
|
-
const routeTypes = routes[value];
|
|
998
|
-
const resourceName = ((_a = models == null ? void 0 : models[value]) == null ? void 0 : _a.name) ? models[value].name : (routesMap == null ? void 0 : routesMap[value]) || value;
|
|
999
|
-
const tag = ((_c = (_b = modelsConfig == null ? void 0 : modelsConfig[value]) == null ? void 0 : _b.tag) == null ? void 0 : _c.name) || value;
|
|
1000
|
-
if (routeTypes.includes("CREATE" /* CREATE */) || routeTypes.includes("READ_ALL" /* READ_ALL */)) {
|
|
1001
|
-
const path = `/${resourceName}`;
|
|
1002
|
-
const routeTypesToUse = ["READ_ALL" /* READ_ALL */, "CREATE" /* CREATE */].filter((routeType) => routeTypes.includes(routeType));
|
|
1003
|
-
accumulator[path] = generateSwaggerPathObject({
|
|
1004
|
-
tag,
|
|
1005
|
-
modelName: value,
|
|
1006
|
-
modelsConfig,
|
|
1007
|
-
routeTypes: routeTypesToUse
|
|
1008
|
-
});
|
|
1009
|
-
}
|
|
1010
|
-
if (routeTypes.includes("READ_ONE" /* READ_ONE */) || routeTypes.includes("UPDATE" /* UPDATE */) || routeTypes.includes("DELETE" /* DELETE */)) {
|
|
1011
|
-
const path = `/${resourceName}/{id}`;
|
|
1012
|
-
const routeTypesToUse = ["READ_ONE" /* READ_ONE */, "UPDATE" /* UPDATE */, "DELETE" /* DELETE */].filter((routeType) => routeTypes.includes(routeType));
|
|
1013
|
-
accumulator[path] = generateSwaggerPathObject({
|
|
1014
|
-
tag,
|
|
1015
|
-
modelName: value,
|
|
1016
|
-
modelsConfig,
|
|
1017
|
-
routeTypes: routeTypesToUse,
|
|
1018
|
-
hasId: true
|
|
1019
|
-
});
|
|
1020
|
-
}
|
|
1021
|
-
return accumulator;
|
|
1022
|
-
}, {});
|
|
1023
|
-
var get_swagger_paths_default = getSwaggerPaths;
|
|
1024
|
-
|
|
1025
|
-
// src/swagger/utils/get-swagger-tags.ts
|
|
1026
|
-
var getSwaggerTags = (modelNames, modelsConfig) => modelNames.map((modelName) => {
|
|
1027
|
-
var _a;
|
|
1028
|
-
if ((_a = modelsConfig == null ? void 0 : modelsConfig[modelName]) == null ? void 0 : _a.tag) {
|
|
1029
|
-
return modelsConfig[modelName].tag;
|
|
1030
|
-
}
|
|
1031
|
-
return {
|
|
1032
|
-
name: modelName
|
|
1033
|
-
};
|
|
1034
|
-
});
|
|
1035
|
-
var get_swagger_tags_default = getSwaggerTags;
|
|
1036
|
-
|
|
1037
|
-
// src/swagger/adapter/prisma/index.ts
|
|
1038
|
-
var overwritePathsExampleWithModel = (swaggerPaths, examples) => {
|
|
1039
|
-
Object.values(swaggerPaths).forEach((pathSpec) => {
|
|
1040
|
-
Object.values(pathSpec).forEach((methodSpec) => {
|
|
1041
|
-
if (typeof methodSpec.responses === "object") {
|
|
1042
|
-
Object.values(methodSpec.responses).forEach((responseSpec) => {
|
|
1043
|
-
if (typeof responseSpec.content === "object") {
|
|
1044
|
-
Object.values(
|
|
1045
|
-
responseSpec.content
|
|
1046
|
-
).forEach((contentSpec) => {
|
|
1047
|
-
var _a;
|
|
1048
|
-
if (typeof contentSpec.example === "string") {
|
|
1049
|
-
const example = contentSpec.example.replace("#/components/examples/", "");
|
|
1050
|
-
if (typeof examples[example] !== void 0 && typeof ((_a = examples[example]) == null ? void 0 : _a.value) !== void 0) {
|
|
1051
|
-
contentSpec.example = examples[example].value;
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
});
|
|
1055
|
-
}
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
});
|
|
1059
|
-
});
|
|
1060
|
-
return swaggerPaths;
|
|
1061
|
-
};
|
|
1062
|
-
var modelsToOpenApi = async ({
|
|
1063
|
-
prismaClient,
|
|
1064
|
-
models: ctorModels,
|
|
1065
|
-
swagger = { models: {}, allowedMediaTypes: { "application/json": true } },
|
|
1066
|
-
crud = { models: {} },
|
|
1067
|
-
defaultExposeStrategy = "all"
|
|
1068
|
-
}) => {
|
|
1069
|
-
let dmmf;
|
|
1070
|
-
let prismaDmmfModels;
|
|
1071
|
-
if (prismaClient._dmmf) {
|
|
1072
|
-
dmmf = prismaClient._dmmf;
|
|
1073
|
-
prismaDmmfModels = dmmf == null ? void 0 : dmmf.mappingsMap;
|
|
1074
|
-
} else if (prismaClient._getDmmf) {
|
|
1075
|
-
dmmf = await prismaClient._getDmmf();
|
|
1076
|
-
prismaDmmfModels = dmmf.mappingsMap;
|
|
1077
|
-
}
|
|
1078
|
-
if (typeof dmmf === void 0) {
|
|
1079
|
-
throw new TypeError("Couldn't get prisma client models");
|
|
1080
|
-
}
|
|
1081
|
-
const parser = new json_schema_parser_default(dmmf);
|
|
1082
|
-
const definitions = parser.parseModels();
|
|
1083
|
-
const dModels = Object.keys(definitions);
|
|
1084
|
-
const schema = JSON.stringify({
|
|
1085
|
-
...definitions,
|
|
1086
|
-
...parser.parseInputTypes(dModels),
|
|
1087
|
-
...parser.getPaginationDataSchema(),
|
|
1088
|
-
...parser.getPaginatedModelsSchemas(dModels)
|
|
1089
|
-
});
|
|
1090
|
-
if (ctorModels !== void 0) {
|
|
1091
|
-
ctorModels.forEach((model) => {
|
|
1092
|
-
if (!Object.keys(prismaDmmfModels).includes(model)) {
|
|
1093
|
-
throw new Error(`Model name ${model} is invalid.`);
|
|
1094
|
-
}
|
|
1095
|
-
});
|
|
1096
|
-
}
|
|
1097
|
-
const models = _nullishCoalesce(ctorModels, () => ( Object.keys(prismaDmmfModels)));
|
|
1098
|
-
const swaggerRoutes = get_models_accessible_routes_default(models, crud.models || {}, defaultExposeStrategy);
|
|
1099
|
-
const swaggerTags = get_swagger_tags_default(models, (swagger == null ? void 0 : swagger.models) || {});
|
|
1100
|
-
const swaggerPaths = get_swagger_paths_default({
|
|
1101
|
-
routes: swaggerRoutes,
|
|
1102
|
-
modelsConfig: (swagger == null ? void 0 : swagger.models) || {},
|
|
1103
|
-
models: crud.models || {},
|
|
1104
|
-
routesMap: models_to_route_names_default(prismaDmmfModels, models)
|
|
1105
|
-
});
|
|
1106
|
-
const schemas = JSON.parse(schema.replace(/#\/definitions/g, "#/components/schemas"));
|
|
1107
|
-
const examples = parser.getExampleModelsSchemas(dModels, schemas);
|
|
1108
|
-
return {
|
|
1109
|
-
schemas,
|
|
1110
|
-
examples,
|
|
1111
|
-
tags: swaggerTags,
|
|
1112
|
-
paths: overwritePathsExampleWithModel(swaggerPaths, examples)
|
|
1113
|
-
};
|
|
1114
|
-
};
|
|
1115
|
-
var prisma_default = modelsToOpenApi;
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
exports.PrismaAdapter = PrismaAdapter; exports.RouteType = _chunkZY3WOLEPjs.RouteType; exports.modelsToOpenApi = prisma_default;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _chunkU35WXGEFjs = require('./chunk-U35WXGEF.js');var _httperrors = require('http-errors'); var _httperrors2 = _interopRequireDefault(_httperrors);var te=(s,e)=>{let t={};return e==null||e.forEach(r=>{t[r]=s[r].plural}),t},E=te;var re=new Set(["string","boolean","number"]),se=s=>re.has(typeof s),j=se;var ne=s=>{let e={};if(Object.keys(s).forEach(t=>{let r=s[t];j(r)&&(e[t]=r)}),Object.keys(e).length!==1)throw new Error("cursor needs to be an object with exactly 1 property with a primitive value");return e},F=ne;var ie={$asc:"asc",$desc:"desc"},ae=s=>{let e={};return Object.keys(s).forEach(t=>{let r=s[t];e[t]=ie[r]}),e},V=ae;var v=(s,e)=>{let t={};return Object.keys(s).forEach(r=>{t[r]=s[r]===!0?!0:{[e]:v(s[r],e)}}),t},I=v;var R=s=>s instanceof Object,oe={$eq:"equals",$neq:"not",$cont:"contains",$ends:"endsWith",$gt:"gt",$gte:"gte",$in:"in",$lt:"lt",$lte:"lte",$notin:"notIn",$starts:"startsWith"},pe=s=>/^\d{4}-[01]\d-[0-3]\d(?:T[0-2](?:\d:[0-5]){2}\d(?:\.\d+)?(?:Z|[+-][0-2]\d(?::?[0-5]\d)?)?)?$/g.test(s),ce=s=>pe(s)?new Date(s):typeof s=="string"&&s==="$isnull"?null:s,B=(s,e)=>{let t=s.split(".");return t.splice(-1,1),e.includes(t.join("."))},W=s=>{let e=Object.keys(s)[0],t=oe[e];if(t)return{[t]:s[e]}},L=(s,e,t,r)=>{var m;let n=e.split(".").reverse(),i={};n.forEach((p,f)=>{f===0?U(s,p,i,r):i={[p]:{some:i}}});let a=n.reverse()[0],o=t[a];t[a]={some:{...o==null?void 0:o.some,...(m=i[a])==null?void 0:m.some}}},D=(s,e)=>{let t={};return Object.keys(s).forEach(r=>{let n=s[r];if(B(r,e))L(n,r,t,e);else if(j(n))t[r]=n;else if(R(n)){let i=W(n);i&&(t[r]=i)}}),t},U=(s,e,t,r)=>{if(j(s))t[e]=ce(s);else switch(e){case"$or":{R(s)&&(t.OR=D(s,r));break}case"$and":{R(s)&&(t.AND=D(s,r));break}case"$not":{R(s)&&(t.NOT=D(s,r));break}default:{t[e]=W(s);break}}},me=(s,e)=>{let t={};return Object.keys(s).forEach(r=>{let n=s[r];B(r,e)?L(n,r,t,e):U(n,r,t,e)}),t},Q=me;var T=class{constructor({primaryKey:e="id",prismaClient:t,manyRelations:r={},models:n}){this.getPrismaClientModels=async()=>{var e;if(this.prismaClient._dmmf)return this.dmmf=this.prismaClient._dmmf,(e=this.dmmf)==null?void 0:e.mappingsMap;if(this.prismaClient._getDmmf)return this.dmmf=await this.prismaClient._getDmmf(),this.dmmf.mappingsMap;throw new Error("Couldn't get prisma client models")};this.prismaClient=t,this.primaryKey=e,this.manyRelations=r,this.ctorModels=n}async init(){let e=this.ctorModels,t=await this.getPrismaClientModels();e!==void 0&&e.forEach(r=>{if(!Object.keys(t).includes(r))throw new Error(`Model name ${r} is invalid.`)}),this.models=_nullishCoalesce(e, () => (Object.keys(t)))}async getPaginationData(e,t){let r=await this.getPrismaDelegate(e).count({where:t.where,distinct:t.distinct});return{total:r,pageCount:Math.ceil(r/t.take),page:Math.ceil(t.skip/t.take)+1}}handleError(e){throw console.error(e),e instanceof Error&&e.stack&&console.error(e.stack),e.constructor.name==="PrismaClientKnownRequestError"||e.constructor.name==="PrismaClientValidationError"?_httperrors2.default.call(void 0, 400,"invalid request, check your server logs for more info"):_httperrors2.default.call(void 0, 500,"an unknown error occured, check your server logs for more info")}parseQuery(e,t){var n,i;let r={};return t.select&&(r.select=I(t.select,"select")),t.include&&(r.include=I(t.include,"include")),(n=t.originalQuery)!=null&&n.where&&(r.where=Q(JSON.parse(t.originalQuery.where),_nullishCoalesce(this.manyRelations[e], () => ([])))),t.orderBy&&(r.orderBy=V(t.orderBy)),t.limit!==void 0&&(r.take=t.limit),t.skip!==void 0&&(r.skip=t.skip),(i=t.originalQuery)!=null&&i.cursor&&(r.cursor=F(JSON.parse(t.originalQuery.cursor))),t.distinct&&(r.distinct=t.distinct),r}async getAll(e,t){return await this.getPrismaDelegate(e).findMany({select:t.select,include:t.include,where:t.where,orderBy:t.orderBy,cursor:t.cursor,take:t.take,skip:t.skip,distinct:t.distinct})}async getOne(e,t,r){let n=this.getPrismaDelegate(e);return(n.findUnique||n.findOne)({where:{[this.primaryKey]:t},select:r.select,include:r.include})}async create(e,t,r){return this.getPrismaDelegate(e).create({data:t,select:r.select,include:r.include})}async update(e,t,r,n){return this.getPrismaDelegate(e).update({where:{[this.primaryKey]:t},data:r,select:n.select,include:n.include})}async delete(e,t,r){return this.getPrismaDelegate(e).delete({where:{[this.primaryKey]:t},select:r.select,include:r.include})}connect(){return this.prismaClient.$connect()}disconnect(){return this.prismaClient.$disconnect()}get client(){return this.prismaClient}getModels(){return this.models||[]}getPrismaDelegate(e){return this.prismaClient[`${e.charAt(0).toLowerCase()}${e.slice(1)}`]}async mapModelsToRouteNames(){return E(await this.getPrismaClientModels(),this.getModels())}};var _prismadmmftransformer = require('@visulima/prisma-dmmf-transformer');var de=s=>`#/components/schemas/${s}`,g=de;var H=s=>{switch(s){case"Int":case"BigInt":return"integer";case"DateTime":case"Bytes":case"String":return"string";case"Float":case"Decimal":return"number";case"Json":return"object";case"Boolean":return"boolean";case"Null":return"null";default:return""}},w="PaginationData",fe=[{methodStart:"createOne",schemaNameStart:"Create"},{methodStart:"updateOne",schemaNameStart:"Update"}],k=class{constructor(e){this.dmmf=e;this.schemaInputTypes=new Map}parseModels(){var t;let e=_prismadmmftransformer.transformDMMF.call(void 0, this.dmmf).definitions;return(t=Object.keys(e||{}))==null||t.forEach(r=>{let{properties:n}=e[r];Object.keys(n).forEach(i=>{Array.isArray(n[i].type)&&n[i].type.includes("null")&&(n[i].type=n[i].type.filter(a=>a!=="null"),n[i].type.length===1&&(n[i].type=n[i].type[0]),n[i].nullable=!0)})}),e}parseInputTypes(e){let t=e.reduce((r,n)=>(fe.map(a=>({name:`${a.methodStart}${n}`,schemaName:`${a.schemaNameStart}${n}`})).forEach(({name:a,schemaName:o})=>{let m=this.dmmf.mutationType.fieldMap[a].args[0].inputTypes[0].type.fields,p=[],f=m.reduce((d,c)=>{if(c.inputTypes[0].kind==="scalar"){let l=_prismadmmftransformer.getJSONSchemaProperty.call(void 0, this.dmmf.datamodel,{})({name:c.name,...c.inputTypes[0]}),{type:u}=l[1];u&&Array.isArray(u)?u.includes("null")&&(d[c.name]={...u,type:u.filter(h=>h!=="null"),nullable:!0},d[c.name].type.length===1&&(d[c.name]={...d[c.name],type:d[c.name].type[0]})):d[c.name]=l[1]}else{let l=this.parseObjectInputType(c.inputTypes[0]);d[c.name]={...l,nullable:c.isNullable}}return c.isRequired&&p.push(c.name),d},{});r[o]={type:"object",xml:{name:o},properties:f},p.length>0&&(r[o].required=p)}),r),{});return this.schemaInputTypes.forEach((r,n)=>{t[n]={type:"object",xml:{name:n},properties:r}}),t}formatInputTypeData(e){if(e.kind==="object"){let r=g(e.type.name);return e.isList?{type:"array",xml:{name:e.type.name,wrapped:!0},items:{$ref:r}}:{$ref:r}}let t=H(e.type);return e.isList?{type:"array",xml:{name:e.type.name,wrapped:!0},items:{type:t}}:{type:t}}parseObjectInputType(e){return e.kind==="object"?(this.schemaInputTypes.has(e.type.name)||(this.schemaInputTypes.set(e.type.name,{}),e.type.fields.forEach(t=>{let r={};if(t.inputTypes.length>1){let n=!1,i=t.inputTypes.map(a=>{let o=this.formatInputTypeData(a);if(o.type==="null"){n=!0;return}return o}).filter(Boolean);i.length===1?r=i[0]:r.anyOf=i,n&&(r.nullable=!0)}else{let n=t.inputTypes[0];r=this.formatInputTypeData(n)}this.schemaInputTypes.set(e.type.name,{...this.schemaInputTypes.get(e.type.name),[t.name]:r}),t.inputTypes.forEach(n=>{n.kind==="object"&&this.parseObjectInputType(n)})})),{$ref:g(e.type.name)}):{type:H(e.type)}}getPaginationDataSchema(){return{[w]:{type:"object",xml:{name:w},properties:{total:{type:"integer",minimum:0,description:"Holds the value for the total number of rows in the database"},perPage:{type:"integer",minimum:0,description:"Returns the value for the limit passed to the paginate method"},page:{type:"integer",minimum:1,description:"Current page number"},lastPage:{type:"integer",minimum:0,description:"Returns the value for the last page by taking the total of rows into account"},firstPage:{type:"integer",minimum:0,description:"Returns the number for the first page. It is always 1"},firstPageUrl:{type:"string",description:"The URL for the first page"},lastPageUrl:{type:"string",description:"The URL for the last page"},nextPageUrl:{type:"string",description:"The URL for the next page"},previousPageUrl:{type:"string",description:"The URL for the previous page"}}}}}getExampleModelsSchemas(e,t){let r=a=>{let o=a.replace("#/components/schemas/",""),m=t[o],p={};return Object.entries((m==null?void 0:m.properties)||{}).forEach(([f,d])=>{let c=d.type;p[f]=c==="array"?[i(d.items)]:c}),p},n=a=>{let o={};return Object.entries(a).forEach(([m,p])=>{o[m]=p.$ref===void 0?p.type:r(p.$ref)}),o},i=a=>{let o={};return Object.entries(a).forEach(([m,p])=>{p.items.$ref!==void 0?o[m]=[r(p.items.$ref)]:p.type==="array"?o[m]=[i(p.items)]:p.type==="object"?o[m]=n(p.properties):o[m]=p.type}),o};return e.reduce((a,o)=>{let m={},p=t[o];Object.entries(p.properties).forEach(([c,l])=>{let u=l.type;u==="array"?m[c]=[r(l.items.$ref)]:u==="object"?m[c]=n(l.properties):m[c]=u});let f=this.getPaginationDataSchema()[w],d={};return Object.entries(f.properties).forEach(([c,l])=>{d[c]=l.type}),{...a,[`${o}`]:{value:m},[`${o}s`]:{value:[m]},[`${o}Page`]:{value:{data:[m],meta:d}}}},{})}getPaginatedModelsSchemas(e){return e.reduce((t,r)=>({...t,[`${r}Page`]:{type:"object",xml:{name:`${r}Page`},properties:{data:{type:"array",xml:{name:"data",wrapped:!0},items:{$ref:g(r)}},meta:{$ref:g(w)}}}}),{})}},J=k;var ge=(s,e,t="all")=>s.reduce((r,n)=>e!=null&&e[n]?{...r,[n]:_chunkU35WXGEFjs.b.call(void 0, e[n].only,e[n].exclude,t)}:{...r,[n]:_chunkU35WXGEFjs.b.call(void 0, void 0,void 0,t)},{}),K=ge;var y={select:{name:"select",description:"Fields to select. For nested fields, chain them separated with a dot, eg: user.posts",schema:{type:"string"}},include:{name:"include",description:"Include relations, same as select",schema:{type:"string"}},where:{name:"where",description:'Fields to filter. See <a href="https://next-crud.js.org/query-params#where">the docs</a>',schema:{type:"string"}},orderBy:{name:"orderBy",description:'Field on which to order by a direction. See <a href="https://next-crud.js.org/query-params#orderBy">the docs</a>',schema:{type:"string"}},limit:{name:"limit",description:"Maximum number of elements to retrieve",schema:{type:"integer",minimum:0}},skip:{name:"skip",description:"Number of rows to skip",schema:{type:"integer",minimum:0}},distinct:{name:"distinct",description:"Fields to distinctively retrieve",schema:{type:"string"}},page:{name:"page",description:"Page number. Use only for pagination.",schema:{type:"integer",minimum:1}}},G=[y.select,y.include],ye=[...G,y.limit,y.skip,y.where,y.orderBy,y.page,y.distinct],Z=(s,e=[])=>s==="READ_ALL"?[...ye,...e]:[...G,...e];var he=s=>`#/components/examples/${s}`,P=he;var O=(s,e)=>e?{type:"array",items:{$ref:g(s)}}:{$ref:g(s)},Pe=(s,e)=>{if(s==="CREATE")return{statusCode:201,content:{description:`${e} created`,content:{"application/json":{schema:O(e),example:P(e)}}}};if(s==="DELETE")return{statusCode:200,content:{description:`${e} item deleted`,content:{"application/json":{schema:O(e),example:P(e)}}}};if(s==="READ_ALL")return{statusCode:200,content:{description:`${e} list retrieved`,content:{"application/json":{schema:{oneOf:[O(e,!0),O(`${e}Page`,!1)]},examples:{Default:{$ref:P(`${e}s`)},Pagination:{$ref:P(`${e}Page`)}}}}}};if(s==="READ_ONE")return{statusCode:200,content:{description:`${e} item retrieved`,content:{"application/json":{schema:O(e),example:P(e)}}}};if(s==="UPDATE")return{statusCode:200,content:{description:`${e} item updated`,content:{"application/json":{schema:O(e),example:P(e)}}}}},z=(s,e)=>({content:{"application/json":{schema:{$ref:g(`${s}${e}`)}}}}),Oe=s=>{switch(s){case"CREATE":return"post";case"READ_ALL":case"READ_ONE":return"get";case"UPDATE":return"put";case"DELETE":return"delete";default:throw new TypeError(`Method for route type ${s} was not found.`)}},X=({tag:s,routeTypes:e,modelName:t,modelsConfig:r,hasId:n})=>{let i={};return e.forEach(a=>{var o,m,p,f,d,c,l,u,h,b,M,C;if(e.includes(a)){let S=_nullishCoalesce(_nullishCoalesce(((f=(p=(m=(o=r==null?void 0:r[t])==null?void 0:o.routeTypes)==null?void 0:m[a])==null?void 0:p.response)==null?void 0:f.name), () => (((c=(d=r==null?void 0:r[t])==null?void 0:d.type)==null?void 0:c.name))), () => (t)),A=Oe(a),x=Pe(a,S);if(x===void 0)throw new TypeError(`Route type ${a}; response config was not found.`);i[A]={tags:[s],summary:(h=(u=(l=r==null?void 0:r[t])==null?void 0:l.routeTypes)==null?void 0:u[a])==null?void 0:h.summary,parameters:Z(a).map(q=>({...q,in:"query"})),responses:{[x.statusCode]:x.content,...(C=(M=(b=r==null?void 0:r[t])==null?void 0:b.routeTypes)==null?void 0:M[a])==null?void 0:C.responses}},n&&i[A].parameters.push({in:"path",name:"id",description:`ID of the ${t}`,required:!0,schema:{type:"string"}}),(a==="UPDATE"||a==="CREATE")&&(a==="UPDATE"?i[A].requestBody=z("Update",S):a==="CREATE"&&(i[A].requestBody=z("Create",S)))}}),i},be=({routes:s,models:e,modelsConfig:t,routesMap:r})=>Object.keys(s).reduce((n,i)=>{var p,f,d;let a=s[i],o=(p=e==null?void 0:e[i])!=null&&p.name?e[i].name:(r==null?void 0:r[i])||i,m=((d=(f=t==null?void 0:t[i])==null?void 0:f.tag)==null?void 0:d.name)||i;if(a.includes("CREATE")||a.includes("READ_ALL")){let c=`/${o}`,l=["READ_ALL","CREATE"].filter(u=>a.includes(u));n[c]=X({tag:m,modelName:i,modelsConfig:t,routeTypes:l})}if(a.includes("READ_ONE")||a.includes("UPDATE")||a.includes("DELETE")){let c=`/${o}/{id}`,l=["READ_ONE","UPDATE","DELETE"].filter(u=>a.includes(u));n[c]=X({tag:m,modelName:i,modelsConfig:t,routeTypes:l,hasId:!0})}return n},{}),Y=be;var Me=(s,e)=>s.map(t=>{var r;return(r=e==null?void 0:e[t])!=null&&r.tag?e[t].tag:{name:t}}),N=Me;var je=(s,e)=>(Object.values(s).forEach(t=>{Object.values(t).forEach(r=>{typeof r.responses=="object"&&Object.values(r.responses).forEach(n=>{typeof n.content=="object"&&Object.values(n.content).forEach(i=>{var a;if(typeof i.example=="string"){let o=i.example.replace("#/components/examples/","");typeof e[o]!==void 0&&typeof((a=e[o])==null?void 0:a.value)!==void 0&&(i.example=e[o].value)}})})})}),s),Ae=async({prismaClient:s,models:e,swagger:t={models:{},allowedMediaTypes:{"application/json":!0}},crud:r={models:{}},defaultExposeStrategy:n="all"})=>{let i,a;if(s._dmmf?(i=s._dmmf,a=i==null?void 0:i.mappingsMap):s._getDmmf&&(i=await s._getDmmf(),a=i.mappingsMap),typeof i===void 0)throw new TypeError("Couldn't get prisma client models");let o=new J(i),m=o.parseModels(),p=Object.keys(m),f=JSON.stringify({...m,...o.parseInputTypes(p),...o.getPaginationDataSchema(),...o.getPaginatedModelsSchemas(p)});e!==void 0&&e.forEach(M=>{if(!Object.keys(a).includes(M))throw new Error(`Model name ${M} is invalid.`)});let d=_nullishCoalesce(e, () => (Object.keys(a))),c=K(d,r.models||{},n),l=N(d,(t==null?void 0:t.models)||{}),u=Y({routes:c,modelsConfig:(t==null?void 0:t.models)||{},models:r.models||{},routesMap:E(a,d)}),h=JSON.parse(f.replace(/#\/definitions/g,"#/components/schemas")),b=o.getExampleModelsSchemas(p,h);return{schemas:h,examples:b,tags:l,paths:je(u,b)}},Ee= exports.modelsToOpenApi =Ae;exports.PrismaAdapter = T; exports.RouteType = _chunkU35WXGEFjs.a; exports.modelsToOpenApi = Ee;
|
|
1121
2
|
//# sourceMappingURL=index.js.map
|