siringa 0.0.3 → 0.0.6
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/README.md +5 -0
- package/dist/index.js +17 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/index.d.ts +8 -1
- package/package.json +7 -7
- package/src/index.ts +23 -4
package/README.md
CHANGED
|
@@ -313,6 +313,11 @@ annotated with _JSDoc_).
|
|
|
313
313
|
Create a new instance of the given injectable (a _class_) injecting all its
|
|
314
314
|
required dependencies
|
|
315
315
|
|
|
316
|
+
* `injections.make(factory: Factory): ReturnType<Factory>` \
|
|
317
|
+
Simple utility method to invoke the factory with the correct `Injections`
|
|
318
|
+
and return its result. This can be used to alleviate issues when top-level
|
|
319
|
+
await is not available.
|
|
320
|
+
|
|
316
321
|
#### Sub Injectors
|
|
317
322
|
|
|
318
323
|
* `injections.injector(): Injector` \
|
package/dist/index.js
CHANGED
|
@@ -2,24 +2,19 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
6
5
|
var __export = (target, all) => {
|
|
7
6
|
for (var name in all)
|
|
8
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
8
|
};
|
|
10
|
-
var
|
|
11
|
-
if (
|
|
12
|
-
for (let key of __getOwnPropNames(
|
|
13
|
-
if (!__hasOwnProp.call(
|
|
14
|
-
__defProp(
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
14
|
}
|
|
16
|
-
return
|
|
15
|
+
return to;
|
|
17
16
|
};
|
|
18
|
-
var __toCommonJS =
|
|
19
|
-
return (module2, temp) => {
|
|
20
|
-
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
|
|
21
|
-
};
|
|
22
|
-
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
18
|
|
|
24
19
|
// src/index.ts
|
|
25
20
|
var src_exports = {};
|
|
@@ -27,6 +22,7 @@ __export(src_exports, {
|
|
|
27
22
|
Injector: () => Injector,
|
|
28
23
|
promise: () => promise
|
|
29
24
|
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
26
|
function bindingName(binding) {
|
|
31
27
|
return typeof binding === "function" ? `[class ${binding.name}]` : `"${binding}"`;
|
|
32
28
|
}
|
|
@@ -71,7 +67,7 @@ var Injector = class {
|
|
|
71
67
|
}
|
|
72
68
|
});
|
|
73
69
|
const injections = promises ? (await Promise.all(promises)).map((i) => {
|
|
74
|
-
return
|
|
70
|
+
return i && typeof i === "object" && i[promisedBinding] ? this.#get(i[promisedBinding], stack) : i;
|
|
75
71
|
}) : [];
|
|
76
72
|
return new injectable(...injections);
|
|
77
73
|
}
|
|
@@ -95,16 +91,22 @@ var Injector = class {
|
|
|
95
91
|
async get(binding) {
|
|
96
92
|
return this.#get(binding, []);
|
|
97
93
|
}
|
|
98
|
-
|
|
94
|
+
inject(injectable) {
|
|
99
95
|
return this.#inject(injectable, []);
|
|
100
96
|
}
|
|
97
|
+
make(factory) {
|
|
98
|
+
return factory({
|
|
99
|
+
get: (component) => this.#get(component, []),
|
|
100
|
+
inject: async (injectable) => this.#inject(injectable, []),
|
|
101
|
+
injector: () => this.injector()
|
|
102
|
+
});
|
|
103
|
+
}
|
|
101
104
|
injector() {
|
|
102
105
|
const injector = new Injector();
|
|
103
106
|
injector.#parent = this;
|
|
104
107
|
return injector;
|
|
105
108
|
}
|
|
106
109
|
};
|
|
107
|
-
module.exports = __toCommonJS(src_exports);
|
|
108
110
|
// Annotate the CommonJS export names for ESM import in node:
|
|
109
111
|
0 && (module.exports = {
|
|
110
112
|
Injector,
|
package/dist/index.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"mappings": "
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2JA,qBAAqB,SAA0B;AAC7C,SAAO,OAAO,YAAY,aAAa,UAAU,QAAQ,UAAU,IAAI;AACzE;AAGA,IAAM,kBAAkB,OAAO,IAAI,yBAAyB;AAGrD,iBAAoC,SAAgC;AACzE,SAAO,GAAG,kBAAkB,QAAQ;AACtC;AA2DO,qBAGyC;AAAA,EACrC,aAA+D,oBAAI,IAAI;AAAA,EACvE,YAAwC,oBAAI,IAAI;AAAA,EACzD;AAAA,QAIM,KAAK,SAAkB,OAAgC;AAC3D,QAAI,MAAM,SAAS,OAAO,GAAG;AAC3B,UAAI;AAAc,eAAO,kBAAkB,SAAS,CAAC,CAAC;AACtD,YAAM,WAAU,gCAAgC,YAAY,OAAO;AACnE,aAAO,QAAQ,OAAO,IAAI,MAAM,QAAO,CAAC;AAAA,IAC1C;AAEA,UAAM,WAAU,eAAe,IAAI,OAAO;AAC1C,QAAI;AAAS,aAAO;AAEpB,UAAM,UAAU,gBAAgB,IAAI,OAAO;AAC3C,QAAI,SAAS;AACX,YAAM,WAAU,QAAQ,QAAQ,EAAE,KAAK,MAAM,QAAQ,CAAE,GAAG,OAAO,OAAQ,CAAC,CAAC;AAC3E,qBAAe,IAAI,SAAS,QAAO;AACnC,aAAO;A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YAAY,CAAC,CAAC;AAAA,EACpC;AAAA,EAQA,KACE,SACe;AACf,WAAO,QAAQ;AAAA,MACb,KAAK,CAAC,cAAmB,UAAU,WAAW,CAAC,CAAC;AAAA,MAChD,QAAQ,OAAO,eAAoB,aAAa,YAAY,CAAC,CAAC;AAAA,MAC9D,UAAU,MAAM,KAAK,SAAS;AAAA,IAChC,CAAC;AAAA,EACH;AAAA,EAKA,WAA6C;AAC3C,UAAM,WAAW,IAAI,SAAiC;AACtD,uBAAmB;AACnB,WAAO;AAAA,EACT;AACF;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -43,7 +43,7 @@ var Injector = class {
|
|
|
43
43
|
}
|
|
44
44
|
});
|
|
45
45
|
const injections = promises ? (await Promise.all(promises)).map((i) => {
|
|
46
|
-
return
|
|
46
|
+
return i && typeof i === "object" && i[promisedBinding] ? this.#get(i[promisedBinding], stack) : i;
|
|
47
47
|
}) : [];
|
|
48
48
|
return new injectable(...injections);
|
|
49
49
|
}
|
|
@@ -67,9 +67,16 @@ var Injector = class {
|
|
|
67
67
|
async get(binding) {
|
|
68
68
|
return this.#get(binding, []);
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
inject(injectable) {
|
|
71
71
|
return this.#inject(injectable, []);
|
|
72
72
|
}
|
|
73
|
+
make(factory) {
|
|
74
|
+
return factory({
|
|
75
|
+
get: (component) => this.#get(component, []),
|
|
76
|
+
inject: async (injectable) => this.#inject(injectable, []),
|
|
77
|
+
injector: () => this.injector()
|
|
78
|
+
});
|
|
79
|
+
}
|
|
73
80
|
injector() {
|
|
74
81
|
const injector = new Injector();
|
|
75
82
|
injector.#parent = this;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"mappings": ";AA2JA,qBAAqB,SAA0B;AAC7C,SAAO,OAAO,YAA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 @@ export interface Injectable<Components extends Constructor, Provisions extends R
|
|
|
68
68
|
* provisioned instances from an `Injector`, injecting new `Injectable`
|
|
69
69
|
* instances, and creating sub-`Injector`s.
|
|
70
70
|
*/
|
|
71
|
-
export interface Injections<Components extends Constructor, Provisions extends Record<string, any
|
|
71
|
+
export interface Injections<Components extends Constructor = never, Provisions extends Record<string, any> = {}> {
|
|
72
72
|
/** Get a _bound_ instance from an `Injector`. */
|
|
73
73
|
get<C extends Components>(component: C): Promise<InstanceType<C>>;
|
|
74
74
|
/** Get a _provisioned_ instance from an `Injector`. */
|
|
@@ -116,6 +116,13 @@ export declare class Injector<Components extends Constructor = never, Provisions
|
|
|
116
116
|
* @param injectable The constructor of the instance to create.
|
|
117
117
|
*/
|
|
118
118
|
inject<I extends Injectable<Components, Provisions>>(injectable: I & CheckInjectable<I, Components, Provisions>): Promise<InstanceType<I>>;
|
|
119
|
+
/**
|
|
120
|
+
* Simple utility method to invoke the factory with the correct `Injections`
|
|
121
|
+
* and return its result.
|
|
122
|
+
*
|
|
123
|
+
* This can be used to alleviate issues when top-level await is not available.
|
|
124
|
+
*/
|
|
125
|
+
make<F extends Factory<Components, Provisions>>(factory: F): ReturnType<F>;
|
|
119
126
|
/** Create a sub-`Injector` child of this one. */
|
|
120
127
|
injector(): Injector<Components, Provisions>;
|
|
121
128
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "siringa",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Simple type-checking injection library for Typescript",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -24,19 +24,19 @@
|
|
|
24
24
|
"@types/chai-as-promised": "^7.1.5",
|
|
25
25
|
"@types/mocha": "^9.1.0",
|
|
26
26
|
"@types/node": "~16.11.26",
|
|
27
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
28
|
-
"@typescript-eslint/parser": "^5.
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^5.17.0",
|
|
28
|
+
"@typescript-eslint/parser": "^5.17.0",
|
|
29
29
|
"chai": "^4.3.6",
|
|
30
30
|
"chai-as-promised": "^7.1.1",
|
|
31
|
-
"esbuild": "^0.14.
|
|
32
|
-
"eslint": "^8.
|
|
31
|
+
"esbuild": "^0.14.28",
|
|
32
|
+
"eslint": "^8.12.0",
|
|
33
33
|
"eslint-config-google": "^0.14.0",
|
|
34
|
-
"mocha": "^9.2.
|
|
34
|
+
"mocha": "^9.2.2",
|
|
35
35
|
"nodemon": "^2.0.15",
|
|
36
36
|
"nyc": "^15.1.0",
|
|
37
37
|
"source-map-support": "^0.5.21",
|
|
38
38
|
"tsd": "^0.19.1",
|
|
39
|
-
"typescript": "^4.6.
|
|
39
|
+
"typescript": "^4.6.3"
|
|
40
40
|
},
|
|
41
41
|
"directories": {
|
|
42
42
|
"test": "test"
|
package/src/index.ts
CHANGED
|
@@ -188,8 +188,8 @@ export interface Injectable<
|
|
|
188
188
|
* instances, and creating sub-`Injector`s.
|
|
189
189
|
*/
|
|
190
190
|
export interface Injections<
|
|
191
|
-
Components extends Constructor,
|
|
192
|
-
Provisions extends Record<string, any
|
|
191
|
+
Components extends Constructor = never,
|
|
192
|
+
Provisions extends Record<string, any> = {},
|
|
193
193
|
> {
|
|
194
194
|
/** Get a _bound_ instance from an `Injector`. */
|
|
195
195
|
get<C extends Components>(component: C): Promise<InstanceType<C>>
|
|
@@ -267,7 +267,8 @@ export class Injector<
|
|
|
267
267
|
})
|
|
268
268
|
|
|
269
269
|
const injections = promises ? (await Promise.all(promises)).map((i) => {
|
|
270
|
-
return
|
|
270
|
+
return i && (typeof i === 'object') && (i[promisedBinding]) ?
|
|
271
|
+
this.#get(i[promisedBinding], stack) : i
|
|
271
272
|
}) : []
|
|
272
273
|
|
|
273
274
|
// eslint-disable-next-line new-cap
|
|
@@ -371,18 +372,36 @@ export class Injector<
|
|
|
371
372
|
return this.#get(binding, [])
|
|
372
373
|
}
|
|
373
374
|
|
|
375
|
+
/* INJECTIONS ============================================================= */
|
|
376
|
+
|
|
374
377
|
/**
|
|
375
378
|
* Create a new instance of the specified `Injectable`, providing all
|
|
376
379
|
* necessary injections.
|
|
377
380
|
*
|
|
378
381
|
* @param injectable The constructor of the instance to create.
|
|
379
382
|
*/
|
|
380
|
-
|
|
383
|
+
inject<I extends Injectable<Components, Provisions>>(
|
|
381
384
|
injectable: I & CheckInjectable<I, Components, Provisions>,
|
|
382
385
|
): Promise<InstanceType<I>> {
|
|
383
386
|
return this.#inject(injectable, [])
|
|
384
387
|
}
|
|
385
388
|
|
|
389
|
+
/**
|
|
390
|
+
* Simple utility method to invoke the factory with the correct `Injections`
|
|
391
|
+
* and return its result.
|
|
392
|
+
*
|
|
393
|
+
* This can be used to alleviate issues when top-level await is not available.
|
|
394
|
+
*/
|
|
395
|
+
make<F extends Factory<Components, Provisions>>(
|
|
396
|
+
factory: F,
|
|
397
|
+
): ReturnType<F> {
|
|
398
|
+
return factory({
|
|
399
|
+
get: (component: any) => this.#get(component, []),
|
|
400
|
+
inject: async (injectable: any) => this.#inject(injectable, []),
|
|
401
|
+
injector: () => this.injector(),
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
386
405
|
/* CHILD INJECTORS ======================================================== */
|
|
387
406
|
|
|
388
407
|
/** Create a sub-`Injector` child of this one. */
|