2023-01-31 21:36:48 +00:00
|
|
|
import type {
|
|
|
|
Input,
|
2023-02-13 21:21:30 +00:00
|
|
|
InputToDataByTarget,
|
2023-01-31 21:36:48 +00:00
|
|
|
ParamKeys,
|
|
|
|
ParamKeyToRecord,
|
|
|
|
RemoveQuestion,
|
|
|
|
UndefinedIfHavingQuestion,
|
2023-02-13 21:21:30 +00:00
|
|
|
ValidationTargets,
|
2023-01-31 21:36:48 +00:00
|
|
|
} from './types.ts'
|
2022-07-12 14:25:47 +00:00
|
|
|
import { parseBody } from './utils/body.ts'
|
2022-09-20 13:01:03 +00:00
|
|
|
import type { BodyData } from './utils/body.ts'
|
2022-07-10 15:17:29 +00:00
|
|
|
import type { Cookie } from './utils/cookie.ts'
|
|
|
|
import { parse } from './utils/cookie.ts'
|
2023-01-22 04:32:33 +00:00
|
|
|
import type { UnionToIntersection } from './utils/types.ts'
|
2023-04-30 12:07:00 +00:00
|
|
|
import { getQueryParam, getQueryParams, decodeURIComponent_ } from './utils/url.ts'
|
2022-07-10 15:17:29 +00:00
|
|
|
|
2023-03-11 13:17:08 +00:00
|
|
|
export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
|
2022-12-20 22:05:00 +00:00
|
|
|
raw: Request
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-12-20 22:05:00 +00:00
|
|
|
private paramData: Record<string, string> | undefined
|
2023-04-30 12:07:00 +00:00
|
|
|
private vData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData
|
2023-03-17 07:32:19 +00:00
|
|
|
path: string
|
2022-12-20 22:05:00 +00:00
|
|
|
|
2023-03-17 07:32:19 +00:00
|
|
|
constructor(
|
|
|
|
request: Request,
|
|
|
|
path: string = '/',
|
|
|
|
paramData?: Record<string, string> | undefined
|
|
|
|
) {
|
2022-12-20 22:05:00 +00:00
|
|
|
this.raw = request
|
2023-03-17 07:32:19 +00:00
|
|
|
this.path = path
|
2022-12-20 22:05:00 +00:00
|
|
|
this.paramData = paramData
|
2023-04-30 12:07:00 +00:00
|
|
|
this.vData = {}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 21:36:48 +00:00
|
|
|
param(key: RemoveQuestion<ParamKeys<P>>): UndefinedIfHavingQuestion<ParamKeys<P>>
|
|
|
|
param(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>>
|
2023-01-22 05:28:13 +00:00
|
|
|
param(key?: string): unknown {
|
2022-07-02 06:09:45 +00:00
|
|
|
if (this.paramData) {
|
|
|
|
if (key) {
|
2022-12-13 16:07:07 +00:00
|
|
|
const param = this.paramData[key]
|
2023-04-30 12:07:00 +00:00
|
|
|
return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined
|
2022-07-02 06:09:45 +00:00
|
|
|
} else {
|
2022-11-14 00:10:03 +00:00
|
|
|
const decoded: Record<string, string> = {}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(this.paramData)) {
|
2022-12-20 22:05:00 +00:00
|
|
|
if (value && typeof value === 'string') {
|
2023-04-30 12:07:00 +00:00
|
|
|
decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value
|
2022-12-13 16:07:07 +00:00
|
|
|
}
|
2022-11-14 00:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return decoded
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2023-02-13 12:40:51 +00:00
|
|
|
query(key: string): string | undefined
|
2022-12-20 22:05:00 +00:00
|
|
|
query(): Record<string, string>
|
|
|
|
query(key?: string) {
|
2023-03-29 14:03:00 +00:00
|
|
|
return getQueryParam(this.url, key)
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2023-02-13 12:40:51 +00:00
|
|
|
queries(key: string): string[] | undefined
|
2022-12-20 22:05:00 +00:00
|
|
|
queries(): Record<string, string[]>
|
|
|
|
queries(key?: string) {
|
2023-03-29 14:03:00 +00:00
|
|
|
return getQueryParams(this.url, key)
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 21:47:05 +00:00
|
|
|
header(name: string): string | undefined
|
2022-12-20 22:05:00 +00:00
|
|
|
header(): Record<string, string>
|
|
|
|
header(name?: string) {
|
2023-06-29 07:27:05 +00:00
|
|
|
if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined
|
|
|
|
|
2023-05-02 06:56:17 +00:00
|
|
|
const headerData: Record<string, string | undefined> = {}
|
2023-02-01 15:25:58 +00:00
|
|
|
this.raw.headers.forEach((value, key) => {
|
|
|
|
headerData[key] = value
|
|
|
|
})
|
2023-06-29 07:27:05 +00:00
|
|
|
return headerData
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
2022-07-10 15:17:29 +00:00
|
|
|
|
2023-05-05 02:08:03 +00:00
|
|
|
/** @deprecated
|
|
|
|
* Use Cookie Middleware instead of `c.req.cookie()`. The `c.req.cookie()` will be removed in v4.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* import { getCookie } from 'hono/cookie'
|
|
|
|
* // ...
|
|
|
|
* app.get('/', (c) => c.text(getCookie(c, 'cookie-name')))
|
|
|
|
*/
|
2022-12-20 22:05:00 +00:00
|
|
|
cookie(key: string): string | undefined
|
2023-05-05 02:08:03 +00:00
|
|
|
|
|
|
|
/** @deprecated
|
|
|
|
* Use Cookie Middleware instead of `c.req.cookie()`. The `c.req.cookie()` will be removed in v4.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* import { getCookie } from 'hono/cookie'
|
|
|
|
* // ...
|
|
|
|
* app.get('/', (c) => c.json(getCookie(c)))
|
|
|
|
*/
|
2022-12-20 22:05:00 +00:00
|
|
|
cookie(): Cookie
|
2023-05-05 02:08:03 +00:00
|
|
|
|
2022-12-20 22:05:00 +00:00
|
|
|
cookie(key?: string) {
|
|
|
|
const cookie = this.raw.headers.get('Cookie')
|
|
|
|
if (!cookie) return
|
2022-07-10 15:17:29 +00:00
|
|
|
const obj = parse(cookie)
|
|
|
|
if (key) {
|
|
|
|
const value = obj[key]
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
return obj
|
|
|
|
}
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
2022-07-12 14:25:47 +00:00
|
|
|
|
2023-01-02 23:25:21 +00:00
|
|
|
async parseBody(): Promise<BodyData> {
|
2023-02-01 15:25:58 +00:00
|
|
|
return await parseBody(this.raw)
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
2022-09-12 22:54:06 +00:00
|
|
|
|
2023-02-01 15:25:58 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-02-16 09:08:50 +00:00
|
|
|
json<T = any>(): Promise<T> {
|
|
|
|
return this.raw.json()
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 09:08:50 +00:00
|
|
|
text() {
|
2022-12-20 22:05:00 +00:00
|
|
|
return this.raw.text()
|
|
|
|
}
|
|
|
|
|
2023-02-16 09:08:50 +00:00
|
|
|
arrayBuffer() {
|
2022-12-20 22:05:00 +00:00
|
|
|
return this.raw.arrayBuffer()
|
|
|
|
}
|
|
|
|
|
2023-02-16 09:08:50 +00:00
|
|
|
blob() {
|
2022-12-20 22:05:00 +00:00
|
|
|
return this.raw.blob()
|
|
|
|
}
|
|
|
|
|
2023-02-16 09:08:50 +00:00
|
|
|
formData() {
|
2022-12-20 22:05:00 +00:00
|
|
|
return this.raw.formData()
|
|
|
|
}
|
2022-09-20 01:11:34 +00:00
|
|
|
|
2023-02-13 21:21:30 +00:00
|
|
|
addValidatedData(target: keyof ValidationTargets, data: {}) {
|
2023-04-30 12:07:00 +00:00
|
|
|
this.vData[target] = data
|
2023-02-01 13:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid<
|
2023-02-13 21:21:30 +00:00
|
|
|
T extends keyof ValidationTargets = I extends Record<infer R, unknown>
|
|
|
|
? R extends keyof ValidationTargets
|
2023-02-01 13:02:09 +00:00
|
|
|
? R
|
|
|
|
: never
|
|
|
|
: never
|
2023-02-13 21:21:30 +00:00
|
|
|
>(target: T): InputToDataByTarget<I, T>
|
2023-02-01 23:06:19 +00:00
|
|
|
valid(): never
|
2023-02-13 21:21:30 +00:00
|
|
|
valid(target?: keyof ValidationTargets) {
|
|
|
|
if (target) {
|
2023-04-30 12:07:00 +00:00
|
|
|
return this.vData[target] as unknown
|
2022-09-20 01:11:34 +00:00
|
|
|
}
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
return this.raw.url
|
|
|
|
}
|
|
|
|
get method() {
|
|
|
|
return this.raw.method
|
|
|
|
}
|
|
|
|
get headers() {
|
|
|
|
return this.raw.headers
|
|
|
|
}
|
|
|
|
get body() {
|
|
|
|
return this.raw.body
|
|
|
|
}
|
|
|
|
get bodyUsed() {
|
|
|
|
return this.raw.bodyUsed
|
|
|
|
}
|
|
|
|
get integrity() {
|
|
|
|
return this.raw.integrity
|
|
|
|
}
|
|
|
|
get keepalive() {
|
|
|
|
return this.raw.keepalive
|
|
|
|
}
|
|
|
|
get referrer() {
|
|
|
|
return this.raw.referrer
|
|
|
|
}
|
|
|
|
get signal() {
|
|
|
|
return this.raw.signal
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|