-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
request.ts
163 lines (152 loc) · 4.38 KB
/
request.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
import { parseBody } from './utils/body.ts'
import type { BodyData } from './utils/body.ts'
import type { Cookie } from './utils/cookie.ts'
import { parse } from './utils/cookie.ts'
import { getQueryStringFromURL } from './utils/url.ts'
type ValidatedData = Record<string, any>
declare global {
interface Request<
ParamKeyType extends string = string,
Data extends ValidatedData = ValidatedData
> {
paramData?: Record<ParamKeyType, string>
param: {
(key: ParamKeyType): string
(): Record<ParamKeyType, string>
}
queryData?: Record<string, string>
query: {
(key: string): string
(): Record<string, string>
}
queries: {
(key: string): string[]
(): Record<string, string[]>
}
headerData?: Record<string, string>
header: {
(name: string): string
(): Record<string, string>
}
cookie: {
(name: string): string
(): Cookie
}
bodyData?: BodyData
parseBody<BodyType extends BodyData>(): Promise<BodyType>
jsonData?: any
json<JSONData = any>(): Promise<JSONData>
data: Data
valid: {
(key: string | string[], value: any): Data
(): Data
}
}
}
export function extendRequestPrototype() {
if (!!Request.prototype.param as boolean) {
// already extended
return
}
Request.prototype.param = function (this: Request, key?: string) {
if (this.paramData) {
if (key) {
return this.paramData[key]
} else {
return this.paramData
}
}
return null
} as InstanceType<typeof Request>['param']
Request.prototype.header = function (this: Request, name?: string) {
if (!this.headerData) {
this.headerData = {}
for (const [key, value] of this.headers) {
this.headerData[key] = value
}
}
if (name) {
return this.headerData[name.toLowerCase()]
} else {
return this.headerData
}
} as InstanceType<typeof Request>['header']
Request.prototype.query = function (this: Request, key?: string) {
const queryString = getQueryStringFromURL(this.url)
const searchParams = new URLSearchParams(queryString)
if (!this.queryData) {
this.queryData = {}
for (const key of searchParams.keys()) {
this.queryData[key] = searchParams.get(key) || ''
}
}
if (key) {
return this.queryData[key]
} else {
return this.queryData
}
} as InstanceType<typeof Request>['query']
Request.prototype.queries = function (this: Request, key?: string) {
const queryString = getQueryStringFromURL(this.url)
const searchParams = new URLSearchParams(queryString)
if (key) {
return searchParams.getAll(key)
} else {
const result: Record<string, string[]> = {}
for (const key of searchParams.keys()) {
result[key] = searchParams.getAll(key)
}
return result
}
} as InstanceType<typeof Request>['queries']
Request.prototype.cookie = function (this: Request, key?: string) {
const cookie = this.headers.get('Cookie') || ''
const obj = parse(cookie)
if (key) {
const value = obj[key]
return value
} else {
return obj
}
} as InstanceType<typeof Request>['cookie']
Request.prototype.parseBody = async function <BodyType extends BodyData>(
this: Request
): Promise<BodyType> {
// Cache the parsed body
let body: BodyType
if (!this.bodyData) {
body = await parseBody<BodyType>(this)
this.bodyData = body
} else {
body = this.bodyData as BodyType
}
return body
} as InstanceType<typeof Request>['parseBody']
Request.prototype.json = async function <JSONData>(this: Request): Promise<JSONData> {
// Cache the JSON body
let jsonData: JSONData
if (!this.jsonData) {
jsonData = JSON.parse(await this.text())
this.jsonData = jsonData
} else {
jsonData = this.jsonData
}
return jsonData
} as InstanceType<typeof Request>['jsonData']
Request.prototype.valid = function (this: Request, keys?: string | string[], value?: any) {
if (!this.data) {
this.data = {}
}
if (keys !== undefined) {
if (typeof keys === 'string') {
keys = [keys]
}
let data = this.data
for (let i = 0; i < keys.length - 1; i++) {
data = data[keys[i]] ||= {}
}
data[keys[keys.length - 1]] = value
}
return this.data
} as InstanceType<typeof Request>['valid']
}