ts-ioc-container 45.1.8 → 45.1.10

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/README.hbs.md +0 -275
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "45.1.8",
3
+ "version": "45.1.10",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
package/README.hbs.md DELETED
@@ -1,275 +0,0 @@
1
- # Typescript IoC (Inversion Of Control) container
2
-
3
- ![NPM version:latest](https://img.shields.io/npm/v/ts-ioc-container/latest.svg?style=flat-square)
4
- ![npm downloads](https://img.shields.io/npm/dt/ts-ioc-container.svg?style=flat-square)
5
- ![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/ts-ioc-container)
6
- [![Coverage Status](https://coveralls.io/repos/github/IgorBabkin/ts-ioc-container/badge.svg?branch=master)](https://coveralls.io/github/IgorBabkin/ts-ioc-container?branch=master)
7
- ![License](https://img.shields.io/npm/l/ts-ioc-container)
8
- [![semantic-release](https://img.shields.io/badge/%20%20%20FLO%20-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
9
-
10
- ## Advantages
11
- - battle tested :boom:
12
- - written on `typescript`
13
- - simple and lightweight :heart:
14
- - clean API :green_heart:
15
- - supports `tagged scopes`
16
- - fully test covered :100:
17
- - can be used with decorators `@inject`
18
- - can [inject properties](#inject-property)
19
- - can inject [lazy dependencies](#lazy)
20
- - composable and open to extend
21
-
22
- ## Content
23
-
24
- - [Setup](#setup)
25
- - [Container](#container)
26
- - [Basic usage](#basic-usage)
27
- - [Scope](#scope) `tags`
28
- - [Instances](#instances)
29
- - [Dispose](#dispose)
30
- - [Lazy](#lazy) `lazy`
31
- - [Injector](#injector)
32
- - [Metadata](#metadata) `@inject`
33
- - [Simple](#simple)
34
- - [Proxy](#proxy)
35
- - [Provider](#provider) `provider`
36
- - [Singleton](#singleton) `singleton`
37
- - [Arguments](#arguments) `args` `argsFn`
38
- - [Visibility](#visibility) `visible`
39
- - [Alias](#alias) `asAlias`
40
- - [Decorator](#decorator) `decorate`
41
- - [Registration](#registration) `@register`
42
- - [Key](#key) `asKey`
43
- - [Scope](#scope) `scope`
44
- - [Module](#module)
45
- - [Hook](#hook) `@hook`
46
- - [OnConstruct](#onconstruct) `@onConstruct`
47
- - [OnDispose](#ondispose) `@onDispose`
48
- - [Inject Property](#inject-property)
49
- - [Inject Method](#inject-method)
50
- - [Mock](#mock)
51
- - [Error](#error)
52
-
53
- ## Setup
54
-
55
- ```shell script
56
- npm install ts-ioc-container reflect-metadata
57
- ```
58
- ```shell script
59
- yarn add ts-ioc-container reflect-metadata
60
- ```
61
-
62
- Just put it in the entrypoint file of your project. It should be the first line of the code.
63
- ```typescript
64
- import 'reflect-metadata';
65
- ```
66
-
67
- And `tsconfig.json` should have next options:
68
- ```json
69
- {
70
- "compilerOptions": {
71
- "experimentalDecorators": true,
72
- "emitDecoratorMetadata": true
73
- }
74
- }
75
- ```
76
-
77
- ## Container
78
- `IContainer` consists of:
79
-
80
- - Provider is dependency factory which creates dependency
81
- - Injector describes how to inject dependencies to constructor
82
- - Registration is provider factory which registers provider in container
83
-
84
- ### Basic usage
85
-
86
- ```typescript
87
- {{{include_file './__tests__/readme/basic.spec.ts'}}}
88
- ```
89
-
90
- ### Scope
91
- Sometimes you need to create a scope of container. For example, when you want to create a scope per request in web application. You can assign tags to scope and provider and resolve dependencies only from certain scope.
92
-
93
- - NOTICE: remember that when scope doesn't have dependency then it will be resolved from parent container
94
- - NOTICE: when you create a scope of container then all providers are cloned to new scope. For that reason every provider has methods `clone` and `isValid` to clone itself and check if it's valid for certain scope accordingly.
95
- - NOTICE: when you create a scope then we clone ONLY tags-matched providers.
96
-
97
- ```typescript
98
- {{{include_file './__tests__/readme/scopes.spec.ts'}}}
99
- ```
100
-
101
- ### Instances
102
- Sometimes you want to get all instances from container and its scopes. For example, when you want to dispose all instances of container.
103
-
104
- - you can get instances from container and scope which were created by injector
105
-
106
- ```typescript
107
- {{{include_file './__tests__/readme/instances.spec.ts'}}}
108
- ```
109
-
110
- ### Dispose
111
- Sometimes you want to dispose container and all its scopes. For example, when you want to prevent memory leaks. Or you want to ensure that nobody can use container after it was disposed.
112
-
113
- - container can be disposed
114
- - when container is disposed then all scopes are disposed too
115
- - when container is disposed then it unregisters all providers and remove all instances
116
-
117
- ```typescript
118
- {{{include_file './__tests__/readme/disposing.spec.ts'}}}
119
- ```
120
-
121
- ### Lazy
122
- Sometimes you want to create dependency only when somebody want to invoke it's method or property. This is what `lazy` is for.
123
-
124
- ```typescript
125
- {{{include_file './__tests__/readme/lazy.spec.ts'}}}
126
- ```
127
-
128
- ## Injector
129
- `IInjector` is used to describe how dependencies should be injected to constructor.
130
-
131
- - `MetadataInjector` - injects dependencies using `@inject` decorator
132
- - `ProxyInjector` - injects dependencies as dictionary `Record<string, unknown>`
133
- - `SimpleInjector` - just passes container to constructor with others arguments
134
-
135
- ### Metadata
136
- This type of injector uses `@inject` decorator to mark where dependencies should be injected. It's bases on `reflect-metadata` package. That's why I call it `MetadataInjector`.
137
- Also you can [inject property.](#inject-property)
138
-
139
- ```typescript
140
- {{{include_file './__tests__/readme/metadataInjector.spec.ts'}}}
141
- ```
142
-
143
- ### Simple
144
- This type of injector just passes container to constructor with others arguments.
145
-
146
- ```typescript
147
- {{{include_file './__tests__/injector/SimpleInjector.spec.ts'}}}
148
- ```
149
-
150
- ### Proxy
151
- This type of injector injects dependencies as dictionary `Record<string, unknown>`.
152
-
153
- ```typescript
154
- {{{include_file './__tests__/injector/ProxyInjector.spec.ts'}}}
155
- ```
156
-
157
- ## Provider
158
- Provider is dependency factory which creates dependency.
159
-
160
- - `Provider.fromClass(Logger)`
161
- - `Provider.fromValue(logger)`
162
- - `new Provider((container, ...args) => container.resolve(Logger, {args}))`
163
-
164
- ```typescript
165
- {{{include_file './__tests__/readme/provider.spec.ts'}}}
166
- ```
167
-
168
- ### Singleton
169
- Sometimes you need to create only one instance of dependency per scope. For example, you want to create only one logger per scope.
170
-
171
- - Singleton provider creates only one instance in every scope where it's resolved.
172
- - NOTICE: if you create a scope 'A' of container 'root' then Logger of A !== Logger of root.
173
-
174
- ```typescript
175
- {{{include_file './__tests__/provider/Singleton.spec.ts'}}}
176
- ```
177
-
178
- ### Arguments
179
- Sometimes you want to bind some arguments to provider. This is what `ArgsProvider` is for.
180
- - `provider(args('someArgument'))`
181
- - `provider(argsFn((container) => [container.resolve(Logger), 'someValue']))`
182
- - `Provider.fromClass(Logger).pipe(args('someArgument'))`
183
- - NOTICE: args from this provider has higher priority than args from `resolve` method.
184
-
185
- ```typescript
186
- {{{include_file './__tests__/provider/ArgsProvider.spec.ts'}}}
187
- ```
188
-
189
- ### Visibility
190
- Sometimes you want to hide dependency if somebody wants to resolve it from certain scope
191
- - `provider(visible(({ isParent, child }) => isParent || child.hasTag('root')))` - dependency will be accessible from scope `root` or from scope where it's registered
192
- - `Provider.fromClass(Logger).pipe(visible(({ isParent, child }) => isParent || child.hasTag('root')))`
193
-
194
- ```typescript
195
- {{{include_file './__tests__/readme/visibility.spec.ts'}}}
196
- ```
197
-
198
- ### Alias
199
- Alias is needed to group keys
200
- - `@register(asAlias('logger'))` helper assigns `logger` alias to registration.
201
- - `by.aliases((it) => it.has('logger') || it.has('a'))` resolves dependencies which have `logger` or `a` aliases
202
- - `Provider.fromClass(Logger).pipe(alias('logger'))`
203
-
204
- ```typescript
205
- {{{include_file './__tests__/readme/alias.spec.ts'}}}
206
- ```
207
-
208
- ### Decorator
209
- Sometimes you want to decorate you class with some logic. This is what `DecoratorProvider` is for.
210
- - `provider(decorate((instance, container) => new LoggerDecorator(instance)))`
211
-
212
- ```typescript
213
- {{{include_file './__tests__/readme/decorate.spec.ts'}}}
214
- ```
215
-
216
- ## Registration
217
- Registration is provider factory which registers provider in container.
218
- - `@register(asKey('logger'))`
219
- - `Registration.fromClass(Logger).to('logger')`
220
- - `Registration.fromClass(Logger)`
221
- - `Registration.fromValue(Logger)`
222
- - `Registration.fromFn((container, ...args) => container.resolve(Logger, {args}))`
223
-
224
- ### Key
225
- Sometimes you want to register provider with certain key. This is what `key` is for.
226
-
227
- - by default, key is class name
228
- - you can assign the same key to different registrations
229
-
230
- ```typescript
231
- {{{include_file './__tests__/readme/registration.spec.ts'}}}
232
- ```
233
-
234
- ### Scope
235
- Sometimes you need to register provider only in scope which matches to certain condition and their sub scopes. Especially if you want to register dependency as singleton for some tags, for example `root`
236
- - `@register(scope((container) => container.hasTag('root'))` - register provider only in root scope
237
- - `Registration.fromClass(Logger).when((container) => container.hasTag('root'))`
238
-
239
- ```typescript
240
- {{{include_file './__tests__/provider/ScopeProvider.spec.ts'}}}
241
- ```
242
-
243
- ## Module
244
- Sometimes you want to encapsulate registration logic in separate module. This is what `IContainerModule` is for.
245
-
246
- ```typescript
247
- {{{include_file './__tests__/readme/containerModule.spec.ts'}}}
248
- ```
249
-
250
- ## Hook
251
- Sometimes you need to invoke methods after construct or dispose of class. This is what hooks are for.
252
-
253
- ### OnConstruct
254
- ```typescript
255
- {{{include_file '__tests__/hooks/OnConstruct.spec.ts'}}}
256
- ```
257
-
258
- ### OnDispose
259
- ```typescript
260
- {{{include_file '__tests__/hooks/OnDispose.spec.ts'}}}
261
- ```
262
-
263
- ### Inject property
264
-
265
- ```typescript
266
- {{{include_file '__tests__/readme/injectProp.spec.ts'}}}
267
- ```
268
-
269
- ## Error
270
-
271
- - [DependencyNotFoundError.ts](..%2F..%2Flib%2Ferrors%2FDependencyNotFoundError.ts)
272
- - [MethodNotImplementedError.ts](..%2F..%2Flib%2Ferrors%2FMethodNotImplementedError.ts)
273
- - [DependencyMissingKeyError.ts](..%2F..%2Flib%2Ferrors%2FDependencyMissingKeyError.ts)
274
- - [ContainerDisposedError.ts](..%2F..%2Flib%2Ferrors%2FContainerDisposedError.ts)
275
-