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

207 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-07-12 14:25:47 +00:00
import { parseBody } from './utils/body.ts'
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'
import { getQueryStringFromURL } from './utils/url.ts'
2022-07-10 15:17:29 +00:00
export class HonoRequest<
ParamKey extends string = string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Data = any
> {
raw: Request
2022-07-02 06:09:45 +00:00
private paramData: Record<string, string> | undefined
private headerData: Record<string, string> | undefined
private queryData: Record<string, string> | undefined
private bodyData: BodyData | undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private jsonData: Promise<any> | undefined
private data: Data | undefined
constructor(request: Request, paramData?: Record<string, string> | undefined) {
this.raw = request
this.paramData = paramData
2022-07-02 06:09:45 +00:00
}
param(key: ParamKey): string
param(): Record<ParamKey, string>
param(key?: string) {
2022-07-02 06:09:45 +00:00
if (this.paramData) {
if (key) {
const param = this.paramData[key]
return param ? decodeURIComponent(param) : undefined
2022-07-02 06:09:45 +00:00
} else {
const decoded: Record<string, string> = {}
for (const [key, value] of Object.entries(this.paramData)) {
if (value && typeof value === 'string') {
decoded[key] = decodeURIComponent(value)
}
}
return decoded
2022-07-02 06:09:45 +00:00
}
}
return null
}
2022-07-02 06:09:45 +00:00
query(key: string): string
query(): Record<string, string>
query(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) || ''
2022-07-02 06:09:45 +00:00
}
}
if (key) {
return this.queryData[key]
} else {
return this.queryData
2022-07-02 06:09:45 +00:00
}
}
2022-07-02 06:09:45 +00:00
queries(key: string): string[]
queries(): Record<string, string[]>
queries(key?: string) {
const queryString = getQueryStringFromURL(this.url)
const searchParams = new URLSearchParams(queryString)
2022-07-02 06:09:45 +00:00
if (key) {
return searchParams.getAll(key)
2022-07-02 06:09:45 +00:00
} else {
const result: Record<string, string[]> = {}
for (const key of searchParams.keys()) {
result[key] = searchParams.getAll(key)
2022-07-02 06:09:45 +00:00
}
return result
}
}
header(name: string): string
header(): Record<string, string>
header(name?: string) {
if (!this.headerData) {
this.headerData = {}
this.raw.headers.forEach((value, key) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.headerData![key] = value
})
}
if (name) {
return this.headerData[name.toLowerCase()]
} else {
return this.headerData
}
}
2022-07-10 15:17:29 +00:00
cookie(key: string): string | undefined
cookie(): Cookie
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-07-12 14:25:47 +00:00
async parseBody<BodyType extends BodyData>(): Promise<BodyType> {
// Cache the parsed body
let body: BodyType
if (!this.bodyData) {
body = await parseBody<BodyType>(this.raw)
this.bodyData = body
} else {
body = this.bodyData as BodyType
2022-07-12 14:25:47 +00:00
}
return body
}
async json<JSONData = unknown>() {
// Cache the JSON body
let jsonData: Promise<Partial<JSONData>>
if (!this.jsonData) {
jsonData = this.raw.json()
this.jsonData = jsonData
} else {
jsonData = this.jsonData
}
return jsonData
}
async text() {
return this.raw.text()
}
async arrayBuffer() {
return this.raw.arrayBuffer()
}
async blob() {
return this.raw.blob()
}
async formData() {
return this.raw.formData()
}
valid(data?: unknown) {
if (!this.data) {
this.data = {} as Data
}
if (data) {
this.data = data as Data
}
return this.data
}
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 06:09:45 +00:00
}