https://github.com/betula/node-shared
Shared logic for Node.JS
https://github.com/betula/node-shared
dependency-injection dependency-provider di modules-architecture shared shared-logic
Last synced: 5 months ago
JSON representation
Shared logic for Node.JS
- Host: GitHub
- URL: https://github.com/betula/node-shared
- Owner: betula
- License: mit
- Created: 2019-06-07T04:07:37.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-02-22T14:25:28.000Z (over 4 years ago)
- Last Synced: 2025-05-01T17:39:31.409Z (5 months ago)
- Topics: dependency-injection, dependency-provider, di, modules-architecture, shared, shared-logic
- Language: TypeScript
- Homepage:
- Size: 359 KB
- Stars: 16
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-shared
[](https://www.npmjs.com/package/node-shared) [](https://bundlephobia.com/result?p=node-shared) [](./src/index.ts)
Service provider for Node.JS without pain with Dependency Injection Container, dependency registration, and configuration.
- You can use it at any place of your application without rewrite your applications architecture or other preparations or initializations.
- Each dependency can be class or function.
- You can override your dependencies for unit testing without hack standard Node.JS require mechanism.
- You can use TypeScript or JavaScript.## Examples
```javascript
import { shared } from "node-shared";// It is can be service or shared state or cross requests data
class Users {
async getById(id) {
...
}
}const sharedUsers = () => shared(Users);
// And with express for example
app.get("/user/:id", async (req, res) => {
const users = sharedUsers();
res.json(await users.getById(req.params.id));
});
```And
```javascript
import { shared } from "node-shared";
// ...class Db { /* ... */ }
class Server { /* ... */ }
// ...// Inject dependencies using a shared function and class properties
export default class App {
db = shared(Db);
server = shared(Server);
// ...
start() {
this.db.init();
// ...
}
}// index.ts
new App().start(); // You can create an instance directly as usually class
```## Unit testing
You can use `mock` to provide mocks into your dependencies.
```javascript
import { mock } from "node-shared";
import { Users, Api } from "./shareds";test("Users service should call api service inside", async () => {
const apiMock = mock(Api, {
getUserById: jest.fn();
});const users = new Users();
await users.getById("John");
expect(apiMock.getUserById).toHaveBeenCalledWith("John");
});
```If you use Jest for unit testing you need to add some code to your `jest.config.json` file.
```javascript
// jest.config.json
{
...
setupFilesAfterEnv: [
"node-shared/jest"
],
...
}
```This code means that after each test cached shareds instances will be clear. For another testing frameworks, you need call `free` after each test case manually for cleanup cached instances of dependencies.
```javascript
afterEach(require("node-shared").free);
```## API Reference
**shared**
Returns instance of your dependency. Each dependency can be class or function.
- For class. The class will be instantiated once and cached
- For function. The function will be called and result cached```javascript
const db = shared(Db);
```**mock**
Define resolved value for any dependency.
```javascript
const mockedDb = mock(Db, {
connect: jest.fn()
});shared(Db).connect();
expect(mockedDb.connect).toHaveBeenCalled();
```**free**
Clean all cached shared instances. It's needed for testing usually. Has no parameters.
```javascript
free()
```## Install
```sh
npm i node-shared
```Enjoy and Happy Coding!