0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/deno_dist/request.ts

202 lines
4.6 KiB
TypeScript
Raw Normal View History

import type {
Input,
InputToDataByTarget,
ParamKeys,
ParamKeyToRecord,
RemoveQuestion,
UndefinedIfHavingQuestion,
ValidationTargets,
} from './types.ts'
2022-07-12 16:25:47 +02:00
import { parseBody } from './utils/body.ts'
import type { BodyData } from './utils/body.ts'
2022-07-10 17:17:29 +02:00
import type { Cookie } from './utils/cookie.ts'
import { parse } from './utils/cookie.ts'
import type { UnionToIntersection } from './utils/types.ts'
2023-04-30 14:07:00 +02:00
import { getQueryParam, getQueryParams, decodeURIComponent_ } from './utils/url.ts'
2022-07-10 17:17:29 +02:00
export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
raw: Request
2022-07-02 08:09:45 +02:00
private paramData: Record<string, string> | undefined
2023-04-30 14:07:00 +02:00
private vData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData
path: string
constructor(
request: Request,
path: string = '/',
paramData?: Record<string, string> | undefined
) {
this.raw = request
this.path = path
this.paramData = paramData
2023-04-30 14:07:00 +02:00
this.vData = {}
2022-07-02 08:09:45 +02:00
}
param(key: RemoveQuestion<ParamKeys<P>>): UndefinedIfHavingQuestion<ParamKeys<P>>
param(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>>
2023-01-22 06:28:13 +01:00
param(key?: string): unknown {
2022-07-02 08:09:45 +02:00
if (this.paramData) {
if (key) {
const param = this.paramData[key]
2023-04-30 14:07:00 +02:00
return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined
2022-07-02 08:09:45 +02:00
} else {
const decoded: Record<string, string> = {}
for (const [key, value] of Object.entries(this.paramData)) {
if (value && typeof value === 'string') {
2023-04-30 14:07:00 +02:00
decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value
}
}
return decoded
2022-07-02 08:09:45 +02:00
}
}
return null
}
2022-07-02 08:09:45 +02:00
query(key: string): string | undefined
query(): Record<string, string>
query(key?: string) {
return getQueryParam(this.url, key)
}
2022-07-02 08:09:45 +02:00
queries(key: string): string[] | undefined
queries(): Record<string, string[]>
queries(key?: string) {
return getQueryParams(this.url, key)
}
2023-02-12 22:47:05 +01:00
header(name: string): string | undefined
header(): Record<string, string>
header(name?: string) {
const headerData: Record<string, string | undefined> = {}
2023-02-01 16:25:58 +01:00
this.raw.headers.forEach((value, key) => {
headerData[key] = value
})
2023-02-12 22:47:05 +01:00
if (!name) {
2023-02-01 16:25:58 +01:00
return headerData
}
return headerData[name.toLowerCase()]
}
2022-07-10 17:17:29 +02: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')))
*/
cookie(key: string): string | undefined
/** @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)))
*/
cookie(): Cookie
cookie(key?: string) {
const cookie = this.raw.headers.get('Cookie')
if (!cookie) return
2022-07-10 17:17:29 +02:00
const obj = parse(cookie)
if (key) {
const value = obj[key]
return value
} else {
return obj
}
}
2022-07-12 16:25:47 +02:00
async parseBody(): Promise<BodyData> {
2023-02-01 16:25:58 +01:00
return await parseBody(this.raw)
}
2023-02-01 16:25:58 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2023-02-16 10:08:50 +01:00
json<T = any>(): Promise<T> {
return this.raw.json()
}
2023-02-16 10:08:50 +01:00
text() {
return this.raw.text()
}
2023-02-16 10:08:50 +01:00
arrayBuffer() {
return this.raw.arrayBuffer()
}
2023-02-16 10:08:50 +01:00
blob() {
return this.raw.blob()
}
2023-02-16 10:08:50 +01:00
formData() {
return this.raw.formData()
}
addValidatedData(target: keyof ValidationTargets, data: {}) {
2023-04-30 14:07:00 +02:00
this.vData[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) {
2023-04-30 14:07:00 +02:00
return this.vData[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
}
2022-07-02 08:09:45 +02:00
}