# ts-registry-express

> ExpressJS request-scope provider for ts-registry

Latest version **1.2.2** (published 2025-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-registry-express
pnpm add ts-registry-express
yarn add ts-registry-express
bun add ts-registry-express
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2025-05-19 |
| First published | 2019-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Steve Konves |
| Maintainers | skonves |
| Keywords | typescript, registry, express, request |

## Links

- npm: https://www.npmjs.com/package/ts-registry-express
- Repository: https://github.com/skonves/ts-registry-express
- Homepage: https://github.com/skonves/ts-registry-express#readme
- Issues: https://github.com/skonves/ts-registry-express/issues
- npm.io page: https://npm.io/package/ts-registry-express

## Dependencies (1)

- [express-http-context](https://npm.io/package/express-http-context.md) ^2.0.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2025-05-19
- 1.1.0-rc.0 (rc) — 2025-03-19
- 1.2.1 — 2025-04-21
- 1.2.0 — 2025-04-07
- 1.1.0 — 2025-04-03
- 1.0.3 — 2025-03-19
- 1.0.2 — 2019-10-29
- 1.0.1 — 2019-10-29

## README

[![build](https://img.shields.io/github/actions/workflow/status/skonves/ts-registry-express/build.yml?branch=master)](https://github.com/skonves/ts-registry-express/actions/workflows/build.yml)
[![coveralls](https://img.shields.io/coveralls/skonves/ts-registry-express.svg)](https://coveralls.io/github/skonves/ts-registry-express)
[![NPM Version](https://img.shields.io/npm/v/ts-registry-express.svg)](https://npmjs.com/package/ts-registry-express)

# ts-registry-express

Custom [`ts-registry`](https://github.com/ChristianAlexander/ts-registry) scope provider for [Express](https://expressjs.com/) requests.

## Quick Start

```Typescript
import * as express from 'express';
import { Registry } from 'ts-registry';
import { middleware, request } from 'ts-registry-express';

// define a request-scoped service
const registry = new Registry();
registry
  .for('my-service')
  .withScope(request)
  .use(() => new MyService())

// use the middleware
const app = express();
app.use(middleware);

// get an instance of the service
app.get('/', (req, res) => {
  // service instance is unique to this request
  const service = registry.getService('my-service');
})
```

## Access to the current Request

The target scope for this `ScopeProvider` is the current `Request` object and can be accessed via the second parameter of the service initializer.

Example:

```Typescript
registry
  .for('my-service')
  .withScope(request)
  .use((_get, req) => new MyService(req.session))
```

Because service instantiation is deferred, request-scoped service initializers can be defined outside of standard Express middleware, routers, or other handlers. However, when the `registry.get()` is called during a request (even if the call is made from another function or module) the service initializer is passed the instance of the current request as the second argument.

Note that if you hold onto a reference of `req` within either the service intializer or other code, it will not be garbage collected and will lead to a memory leak.

## Troubleshooting

If you encounter the error "Request not found in context", it means the middleware hasn't been properly registered. Make sure to:

1. Import and use the `ts-registry-express` middleware
2. Register them before any routes that need the context

```typescript
import express from 'express';
import { middleware } from 'ts-registry-express';

const app = express();

// Register middleware first
app.use(middleware);

// Then register your routes that get request-scoped services
app.get('/', (req, res) => {
  const service = registry.getService('my-service');
});
```

If you're using multiple routers, remember to register the middleware on each router that needs the context.

If you're still experiencing issues, please check:

- The order of middleware registration
- That you're using the latest version of the package
- Your Node.js version compatibility
- That all required dependencies are properly installed

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