@goodrequest/flow
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Build and run tests Publish package to GitHub Packages

Flow Library

Helper library for using async hooks, using AsyncLocalStorage class.

Installation

npm i --save @goodrequest/flow

Initialization:

You need to extend abstract FlowDefinition class and then instantiate it. Best way to do it is to create a new file, e.g. flow.ts (example:

import { FlowDefinition } from '../../src'
import { UserModel } from './user'

class TestFlow extends FlowDefinition<UserModel> {}

export const Flow = new TestFlow()

Next step is to setup middleware for hydrating storage:

app.use(flowMiddleware(Flow))

Usage

When class is instantiated and data are saved using middleware, you can start using it:

const asyncData = Flow.get()

Stored data

export interface AsyncStorageData<T extends Model<any, any>> {
	user?: T
	t: TFunction
	requestID: string
	request: RequestData
}

export class RequestData {
	method: string
	url: string
	ip: string
	headers: any
	query: any
	body: any

	constructor(req: Request) {
		this.method = req.method
		this.url = req.originalUrl
		// NOTE: x-real-ip is from nginx reverse proxy
		this.ip = req.header('x-real-ip') || req.ip
		this.headers = req.headers
		this.query = req.query
		this.body = req.body
	}
}

Modifications

You can implement this by yourself following this example.

Dependents (0)

Package Sidebar

Install

npm i @goodrequest/flow

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

18.2 kB

Total Files

38

Last publish

Collaborators

  • logingoodrequest
  • lubomirigonda