nestjs-prisma-base
Version:
A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration, featuring pagination, search, filtering, relation loading, configurable DTOs, and modular composition capabilities.
169 lines • 6.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerBaseResponseDto = exports.SwaggerBaseUpdateDto = exports.SwaggerBaseCreateDto = void 0;
exports.configureSwaggerDTOs = configureSwaggerDTOs;
exports.getSwaggerDTOConfig = getSwaggerDTOConfig;
exports.EnableSwaggerBaseFields = EnableSwaggerBaseFields;
exports.applySwaggerDecoratorsToClass = applySwaggerDecoratorsToClass;
exports.isSwaggerIntegrationEnabled = isSwaggerIntegrationEnabled;
const configurable_dtos_1 = require("./configurable-dtos");
let isSwaggerAvailable = false;
let ApiProperty = null;
try {
const swaggerModule = require('@nestjs/swagger');
ApiProperty = swaggerModule.ApiProperty;
isSwaggerAvailable = true;
}
catch (error) {
isSwaggerAvailable = false;
}
let globalSwaggerConfig = {
enabled: false,
includeTimestamps: true,
includeExamples: true,
includeDescriptions: true,
fieldConfig: {
id: {
description: 'Unique identifier for the record',
example: 1,
},
createdAt: {
description: 'Timestamp when the record was created',
example: '2023-01-01T00:00:00.000Z',
},
updatedAt: {
description: 'Timestamp when the record was last updated',
example: '2023-01-01T12:00:00.000Z',
},
message: {
description: 'Response message providing additional context about the operation',
example: 'Operation completed successfully',
},
},
};
const processedClasses = new WeakSet();
const pendingClasses = new Set();
function configureSwaggerDTOs(config) {
globalSwaggerConfig = { ...globalSwaggerConfig, ...config };
}
function getSwaggerDTOConfig() {
return { ...globalSwaggerConfig };
}
function createConditionalApiProperty(options = {}) {
if (!isSwaggerAvailable || !globalSwaggerConfig.enabled) {
return function (target, propertyKey) {
};
}
return ApiProperty(options);
}
function getFieldConfig(fieldName) {
const config = globalSwaggerConfig.fieldConfig?.[fieldName];
const includeDescriptions = globalSwaggerConfig.includeDescriptions ?? true;
const includeExamples = globalSwaggerConfig.includeExamples ?? true;
return {
description: includeDescriptions ? config?.description : undefined,
example: includeExamples ? config?.example : undefined,
};
}
function applyBaseResponseDecorators(targetClass) {
if (!isSwaggerAvailable || !globalSwaggerConfig.enabled) {
return;
}
const swaggerConfig = globalSwaggerConfig;
const dtoConfig = (0, configurable_dtos_1.getDTOConfig)();
const idConfig = getFieldConfig('id');
const createdAtConfig = getFieldConfig('createdAt');
const updatedAtConfig = getFieldConfig('updatedAt');
const messageConfig = getFieldConfig('message');
if (dtoConfig.includeId !== false) {
createConditionalApiProperty({
description: idConfig.description || 'Unique identifier',
example: idConfig.example || 1,
type: () => Number,
required: false,
})(targetClass.prototype, 'id');
}
if (swaggerConfig.includeTimestamps && dtoConfig.includeTimestamps !== false) {
createConditionalApiProperty({
description: createdAtConfig.description || 'Creation timestamp',
example: createdAtConfig.example || '2023-01-01T00:00:00.000Z',
type: () => Date,
required: false,
})(targetClass.prototype, 'createdAt');
createConditionalApiProperty({
description: updatedAtConfig.description || 'Last update timestamp',
example: updatedAtConfig.example || '2023-01-01T12:00:00.000Z',
type: () => Date,
required: false,
})(targetClass.prototype, 'updatedAt');
}
if (dtoConfig.includeMessage === true) {
createConditionalApiProperty({
description: messageConfig.description || 'Response message providing additional context about the operation',
example: messageConfig.example || dtoConfig.messageField?.defaultValue || 'Operation completed successfully',
type: () => String,
required: false,
maxLength: dtoConfig.messageField?.maxLength,
})(targetClass.prototype, 'message');
}
}
class SwaggerBaseCreateDto {
static configureSwagger(config) {
this.swaggerConfig = { ...this.swaggerConfig, ...config };
}
static getSwaggerConfig() {
return { ...globalSwaggerConfig, ...this.swaggerConfig };
}
}
exports.SwaggerBaseCreateDto = SwaggerBaseCreateDto;
SwaggerBaseCreateDto.swaggerConfig = {};
class SwaggerBaseUpdateDto {
static configureSwagger(config) {
this.swaggerConfig = { ...this.swaggerConfig, ...config };
}
static getSwaggerConfig() {
return { ...globalSwaggerConfig, ...this.swaggerConfig };
}
}
exports.SwaggerBaseUpdateDto = SwaggerBaseUpdateDto;
SwaggerBaseUpdateDto.swaggerConfig = {};
class SwaggerBaseResponseDto {
static configureSwagger(config) {
this.swaggerConfig = { ...this.swaggerConfig, ...config };
}
static getSwaggerConfig() {
return { ...globalSwaggerConfig, ...this.swaggerConfig };
}
}
exports.SwaggerBaseResponseDto = SwaggerBaseResponseDto;
SwaggerBaseResponseDto.swaggerConfig = {};
function EnableSwaggerBaseFields(constructor) {
if (globalSwaggerConfig.enabled) {
applyBaseResponseDecorators(constructor);
}
else {
setTimeout(() => {
if (globalSwaggerConfig.enabled) {
applyBaseResponseDecorators(constructor);
}
}, 0);
}
return constructor;
}
function applySwaggerDecoratorsToClass(target, fields) {
if (!isSwaggerAvailable || !globalSwaggerConfig.enabled) {
return;
}
fields.forEach(({ propertyKey, options = {} }) => {
createConditionalApiProperty(options)(target.prototype, propertyKey);
});
}
function isSwaggerIntegrationEnabled() {
return isSwaggerAvailable && globalSwaggerConfig.enabled;
}
(0, configurable_dtos_1.registerDTOConfigChangeCallback)(() => {
if (globalSwaggerConfig.enabled) {
applyBaseResponseDecorators(SwaggerBaseResponseDto);
}
});
//# sourceMappingURL=swagger-dtos.js.map