Installations
npm install @teamteanpm2024/distinctio-voluptate-veritatis
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
20.12.2
NPM Version
10.5.0
Releases
Unable to fetch releases
Total Downloads
Cumulative downloads
Total Downloads
378
Last day
0%
1
Compared to previous day
Last week
-25%
3
Compared to previous week
Last month
41.7%
17
Compared to previous month
Last year
0%
378
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
37
GENERAL
An implementation of Dependency Injection pattern in a way, which enables its usages in a simplest way possible.
[!NOTE] DI decreases interdependence between modules, what leads to simplifying of tests writing and application logic.
Looks as follows:
1// Module's dependencies listed as function parameters. 2 3// user_controller.js - a SYNCHRONOUS module: 4exports.UserController = ($conf, DataProvider) => { // <--- dependencies are in braces 5 // ... 6 router.get("/get-some-data", async (req, res, next) => { 7 // ... 8 const result = await DataProvider.getSomeData(); 9 // ... 10 }); 11 return router; 12} 13 14// data_provider.js - an ASYNCHRONOUS module: 15exports.DataProvider = async ($conf) => { // <--- dependencies are in braces 16 // YOUR PREFERRED DB CONNECTOR 17 return connector; 18} 19 20// server.js 21const express = require("express"); 22const di = require("@teamteanpm2024/distinctio-voluptate-veritatis"); 23 24di.init((ctx) => { 25 ctx.registerOne(require(process.env.APP_CONFIG_PATH), "$conf"); 26 ctx.registerAll(require("./data_provider")); 27 ctx.registerAll(require("./user_controller")); 28 return ctx.invoke(); 29 // use an object destruction to get a list of specific modules here, when needed 30}).then(({ $conf, UserController }) => { // <---- 31 const server = express(); 32 // ... 33 server.use("/user", UserController); 34 // ... 35 server.listen($conf.server.port, () => { 36 console.log(`Server is listening at ${$conf.server.port} port.`); 37 }); 38});
EXAMPLE
A runnable working example could be found here.
USAGE
@teamteanpm2024/distinctio-voluptate-veritatis exposes next 4 functions to the end user:
To define a dependency, create a function and export it under any name. Names MUST be unique among each other.
1) SYNCHRONOUS DEPENDENCY:
1exports.SomeModule = () => { /**/ }
2) ASYNCHRONOUS DEPENDENCY:
1exports.SomeModule = async () => { /**/ } 2 3// or 4 5exports.SomeModule = () => { 6 // ... 7 return new Promise((resolve, reject) => { /**/ }); 8}
[!NOTE] If a function returns a Promise, it will be automatically converted into async dependency.
3) SETTING DEPENDENCIES LIST:
To define module's dependencies, list them as typical function parameters, assuming they are defined in the same way, described already above. They can have a list of it's own dependencies too:
1exports.Module_A = (Module_B, Module_C, ... Module_N) => { /**/ }
4) MULTIPLE DEPENDENCIES IN A SINGLE FILE:
There is nothing special about this, since it is a plain Node.js export:
1exports.Module_A = (Module_C, Module_B) => { /**/ } 2exports.Module_B = (Module_C) => { /**/ } 3exports.Module_C = () => { /**/ }
5) CONTEXT BUILD UP:
1const di = require("@teamteanpm2024/distinctio-voluptate-veritatis"); 2 3di.init((ctx) => { 4 ctx.registerAll(require("./some_module_1")); 5 ctx.registerAll(require("./some_module_2")); 6 return ctx.invoke(); 7}).then((ctx) => { 8 // ... 9});
di.init resolves with an object which provides an access to all registered modules. An object destruction could be applied for receiving a list of specific modules:
1di.init((ctx) => { 2 // ... 3 return ctx.invoke(); 4}).then(({ module_1, modul_2, module_n }) => {});
[!NOTE] Before returning a result, ctx.invoke() will wait for all asynchronous dependencies to resolve.
6) ANONYMOUS MODULE EXPORT:
It is also possible to register an anonymous function or an object, but a name of a module MUST be set additionally. For this purpose serves registerOne:
1// module_a.js 2module.exports = () => { /**/ } 3 4// index.js 5di.init((ctx) => { 6 ctx.registerOne(require("./module_a"), "MyModule"); 7 return ctx.invoke(); 8}).then((ctx) => { 9 // ... 10});
MODULE API
init(callback): Promise
1/** 2 * @param { Function } callback - A function to provide a context with. 3 * 4 * @returns Must return a result(Promise) of *** invoke *** call. 5*/
registerAll(dependencies): undefined
1/** 2 * @param { Object } dependencies - An map of dependencies. 3 * 4 * @returns undefined. 5*/
registerOne(dependency, name): undefined
1/** 2 * @param { Object } dependency - A single dependency map. 3 * @param { String } name - A module name to set. 4 * 5 * @returns undefined. 6*/
invoke(): Promise
1/** 2 * @returns A Promise, which resolves with DI context object. 3*/
No vulnerabilities found.
No security vulnerabilities found.