@poppinss/manager
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -0,2 +1,5 @@ | ||
/** | ||
* @module @poppinss/manager | ||
*/ | ||
export { Manager } from './src/Manager'; | ||
export * from './src/contracts'; | ||
export * from './src/Contracts'; |
"use strict"; | ||
/** | ||
* @module @poppinss/manager | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* | ||
* @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. | ||
*/ | ||
var Manager_1 = require("./src/Manager"); | ||
exports.Manager = Manager_1.Manager; |
@@ -1,2 +0,12 @@ | ||
import { ManagerContract } from './contracts'; | ||
/** | ||
* @module @poppinss/manager | ||
*/ | ||
import { ManagerContract } 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<DriverContract extends any, ReturnValueContract extends any = DriverContract, MappingsList extends { | ||
@@ -8,20 +18,72 @@ [key: string]: ReturnValueContract; | ||
protected $container: any; | ||
private _mappingsCache; | ||
private _extendedDrivers; | ||
/** | ||
* Mappings cache (if caching is enabled) | ||
*/ | ||
private mappingsCache; | ||
/** | ||
* List of drivers added at runtime | ||
*/ | ||
private extendedDrivers; | ||
/** | ||
* Whether or not to cache mappings | ||
*/ | ||
protected abstract $cacheMappings: boolean; | ||
/** | ||
* Getting the default mapping name, incase a mapping | ||
* is not defined | ||
*/ | ||
protected abstract getDefaultMappingName(): string; | ||
/** | ||
* Getting config for the mapping. It is required for making | ||
* extended drivers | ||
*/ | ||
protected abstract getMappingConfig(mappingName: string): any | undefined; | ||
/** | ||
* Getting the driver name for the mapping | ||
*/ | ||
protected abstract getMappingDriver(mappingName: string): string | undefined; | ||
constructor($container: any); | ||
private _getFromCache; | ||
private _saveToCache; | ||
private _makeExtendedDriver; | ||
private _makeDriver; | ||
protected wrapDriverResponse(_mappingName: string, value: DriverContract): ReturnValueContract; | ||
/** | ||
* 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; | ||
/** | ||
* 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(_: string, value: DriverContract): ReturnValueContract; | ||
/** | ||
* Returns the instance of a given driver. If `name` is not defined | ||
* the default driver will be resolved. | ||
*/ | ||
use<K extends keyof MappingsList>(name: K): MappingsList[K]; | ||
use(name: string): ReturnValueContract; | ||
use(): DefaultItem; | ||
/** | ||
* Removes the mapping from internal cache. | ||
*/ | ||
release<K extends keyof MappingsList>(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: (container: any, mappingName: string, config: any) => DriverContract): void; | ||
} |
"use strict"; | ||
/** | ||
* @module @poppinss/manager | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* 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($container) { | ||
this.$container = $container; | ||
this._mappingsCache = new Map(); | ||
this._extendedDrivers = {}; | ||
/** | ||
* Mappings cache (if caching is enabled) | ||
*/ | ||
this.mappingsCache = new Map(); | ||
/** | ||
* List of drivers added at runtime | ||
*/ | ||
this.extendedDrivers = {}; | ||
} | ||
_getFromCache(name) { | ||
return this._mappingsCache.get(name) || null; | ||
/** | ||
* 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; | ||
} | ||
_saveToCache(name, value) { | ||
/** | ||
* 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.$cacheMappings) { | ||
this._mappingsCache.set(name, value); | ||
this.mappingsCache.set(name, value); | ||
} | ||
} | ||
_makeExtendedDriver(mappingName, driver, config) { | ||
const value = this.wrapDriverResponse(mappingName, this._extendedDrivers[driver](this.$container, mappingName, config)); | ||
this._saveToCache(mappingName, 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.$container, mappingName, config)); | ||
this.saveToCache(mappingName, value); | ||
return value; | ||
} | ||
_makeDriver(mappingName, driver, config) { | ||
/** | ||
* 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 = `create${driver.replace(/^\w|-\w/g, (g) => g.replace(/^-/, '').toUpperCase())}`; | ||
/** | ||
* Raise error when the parent class doesn't implement the function | ||
*/ | ||
if (typeof (this[driverCreatorName]) !== 'function') { | ||
@@ -28,6 +65,9 @@ throw new Error(`${mappingName} driver is not supported by ${this.constructor.name}`); | ||
const value = this.wrapDriverResponse(mappingName, this[driverCreatorName](mappingName, config)); | ||
this._saveToCache(mappingName, value); | ||
this.saveToCache(mappingName, value); | ||
return value; | ||
} | ||
wrapDriverResponse(_mappingName, value) { | ||
/** | ||
* Optional method to wrap the driver response | ||
*/ | ||
wrapDriverResponse(_, value) { | ||
return value; | ||
@@ -37,6 +77,9 @@ } | ||
name = (name || this.getDefaultMappingName()); | ||
const cached = this._getFromCache(name); | ||
const cached = this.getFromCache(name); | ||
if (cached) { | ||
return cached; | ||
} | ||
/** | ||
* Ensure that driver exists for a given mapping | ||
*/ | ||
const driver = this.getMappingDriver(name); | ||
@@ -46,14 +89,24 @@ if (!driver) { | ||
} | ||
if (this._extendedDrivers[driver]) { | ||
return this._makeExtendedDriver(name, driver, this.getMappingConfig(name)); | ||
/** | ||
* Making the extended driver | ||
*/ | ||
if (this.extendedDrivers[driver]) { | ||
return this.makeExtendedDriver(name, driver, this.getMappingConfig(name)); | ||
} | ||
return this._makeDriver(name, driver, this.getMappingConfig(name)); | ||
/** | ||
* Making the predefined driver | ||
*/ | ||
return this.makeDriver(name, driver, this.getMappingConfig(name)); | ||
} | ||
release(name) { | ||
this._mappingsCache.delete(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; | ||
this.extendedDrivers[name] = callback; | ||
} | ||
} | ||
exports.Manager = Manager; |
{ | ||
"name": "@poppinss/manager", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "The builder (Manager) pattern implementation", | ||
"main": "build/index.js", | ||
"files": [ | ||
"build/src", | ||
"build/index.d.ts", | ||
"build/index.js" | ||
], | ||
"scripts": { | ||
@@ -9,3 +15,3 @@ "mrm": "mrm --preset=@adonisjs/mrm-preset", | ||
"test": "node japaFile.js", | ||
"lint": "tslint --project tsconfig.json", | ||
"lint": "eslint . --ext=.ts", | ||
"clean": "del build", | ||
@@ -25,4 +31,4 @@ "compile": "npm run lint && npm run clean && tsc", | ||
"devDependencies": { | ||
"@adonisjs/mrm-preset": "^2.1.0", | ||
"@types/node": "^12.7.5", | ||
"@adonisjs/mrm-preset": "^2.2.3", | ||
"@types/node": "^12.12.21", | ||
"commitizen": "^4.0.3", | ||
@@ -32,13 +38,13 @@ "cz-conventional-changelog": "^3.0.2", | ||
"doctoc": "^1.4.0", | ||
"husky": "^3.0.5", | ||
"eslint": "^6.7.2", | ||
"eslint-plugin-adonis": "^1.0.4", | ||
"husky": "^3.1.0", | ||
"japa": "^3.0.1", | ||
"mrm": "^1.2.2", | ||
"np": "^5.1.0", | ||
"ts-node": "^8.4.1", | ||
"tslint": "^5.20.0", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typedoc": "^0.15.0", | ||
"mrm": "^2.0.2", | ||
"np": "^5.2.1", | ||
"ts-node": "^8.5.4", | ||
"typedoc": "^0.15.5", | ||
"typedoc-plugin-external-module-name": "^2.1.0", | ||
"typedoc-plugin-markdown": "^2.2.4", | ||
"typescript": "^3.6.3" | ||
"typedoc-plugin-markdown": "^2.2.14", | ||
"typescript": "^3.7.3" | ||
}, | ||
@@ -53,8 +59,2 @@ "nyc": { | ||
}, | ||
"main": "build/index.js", | ||
"files": [ | ||
"build/src", | ||
"build/index.d.ts", | ||
"build/index.js" | ||
], | ||
"husky": { | ||
@@ -61,0 +61,0 @@ "hooks": { |
@@ -8,3 +8,3 @@ <div align="center"> | ||
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] | ||
[![circleci-image]][circleci-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] | ||
@@ -28,3 +28,3 @@ The module is used heavily by AdonisJs to build it's driver based features for components like `hash`, `mail`, `auth`, `social auth` and so on. | ||
- [Note](#note) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
@@ -39,12 +39,7 @@ - [Release mapping from cache](#release-mapping-from-cache) | ||
- [Updating mapping list generic for Manager](#updating-mapping-list-generic-for-manager) | ||
- [API Docs](#api-docs) | ||
- [Maintainers](#maintainers) | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
## Note | ||
The API for `2.x.x` is completely different from `1.x.x`, since the newer version supports multiple mappings of a single driver. [Click here](https://github.com/poppinss/manager/tree/v1.1.4) to check docs for older version. | ||
## Installation | ||
## Usage | ||
Install the package from npm registry as follows: | ||
@@ -59,2 +54,4 @@ | ||
## Usage | ||
Let's imagine we are building a mailer (with dummy implementation) that supports multiple drivers and each driver can be used for multiple times. Example config for same | ||
@@ -107,3 +104,3 @@ | ||
class Mailer extends Manager { | ||
constructor (container, private _config) { | ||
constructor (container, private config) { | ||
super(container) | ||
@@ -115,11 +112,11 @@ } | ||
protected getDefaultMappingName (): string { | ||
return this._config.mailer | ||
return this.config.mailer | ||
} | ||
protected getMappingConfig (name): any { | ||
return this._config.mailers[name] | ||
return this.config.mailers[name] | ||
} | ||
protected getMappingDriver (name): any { | ||
return this._config.mailers[name].driver | ||
return this.config.mailers[name].driver | ||
} | ||
@@ -141,3 +138,3 @@ | ||
- **getDefaultMappingName**: The name of the default mapping. In this case, it is the name of the `mailer` set in the config | ||
- **getMappingDriver**: Returning the config for a mapping. We pull it from the `mailers` object defined in the config. | ||
- **getMappingConfig**: Returning the config for a mapping. We pull it from the `mailers` object defined in the config. | ||
- **getMappingDriver**: Returning the driver for a mapping inside config. | ||
@@ -152,3 +149,3 @@ | ||
The `mailer.use('transactional')` will invoke `createSmtp` as part of the following convention. | ||
The `mailer.use('transactional')` will invoke `createTransactional` as part of the following convention. | ||
@@ -188,3 +185,3 @@ - `create` + `PascalCaseDriverName` | ||
In order for intellisense to work, you have do some ground work of defining additional types. This in-fact is a common theme with static languages, that you have to rely on loose coupling when creating or using extensible objects. | ||
In order for intellisense to work, you have to do some ground work of defining additional types. This in-fact is a common theme with static languages, that you have to rely on loose coupling when creating or using extensible objects. | ||
@@ -307,20 +304,12 @@ ### Driver interface | ||
## API Docs | ||
Following are the autogenerated files via Typedoc | ||
* [API](docs/README.md) | ||
## Maintainers | ||
[Harminder virk](https://github.com/thetutlage) | ||
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/manager/master.svg?style=for-the-badge&logo=circleci | ||
[circleci-url]: https://circleci.com/gh/poppinss/manager "circleci" | ||
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript | ||
[typescript-url]: "typescript" | ||
[npm-image]: https://img.shields.io/npm/v/@poppinss/manager.svg?style=for-the-badge&logo=npm | ||
[npm-url]: https://npmjs.org/package/@poppinss/manager "npm" | ||
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript | ||
[license-url]: LICENSE.md | ||
[license-image]: https://img.shields.io/aur/license/pac.svg?style=for-the-badge | ||
` | ||
[license-image]: https://img.shields.io/npm/l/@poppinss/manager?color=blueviolet&style=for-the-badge | ||
[license-url]: LICENSE.md "license" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19949
249
306
1