New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@drewjbartlett/tiny-ioc

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@drewjbartlett/tiny-ioc - npm Package Compare versions

Comparing version 0.0.1 to 0.0.3

2

package.json

@@ -8,3 +8,3 @@ {

"type": "module",
"version": "0.0.1",
"version": "0.0.3",
"files": [

@@ -11,0 +11,0 @@ "dist"

@@ -22,12 +22,129 @@ # Tiny IOC

Create a `container.ts` file that creates, registers, and then exports the container.
```ts
// container.ts
import { createContainer, Scope } from '@drewjbartlett/tiny-ioc';
const container = createContainer();
container.bind(MyClass, () => new MyClass(), Scope.Singleton);
container.registerFactory(DataSource, () => new DataSource(container.get(HttpClient)));
container.registerSingleton(HttpClient, () => new HttpClient());
export { container }
```
```ts
// some-other-file.ts
import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';
export async function makeRequest() {
try {
const dataSource = container.get(DataSource);
return await dataSource.get('/foo/bar');
} catch (e) {
//
}
}
```
### API
- bind
- bindSingleton
- bindFactory
- bindOnce
- resetSingleton
- bound
- unbind
- swap
- get
- `bind` - Bind a dependency with a given scope.
```ts
```
- `bindSingleton` - Bind a dependency to the container as a singleton.
```ts
```
- `bindFactory` - Bind a dependency to the container as a factory. Each time the dependency is resolved the container will call the factory function.
```ts
```
- `bindOnce` - Only bind the given value if there is not already a binding.
```ts
```
- `get` - Attempt to resolve a given binding. Will throw if there is no binding found.
```ts
```
- `resetSingleton` - Reset a singleton value. If a value has been previously resolved and is registered as a singleton, this will keep the binding but reset the singleton value until the next resolve.
```ts
```
- `bound` - Determine if a binding exists or not.
```ts
```
- `unbind` - Remove the given binding from the container entirely.
```ts
```
- `swap` - Swap the old binding's value with the new value. This is useful when testing.
```ts
```
### Using in tests
```ts
// some-other-file.ts
import { container } from 'path/to/container';
import { DataSource } from 'path/to/data-source';
export async function makeRequest() {
try {
const dataSource = container.get(DataSource);
return await dataSource.get('/foo/bar');
} catch (e) {
//
}
}
```
```ts
// unit-test.test.ts
import { container } from 'path/to/container';
import { HttpClient } from 'path/to/http-client';
import { makeRequest } from 'path/top/make-request';
class DummyHttpClient {
get(url: string) {
return dummyData;
}
}
it('should make the request', () => {
container.swap(HttpClient, () => new DummyHttpClient());
await myRequest(); // this calls .get() on DummyHttpClient
})
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc