@universal-packages/dynamic-api
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -1,1 +0,2 @@ | ||
export declare function Dynamic<D = Record<string, any>>(name: keyof D, defaultDynamic?: boolean): (target: any) => void; | ||
export declare function Dynamic<D = Record<string, any>>(name: keyof D, defaultDynamic?: boolean): any; | ||
export declare function Dynamic<D = Record<string, any>>(module: string, name: keyof D, defaultDynamic?: boolean): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Dynamic = void 0; | ||
function Dynamic(name, defaultDynamic) { | ||
function Dynamic(moduleOrName, nameOrDefaultDynamic, defaultDynamic) { | ||
let finalModule = String(moduleOrName); | ||
let finalName = String(nameOrDefaultDynamic); | ||
let finalDefaultDynamic = defaultDynamic; | ||
if (nameOrDefaultDynamic === undefined && defaultDynamic === undefined) { | ||
finalName = String(moduleOrName); | ||
finalModule = undefined; | ||
} | ||
else if (typeof nameOrDefaultDynamic === 'boolean') { | ||
finalName = String(moduleOrName); | ||
finalModule = undefined; | ||
finalDefaultDynamic = nameOrDefaultDynamic; | ||
} | ||
return (target) => { | ||
target.__name = name; | ||
target.__defaultDynamic = defaultDynamic; | ||
target.__module = finalModule; | ||
target.__name = finalName; | ||
target.__defaultDynamic = finalDefaultDynamic; | ||
}; | ||
@@ -9,0 +22,0 @@ } |
@@ -9,3 +9,3 @@ "use strict"; | ||
this.dynamics = {}; | ||
this.options = { debug: process.env['NODE_ENV'] === 'test', dynamicsLocation: './src', ...options }; | ||
this.options = { debug: process.env['NODE_ENV'] === 'test', dynamicsLocation: './src', modules: [], ...options }; | ||
if (this.options.debug && process.env['NODE_ENV'] !== 'test' && process.env['NODE_ENV'] !== 'development') { | ||
@@ -34,6 +34,22 @@ const message = `dynamic api (${this.options.apiName || this.constructor.name || 'unnamed'}) debug mode is enabled`; | ||
const DynamicClass = currentModule.exports; | ||
let moduleOptions = {}; | ||
if (DynamicClass) { | ||
if (DynamicClass.__module) { | ||
const dynamicModuleEntry = this.options.modules.find((module) => module.name === DynamicClass.__module); | ||
// Skip if module is not enabled | ||
if (!dynamicModuleEntry) | ||
continue; | ||
if (dynamicModuleEntry.enabled !== true) | ||
continue; | ||
moduleOptions = dynamicModuleEntry.options || {}; | ||
} | ||
if (DynamicClass.__name) { | ||
DynamicClass.__api = this.constructor; | ||
const dynamicEntry = this.dynamics[DynamicClass.__name] || { afterHooks: [], beforeHooks: [], implementations: [], name: DynamicClass.__name }; | ||
const dynamicEntry = this.dynamics[DynamicClass.__name] || { | ||
afterHooks: [], | ||
beforeHooks: [], | ||
implementations: [], | ||
name: DynamicClass.__name, | ||
moduleOptions | ||
}; | ||
if (DynamicClass.__defaultDynamic) { | ||
@@ -79,10 +95,10 @@ if (!dynamicEntry.default) | ||
debugEntry.hooks.before.push(dynamicEntry.beforeHooks[i]); | ||
await this.perform(dynamicEntry.beforeHooks[i], payload); | ||
await this.perform(dynamicEntry.beforeHooks[i], payload, dynamicEntry.moduleOptions); | ||
} | ||
if (this.options.accumulate) { | ||
if (dynamicEntry.default) | ||
results.push(await this.perform(dynamicEntry.default, payload)); | ||
results.push(await this.perform(dynamicEntry.default, payload, dynamicEntry.moduleOptions)); | ||
for (let i = 0; i < dynamicEntry.implementations.length; i++) { | ||
const CurrentImplementation = dynamicEntry.implementations[i]; | ||
results.push(await this.perform(CurrentImplementation, payload)); | ||
results.push(await this.perform(CurrentImplementation, payload, dynamicEntry.moduleOptions)); | ||
} | ||
@@ -92,6 +108,6 @@ } | ||
if (dynamicEntry.implementations[0]) { | ||
results.push(await this.perform(dynamicEntry.implementations[0], payload)); | ||
results.push(await this.perform(dynamicEntry.implementations[0], payload, dynamicEntry.moduleOptions)); | ||
} | ||
else if (dynamicEntry.default) { | ||
results.push(await this.perform(dynamicEntry.default, payload)); | ||
results.push(await this.perform(dynamicEntry.default, payload, dynamicEntry.moduleOptions)); | ||
} | ||
@@ -110,3 +126,3 @@ else { | ||
debugEntry.hooks.after.push(dynamicEntry.afterHooks[i]); | ||
await this.perform(dynamicEntry.afterHooks[i], payload, true, this.options.accumulate ? results : results[0]); | ||
await this.perform(dynamicEntry.afterHooks[i], payload, dynamicEntry.moduleOptions, this.options.accumulate ? results : results[0]); | ||
} | ||
@@ -122,6 +138,6 @@ if (this.options.debug) | ||
} | ||
async perform(DynamicClass, payload, shareResult = false, result) { | ||
const instance = new DynamicClass(); | ||
async perform(DynamicClass, payload, options, result) { | ||
const instance = new DynamicClass(options); | ||
if (instance.perform) { | ||
if (shareResult) { | ||
if (result) { | ||
return await instance.perform(payload, result, this); | ||
@@ -147,10 +163,10 @@ } | ||
debugEntry.hooks.before.push(dynamicEntry.beforeHooks[i]); | ||
this.performSync(dynamicEntry.beforeHooks[i], payload); | ||
this.performSync(dynamicEntry.beforeHooks[i], payload, dynamicEntry.moduleOptions); | ||
} | ||
if (this.options.accumulate) { | ||
if (dynamicEntry.default) | ||
results.push(this.performSync(dynamicEntry.default, payload)); | ||
results.push(this.performSync(dynamicEntry.default, payload, dynamicEntry.moduleOptions)); | ||
for (let i = 0; i < dynamicEntry.implementations.length; i++) { | ||
const CurrentImplementation = dynamicEntry.implementations[i]; | ||
results.push(this.performSync(CurrentImplementation, payload)); | ||
results.push(this.performSync(CurrentImplementation, payload, dynamicEntry.moduleOptions)); | ||
} | ||
@@ -160,6 +176,6 @@ } | ||
if (dynamicEntry.implementations[0]) { | ||
results.push(this.performSync(dynamicEntry.implementations[0], payload)); | ||
results.push(this.performSync(dynamicEntry.implementations[0], payload, dynamicEntry.moduleOptions)); | ||
} | ||
else if (dynamicEntry.default) { | ||
results.push(this.performSync(dynamicEntry.default, payload)); | ||
results.push(this.performSync(dynamicEntry.default, payload, dynamicEntry.moduleOptions)); | ||
} | ||
@@ -178,3 +194,3 @@ else { | ||
debugEntry.hooks.after.push(dynamicEntry.afterHooks[i]); | ||
this.performSync(dynamicEntry.afterHooks[i], payload, true, this.options.accumulate ? results : results[0]); | ||
this.performSync(dynamicEntry.afterHooks[i], payload, dynamicEntry.moduleOptions, this.options.accumulate ? results : results[0]); | ||
} | ||
@@ -190,6 +206,6 @@ if (this.options.debug) | ||
} | ||
performSync(DynamicClass, payload, shareResult = false, result) { | ||
const instance = new DynamicClass(); | ||
performSync(DynamicClass, payload, options, result) { | ||
const instance = new DynamicClass(options); | ||
if (instance.perform) { | ||
if (shareResult) { | ||
if (result) { | ||
return instance.perform(payload, result, this); | ||
@@ -196,0 +212,0 @@ } |
{ | ||
"name": "@universal-packages/dynamic-api", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "Dynamic decoupling-adapting system", | ||
@@ -5,0 +5,0 @@ "author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)", |
@@ -61,2 +61,25 @@ # Dynamic API | ||
-- **`modules`** `Array` | ||
When decorating dynamics you can mark them as part of a module, you need to enable modules explicitly in the dynamic api. | ||
```js | ||
import { Dynamic } from '@universal-packages/dynamic-api' | ||
@Dynamic('sub-calculations', 'extra') | ||
export default class CalculateDynamic { | ||
async perform(payload) { | ||
return 'I am an extra calculation' | ||
} | ||
} | ||
``` | ||
```js | ||
const dynamicApi = new DynamicApi({ modules: [{ name: 'sub-calculations', enabled: true }] }) | ||
const result = await dynamicApi.performDynamic('extra') | ||
console.log(result) | ||
// > 'I am an extra calculation' | ||
``` | ||
### Instance methods | ||
@@ -74,4 +97,7 @@ | ||
#### **`@Dynamic(name: string, [default: boolean])`** | ||
### @Dynamic | ||
`@Dynamic(name: string, [default: boolean])` | ||
`@Dynamic(module: string, name: string, [default: boolean])` | ||
Dynamics are classes as a default export, decorated with `@Dynamic` decorator and implementing the method `perform`. | ||
@@ -130,2 +156,15 @@ | ||
You can make a dynamic part of a module by providing a module name as the first argument. | ||
```js | ||
import { Dynamic } from '@universal-packages/dynamic-api' | ||
@Dynamic('sub-calculations', 'extra') | ||
export default class CalculateDynamic { | ||
async perform(payload) { | ||
return 'I am an extra calculation' | ||
} | ||
} | ||
``` | ||
#### **`@DynamicHook(lifeCycle: before | after, name: string)`** | ||
@@ -132,0 +171,0 @@ |
@@ -9,4 +9,10 @@ import DynamicApi from './DynamicApi'; | ||
dynamicsLocation: string; | ||
modules?: DynamicModule[]; | ||
namespace?: string; | ||
} | ||
export interface DynamicModule { | ||
name: string; | ||
enabled: boolean; | ||
options?: Record<string, any>; | ||
} | ||
export interface DynamicRegistry { | ||
@@ -18,2 +24,3 @@ afterHooks: DynamicClassLike[]; | ||
name: string; | ||
moduleOptions: Record<string, any>; | ||
} | ||
@@ -27,2 +34,3 @@ export interface Dynamics { | ||
export interface DynamicClassLike { | ||
__module: string; | ||
__name: string; | ||
@@ -29,0 +37,0 @@ __defaultDynamic: boolean; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
37164
349
272