@slimlib/injector 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Konstantin Shutkin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # injector
2
+
3
+ Parameter names based dependency injector for nodejs.
4
+
5
+ *Limitations*
6
+
7
+ - does not work with default parameters
8
+ - minification of code is not supported
9
+ - not typesafe
10
+ - classes not supported
11
+ - slower than normal function call
12
+
13
+ ## API
14
+
15
+ ### createInject()
16
+
17
+ returns new instance of an injector function to work with.
18
+
19
+ ### injector(function, scope)
20
+
21
+ injects arguments into function and invoke it
22
+
23
+ `function` - *required*, function to inject parameters and call
24
+ `scope` - *optional*, *default* = `{}`, this argument for the function
25
+
26
+ ### $provide(key, value)
27
+
28
+ predefined injectable function
29
+
30
+ `key` - string, required
31
+ `value` - unknown
32
+
33
+ to get it, inject it in the function
34
+
35
+ ```typescript
36
+ inject(($provide: Provider) => {
37
+ $provide('service', service);
38
+ });
39
+ ```
40
+
41
+ ## Example
42
+
43
+ ```typescript
44
+ import createInject from '@slimlib/injector';
45
+
46
+ const inject = createInject();
47
+
48
+ inject(($provide: Provider) => {
49
+ $provide('config', {
50
+ url: 'http://example.com/json',
51
+ format: 'json'
52
+ });
53
+ });
54
+
55
+ inject(async (config: Json) => {
56
+ const data = await fetch(config.url);
57
+ const result = config.json ? await data.json() : data;
58
+ // and so on
59
+ });
60
+ ```
61
+
62
+ # FAQ
63
+
64
+ 1. Is it good solution to mock something in unit tests?
65
+
66
+ - no, please use [jest](https://jestjs.io/), [vitest](https://vitest.dev/), [proxyquire](https://www.npmjs.com/package/proxyquire), [proxyrequire](https://www.npmjs.com/package/proxyrequire) and other similar approaches to mock modules.
67
+
68
+ 2. Is it good solution to use in frontend code?
69
+
70
+ - no, it will not work after minification
71
+
72
+ 3. Is it good for nodejs applications?
73
+
74
+ - only in some edge cases, please use singletones / factories / something else if possible
75
+
76
+ # License
77
+
78
+ [MIT](./LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ var getParameterNames = require('get-parameter-names');
4
+
5
+ var index = () => {
6
+ const dependencies = Object.create(null);
7
+ dependencies['$provide'] = (key, value) => {
8
+ dependencies[key] = value;
9
+ };
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ return (func, scope = {}) => func.apply(scope, getParameterNames(func)
12
+ .map((key) => dependencies[key]));
13
+ };
14
+
15
+ module.exports = index;
@@ -0,0 +1,3 @@
1
+ export declare type Provider = (key: string, value: unknown) => void;
2
+ declare const _default: () => <F extends (...args: any[]) => any>(func: F, scope?: object) => ReturnType<F>;
3
+ export default _default;
package/dist/index.mjs ADDED
@@ -0,0 +1,13 @@
1
+ import getParameterNames from 'get-parameter-names';
2
+
3
+ var index = () => {
4
+ const dependencies = Object.create(null);
5
+ dependencies['$provide'] = (key, value) => {
6
+ dependencies[key] = value;
7
+ };
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ return (func, scope = {}) => func.apply(scope, getParameterNames(func)
10
+ .map((key) => dependencies[key]));
11
+ };
12
+
13
+ export { index as default };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "license": "MIT",
4
+ "name": "@slimlib/injector",
5
+ "author": "Konstantin Shutkin",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.cjs",
10
+ "default": "./dist/index.mjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.mjs",
16
+ "typings": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=15"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/kshutkin/slimlib.git"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/kshutkin/slimlib/issues"
29
+ },
30
+ "homepage": "https://github.com/kshutkin/slimlib/injector/README.md",
31
+ "readme": "README.md",
32
+ "description": "Parameter names based dependency injector for nodejs",
33
+ "scripts": {
34
+ "build": "pkgbld",
35
+ "test": "jest --collectCoverage",
36
+ "lint": "eslint ./src",
37
+ "semantic-release": "npx semantic-release"
38
+ },
39
+ "devDependencies": {
40
+ "@semantic-release/changelog": "6.0.1",
41
+ "@semantic-release/commit-analyzer": "9.0.2",
42
+ "@semantic-release/git": "10.0.1",
43
+ "@semantic-release/npm": "9.0.0",
44
+ "@semantic-release/release-notes-generator": "10.0.3",
45
+ "@types/jest": "27.4.0",
46
+ "@typescript-eslint/eslint-plugin": "5.11.0",
47
+ "@typescript-eslint/parser": "5.11.0",
48
+ "conventional-changelog-angular": "5.0.13",
49
+ "eslint": "8.8.0",
50
+ "jest": "27.5.1",
51
+ "semantic-release": "19.0.2",
52
+ "semantic-release-monorepo": "7.0.5",
53
+ "ts-jest": "27.1.3",
54
+ "typescript": "4.5.x",
55
+ "update-monorepo-package-json": "0.2.0",
56
+ "pkgbld": "1.7.1"
57
+ },
58
+ "dependencies": {
59
+ "get-parameter-names": "0.3.0"
60
+ }
61
+ }