2023-01-22 23:47:29 +01:00
|
|
|
import type { InputToData, InputToTypeData, ValidationTypes } from './types.ts'
|
2022-07-12 16:25:47 +02:00
|
|
|
import { parseBody } from './utils/body.ts'
|
2022-09-20 15:01:03 +02:00
|
|
|
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'
|
2023-01-22 23:47:29 +01:00
|
|
|
import { mergeObjects } from './utils/object.ts'
|
2023-01-22 05:32:33 +01:00
|
|
|
import type { UnionToIntersection } from './utils/types.ts'
|
2023-01-17 10:30:02 +01:00
|
|
|
import { getQueryStringFromURL, getQueryParam, getQueryParams } from './utils/url.ts'
|
2022-07-10 17:17:29 +02:00
|
|
|
|
2023-01-22 06:28:13 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}`
|
|
|
|
? Name
|
|
|
|
: NameWithPattern
|
|
|
|
|
|
|
|
type ParamKey<Component> = Component extends `:${infer NameWithPattern}`
|
|
|
|
? ParamKeyName<NameWithPattern>
|
|
|
|
: never
|
|
|
|
|
|
|
|
type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}`
|
|
|
|
? ParamKey<Component> | ParamKeys<Rest>
|
|
|
|
: ParamKey<Path>
|
2022-07-10 17:17:29 +02:00
|
|
|
|
2023-01-22 05:32:33 +01:00
|
|
|
type RemoveQuestion<T> = T extends `${infer R}?` ? R : T
|
2023-01-22 06:28:13 +01:00
|
|
|
|
2023-01-22 05:32:33 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
type UndefinedIfHavingQuestion<T> = T extends `${infer _}?` ? string | undefined : string
|
2023-01-22 06:28:13 +01:00
|
|
|
|
2023-01-22 05:32:33 +01:00
|
|
|
type ParamKeyToRecord<T extends string> = T extends `${infer R}?`
|
|
|
|
? Record<R, string | undefined>
|
|
|
|
: Record<T, string>
|
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
export class HonoRequest<Path extends string = '/', Input = {}> {
|
2022-12-20 23:05:00 +01:00
|
|
|
raw: Request
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-12-20 23:05:00 +01:00
|
|
|
private paramData: Record<string, string> | undefined
|
|
|
|
private headerData: Record<string, string> | undefined
|
|
|
|
private bodyData: BodyData | undefined
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private jsonData: Promise<any> | undefined
|
2023-01-22 23:47:29 +01:00
|
|
|
private validatedData: { [K in keyof ValidationTypes]?: {} }
|
2023-01-17 10:30:02 +01:00
|
|
|
private queryIndex: number
|
2022-12-20 23:05:00 +01:00
|
|
|
|
2023-01-10 13:24:21 +01:00
|
|
|
constructor(
|
|
|
|
request: Request,
|
|
|
|
paramData?: Record<string, string> | undefined,
|
2023-01-17 10:30:02 +01:00
|
|
|
queryIndex: number = -1
|
2023-01-10 13:24:21 +01:00
|
|
|
) {
|
2022-12-20 23:05:00 +01:00
|
|
|
this.raw = request
|
|
|
|
this.paramData = paramData
|
2023-01-10 13:24:21 +01:00
|
|
|
this.queryIndex = queryIndex
|
2023-01-22 23:47:29 +01:00
|
|
|
this.validatedData = {}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-22 06:28:13 +01:00
|
|
|
param(key: RemoveQuestion<ParamKeys<Path>>): UndefinedIfHavingQuestion<ParamKeys<Path>>
|
|
|
|
param(): UnionToIntersection<ParamKeyToRecord<ParamKeys<Path>>>
|
|
|
|
param(key?: string): unknown {
|
2022-07-02 08:09:45 +02:00
|
|
|
if (this.paramData) {
|
|
|
|
if (key) {
|
2022-12-13 17:07:07 +01:00
|
|
|
const param = this.paramData[key]
|
2023-01-22 05:59:31 +01:00
|
|
|
return param ? (/\%/.test(param) ? decodeURIComponent(param) : param) : undefined
|
2022-07-02 08:09:45 +02:00
|
|
|
} else {
|
2022-11-14 01:10:03 +01:00
|
|
|
const decoded: Record<string, string> = {}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(this.paramData)) {
|
2022-12-20 23:05:00 +01:00
|
|
|
if (value && typeof value === 'string') {
|
2023-01-22 05:59:31 +01:00
|
|
|
decoded[key] = /\%/.test(value) ? decodeURIComponent(value) : value
|
2022-12-13 17:07:07 +01:00
|
|
|
}
|
2022-11-14 01:10:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return decoded
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-12-20 23:05:00 +01:00
|
|
|
query(key: string): string
|
|
|
|
query(): Record<string, string>
|
|
|
|
query(key?: string) {
|
2023-01-10 13:24:21 +01:00
|
|
|
const queryString = getQueryStringFromURL(this.url, this.queryIndex)
|
2023-01-17 10:30:02 +01:00
|
|
|
return getQueryParam(queryString, key)
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-12-20 23:05:00 +01:00
|
|
|
queries(key: string): string[]
|
|
|
|
queries(): Record<string, string[]>
|
|
|
|
queries(key?: string) {
|
2023-01-10 13:24:21 +01:00
|
|
|
const queryString = getQueryStringFromURL(this.url, this.queryIndex)
|
2023-01-17 10:30:02 +01:00
|
|
|
return getQueryParams(queryString, key)
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 17:17:29 +02:00
|
|
|
|
2022-12-20 23:05:00 +01:00
|
|
|
cookie(key: string): string | undefined
|
|
|
|
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-12-20 23:05:00 +01:00
|
|
|
}
|
2022-07-12 16:25:47 +02:00
|
|
|
|
2023-01-03 00:25:21 +01:00
|
|
|
async parseBody(): Promise<BodyData> {
|
2022-09-13 00:54:06 +02:00
|
|
|
// Cache the parsed body
|
2023-01-03 00:25:21 +01:00
|
|
|
let body: BodyData
|
2022-09-13 00:54:06 +02:00
|
|
|
if (!this.bodyData) {
|
2023-01-03 00:25:21 +01:00
|
|
|
body = await parseBody(this.raw)
|
2022-09-13 00:54:06 +02:00
|
|
|
this.bodyData = body
|
2022-08-25 02:25:11 +02:00
|
|
|
} else {
|
2023-01-03 00:25:21 +01:00
|
|
|
body = this.bodyData
|
2022-07-12 16:25:47 +02:00
|
|
|
}
|
2022-08-25 02:25:11 +02:00
|
|
|
return body
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
|
2022-12-20 23:05:00 +01:00
|
|
|
async json<JSONData = unknown>() {
|
2022-09-13 00:54:06 +02:00
|
|
|
// Cache the JSON body
|
2022-12-20 23:05:00 +01:00
|
|
|
let jsonData: Promise<Partial<JSONData>>
|
2022-09-13 00:54:06 +02:00
|
|
|
if (!this.jsonData) {
|
2022-12-20 23:05:00 +01:00
|
|
|
jsonData = this.raw.json()
|
2022-09-13 00:54:06 +02:00
|
|
|
this.jsonData = jsonData
|
|
|
|
} else {
|
|
|
|
jsonData = this.jsonData
|
|
|
|
}
|
|
|
|
return jsonData
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async text() {
|
|
|
|
return this.raw.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
async arrayBuffer() {
|
|
|
|
return this.raw.arrayBuffer()
|
|
|
|
}
|
|
|
|
|
|
|
|
async blob() {
|
|
|
|
return this.raw.blob()
|
|
|
|
}
|
|
|
|
|
|
|
|
async formData() {
|
|
|
|
return this.raw.formData()
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
|
2023-01-22 23:47:29 +01:00
|
|
|
addValidatedData(type: keyof ValidationTypes, data: {}) {
|
|
|
|
const storedData = this.validatedData[type] || {}
|
|
|
|
const merged = mergeObjects(storedData, data)
|
|
|
|
this.validatedData[type] = merged
|
|
|
|
}
|
|
|
|
|
|
|
|
valid(): InputToData<Input>
|
|
|
|
valid<T extends keyof ValidationTypes>(type: T): InputToTypeData<T, Input>
|
|
|
|
valid<T extends keyof ValidationTypes>(type?: T) {
|
|
|
|
if (type) {
|
|
|
|
const data = this.validatedData[type]
|
|
|
|
return data
|
|
|
|
} else {
|
|
|
|
let data: Record<string, unknown> = {}
|
|
|
|
for (const v of Object.values(this.validatedData)) {
|
|
|
|
data = mergeObjects(data, v)
|
|
|
|
}
|
|
|
|
return data
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|