ncrudify
Version:
Configurable CRUD module for NestJS and Mongoose.
375 lines • 15.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrudifyRoutesDecorator = void 0;
const common_1 = require("@nestjs/common");
const disable_guard_1 = require("./disable.guard");
const swagger_1 = require("@nestjs/swagger");
var CrudifyRoutesDecorator;
(function (CrudifyRoutesDecorator) {
function getDecorators(options, route) {
var _a, _b, _c, _d, _e;
const routeconfig = (_b = (_a = options.routes) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b[route];
if (((_d = (_c = options.routes) === null || _c === void 0 ? void 0 : _c.exclude) === null || _d === void 0 ? void 0 : _d.includes(route)) ||
(routeconfig === null || routeconfig === void 0 ? void 0 : routeconfig.disabled) == true)
return [(0, swagger_1.ApiExcludeEndpoint)(), (0, common_1.UseGuards)(disable_guard_1.DisableRouteGuard)];
let routeDecorators = getRouteDecorators(options, route);
const generalUserRouteDecorators = ((_e = options === null || options === void 0 ? void 0 : options.routes) === null || _e === void 0 ? void 0 : _e.decorators) || [];
const userRouteDecorators = (routeconfig === null || routeconfig === void 0 ? void 0 : routeconfig.decorators) || [];
return [
...routeDecorators,
...generalUserRouteDecorators,
...userRouteDecorators,
];
}
CrudifyRoutesDecorator.getDecorators = getDecorators;
function getRouteDecorators(options, route) {
switch (route) {
case "create":
return createDecorators(options, route);
case "createBulk":
return createBulkDecorators(options, route);
case "findAll":
return findAllDecorators(options, route);
case "findOne":
return findOneDecorators(options, route);
case "put":
return overwriteDecorators(options, route);
case "update":
return updateDecorators(options, route);
case "updateBulk":
return updateBulkDecorators(options, route);
case "delete":
return deleteDecorators(options, route);
case "deleteBulk":
return deleteBulkDecorators(options, route);
case "restore":
return restoreDecorators(options, route);
case "restoreBulk":
return restoreBulkDecorators(options, route);
default:
return [];
}
}
function createDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Create a ${name} resource`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiCreatedResponse)({
description: `The resource ${name} has been successfully created`,
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid input data" }),
(0, swagger_1.ApiConflictResponse)({
description: "Conflict in creating the resource",
}),
(0, swagger_1.ApiBody)({
description: "Data of the new resource",
type: options.model.cdto || options.model.type,
}),
];
}
function createBulkDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Create multiple ${name} resources`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiCreatedResponse)({
description: "The resources have been created",
type: [options.model.type],
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid input data" }),
(0, swagger_1.ApiConflictResponse)({
description: "Conflict in creating the resources",
}),
(0, swagger_1.ApiBody)({
description: "Data of the new resources",
type: [options.model.cdto || options.model.type],
}),
];
}
function findAllDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
let path_types = {
String: "string",
Number: "number",
Date: "string",
Buffer: "string",
Boolean: "boolean",
Mixed: "string",
ObjectId: "string",
Array: "array",
Decimal128: "number",
Map: "object",
Schema: "object",
UUID: "string",
BigInt: "number",
Double: "number",
Int32: "number",
};
let list = [];
if (options.model.schema) {
const fields = Object.keys(options.model.schema.paths);
const operators = ["eq", "ne", "gt", "gte", "lt", "lte", "in"];
fields.forEach((field) => {
let type = options.model.schema.paths[field].instance;
let path_type = path_types[type];
if (!path_type) {
path_type = "string";
}
operators.forEach((operator) => {
list.push({ [`${field}[${operator}]`]: { type: path_type } });
});
});
}
const patternProperties = list.reduce((acc, item) => {
const key = Object.keys(item)[0];
acc[key] = item[key];
return acc;
}, {});
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Retrieve all ${name} resources`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `List of ${name} resources`,
type: [options.model.type],
}),
(0, swagger_1.ApiNotFoundResponse)({ description: "Resources not found" }),
(0, swagger_1.ApiQuery)({
name: "filters",
required: false,
type: "object",
style: "form",
explode: true,
description: "Filters to apply",
properties: patternProperties,
examples: {
void: { value: {} },
example: {
value: {
"filter_string[eq]": "filter_value",
"filter_num[gt]": -1,
},
},
},
default: {},
}),
(0, swagger_1.ApiQuery)({
name: "limit",
required: false,
type: Number,
description: "Number of records to return",
}),
(0, swagger_1.ApiQuery)({
name: "skip",
required: false,
type: Number,
description: "Number of records to skip",
}),
(0, swagger_1.ApiQuery)({
name: "sort",
required: false,
type: String,
description: "Sorting fields",
}),
];
}
function findOneDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Retrive a ${name} resource by ID`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `Resource ${name} found`,
type: options.model.type,
}),
(0, swagger_1.ApiNotFoundResponse)({ description: "Resource not found" }),
(0, swagger_1.ApiParam)({
name: "id",
description: "ID of the resource",
type: String,
}),
];
}
function overwriteDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Overwrite a ${name} resource`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: "The resource has been overwrited",
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid data" }),
(0, swagger_1.ApiParam)({
name: "id",
description: `ID of the ${name} resource`,
type: String,
}),
(0, swagger_1.ApiBody)({
description: "Overwrited data of the resource",
type: options.model.udto || options.model.type,
}),
];
}
function updateDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Update a ${name} resource`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `The resource ${name} has been updated`,
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid data" }),
(0, swagger_1.ApiParam)({
name: "id",
description: `ID of the ${name} resource`,
type: String,
}),
(0, swagger_1.ApiBody)({
description: "Updated data of the resource",
type: options.model.udto || options.model.type,
}),
];
}
function updateBulkDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Update multiple ${name} resources`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `The resources have been updated`,
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid data" }),
(0, swagger_1.ApiBody)({
description: `Object containing filter and updated data`,
schema: {
type: "object",
properties: {
filter: {
type: "object",
description: "Filter to select the resources to update",
},
updateDto: {
type: "object",
description: "Data to apply to the selected resources",
},
},
},
}),
];
}
function deleteDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, swagger_1.ApiOperation)({
summary: `Delete a ${name} resource`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({ description: "The resource has been deleted" }),
(0, swagger_1.ApiNotFoundResponse)({ description: "Resource not found" }),
(0, swagger_1.ApiParam)({
name: "id",
description: "ID of the resource",
type: String,
}),
];
}
function deleteBulkDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, swagger_1.ApiOperation)({
summary: `Delete multiple ${name} resources`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({ description: "The resources have been deleted" }),
(0, swagger_1.ApiNotFoundResponse)({ description: "Some resources not found" }),
(0, swagger_1.ApiBadRequestResponse)({
description: "Invalid input, expected an array of IDs",
}),
(0, swagger_1.ApiBody)({
description: `Object containing filter`,
schema: {
type: "object",
properties: {
filter: {
type: "object",
description: "Filter to select the resources to delete",
},
},
},
}),
];
}
function restoreDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Restore a ${name} resource`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `The resource ${name} has been restored`,
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid data" }),
(0, swagger_1.ApiParam)({
name: "id",
description: `ID of the ${name} resource`,
type: String,
}),
];
}
function restoreBulkDecorators(options, route) {
const name = options.model.type.name.toLowerCase();
return [
(0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
(0, swagger_1.ApiOperation)({
summary: `Restore multiple ${name} resources`,
operationId: `${name}_${route}`,
}),
(0, swagger_1.ApiOkResponse)({
description: `The resources have been restored`,
type: options.model.type,
}),
(0, swagger_1.ApiBadRequestResponse)({ description: "Invalid data" }),
(0, swagger_1.ApiBody)({
description: `Object containing filter`,
schema: {
type: "object",
properties: {
filter: {
type: "object",
description: "Filter to select the resources to restore",
},
},
},
}),
];
}
})(CrudifyRoutesDecorator || (exports.CrudifyRoutesDecorator = CrudifyRoutesDecorator = {}));
//# sourceMappingURL=crudify.routesdecorator.js.map