https://github.com/ziv/local-storage-nest
AsyncLocalStorage middleware for Nest
https://github.com/ziv/local-storage-nest
Last synced: 8 months ago
JSON representation
AsyncLocalStorage middleware for Nest
- Host: GitHub
- URL: https://github.com/ziv/local-storage-nest
- Owner: ziv
- License: mit
- Created: 2021-07-08T13:30:57.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-19T08:02:33.000Z (over 3 years ago)
- Last Synced: 2025-02-03T05:34:58.688Z (over 1 year ago)
- Language: TypeScript
- Size: 576 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# local-storage-nest
[](https://github.com/ziv/local-storage-nest/actions/workflows/main.yml)
[](https://github.com/ziv/local-storage-nest/actions/workflows/codeql-analysis.yml)
[](https://codecov.io/gh/ziv/local-storage-nest)
[AsyncLocalStorage](https://nodejs.org/api/async_context.html#async_context_class_asynclocalstorage) Module for NestJs
wrap [local-storage](https://github.com/ziv/local-storage) middleware.
## Install
```shell
npm i @xpr/local-storage-nest
```
## Usage
### Configure Routes
Register routes in your `root` module:
```typescript
import { LocalStorageModule } from '@xpr/local-storage-nest';
import { RequestMethod } from '@nestjs/common';
@Module({
imports: [
// assign localStorage middleware
// on all routes
LocalStorageModule.forRoutes({
path: '*',
method: RequestMethod.ALL
})
]
})
```
### Injectable
The provided `AsyncStorage` works with injectable classes.
```typescript
import { Injectable } from '@nestjs/common';
import { AsyncStorage } from '@xpr/local-storage-nest';
@Injectable()
class Service {
constructor(storage: AsyncStorage) {
}
}
```
### Independent
Since `AsyncStorage` have no constructor, you can initiate it without injecting it.
```typescript
import { AsyncStorage } from '@xpr/local-storage-nest';
// ready to go
const asyncStorage = new AsyncStorage();
```
### Async Context
Accessing `AsyncStorage` outside of the async context created by the `AsyncStorage` will throw an exception.
---


