@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
48 lines (44 loc) • 2.56 kB
JavaScript
;
var exception_utility = require('../../../../utility/error/exception.utility.js');
var objectFindPropertyDescriptor_utility = require('../../../../utility/object-find-property-descriptor.utility.js');
/**
* Tracks exact built-in decorated service function identities used by generated mandatory calls.
*/
class ApiControllerGeneratedFunctionCapability {
static REGISTRY = new WeakMap();
static mark(implementation, functionType, entity) {
if (typeof implementation !== "function") {
throw exception_utility.ErrorException(`Generated ${functionType} capability must mark a function`);
}
this.REGISTRY.set(implementation, { entity, functionType });
}
static markOwn(service, propertyKey, functionType, entity) {
const descriptor = Object.getOwnPropertyDescriptor(service, propertyKey);
if (!descriptor || !("value" in descriptor) || typeof descriptor.value !== "function") {
throw exception_utility.ErrorException(`Generated ${functionType} service capability must be an own data function`);
}
this.mark(descriptor.value, functionType, entity);
}
// eslint-disable-next-line @elsikora/typescript/no-unnecessary-type-parameters
static resolve(service, propertyKey, functionType, entity) {
if (typeof entity !== "function") {
throw exception_utility.ErrorException(`Generated ${functionType} entity capability must be a constructor`);
}
const entityConstructor = entity;
const descriptor = objectFindPropertyDescriptor_utility.ObjectFindPropertyDescriptor(service, propertyKey);
if (!descriptor) {
throw exception_utility.ErrorException(`Generated ${functionType} service function is not available`);
}
if (!("value" in descriptor) || typeof descriptor.value !== "function") {
throw exception_utility.ErrorException(`Generated ${functionType} service capability must be a data function`);
}
const implementation = descriptor.value;
const capability = this.REGISTRY.get(implementation);
if (capability?.entity !== entityConstructor || capability.functionType !== functionType) {
throw exception_utility.ErrorException(`Generated ${functionType} service function is not protected by its built-in decorator`);
}
return implementation;
}
}
exports.ApiControllerGeneratedFunctionCapability = ApiControllerGeneratedFunctionCapability;
//# sourceMappingURL=function-capability.class.js.map