0.0.1 • Published 2 years ago

@goodrequest/flow v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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.

0.0.1

2 years ago