Skip to content

Commit

Permalink
chore: Add registerInjections helper function for dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandotv committed Jun 29, 2024
1 parent 03e55af commit 9390bf1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-buttons-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pumpit": minor
---

create `registerInjections` helper function for an easier way to register dependencies for a class or a function
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ It supports different injection scopes, child containers, hooks etc...
* [Checking for values](#checking-for-values)
* [Child singletons](#child-singletons)
* [Validating bindings](#validating-bindings)
- [Helpers](#helpers)
* [Register injections](#register-injections)
- [API docs](#api-docs)
- [License](#license)

Expand Down Expand Up @@ -884,6 +886,38 @@ expect(result).toEqual({

```

## Helpers

### Register injections

`registerInjections` helper function with a class or factory. It will automatically create `inject` property on the class or factory function.

```ts
test("use helper to inject in to class", () => {
const pumpIt = new PumpIt()

class TestA {}
class TestB {}
class TestC {
constructor(
public a: TestA,
public b: TestB,
) {}
}

registerInjections(TestC, [TestA, TestB])

pumpIt
.bindClass(TestA, TestA)
.bindClass(TestB, TestB)
.bindClass(TestC, TestC)

const result = pumpIt.resolve<TestC>(TestC)

expect(result.a).toBeInstanceOf(TestA)
expect(result.b).toBeInstanceOf(TestB)
})
```
## API docs

`PumpIt` is written in TypeScript, [auto generated API documentation](docs/api/README.md) is available.
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/injection-helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, test } from "vitest"
import { PumpIt, SCOPE } from "../pumpit"
import { registerInjections } from "../utils"

describe("injection helper", () => {
describe("factory", () => {
test("use helper to inject in to factory", () => {
const pumpIt = new PumpIt()

type Fn = ReturnType<typeof factory>

function factory(a: TestA, b: TestB) {
return () => {
return { a, b }
}
}

class TestA {}
class TestB {}

registerInjections(factory, [TestA, TestB])

pumpIt
.bindFactory(factory, factory)
.bindClass(TestA, TestA)
.bindClass(TestB, TestB)

const result = pumpIt.resolve<Fn>(factory)()

expect(result.a).toBeInstanceOf(TestA)
expect(result.b).toBeInstanceOf(TestB)
})
})

describe("class", () => {
test("use helper to inject in to class", () => {
const pumpIt = new PumpIt()

class TestA {}
class TestB {}
class TestC {
constructor(
public a: TestA,
public b: TestB,
) {}
}

registerInjections(TestC, [TestA, TestB])

pumpIt
.bindClass(TestA, TestA)
.bindClass(TestB, TestB)
.bindClass(TestC, TestC)

const result = pumpIt.resolve<TestC>(TestC)

expect(result.a).toBeInstanceOf(TestA)
expect(result.b).toBeInstanceOf(TestB)
})
})
})
14 changes: 10 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ export function keyToString(key: BindKey) {
return key.name || key.toString()
}

//TODO - write tests for this
export function registerInjections<T>(
cls: new (...args: unknown[]) => T | ((...args: unknown[]) => any),
/**
* Registers the dependencies for a class or function.
* @param f - The class or function to register the dependencies for.
* @param deps - An array of dependencies to be injected.
*/
export function registerInjections(
f:
| { new (...args: any[]): any }
| ((...args: any[]) => (...args: any[]) => any),
deps: unknown[],
): void {
// @ts-expect-error - no inject property on cls
cls[INJECT_KEY] = deps
f[INJECT_KEY] = deps
}

0 comments on commit 9390bf1

Please sign in to comment.