import { ObjectGraph } from '../../graph/ObjectGraph'; import { Graph } from '../../graph/Graph'; import useGraph from '../components/useGraph'; import { Constructable } from '../../types'; export default class HookInjector { inject( hook: (args: Args) => Result, keyOrGraph: string | Constructable, ): (args?: Partial) => Result { return (args?: Partial): Result => { const graph = useGraph(keyOrGraph, hook, args); return hook(new Proxy(args ?? {}, new Injector(graph))); }; } } class Injector implements ProxyHandler { constructor(private graph: Graph) {} get(obj: any, property: string, receiver: any): any { return property in obj ? obj[property] : this.graph.retrieve(property, receiver); } }