https://github.com/skonves/ts-registry-express
Custom ts-registry scope provider for Express requests.
https://github.com/skonves/ts-registry-express
Last synced: 3 months ago
JSON representation
Custom ts-registry scope provider for Express requests.
- Host: GitHub
- URL: https://github.com/skonves/ts-registry-express
- Owner: skonves
- License: mit
- Created: 2019-10-29T01:06:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-12-01T21:16:21.000Z (7 months ago)
- Last Synced: 2025-12-31T21:14:45.296Z (6 months ago)
- Language: TypeScript
- Size: 387 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/skonves/ts-registry-express/actions/workflows/build.yml)
[](https://coveralls.io/github/skonves/ts-registry-express)
[](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