forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.ts
187 lines (168 loc) · 4.32 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type {
Input,
InputToDataByTarget,
ParamKeys,
ParamKeyToRecord,
RemoveQuestion,
UndefinedIfHavingQuestion,
ValidationTargets,
} from './types.ts'
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 type { UnionToIntersection } from './utils/types.ts'
import { getQueryStringFromURL, getQueryParam, getQueryParams } from './utils/url.ts'
export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
raw: Request
private paramData: Record<string, string> | undefined
private validatedData: { [K in keyof ValidationTargets]?: {} }
path: string
constructor(
request: Request,
path: string = '/',
paramData?: Record<string, string> | undefined
) {
this.raw = request
this.path = path
this.paramData = paramData
this.validatedData = {}
}
param(key: RemoveQuestion<ParamKeys<P>>): UndefinedIfHavingQuestion<ParamKeys<P>>
param(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>>
param(key?: string): unknown {
if (this.paramData) {
if (key) {
const param = this.paramData[key]
return param ? (/\%/.test(param) ? decodeURIComponent(param) : param) : undefined
} else {
const decoded: Record<string, string> = {}
for (const [key, value] of Object.entries(this.paramData)) {
if (value && typeof value === 'string') {
decoded[key] = /\%/.test(value) ? decodeURIComponent(value) : value
}
}
return decoded
}
}
return null
}
query(key: string): string | undefined
query(): Record<string, string>
query(key?: string) {
const queryString = getQueryStringFromURL(this.url)
const result = getQueryParam(queryString, key)
if (result === null) return undefined
return result
}
queries(key: string): string[] | undefined
queries(): Record<string, string[]>
queries(key?: string) {
const queryString = getQueryStringFromURL(this.url)
const result = getQueryParams(queryString, key)
if (result === null) return undefined
return result
}
header(name: string): string | undefined
header(): Record<string, string>
header(name?: string) {
const headerData: Record<string, string> = {}
this.raw.headers.forEach((value, key) => {
headerData[key] = value
})
if (!name) {
return headerData
}
return headerData[name.toLowerCase()] || undefined
}
cookie(key: string): string | undefined
cookie(): Cookie
cookie(key?: string) {
const cookie = this.raw.headers.get('Cookie')
if (!cookie) return
const obj = parse(cookie)
if (key) {
const value = obj[key]
return value
} else {
return obj
}
}
async parseBody(): Promise<BodyData> {
return await parseBody(this.raw)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json<T = any>(): Promise<T> {
return this.raw.json()
}
text() {
return this.raw.text()
}
arrayBuffer() {
return this.raw.arrayBuffer()
}
blob() {
return this.raw.blob()
}
formData() {
return this.raw.formData()
}
addValidatedData(target: keyof ValidationTargets, data: {}) {
this.validatedData[target] = data
}
valid<
T extends keyof ValidationTargets = I extends Record<infer R, unknown>
? R extends keyof ValidationTargets
? R
: never
: never
>(target: T): InputToDataByTarget<I, T>
valid(): never
valid(target?: keyof ValidationTargets) {
if (target) {
return this.validatedData[target] as unknown
}
}
get url() {
return this.raw.url
}
get method() {
return this.raw.method
}
get headers() {
return this.raw.headers
}
get redirect() {
return this.raw.redirect
}
get body() {
return this.raw.body
}
get bodyUsed() {
return this.raw.bodyUsed
}
get cache() {
return this.raw.cache
}
get credentials() {
return this.raw.credentials
}
get integrity() {
return this.raw.integrity
}
get keepalive() {
return this.raw.keepalive
}
get mode() {
return this.raw.mode
}
get referrer() {
return this.raw.referrer
}
get refererPolicy() {
return this.raw.referrerPolicy
}
get signal() {
return this.raw.signal
}
}