-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp-context.ts
More file actions
32 lines (26 loc) · 767 Bytes
/
http-context.ts
File metadata and controls
32 lines (26 loc) · 767 Bytes
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
export class HttpContextToken<ValueT> {
constructor(public readonly defaultValue: ValueT) {}
}
export class HttpContext {
private readonly map = new Map<HttpContextToken<unknown>, unknown>();
set<T>(token: HttpContextToken<T>, value: T): HttpContext {
this.map.set(token, value);
return this;
}
get<T>(token: HttpContextToken<T>): T | undefined {
if (!this.map.has(token)) {
return token.defaultValue;
}
return this.map.get(token) as T;
}
delete(token: HttpContextToken<unknown>): HttpContext {
this.map.delete(token);
return this;
}
has(token: HttpContextToken<unknown>): boolean {
return this.map.has(token);
}
keys(): IterableIterator<HttpContextToken<unknown>> {
return this.map.keys();
}
}