This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+51
View File
@@ -0,0 +1,51 @@
/**
* Shape of the extend callback
*/
export declare type ExtendCallback<Manager extends ManagerContract<any, any>, Driver extends any, Name extends any> = (manager: Manager, mappingName: Name, config: any) => Driver;
/**
* Manager class shape
*/
export interface ManagerContract<Application extends any, DriverContract extends any, MappingValue extends any = DriverContract, MappingsList extends {
[key: string]: MappingValue;
} = any> {
application: Application;
/**
* Returns concrete type when binding name is from the mappings list
*/
use<K extends keyof MappingsList>(name: K): MappingsList[K];
/**
* Return a overload of mapping when no key is defined
*/
use(): {
[K in keyof MappingsList]: MappingsList[K];
}[keyof MappingsList];
/**
* Extend by adding a new custom driver
*/
extend(name: string, callback: ExtendCallback<this, DriverContract, keyof MappingsList>): void;
/**
* Release bindings from cache
*/
release<K extends keyof MappingsList>(name: K): void;
release(name: string): void;
}
/**
* Extracts and builds a list of mappings implementation
*/
export declare type ExtractImplementations<List extends {
[key: string]: {
implementation: any;
};
}> = {
[P in keyof List]: List[P]['implementation'];
};
/**
* Extracts and builds a list of mappings implementation
*/
export declare type ExtractConfig<List extends {
[key: string]: {
config: any;
};
}> = {
[P in keyof List]: List[P]['config'];
};
+10
View File
@@ -0,0 +1,10 @@
"use strict";
/*
* @poppinss/manager
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
+92
View File
@@ -0,0 +1,92 @@
import { ManagerContract, ExtendCallback } from './Contracts';
/**
* Manager class implements the Builder pattern to make instance of similar
* implementations using a fluent API vs importing each class by hand.
*
* This module is used extensively in AdonisJs. For example: `Mail`, `Sessions`,
* `Auth` and so on.
*/
export declare abstract class Manager<Application extends any, DriverContract extends any, MappingValue extends any = DriverContract, MappingsList extends {
[key: string]: MappingValue;
} = any> implements ManagerContract<Application, DriverContract, MappingValue, MappingsList> {
application: Application;
/**
* Mappings cache (if caching is enabled)
*/
private mappingsCache;
/**
* A cache to store the function names for initiating driver instances.
*/
private driverCreatorNames;
/**
* List of drivers added at runtime
*/
private extendedDrivers;
/**
* Whether or not to cache mappings
*/
protected abstract singleton: boolean;
/**
* Getting the default mapping name, incase a mapping
* is not defined
*/
protected abstract getDefaultMappingName(): keyof MappingsList;
/**
* Getting config for the mapping. It is required for making
* extended drivers
*/
protected abstract getMappingConfig(mappingName: keyof MappingsList): any | undefined;
/**
* Getting the driver name for the mapping
*/
protected abstract getMappingDriver(mappingName: keyof MappingsList): string | undefined;
constructor(application: Application);
/**
* Returns the value saved inside cache, this method will check for
* `cacheDrivers` attribute before entertaining the cache
*/
private getFromCache;
/**
* Saves value to the cache with the driver name. This method will check for
* `cacheDrivers` attribute before entertaining the cache.
*/
private saveToCache;
/**
* Make the extended driver instance and save it to cache (if enabled)
*/
private makeExtendedDriver;
/**
* Returns the creator function name for a given driver.
*/
private getDriverCreatorName;
/**
* Make the custom driver instance by checking for function on the
* parent class.
*
* For example: `stmp` as the driver name will look for `createSmtp`
* method on the parent class.
*/
private makeDriver;
/**
* Optional method to wrap the driver response
*/
protected wrapDriverResponse(_: keyof MappingsList, value: DriverContract): MappingValue;
/**
* Returns the instance of a given driver. If `name` is not defined
* the default driver will be resolved.
*/
use<K extends keyof MappingsList & string>(name: K): MappingsList[K];
use(): {
[K in keyof MappingsList]: MappingsList[K];
}[keyof MappingsList];
/**
* Removes the mapping from internal cache.
*/
release<K extends keyof MappingsList & string>(name: K): void;
release(name: string): void;
/**
* Extend by adding new driver. The compositon of driver
* is the responsibility of the callback function
*/
extend(name: string, callback: ExtendCallback<this, DriverContract, keyof MappingsList>): void;
}
+137
View File
@@ -0,0 +1,137 @@
"use strict";
/*
* @poppinss/manager
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Manager = void 0;
/**
* The simplest implementation of capitalizing a string
*/
const capitalize = (value) => {
if (!value) {
return value;
}
return value.charAt(0).toUpperCase() + value.slice(1);
};
/**
* Manager class implements the Builder pattern to make instance of similar
* implementations using a fluent API vs importing each class by hand.
*
* This module is used extensively in AdonisJs. For example: `Mail`, `Sessions`,
* `Auth` and so on.
*/
class Manager {
constructor(application) {
this.application = application;
/**
* Mappings cache (if caching is enabled)
*/
this.mappingsCache = new Map();
/**
* A cache to store the function names for initiating driver instances.
*/
this.driverCreatorNames = new Map();
/**
* List of drivers added at runtime
*/
this.extendedDrivers = {};
}
/**
* Returns the value saved inside cache, this method will check for
* `cacheDrivers` attribute before entertaining the cache
*/
getFromCache(name) {
return this.mappingsCache.get(name) || null;
}
/**
* Saves value to the cache with the driver name. This method will check for
* `cacheDrivers` attribute before entertaining the cache.
*/
saveToCache(name, value) {
if (this.singleton) {
this.mappingsCache.set(name, value);
}
}
/**
* Make the extended driver instance and save it to cache (if enabled)
*/
makeExtendedDriver(mappingName, driver, config) {
const value = this.wrapDriverResponse(mappingName, this.extendedDrivers[driver](this, mappingName, config));
this.saveToCache(mappingName, value);
return value;
}
/**
* Returns the creator function name for a given driver.
*/
getDriverCreatorName(driver) {
if (!this.driverCreatorNames.has(driver)) {
this.driverCreatorNames.set(driver, `create${capitalize(driver.replace(/-\w|_\w/g, (g) => g.substr(1).toUpperCase()))}`);
}
return this.driverCreatorNames.get(driver);
}
/**
* Make the custom driver instance by checking for function on the
* parent class.
*
* For example: `stmp` as the driver name will look for `createSmtp`
* method on the parent class.
*/
makeDriver(mappingName, driver, config) {
const driverCreatorName = this.getDriverCreatorName(driver);
/**
* Raise error when the parent class doesn't implement the function
*/
if (typeof this[driverCreatorName] !== 'function') {
throw new Error(`"${driver}" driver is not supported by "${this.constructor.name}"`);
}
const value = this.wrapDriverResponse(mappingName, this[driverCreatorName](mappingName, config));
this.saveToCache(mappingName, value);
return value;
}
/**
* Optional method to wrap the driver response
*/
wrapDriverResponse(_, value) {
return value;
}
use(name) {
const mappingName = name || this.getDefaultMappingName();
const cached = this.getFromCache(mappingName);
if (cached) {
return cached;
}
/**
* Ensure that driver exists for a given mapping
*/
const driver = this.getMappingDriver(mappingName);
if (!driver) {
throw new Error(`Make sure to define driver for "${mappingName}" mapping`);
}
/**
* Making the extended driver
*/
if (this.extendedDrivers[driver]) {
return this.makeExtendedDriver(mappingName, driver, this.getMappingConfig(mappingName));
}
/**
* Making the predefined driver
*/
return this.makeDriver(mappingName, driver, this.getMappingConfig(mappingName));
}
release(name) {
this.mappingsCache.delete(name);
}
/**
* Extend by adding new driver. The compositon of driver
* is the responsibility of the callback function
*/
extend(name, callback) {
this.extendedDrivers[name] = callback;
}
}
exports.Manager = Manager;