papaya
Version:
A minimal dependency injection container
93 lines (92 loc) • 3.46 kB
TypeScript
/**
* A Papaya container
*/
export declare class Papaya<T extends {
[name: string]: any;
} = any> {
private _services;
private _functions;
private _factories;
/**
* Gets a service by name.
*
* If the service is not set, will return undefined.
*
* @param name The service name
* @return The service or undefined if it is not set
*/
get<K extends keyof T>(name: K): T[K];
/**
* Creates a constant by name and value.
*
* @param name The service name
* @param constant The value to be set
*/
constant<K extends keyof T>(name: K, constant: T[K]): this;
/**
* Sets a service by name and a service function
*
* The return value of the `service` function will be treated as a singleton
* service meaning it will be initialized the first time it is requested and
* its value will be cached for subsequent requests.
*
* @param name The service name
* @param service The service singleton function or static service
*/
service<K extends keyof T>(name: K, service: (this: this, container: this) => T[K]): this;
/**
* Sets a factory service by name and value.
*
* The `factory` function will be called every time the service is requested.
* So if it returns an object, it will create a new object for every request.
*
* @param name The service name
* @param factory The service factory function or static service
*/
factory<K extends keyof T>(name: K, factory: (this: this, container: this) => T[K]): this;
/**
* Extends an existing service and overrides it.
*
* The `extender` function will be called with 2 argument which will be the
* previous value of the service and the Papaya container. If there is no
* existing `name` service, it will throw an error immediately. The
* `extender` function should return the new value for the service that will
* override the existing one.
*
* If `this.extend` is called for a service that was created with
* `this.service`, the resulting service will be a singleton.
*
* If `this.extend` is called for a service that was created with
* `this.factory` the resulting service will be a factory.
*
* If `this.extend` is called for a service that was created with
* `this.constant`, the resulting service will be a singleton.
*
* @param name The service name
* @param extender The service extender function or static service.
*/
extend<K extends keyof T>(name: string, extender: (this: this, extended: T[K], container: this) => T[K]): this;
/**
* Register a service provider function.
*
* `provider` will be called with the container as the context. Service
* providers are a good place to register related services using
* `this.service` etc.
*
* @param provider The service provider function
*/
register(provider: (this: this, container: Papaya<any>) => void): this;
/**
* Get an array of the regitered service names
*
* @return An array of service names
*/
keys(): Array<keyof T>;
/**
* Check whether a service has been registered for the given name'
*
* @return True if a service has been registered for `name`, false otherwise.
*/
has<K extends keyof T>(name: K): boolean;
private _setService(name, service, registry?);
}