@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
46 lines (43 loc) • 2.35 kB
JavaScript
import { ErrorException } from '../../../../utility/error/exception.utility.js';
import { ObjectFindPropertyDescriptor } from '../../../../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 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 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 ErrorException(`Generated ${functionType} entity capability must be a constructor`);
}
const entityConstructor = entity;
const descriptor = ObjectFindPropertyDescriptor(service, propertyKey);
if (!descriptor) {
throw ErrorException(`Generated ${functionType} service function is not available`);
}
if (!("value" in descriptor) || typeof descriptor.value !== "function") {
throw 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 ErrorException(`Generated ${functionType} service function is not protected by its built-in decorator`);
}
return implementation;
}
}
export { ApiControllerGeneratedFunctionCapability };
//# sourceMappingURL=function-capability.class.js.map