# ts-constructor-injector

> Injector

Latest version **5.0.9** (published 2023-09-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install ts-constructor-injector
pnpm add ts-constructor-injector
yarn add ts-constructor-injector
bun add ts-constructor-injector
```

## Health

**Score 45/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 5.0.9 |
| Published | 2023-09-16 |
| First published | 2022-03-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 51.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | igorbabkin |
| Maintainers | igorbabkin |
| Keywords | injector, ts, typescript |

## Links

- npm: https://www.npmjs.com/package/ts-constructor-injector
- Repository: https://github.com/IgorBabkin/ts-ioc-container.git#master
- Homepage: https://github.com/IgorBabkin/ts-ioc-container/tree/master/packages/injector
- Issues: https://github.com/IgorBabkin/ts-ioc-container/issues
- npm.io page: https://npm.io/package/ts-constructor-injector

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 5.0.9 (latest) — 2023-09-16
- 5.0.8 — 2023-09-09
- 5.0.7 — 2023-08-26
- 5.0.6 — 2023-07-16
- 5.0.5 — 2023-07-09
- 5.0.4 — 2023-07-09
- 5.0.3 — 2023-06-18
- 5.0.2 — 2023-06-18
- 5.0.1 — 2023-06-17
- 4.0.2 — 2023-05-12
- 4.0.1 — 2023-05-12
- 4.0.0 — 2023-05-12
- 3.4.0 — 2023-05-12
- 3.3.0 — 2023-05-01
- 3.2.0 — 2023-04-20
- … 30 more at https://npm.io/package/ts-constructor-injector/versions

## README

![NPM version:latest](https://img.shields.io/npm/v/ts-constructor-injector/latest.svg?style=flat-square)
![npm downloads](https://img.shields.io/npm/dt/ts-constructor-injector.svg?style=flat-square)
![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/ts-constructor-injector)
[![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)
![License](https://img.shields.io/npm/l/ts-constructor-injector)

# ts-constructor-injector
Dependency injection for typescript classes

## Install
```shell script
npm install ts-constructor-injector reflect-metadata
```
```shell script
yarn add ts-constructor-injector reflect-metadata
```

## tsconfig.json
```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
```

## Usage
```typescript
import { inject, resolve } from "ts-constructor-injector";

class Logger {
  constructor(@inject(context => context.topic) private name: string) {
  }
}

const logger = resolve({topic: 'main'})(Logger);
```

### Hooks

```typescript
import { hook, getHooks } from "ts-constructor-injector";

class Logger {
  @hook('onDispose')
  async dispose(message: string): Promise<void> {
    console.log(message);
  }
}

(async () => {
  const instance = new Logger();
  for (const hook of getHooks(instance, 'onDispose')) {
    await instance[hook]('disposed'); // disposed
  }
})()
```

### ErrorHandler

```typescript
import { handleAsyncError } from "ts-constructor-injector";

const prismaToDomainError: HandleErrorParams = (error, context) => {
  if (error instanceof Prisma.PrismaClientKnownRequestError) {
    switch (error.code) {
      case "P2002":
        throw new PersistenceConflictError(errorToString(error));
      case "P2025":
        throw new EntityNotFoundError(errorToString(error));
      default:
        throw new PersistenceError(errorToString(error));
    }
  }

  throw new UnknownError(errorToString(error));
};

class AsyncRepo {
  @handleAsyncError(prismaToDomainError)
  async saveSmth() {
    await sleep(1000);
    throw new Prisma.PrismaClientKnownRequestError("P2002");
  }
}

class Repo {
  @handleError(prismaToDomainError)
  saveSmth() {
    throw new Prisma.PrismaClientKnownRequestError("P2002");
  }
}
```

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