# hook-di

> hook style dependency-injection implement

Latest version **0.1.0** (published 2024-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install hook-di
pnpm add hook-di
yarn add hook-di
bun add hook-di
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2024-10-26 |
| First published | 2022-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >= 16 |
| Dependencies | 0 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | cj97 |
| Keywords | typescript, di, ioc |

## Links

- npm: https://www.npmjs.com/package/hook-di
- Repository: https://github.com/jcc1997/hook-di
- Homepage: https://github.com/jcc1997/hook-di#readme
- Issues: https://github.com/jcc1997/hook-di/issues
- npm.io page: https://npm.io/package/hook-di

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2024-10-26
- 0.0.15 — 2022-12-28
- 0.0.14 — 2022-10-08
- 0.0.13 — 2022-10-04
- 0.0.12 — 2022-10-04
- 0.0.11 — 2022-09-19
- 0.0.10 — 2022-07-21
- 0.0.9 — 2022-07-12
- 0.0.8 — 2022-06-10

## README

# hook-di

Reconsidering `dependencies injection` in hook style programming.

Highly inspired by vue composition apis.

## usage

here is the `interface`s

```typescript
import { InjectionKey } from 'hook-di'

export interface MyService {
  hello: () => string
}
// InjectionKey
export const MyServiceKey: InjectionKey<MyService> = InjectionKey('my-service')

// use MyService hook
export function useMyService(): MyService {
  return {
    hello() {
      return `storea`
    },
  }
}
```

manually register dependencies and auto inject.

```typescript
import { createScope, use } from '../src'

const scope = createScope()
scope.register(MyServiceKey, useMyService)

scope.run(() => {
  const service = use(MyServiceKey)
  service.hello() // "storebstorea"
})
```

---
_Source: https://npm.io/package/hook-di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
