-
Notifications
You must be signed in to change notification settings - Fork 1
/
Context.ts
47 lines (40 loc) · 1.23 KB
/
Context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import httpRequestContext from "http-request-context";
import cuid from "cuid";
import express = require("express");
import User from "./User";
import path from "path";
import config from "config";
const appName = "h5pServer";
export default class Context {
path: string;
corrId: string | string[];
requestTime: number;
User: User;
getBaseUrl(): string {
return config.get("server.pathPrefix") + this.User?.token ?? "";
}
static setup(app: express.Application) {
//httpContext.set('Context', new Context(req));
app.use(httpRequestContext.middleware());
app.use((req, res, next) => {
httpRequestContext.set("Context", new Context(req));
next();
});
}
constructor(req: express.Request) {
this.path = req.path;
this.corrId = req.headers["x-correlation-id"] || cuid();
this.requestTime = Date.now();
}
static tenantPath(basePath: string): string {
const context = Context.current();
const tenant = context?.User?.tenant;
if (!tenant) throw new Error("Missing tenant!");
return path.join(basePath, tenant);
}
static current(): Context {
const context = httpRequestContext.get("Context");
if (!context) throw new Error("Missing context!");
return context;
}
}