UNPKG

contexify

Version:

A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.

54 lines 1.66 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { Context } from "../context/context.js"; import { ResolutionSession } from "../resolution/resolution-session.js"; import { invokeMethodWithInterceptors } from "./interceptor.js"; class ProxySource { static { __name(this, "ProxySource"); } value; type = "proxy"; constructor(value) { this.value = value; } toString() { return this.value.getBindingPath(); } } class InterceptionHandler { static { __name(this, "InterceptionHandler"); } context; session; source; constructor(context = new Context(), session, source) { this.context = context; this.session = session; this.source = source; } get(target, propertyName, _receiver) { const targetObj = target; if (typeof propertyName !== "string") return targetObj[propertyName]; const propertyOrMethod = targetObj[propertyName]; if (typeof propertyOrMethod === "function") { return (...args) => { return invokeMethodWithInterceptors(this.context, target, propertyName, args, { source: this.source ?? (this.session && new ProxySource(this.session)) }); }; } return propertyOrMethod; } } function createProxyWithInterceptors(target, context, session, source) { return new Proxy(target, new InterceptionHandler(context, ResolutionSession.fork(session), source)); } __name(createProxyWithInterceptors, "createProxyWithInterceptors"); export { InterceptionHandler, ProxySource, createProxyWithInterceptors }; //# sourceMappingURL=interception-proxy.js.map