UNPKG

contexify

Version:

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

157 lines (154 loc) 5.3 kB
import { MetadataAccessor } from 'metarize'; import { Provider } from '../provider/provider.js'; import { Constructor } from '../utils/value-promise.js'; import { e as BindingTemplate, B as BindingScope, d as BindingTag, h as DynamicValueProviderClass, j as Binding } from '../index-CSgk2Bzc.js'; import { BindingAddress } from './binding-key.js'; import 'events'; import '../utils/json-types.js'; import '../utils/debug.js'; /** * Binding metadata from `@injectable` * * @typeParam T - Value type */ type BindingMetadata<T = unknown> = { /** * An array of template functions to configure a binding */ templates: BindingTemplate<T>[]; /** * The target class where binding metadata is decorated */ target: Constructor<T>; }; /** * Metadata key for binding metadata */ declare const BINDING_METADATA_KEY: MetadataAccessor<BindingMetadata<unknown>, ClassDecorator>; /** * An object to configure binding scope and tags */ type BindingScopeAndTags = { scope?: BindingScope; tags?: BindingTag | BindingTag[]; }; /** * Specification of parameters for `@injectable()` */ type BindingSpec<T = unknown> = BindingTemplate<T> | BindingScopeAndTags; /** * Check if a class implements `Provider` interface * @param cls - A class * * @typeParam T - Value type */ declare function isProviderClass<T>(cls: unknown): cls is Constructor<Provider<T>>; /** * A factory function to create a template function to bind the target class * as a `Provider`. * @param target - Target provider class * * @typeParam T - Value type */ declare function asProvider<T>(target: Constructor<Provider<T>>): BindingTemplate<T>; /** * A factory function to create a template function to bind the target class * as a class or `Provider`. * @param target - Target class, which can be an implementation of `Provider` * or `DynamicValueProviderClass` * * @typeParam T - Value type */ declare function asClassOrProvider<T>(target: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>): BindingTemplate<T>; /** * Convert binding scope and tags as a template function * @param scopeAndTags - Binding scope and tags * * @typeParam T - Value type */ declare function asBindingTemplate<T = unknown>(scopeAndTags: BindingScopeAndTags): BindingTemplate<T>; /** * Get binding metadata for a class * @param target - The target class * * @typeParam T - Value type */ declare function getBindingMetadata<T = unknown>(target: Constructor<unknown>): BindingMetadata<T> | undefined; /** * A binding template function to delete `name` and `key` tags */ declare function removeNameAndKeyTags(binding: Binding<unknown>): void; /** * Get the binding template for a class with binding metadata * * @param cls - A class with optional `@injectable` * * @typeParam T - Value type */ declare function bindingTemplateFor<T>(cls: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>, options?: BindingFromClassOptions): BindingTemplate<T>; /** * Mapping artifact types to binding key namespaces (prefixes). * * @example * ```ts * { * repository: 'repositories' * } * ``` */ type TypeNamespaceMapping = { [name: string]: string; }; declare const DEFAULT_TYPE_NAMESPACES: TypeNamespaceMapping; /** * Options to customize the binding created from a class */ type BindingFromClassOptions = { /** * Binding key */ key?: BindingAddress; /** * Artifact type, such as `server`, `controller`, `repository` or `service` */ type?: string; /** * Artifact name, such as `my-rest-server` and `my-controller`. It * overrides the name tag */ name?: string; /** * Namespace for the binding key, such as `servers` and `controllers`. It * overrides the default namespace or namespace tag */ namespace?: string; /** * Mapping artifact type to binding key namespaces */ typeNamespaceMapping?: TypeNamespaceMapping; /** * Default namespace if the binding does not have an explicit namespace */ defaultNamespace?: string; /** * Default scope if the binding does not have an explicit scope */ defaultScope?: BindingScope; }; /** * Create a binding from a class with decorated metadata. The class is attached * to the binding as follows: * - `binding.toClass(cls)`: if `cls` is a plain class such as `MyController` * - `binding.toProvider(cls)`: if `cls` is a value provider class with a * prototype method `value()` * - `binding.toDynamicValue(cls)`: if `cls` is a dynamic value provider class * with a static method `value()` * * @param cls - A class. It can be either a plain class, a value provider class, * or a dynamic value provider class * @param options - Options to customize the binding key * * @typeParam T - Value type */ declare function createBindingFromClass<T>(cls: Constructor<T | Provider<T>> | DynamicValueProviderClass<T>, options?: BindingFromClassOptions): Binding<T>; export { BINDING_METADATA_KEY, type BindingFromClassOptions, type BindingMetadata, type BindingScopeAndTags, type BindingSpec, DEFAULT_TYPE_NAMESPACES, type TypeNamespaceMapping, asBindingTemplate, asClassOrProvider, asProvider, bindingTemplateFor, createBindingFromClass, getBindingMetadata, isProviderClass, removeNameAndKeyTags };