Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active September 9, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebigredgeek/dc545580a245bb25344f039bea128521 to your computer and use it in GitHub Desktop.
Save thebigredgeek/dc545580a245bb25344f039bea128521 to your computer and use it in GitHub Desktop.
Context Class for Requests
import { Request } from 'express';
export default class Context {
static _bindings = new WeakMap<Request, Context>();
public foo = 'bar';
constructor () {}
static bind (req: Request) : void {
const ctx = new Context();
Context._bindings.set(req, ctx);
}
static get (req: Request) : Context | null {
return Context._bindings.get(req) || null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment