nestjs-prisma-base
Version:
A reusable NestJS module for Prisma ORM with base classes for controller, service and DTOs
80 lines • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ENDPOINT_DISABLED_KEY = exports.ENDPOINT_ENABLED_KEY = exports.DISABLED_ENDPOINTS_KEY = exports.ENABLED_ENDPOINTS_KEY = exports.EndpointType = void 0;
exports.EnableEndpoint = EnableEndpoint;
exports.DisableEndpoint = DisableEndpoint;
exports.EnableAllEndpoints = EnableAllEndpoints;
exports.isEndpointEnabled = isEndpointEnabled;
exports.ApiExcludeDisabledEndpoint = ApiExcludeDisabledEndpoint;
require("reflect-metadata");
const swagger_1 = require("@nestjs/swagger");
var EndpointType;
(function (EndpointType) {
EndpointType["FIND_ALL"] = "findAll";
EndpointType["FIND_ONE"] = "findOne";
EndpointType["CREATE"] = "create";
EndpointType["UPDATE"] = "update";
EndpointType["REMOVE"] = "remove";
})(EndpointType || (exports.EndpointType = EndpointType = {}));
exports.ENABLED_ENDPOINTS_KEY = 'endpoints:enabled';
exports.DISABLED_ENDPOINTS_KEY = 'endpoints:disabled';
exports.ENDPOINT_ENABLED_KEY = 'endpoint:enabled';
exports.ENDPOINT_DISABLED_KEY = 'endpoint:disabled';
function EnableEndpoint(endpointName) {
return function (target, propertyKey, descriptor) {
if (propertyKey && descriptor) {
Reflect.defineMetadata(exports.ENDPOINT_ENABLED_KEY, true, target, propertyKey);
return descriptor;
}
const enabledEndpoints = Reflect.getMetadata(exports.ENABLED_ENDPOINTS_KEY, target) || [];
enabledEndpoints.push(endpointName);
Reflect.defineMetadata(exports.ENABLED_ENDPOINTS_KEY, enabledEndpoints, target);
return target;
};
}
function DisableEndpoint(endpointName) {
return function (target, propertyKey, descriptor) {
if (propertyKey && descriptor) {
Reflect.defineMetadata(exports.ENDPOINT_DISABLED_KEY, true, target, propertyKey);
return descriptor;
}
const disabledEndpoints = Reflect.getMetadata(exports.DISABLED_ENDPOINTS_KEY, target) || [];
disabledEndpoints.push(endpointName);
Reflect.defineMetadata(exports.DISABLED_ENDPOINTS_KEY, disabledEndpoints, target);
return target;
};
}
function EnableAllEndpoints() {
return EnableEndpoint('*');
}
function isEndpointEnabled(target, endpointName) {
if (Reflect.getMetadata(exports.ENDPOINT_DISABLED_KEY, target, endpointName)) {
return false;
}
if (Reflect.getMetadata(exports.ENDPOINT_ENABLED_KEY, target, endpointName)) {
return true;
}
const enabledEndpoints = Reflect.getMetadata(exports.ENABLED_ENDPOINTS_KEY, target.constructor) || [];
if (enabledEndpoints.includes(endpointName) || enabledEndpoints.includes('*')) {
const disabledEndpoints = Reflect.getMetadata(exports.DISABLED_ENDPOINTS_KEY, target.constructor) || [];
return !disabledEndpoints.includes(endpointName);
}
return false;
}
function ApiExcludeDisabledEndpoint(endpointName) {
return (target, propertyKey, descriptor) => {
Reflect.defineMetadata('api:shouldExclude', endpointName, target, propertyKey.toString());
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const thisInstance = this;
const methodName = propertyKey.toString();
const endpointToCheck = Reflect.getMetadata('api:shouldExclude', target, methodName);
if (!isEndpointEnabled(thisInstance, endpointToCheck)) {
(0, swagger_1.ApiExcludeEndpoint)()(target, methodName, descriptor);
}
return originalMethod.apply(this, args);
};
return descriptor;
};
}
//# sourceMappingURL=endpoint.decorator.js.map