Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skonves/ts-registry-express
Custom ts-registry scope provider for Express requests.
https://github.com/skonves/ts-registry-express
Last synced: 1 day 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 (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T00:43:50.000Z (almost 2 years ago)
- Last Synced: 2024-11-10T06:13:25.124Z (10 days ago)
- Language: TypeScript
- Size: 191 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![travis](https://img.shields.io/travis/skonves/ts-registry-express.svg)](https://travis-ci.org/skonves/ts-registry-express)
[![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 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.