@clawject/di
Version:
<p align="center"> <a href="https://clawject.com/" target="_blank"><img src="https://clawject.com/img/logo.svg" align="center" alt="Clawject Logo" width="120" height="120" /></a> </p>
76 lines (75 loc) • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxyBuilder = void 0;
const Utils_1 = require("../Utils");
const RuntimeErrors_1 = require("../api/RuntimeErrors");
class ProxyBuilder {
static build(parentBean, targetAccessor) {
return new Proxy({}, {
apply: (_, thisArg, argArray) => {
return Reflect.apply(this.assertValidProxyValueAndGet(parentBean, targetAccessor), thisArg, argArray);
},
// eslint-disable-next-line @typescript-eslint/ban-types
construct: (_, argArray, newTarget) => {
return Reflect.construct(this.assertValidProxyValueAndGet(parentBean, targetAccessor), argArray, newTarget);
},
defineProperty: (_, property, attributes) => {
return Reflect.defineProperty(this.assertValidProxyValueAndGet(parentBean, targetAccessor), property, attributes);
},
deleteProperty: (_, property) => {
return Reflect.deleteProperty(this.assertValidProxyValueAndGet(parentBean, targetAccessor), property);
},
get: (_, property, receiver) => {
const obj = this.assertValidProxyValueAndGet(parentBean, targetAccessor);
const propertyValue = Reflect.get(obj, property, receiver);
if (typeof propertyValue === 'function') {
return propertyValue.bind(obj);
}
return propertyValue;
},
getOwnPropertyDescriptor: (_, property) => {
return Reflect.getOwnPropertyDescriptor(this.assertValidProxyValueAndGet(parentBean, targetAccessor), property);
},
getPrototypeOf: (_) => {
return Reflect.getPrototypeOf(this.assertValidProxyValueAndGet(parentBean, targetAccessor));
},
has: (_, property) => {
return Reflect.has(this.assertValidProxyValueAndGet(parentBean, targetAccessor), property);
},
isExtensible: (_) => {
return Reflect.isExtensible(this.assertValidProxyValueAndGet(parentBean, targetAccessor));
},
ownKeys: (_) => {
return Reflect.ownKeys(this.assertValidProxyValueAndGet(parentBean, targetAccessor));
},
preventExtensions: (_) => {
return Reflect.preventExtensions(this.assertValidProxyValueAndGet(parentBean, targetAccessor));
},
set: (_, property, newValue, receiver) => {
return Reflect.set(this.assertValidProxyValueAndGet(parentBean, targetAccessor), property, newValue, receiver);
},
setPrototypeOf: (_, value) => {
return Reflect.setPrototypeOf(this.assertValidProxyValueAndGet(parentBean, targetAccessor), value);
}
});
}
}
exports.ProxyBuilder = ProxyBuilder;
ProxyBuilder.assertValidProxyValueAndGet = (parentBean, targetAccessor) => {
const value = targetAccessor();
const isFunction = Utils_1.Utils.isFunction(value);
if (isFunction) {
const msg = `Bean named "${parentBean.name}", with scope: "${parentBean.scopeName}" - ` +
'contains function value which is not allowed to be proxied, ' +
'To solve this issue - you can wrap function in object that contains function as a property.';
throw new RuntimeErrors_1.RuntimeErrors.CouldNotBeProxiedError(msg);
}
if (Utils_1.Utils.isObject(value)) {
return value;
}
const msg = `Bean named "${parentBean.name}", with scope: "${parentBean.scopeName}" - ` +
'contains primitive value which could not be proxied, ' +
'ES standard allows only object-like proxies.' +
'To solve this issue - you can wrap your primitive value in object that contains value as a property.';
throw new RuntimeErrors_1.RuntimeErrors.CouldNotBeProxiedError(msg);
};