UNPKG

3.88 kBMarkdownView Raw
1# Packet JS
2
3[![node](https://img.shields.io/node/v/@fdosruiz/packetjs.svg?maxAge=1000)](https://www.npmjs.com/package/@fdosruiz/packetjs)
4[![install size](https://packagephobia.com/badge?p=@fdosruiz/packetjs)](https://packagephobia.com/result?p=@fdosruiz/packetjs)
5[![npm downloads](https://img.shields.io/npm/dm/@fdosruiz/packetjs.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@fdosruiz/packetjs)
6[![Build status](https://github.com/fdosruiz/packetjs/actions/workflows/ci-cd.yml/badge.svg)](https://github.com/fdosruiz/packetjs/actions)
7[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/fdosruiz/packetjs/blob/main/LICENSE)
8[![Gihub package dependents](https://badgen.net/github/dependents-pkg/fdosruiz/packetjs)](https://github.com/fdosruiz/packetjs/network/dependents?dependent_type=PACKAGE)
9
10Packet JS is a micro-dependency injection container for JavaScript/Node applications, written in TypeScript and with lazy loading, instantiating each service on demand with dependency on each other.
11
12## Installation
13
14Using npm:
15
16```bash
17$ npm install @fdosruiz/packetjs
18```
19
20Using yarn:
21
22```bash
23$ yarn add @fdosruiz/packetjs
24```
25
26## Importing
27
28```javascript
29// Using Node.js `require()`
30const container = require('@fdosruiz/packetjs');
31
32// Using ES6 imports
33import container from '@fdosruiz/packetjs';
34```
35
36## Basic usage
37
38Registering a service:
39
40```javascript
41const container = require('@fdosruiz/packetjs'); // Get the container instance.
42
43container.add('Service', () => { /* here the logic */ return new SomeService() });
44```
45
46In some other place:
47
48```javascript
49const container = require('@fdosruiz/packetjs'); // Always get the same instance of the container. (singleton pattern)
50
51const service = container.get('Service'); // Make an instance of the service on demand, with lazy loading.
52```
53
54## Adding configuration properties
55
56It is possible to add configuration properties, for service registration and to make the configuration available throughout the application.
57
58```javascript
59const properties = require('./some-configuration-object-in-json-format');
60container.addProps(properties);
61```
62
63To get the properties when registering a service:
64
65```javascript
66container.add('Service', ({ props }) => { /* here the logic */ return new SomeService(props.someParam) });
67```
68
69Also, we can get all the properties:
70
71```javascript
72const props = container.getProps();
73```
74
75### Factory with multiples dependencies
76
77For configuring the dependency Injection container, the order of registration of each service is indifferent. The callback function receives the container and the properties as an object in the argument: `{ container, props }`:
78
79```javascript
80// Configuring some service
81container.add('service', ({ container: c }) => {
82 const dao = c.get('dao');
83 return new Service(dao);
84});
85
86// Configuring a dependency with dao
87container.add('dao', ({ container: c }) => {
88 const db = c.get('db');
89 return new dao(db);
90});
91
92// Configuring a database
93container.add('db', ({ container: c, props: p }) => {
94 return new someDatabase(process.env.USER, process.PASS, p.someConfigurationProp);
95});
96```
97
98In another place we can use the container like this:
99
100```javascript
101cosnt something = container.get('service').getSomeThing(id); // Makes an instance of the service with lazy loading for each service.
102```
103
104### Instantiating new containers
105
106If you need standalone containers, it is possible to create isolated instances by accessing the Core Container Class:
107
108```javascript
109const { Container } = require('@fdosruiz/packetjs/lib/core');
110const a = new Container();
111const b = new Container();
112// a !== b
113```
114
115## Credits
116
117Packet JS is inspired by [pimple](https://github.com/silexphp/Pimple), a small PHP dependency injection container.
118
119## License
120
121[MIT](https://github.com/fdosruiz/packetjs/blob/main/LICENSE)
122