import { AsyncLocalStorage } from 'async_hooks'
import { Context } from 'koa'
export interface RequestContext {
id: string;
...
}
export const asyncLocalStorage = new AsyncLocalStorage<RequestContext>()
export const requestContextMiddleware = async (ctx: Context, next: () => Promise<void>) => {
... get id from header or create anew
... get other stuff
await asyncLocalStorage.run({ id, ...otherStuff }, async () => {
return await next()
})
}
import { AsyncLocalStorage } from 'async_hooks'
import { Context } from 'koa'
export interface RequestContext {
id: string;
...
}
export const asyncLocalStorage = new AsyncLocalStorage<RequestContext>()
export const requestContextMiddleware = async (ctx: Context, next: () => Promise<void>) => {
... get id from header or create anew
... get other stuff
await asyncLocalStorage.run({ id, ...otherStuff }, async () => {
return await next()
})
}