dino-core
Version:
A dependency injection framework for NodeJS applications
74 lines • 3.31 kB
JavaScript
;
// Copyright 2021 Quirino Brizi [quirino.brizi@gmail.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultProxyHandler = void 0;
const Logger_1 = require("../../Logger");
const method_missing_exception_1 = require("../../exceptions/method.missing.exception");
const cache_interceptor_1 = require("./interceptors/cache.interceptor");
const default_interceptor_1 = require("./interceptors/default.interceptor");
/**
* Create a new basic proxy which will access properties or methods of the target object and will
* invoke a specific method on the target object if the method or property is no part of the target object.
*/
class DefaultProxyHandler {
constructor() {
this.excludedProperties = [
'debug.description',
'nodejs.util.inspect.custom',
'prototype',
'postConstruct',
'preDestroy',
'then',
'catch',
'finally'
];
}
/**
* Implement the get method for the {@link Proxy} class
*
* @param {Injectable} obj the target object
* @param {String} prop the property to access
* @returns {any} The value of the accessed property or the result of the function execution
*/
get(obj, prop) {
if (Reflect.has(obj, prop)) {
if (['postConstruct', 'preDestroy', 'isPrototypeOf', 'isInstanceOf', 'instanceof', 'hasOwnProperty', 'toString'].includes(prop)) {
return Reflect.get(obj, prop);
}
const interceptor = this.interceptors().find((interceptor) => interceptor.isApplicable(obj, prop));
if (interceptor === undefined) {
return;
}
return interceptor.intercept(obj, prop);
}
else {
if (!this.excludedProperties.includes(prop)) {
Logger_1.Logger.debug(`Method [${String(prop)}] has been invoked but has not been found, if you would like more information override the method __call(method, args) in your class`);
if (Reflect.has(obj, '__call')) {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return (...args) => obj.__call(prop, args);
}
else {
throw method_missing_exception_1.MethodMissingException.create(`${obj.constructor.name}#${prop}`);
}
}
}
}
interceptors() {
return [cache_interceptor_1.CacheInterceptor.create(), default_interceptor_1.DefaultInterceptor.create()];
}
}
exports.DefaultProxyHandler = DefaultProxyHandler;
//# sourceMappingURL=default.proxy.handler.js.map