contexify
Version:
A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.
85 lines (82 loc) • 3.01 kB
TypeScript
import { _ as ResolutionSession, z as Context } from '../index-CSgk2Bzc.js';
import { ValueOrPromise } from '../utils/value-promise.js';
import { InvocationArgs, InvocationSource } from './invocation.js';
import 'events';
import '../provider/provider.js';
import '../binding/binding-key.js';
import 'metarize';
import '../utils/json-types.js';
import '../utils/debug.js';
/**
* Create the Promise type for `T`. If `T` extends `Promise`, the type is `T`,
* otherwise the type is `ValueOrPromise<T>`.
*/
type AsValueOrPromise<T> = T extends Promise<unknown> ? T : ValueOrPromise<T>;
/**
* The intercepted variant of a function to return `ValueOrPromise<T>`.
* If `T` is not a function, the type is `T`.
*/
type AsInterceptedFunction<T> = T extends (...args: InvocationArgs) => infer R ? (...args: Parameters<T>) => AsValueOrPromise<R> : T;
/**
* The proxy type for `T`. The return type for any method of `T` with original
* return type `R` becomes `ValueOrPromise<R>` if `R` does not extend `Promise`.
* Property types stay untouched.
*
* @example
* ```ts
* class MyController {
* name: string;
*
* greet(name: string): string {
* return `Hello, ${name}`;
* }
*
* async hello(name: string) {
* return `Hello, ${name}`;
* }
* }
* ```
*
* `AsyncProxy<MyController>` will be:
* ```ts
* {
* name: string; // the same as MyController
* greet(name: string): ValueOrPromise<string>; // the return type becomes `ValueOrPromise<string>`
* hello(name: string): Promise<string>; // the same as MyController
* }
* ```
*/
type AsyncProxy<T> = {
[P in keyof T]: AsInterceptedFunction<T[P]>;
};
/**
* Invocation source for injected proxies. It wraps a snapshot of the
* `ResolutionSession` that tracks the binding/injection stack.
*/
declare class ProxySource implements InvocationSource<ResolutionSession> {
readonly value: ResolutionSession;
type: string;
constructor(value: ResolutionSession);
toString(): string;
}
/**
* A proxy handler that applies interceptors
*
* See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy
*/
declare class InterceptionHandler<T extends object> implements ProxyHandler<T> {
private context;
private session?;
private source?;
constructor(context?: Context, session?: ResolutionSession | undefined, source?: InvocationSource | undefined);
get(target: T, propertyName: PropertyKey, _receiver: unknown): unknown;
}
/**
* Create a proxy that applies interceptors for method invocations
* @param target - Target class or object
* @param context - Context object
* @param session - Resolution session
* @param source - Invocation source
*/
declare function createProxyWithInterceptors<T extends object>(target: T, context?: Context, session?: ResolutionSession, source?: InvocationSource): AsyncProxy<T>;
export { type AsInterceptedFunction, type AsValueOrPromise, type AsyncProxy, InterceptionHandler, ProxySource, createProxyWithInterceptors };