@kitstack/nest-powertools
Version:
A comprehensive collection of NestJS powertools, decorators, and utilities to supercharge your backend development
170 lines • 5.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecureEndpoint = SecureEndpoint;
exports.PublicCachedEndpoint = PublicCachedEndpoint;
exports.AdminOnly = AdminOnly;
exports.UserEndpoint = UserEndpoint;
exports.RequireRoles = RequireRoles;
exports.RequirePermissions = RequirePermissions;
exports.PowerEndpoint = PowerEndpoint;
exports.CrudEndpoint = CrudEndpoint;
exports.UseCustomGuard = UseCustomGuard;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const enums_1 = require("../types/enums");
const powertools_config_1 = require("../config/powertools.config");
function SecureEndpoint(config) {
const configService = powertools_config_1.PowertoolsConfigService.getInstance();
const globalConfig = configService.getConfig();
const decorators = [
(0, swagger_1.ApiBearerAuth)(),
(0, common_1.SetMetadata)('auth-config', { ...globalConfig.auth, ...config }),
];
if (config?.description) {
decorators.push((0, swagger_1.ApiOperation)({ summary: config.description }));
}
if (config?.responses?.length) {
config.responses.forEach((response) => {
decorators.push((0, swagger_1.ApiResponse)({
status: response.status,
description: response.description,
type: response.type,
}));
});
}
return (0, common_1.applyDecorators)(...decorators);
}
function PublicCachedEndpoint(options) {
const configService = powertools_config_1.PowertoolsConfigService.getInstance();
const decorators = [];
if (options?.description) {
decorators.push((0, swagger_1.ApiOperation)({ summary: options.description }));
}
if (options?.responseType) {
decorators.push((0, swagger_1.ApiResponse)({
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: options.responseType,
}));
}
return (0, common_1.applyDecorators)(...decorators);
}
function AdminOnly(description, responseType) {
return SecureEndpoint({
roles: [enums_1.DefaultRoles.ADMIN],
description: description || 'Admin only endpoint',
responses: [
{
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: responseType,
},
{
status: enums_1.HttpStatusCodes.UNAUTHORIZED,
description: 'Unauthorized',
},
{
status: enums_1.HttpStatusCodes.FORBIDDEN,
description: 'Forbidden',
},
],
});
}
function UserEndpoint(description, responseType) {
return SecureEndpoint({
roles: [enums_1.DefaultRoles.USER, enums_1.DefaultRoles.ADMIN],
description: description || 'User endpoint',
validation: { transform: true, whitelist: true },
responses: [
{
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: responseType,
},
{
status: enums_1.HttpStatusCodes.UNAUTHORIZED,
description: 'Unauthorized',
},
],
});
}
function RequireRoles(roles, description, responseType) {
return SecureEndpoint({
roles,
description: description || `Requires roles: ${roles.join(', ')}`,
responses: responseType
? [
{
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: responseType,
},
]
: undefined,
});
}
function RequirePermissions(permissions, description, responseType) {
return SecureEndpoint({
permissions,
description: description || `Requires permissions: ${permissions.join(', ')}`,
responses: responseType
? [
{
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: responseType,
},
]
: undefined,
});
}
function PowerEndpoint(options) {
const decorators = [];
if (options.auth || options.guards) {
decorators.push((0, common_1.SetMetadata)('power-endpoint-config', options));
}
if (options.description) {
decorators.push((0, swagger_1.ApiOperation)({ summary: options.description }));
}
if (options.responses?.length) {
options.responses.forEach((response) => {
decorators.push((0, swagger_1.ApiResponse)({
status: response.status,
description: response.description,
type: response.type,
}));
});
}
else if (options.responseType) {
decorators.push((0, swagger_1.ApiResponse)({
status: enums_1.HttpStatusCodes.OK,
description: 'Success',
type: options.responseType,
}));
}
return (0, common_1.applyDecorators)(...decorators);
}
function CrudEndpoint(options) {
return PowerEndpoint({
auth: {
roles: options.roles,
permissions: options.permissions,
},
audit: options.audit
? {
action: enums_1.AuditAction.CREATE,
resource: options.entity.name,
includeRequestBody: true,
}
: undefined,
cache: options.cache ? { ttl: 300000 } : undefined,
description: options.description,
responseType: options.entity,
});
}
function UseCustomGuard(guardName, description) {
return SecureEndpoint({
description: description || `Protected by ${guardName} guard`,
});
}
//# sourceMappingURL=endpoint-combinations.js.map