@stone-js/service-container 0.1.0 → 0.1.1
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 +22 -12
- package/dist/index.d.ts +153 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
# Stone.js
|
|
1
|
+
# Stone.js - Service Container
|
|
2
2
|
|
|
3
|
-
[](https://opensource.org/licenses/
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
4
|
[](https://www.npmjs.com/package/@stone-js/service-container)
|
|
5
5
|
[](https://www.npmjs.com/package/@stone-js/service-container)
|
|
6
6
|

|
|
7
|
-
[](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/main.yml)
|
|
8
|
+
[](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/release.yml)
|
|
9
|
+
[](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-service-container)
|
|
10
|
+
[](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-service-container)
|
|
11
|
+
[](./SECURITY.md)
|
|
12
|
+
[](https://github.com/stone-foundation/stone-js-service-container/security/code-scanning)
|
|
13
|
+
[](https://github.com/stone-foundation/stone-js-service-container/network/updates)
|
|
8
14
|
[](https://conventionalcommits.org)
|
|
9
15
|
|
|
10
|
-
IoC Service Container with
|
|
16
|
+
IoC Service Container with proxy resolver and destructuring injection provides a very simple, centralized container that stores and resolves libraries, objects, and values to better organize code, manage dependencies, and enhance testability.
|
|
11
17
|
|
|
12
18
|
---
|
|
13
19
|
|
|
14
|
-
##
|
|
20
|
+
## Overview
|
|
15
21
|
|
|
16
22
|
The **Service Container** is an advanced JavaScript/TypeScript library that helps you manage dependencies effectively in your applications. It utilizes the Inversion of Control (IoC) pattern to simplify dependency injection, allowing you to create highly decoupled and maintainable codebases.
|
|
17
23
|
|
|
@@ -90,16 +96,14 @@ class A {
|
|
|
90
96
|
Now, create a class `B` that depends on `Logger` and `A` to log the message:
|
|
91
97
|
|
|
92
98
|
```typescript
|
|
93
|
-
import { Binding } from '@stone-js/service-container';
|
|
94
|
-
|
|
95
99
|
// Your class B
|
|
96
100
|
class B {
|
|
97
101
|
private readonly a: A;
|
|
98
102
|
private readonly logger: Logger;
|
|
99
103
|
|
|
100
|
-
constructor({ a, logger }:
|
|
101
|
-
this.a = a
|
|
102
|
-
this.logger = logger
|
|
104
|
+
constructor({ a, logger }: { a: A, logger: Logger }) { // Dependency injection by destructuring
|
|
105
|
+
this.a = a
|
|
106
|
+
this.logger = logger
|
|
103
107
|
}
|
|
104
108
|
|
|
105
109
|
logMessage() {
|
|
@@ -331,13 +335,19 @@ With this library, managing dependencies in your application becomes much simple
|
|
|
331
335
|
|
|
332
336
|
The Stone Service Container simplifies dependency management in your applications, making them more modular, testable, and maintainable. It provides tools for binding instances, singletons, and factories, as well as auto and conditional binding. By leveraging Inversion of Control and dependency injection, the service container ensures efficient, flexible, and scalable management of dependencies, fitting projects of any size.
|
|
333
337
|
|
|
338
|
+
## Learn More
|
|
339
|
+
|
|
340
|
+
This package is part of the Stone.js ecosystem, a modern JavaScript framework built around the Continuum Architecture.
|
|
341
|
+
|
|
342
|
+
Explore the full documentation: https://stonejs.dev
|
|
343
|
+
|
|
334
344
|
## API documentation
|
|
335
345
|
|
|
336
|
-
- [API](https://github.com/
|
|
346
|
+
- [API](https://github.com/stone-foundation/stone-js-service-container/blob/main/docs/modules.md)
|
|
337
347
|
|
|
338
348
|
## Contributing
|
|
339
349
|
|
|
340
|
-
See [Contributing Guide](https://github.com/
|
|
350
|
+
See [Contributing Guide](https://github.com/stone-foundation/stone-js-service-container/blob/main/CONTRIBUTING.md).
|
|
341
351
|
|
|
342
352
|
## Credits
|
|
343
353
|
- [Laravel Service Container](https://github.com/illuminate/container)
|
package/dist/index.d.ts
CHANGED
|
@@ -272,5 +272,157 @@ declare class Container extends Proxiable {
|
|
|
272
272
|
autoBinding<V extends BindingValue>(name: BindingKey, item?: V, singleton?: boolean, alias?: string | string[]): this;
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
|
|
275
|
+
/**
|
|
276
|
+
* Class representing a ContainerError.
|
|
277
|
+
*
|
|
278
|
+
* @author Mr. Stone <evensstone@gmail.com>
|
|
279
|
+
*/
|
|
280
|
+
declare class ContainerError extends Error {
|
|
281
|
+
/**
|
|
282
|
+
* Error type indicating an alias conflict.
|
|
283
|
+
*/
|
|
284
|
+
static readonly ALIAS_TYPE = "alias";
|
|
285
|
+
/**
|
|
286
|
+
* Error type indicating that the resolver is not a function.
|
|
287
|
+
*/
|
|
288
|
+
static readonly RESOLVER_TYPE = "resolver";
|
|
289
|
+
/**
|
|
290
|
+
* Error type indicating a resolution failure.
|
|
291
|
+
*/
|
|
292
|
+
static readonly RESOLUTION_TYPE = "resolution";
|
|
293
|
+
/**
|
|
294
|
+
* Error type indicating an attempt to alias an unbound value.
|
|
295
|
+
*/
|
|
296
|
+
static readonly ALIAS_UNBOUND_TYPE = "alias_unbound";
|
|
297
|
+
/**
|
|
298
|
+
* Error type indicating that a value is not a service.
|
|
299
|
+
*/
|
|
300
|
+
static readonly NOT_A_SERVICE_TYPE = "not_a_service";
|
|
301
|
+
/**
|
|
302
|
+
* Error type indicating an error thrown by the resolver function.
|
|
303
|
+
*/
|
|
304
|
+
static readonly CANNOT_RESOLVE_TYPE = "cannot_resolve";
|
|
305
|
+
/**
|
|
306
|
+
* Error type indicating a circular dependency.
|
|
307
|
+
*/
|
|
308
|
+
static readonly CIRCULAR_DEPENDENCY_TYPE = "circular_dependency";
|
|
309
|
+
/**
|
|
310
|
+
* The type of the error.
|
|
311
|
+
*/
|
|
312
|
+
private readonly type;
|
|
313
|
+
/**
|
|
314
|
+
* Create a ContainerError.
|
|
315
|
+
*
|
|
316
|
+
* @param type - The type of the error.
|
|
317
|
+
* @param message - The error message or key related to the error.
|
|
318
|
+
*/
|
|
319
|
+
constructor(type: string, message: BindingKey);
|
|
320
|
+
/**
|
|
321
|
+
* Retrieve the error message based on the type and provided message.
|
|
322
|
+
*
|
|
323
|
+
* @param type - The type of the error.
|
|
324
|
+
* @param message - The error message or key related to the error.
|
|
325
|
+
* @returns The formatted error message.
|
|
326
|
+
*/
|
|
327
|
+
private getMessage;
|
|
328
|
+
/**
|
|
329
|
+
* Retrieve the resolution message based on the key.
|
|
330
|
+
*
|
|
331
|
+
* @param key - The key for which the resolution failed.
|
|
332
|
+
* @returns The formatted resolution error message.
|
|
333
|
+
*/
|
|
334
|
+
private getResolutionMessage;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Class representing a ResolverBinding.
|
|
339
|
+
*
|
|
340
|
+
* This class extends the Binding class, using a resolver function to lazily resolve the value when needed.
|
|
341
|
+
*
|
|
342
|
+
* @template V - The type of value that this binding holds.
|
|
343
|
+
* @author Mr. Stone <evensstone@gmail.com>
|
|
344
|
+
*/
|
|
345
|
+
declare abstract class ResolverBinding<V extends BindingValue> extends Binding<V> {
|
|
346
|
+
/**
|
|
347
|
+
* The resolver function used to provide the binding value.
|
|
348
|
+
*
|
|
349
|
+
* This function will be called when the value is needed, allowing for lazy instantiation
|
|
350
|
+
* and dependency resolution. It should return an instance of type `V`.
|
|
351
|
+
*/
|
|
352
|
+
protected readonly resolver: Resolver<V>;
|
|
353
|
+
/**
|
|
354
|
+
* Create a new instance of ResolverBinding.
|
|
355
|
+
*
|
|
356
|
+
* @param resolver - The resolver function to provide the binding value.
|
|
357
|
+
* @throws ContainerError if the resolver is not a function.
|
|
358
|
+
*/
|
|
359
|
+
constructor(resolver: Resolver<V>);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Class representing a Factory.
|
|
364
|
+
*
|
|
365
|
+
* The Factory class extends the ResolverBinding class, providing a mechanism to resolve a new instance each time the binding is resolved.
|
|
366
|
+
* This ensures that a fresh instance is created with each call to the `resolve` method.
|
|
367
|
+
*
|
|
368
|
+
* @template V - The type of value that this binding holds.
|
|
369
|
+
* @author Mr. Stone <evensstone@gmail.com>
|
|
370
|
+
*/
|
|
371
|
+
declare class Factory<V extends BindingValue> extends ResolverBinding<V> {
|
|
372
|
+
/**
|
|
373
|
+
* Resolve and return the value of the binding.
|
|
374
|
+
*
|
|
375
|
+
* Each time this method is called, a new value is resolved using the resolver function.
|
|
376
|
+
* This is intended for cases where a fresh instance is required for each resolution, such as factories or transient dependencies.
|
|
377
|
+
*
|
|
378
|
+
* @param container - The container to resolve dependencies from.
|
|
379
|
+
* @returns The resolved value of the binding.
|
|
380
|
+
* @throws ContainerError if the value cannot be resolved.
|
|
381
|
+
*/
|
|
382
|
+
resolve(container: Container): V;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Class representing an Instance.
|
|
387
|
+
*
|
|
388
|
+
* This class extends the Binding class and directly holds an instance value.
|
|
389
|
+
* It provides a straightforward resolution mechanism that simply returns the stored value.
|
|
390
|
+
*
|
|
391
|
+
* @template V - The type of value that this binding holds.
|
|
392
|
+
* @author Mr. Stone <evensstone@gmail.com>
|
|
393
|
+
*/
|
|
394
|
+
declare class Instance<V extends BindingValue> extends Binding<V> {
|
|
395
|
+
/**
|
|
396
|
+
* Resolve and return the value of the binding.
|
|
397
|
+
*
|
|
398
|
+
* @param _container - Container to resolve dependencies (not used in this implementation).
|
|
399
|
+
* @returns The resolved value of the binding.
|
|
400
|
+
*/
|
|
401
|
+
resolve(_container: Container): V | undefined;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Class representing a Singleton.
|
|
406
|
+
*
|
|
407
|
+
* The Singleton class extends the ResolverBinding class, ensuring that the value is only resolved once.
|
|
408
|
+
* Subsequent calls to the `resolve` method will return the previously resolved value, making it behave as a singleton.
|
|
409
|
+
*
|
|
410
|
+
* @template V - The type of value that this binding holds.
|
|
411
|
+
* @author Mr. Stone <evensstone@gmail.com>
|
|
412
|
+
*/
|
|
413
|
+
declare class Singleton<V extends BindingValue> extends ResolverBinding<V> {
|
|
414
|
+
/**
|
|
415
|
+
* Resolve and return the value of the binding.
|
|
416
|
+
*
|
|
417
|
+
* If the value has already been resolved, return the cached value. Otherwise, use the resolver function
|
|
418
|
+
* to resolve the value, store it, and return it.
|
|
419
|
+
*
|
|
420
|
+
* @param container - The container to resolve dependencies from.
|
|
421
|
+
* @returns The resolved value of the binding.
|
|
422
|
+
* @throws ContainerError if the value cannot be resolved.
|
|
423
|
+
*/
|
|
424
|
+
resolve(container: Container): V | undefined;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export { Binding, Container, ContainerError, Factory, Instance, Proxiable, ResolverBinding, Singleton };
|
|
276
428
|
export type { BindingKey, BindingValue, Resolver };
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stone-js/service-container",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Javascript/Typescript IoC Service Container with proxy resolver and destructuring injection",
|
|
5
5
|
"author": "Mr. Stone <evensstone@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+ssh://git@github.com/
|
|
9
|
+
"url": "git+ssh://git@github.com/stone-foundation/stone-js-service-container.git"
|
|
10
10
|
},
|
|
11
|
-
"homepage": "https://
|
|
11
|
+
"homepage": "https://stonejs.dev",
|
|
12
12
|
"bugs": {
|
|
13
|
-
"url": "https://github.com/
|
|
13
|
+
"url": "https://github.com/stone-foundation/stone-js-service-container/issues"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
16
|
"DI",
|
|
@@ -59,20 +59,20 @@
|
|
|
59
59
|
"@rollup/plugin-multi-entry": "^6.0.1",
|
|
60
60
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
61
61
|
"@rollup/plugin-typescript": "^12.1.1",
|
|
62
|
-
"@types/node": "^
|
|
63
|
-
"@vitest/coverage-v8": "^3.
|
|
62
|
+
"@types/node": "^24.0.0",
|
|
63
|
+
"@vitest/coverage-v8": "^3.2.3",
|
|
64
64
|
"husky": "^9.1.7",
|
|
65
65
|
"rimraf": "^6.0.1",
|
|
66
|
-
"rollup": "^4.
|
|
66
|
+
"rollup": "^4.43.0",
|
|
67
67
|
"rollup-plugin-delete": "^3.0.1",
|
|
68
68
|
"rollup-plugin-dts": "^6.2.1",
|
|
69
69
|
"rollup-plugin-node-externals": "^8.0.0",
|
|
70
70
|
"ts-standard": "^12.0.2",
|
|
71
71
|
"tslib": "^2.8.1",
|
|
72
|
-
"typedoc": "^0.28.
|
|
73
|
-
"typedoc-plugin-markdown": "^4.6.
|
|
72
|
+
"typedoc": "^0.28.5",
|
|
73
|
+
"typedoc-plugin-markdown": "^4.6.4",
|
|
74
74
|
"typescript": "^5.6.3",
|
|
75
|
-
"vitest": "^3.
|
|
75
|
+
"vitest": "^3.2.3"
|
|
76
76
|
},
|
|
77
77
|
"ts-standard": {
|
|
78
78
|
"globals": [
|