https://github.com/ziv/async-hooks-state
A (very) simple AsyncLocalStorage wrapper & expressjs middleware.
https://github.com/ziv/async-hooks-state
Last synced: 11 months ago
JSON representation
A (very) simple AsyncLocalStorage wrapper & expressjs middleware.
- Host: GitHub
- URL: https://github.com/ziv/async-hooks-state
- Owner: ziv
- License: mit
- Created: 2021-04-24T15:19:12.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-09T09:34:24.000Z (about 5 years ago)
- Last Synced: 2025-05-25T00:22:59.485Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 1.16 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# async-hooks-state
A (very) simple AsyncLocalStorage wrapper & `expressjs` middleware.
### Install
```shell
npm i async-hooks-state
```
### Usage
`expressjs` example:
```typescript
import {hooked, getStorage, getRequest} from 'async-hooks-state';
import {NextFunction, Request, Response} from 'express';
const app = express();
const optionalContext = { foo: 'bar' };
app.use(hooked(optionalContext));
// later, in any async context
const route = (req: Request, res: Response, next: NextFunction) => {
const r = getRequest();
assert(r === req);
assert(getStorage().getStore().get('foo') === 'bar');
}
```
### Type aliases
- [Context](#context)
- [ContextKey](#contextkey)
### Variables
- [RequestSymbol](#requestsymbol)
- [ResponseSymbol](#responsesymbol)
### Functions
- [getRequest](#getrequest)
- [getResponse](#getresponse)
- [getStorage](#getstorage)
- [getStore](#getstore)
- [hooked](#hooked)
## Type aliases
### Context
Ƭ **Context**: *Map*<[*ContextKey*](#contextkey), any\>
Defined in: [lib/async-hooks-state.ts:5](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L5)
___
### ContextKey
Ƭ **ContextKey**: *number* \| *string* \| *symbol*
Defined in: [lib/async-hooks-state.ts:4](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L4)
## Variables
### RequestSymbol
• `Const` **RequestSymbol**: *typeof* [*RequestSymbol*](#requestsymbol)
Defined in: [lib/async-hooks-state.ts:9](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L9)
___
### ResponseSymbol
• `Const` **ResponseSymbol**: *typeof* [*ResponseSymbol*](#responsesymbol)
Defined in: [lib/async-hooks-state.ts:10](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L10)
## Functions
### getRequest
▸ `Const` **getRequest**(): *Request*\>
**Returns:** *Request*\>
Defined in: [lib/async-hooks-state.ts:23](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L23)
___
### getResponse
▸ `Const` **getResponse**(): *Response*\>
**Returns:** *Response*\>
Defined in: [lib/async-hooks-state.ts:25](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L25)
___
### getStorage
▸ `Const` **getStorage**(): *AsyncLocalStorage*<[*Context*](#context)\>
**Returns:** *AsyncLocalStorage*<[*Context*](#context)\>
Defined in: [lib/async-hooks-state.ts:13](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L13)
___
### getStore
▸ `Const` **getStore**(): [*Context*](#context)
**Returns:** [*Context*](#context)
Defined in: [lib/async-hooks-state.ts:15](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L15)
___
### hooked
▸ `Const` **hooked**(`defaultContext?`: *Record*): *function*
#### Parameters
| Name | Type | Default value |
| :------ | :------ | :------ |
| `defaultContext` | *Record* | {} |
**Returns:** (`req`: *Request*\>, `res`: *Response*\>, `next`: NextFunction) => *void*
Defined in: [lib/async-hooks-state.ts:27](https://github.com/ziv/async-hooks-state/blob/d3fdba2/lib/async-hooks-state.ts#L27)