-
Notifications
You must be signed in to change notification settings - Fork 1
/
context-accessor.ts
175 lines (156 loc) · 3.64 KB
/
context-accessor.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { ServerPlatform } from "./platform.ts";
/**
* Wraps the platform specific context object with a standard interface
*/
export class ContextAccessor {
#context: unknown;
#platform: ServerPlatform;
constructor(context: unknown, platform: ServerPlatform) {
this.#context = context;
this.#platform = platform;
}
/**
* Get the underlying platform context object
*/
getContext() {
return this.#context;
}
/**
* Get the requested URL
*/
async getUrl() {
return (await this.#platform.getContextItem("url", this.#context)) as URL;
}
/**
* Get the underlying platform request object
*/
async getRequest() {
return await this.#platform.getContextItem("request", this.#context);
}
/**
* Get the underlying platform response object
*/
async getResponse() {
return await this.#platform.getContextItem("response", this.#context);
}
/**
* Get the request body
*/
async getBody(): Promise<unknown>;
/**
* Get a field from the request body
*/
async getBody(name: string | undefined): Promise<unknown>;
async getBody(name?: string) {
return await this.#platform.getContextItem("body", this.#context, name);
}
/**
* Set the response body
*/
async setBody(value: unknown) {
await this.#platform.setContextItem("body", this.#context, value);
}
/**
* Get a parameter from the requested route
*/
async getParameter(name: string) {
return (await this.#platform.getContextItem(
"parameter",
this.#context,
name,
)) as string;
}
/**
* Get an item from the request query string
*/
async getQuery(name: string) {
return (await this.#platform.getContextItem(
"query",
this.#context,
name,
)) as string;
}
/**
* Get a cookie from the request
*/
async getCookie(name: string) {
return (await this.#platform.getContextItem(
"cookie",
this.#context,
name,
)) as string;
}
/**
* Get a state item from the context
*/
async getState(name: string) {
return await this.#platform.getContextItem("state", this.#context, name);
}
/**
* Get a request state item from the context
*/
async getRequestState(name: string) {
return await this.#platform.getContextItem(
"requestState",
this.#context,
name,
);
}
/**
* Set a cookie on the response
*/
async setCookie(name: string, value: string, options?: unknown) {
await this.#platform.setContextItem(
"cookie",
this.#context,
value,
name,
options,
);
}
/**
* Get the value of a request header
*/
async getHeader(name: string) {
return (await this.#platform.getContextItem(
"header",
this.#context,
name,
)) as string;
}
/**
* Set a header on the response
*/
async setHeader(name: string, value: string) {
await this.#platform.setContextItem("header", this.#context, value, name);
}
/**
* Set a status code on the response
*/
async setStatus(status: number) {
await this.#platform.setContextItem("status", this.#context, status);
}
/**
* Set a state item on the context
*/
async setState(name: string, value: unknown) {
await this.#platform.setContextItem("state", this.#context, value, name);
}
/**
* Set a request state item on the context
*/
async setRequestState(name: string, value: unknown) {
await this.#platform.setContextItem(
"requestState",
this.#context,
value,
name,
);
}
/**
* Set a file as a response
*/
async sendFile(path: string) {
await this.#platform.sendFile(this.#context, path);
}
}