@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
48 lines (47 loc) • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transactions = exports.queryBuilder = void 0;
exports.component = component;
exports.args = args;
const constants_1 = require("../constants");
const component_1 = require("../constants/component");
function component(params) {
return (target) => {
const options = {
componentType: "default",
scope: constants_1.Scope.Context,
...params,
injectable: true,
};
const scope = options.scope;
// Check scope
// e.g.
// - if Singleton, then all dpendencies must be Singleton
// - if Transient, then dpendencies can be either Singleton or Transient
const dependencies = Reflect.getMetadata("design:paramtypes", target) || [];
const invalidDependencies = dependencies.filter((dep) => {
if (!dep) {
return false;
}
const depOptions = Reflect.getMetadata(component_1.REFLECT_METADATA_KEY_COMPONENT, dep) ?? {};
return scope < depOptions?.scope; // ?? Scope.Transient;
});
if (invalidDependencies.length > 0) {
throw new Error(`${target.name} has dependencies with invalid scope: ${invalidDependencies.map((dep) => dep?.name)}`);
}
Reflect.defineMetadata(component_1.REFLECT_METADATA_KEY_COMPONENT, options, target);
};
}
// Define a parameter decorator factory
function args(...args) {
return function parameterDecorator(target, propertyKey, parameterIndex) {
const argsMap = Reflect.getMetadata(component_1.REFLECT_METADATA_KEY_COMPONENT_ARGUMENTS, target) ??
new Map();
argsMap.set(parameterIndex, args);
Reflect.defineMetadata(component_1.REFLECT_METADATA_KEY_COMPONENT_ARGUMENTS, argsMap, target);
};
}
const queryBuilder = (...args) => component({ scope: constants_1.Scope.Transient, ...args, componentType: "queryBuilder" });
exports.queryBuilder = queryBuilder;
const transactions = (...args) => component({ scope: constants_1.Scope.Transient, ...args, componentType: "transactions" });
exports.transactions = transactions;