Skip to content
Myr

request.ts

This file

Path

src/request.ts

208 lines · 5.2 KiB · TypeScript

Last changed by

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

History of this file

Take it

The walk stopped after 200 commits: lines older than 165d925 point at it.

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 1–2why, one step back
1/* eslint-disable @typescript-eslint/no-explicit-any */
2/* eslint-disable @typescript-eslint/no-non-null-assertion */

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 3–16why, one step back
3import type {
4 Input,5 InputToDataByTarget,6 ParamKeys,7 ParamKeyToRecord,8 RemoveQuestion,9 UndefinedIfHavingQuestion,10 ValidationTargets,11} from './types'12import { parseBody } from './utils/body'13import type { BodyData } from './utils/body'14import type { Cookie } from './utils/cookie'15import { parse } from './utils/cookie'16import type { UnionToIntersection } from './utils/types'

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 17why, one step back
17import { getQueryParam, getQueryParams, decodeURIComponent_ } from './utils/url'

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 18why, one step back
18

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

lines 19–26why, one step back
19type Body = {
20 json: any21 text: string22 arrayBuffer: ArrayBuffer23 blob: Blob24 formData: FormData25}26type BodyCache = Partial<Body & { parsedBody: BodyData }>

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

line 27why, one step back
27

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 28why, one step back
28export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 29–31why, one step back
29 raw: Request
3031 private paramData: Record<string, string> | undefined

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 32why, one step back
32 private vData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 33why, one step back
33 path: string

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

line 34why, one step back
34 bodyCache: BodyCache = {}

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 35why, one step back
35

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

lines 36–40why, one step back
36 constructor(
37 request: Request,38 path: string = '/',39 paramData?: Record<string, string> | undefined40 ) {

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 41why, one step back
41 this.raw = request

Merge pull request #989 from honojs/nextYusuke Wadabefore adoption5c8c9bd

line 42why, one step back
42 this.path = path

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 43why, one step back
43 this.paramData = paramData

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 44why, one step back
44 this.vData = {}

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 45–46why, one step back
45 }
46

feat(req): allow passing generics to `c.req.param()` (#1329)Yusuke Wadabefore adoption4112fc3

lines 47–50why, one step back
47 param<P2 extends string = P>(
48 key: RemoveQuestion<ParamKeys<P2>>49 ): UndefinedIfHavingQuestion<ParamKeys<P2>>50 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 51–54why, one step back
51 param(key?: string): unknown {
52 if (this.paramData) {53 if (key) {54 const param = this.paramData[key]

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 55why, one step back
55 return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 56–60why, one step back
56 } else {
57 const decoded: Record<string, string> = {}5859 for (const [key, value] of Object.entries(this.paramData)) {60 if (value && typeof value === 'string') {

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 61why, one step back
61 decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 62–73why, one step back
62 }
63 }6465 return decoded66 }67 }68 return null69 }7071 query(key: string): string | undefined72 query(): Record<string, string>73 query(key?: string) {

refactor(utils/url): Decode parameters more accurately without performance degradation. (#1015)Taku Amanobefore adoption91c04ac

line 74why, one step back
74 return getQueryParam(this.url, key)

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 75–79why, one step back
75 }
7677 queries(key: string): string[] | undefined78 queries(): Record<string, string[]>79 queries(key?: string) {

refactor(utils/url): Decode parameters more accurately without performance degradation. (#1015)Taku Amanobefore adoption91c04ac

line 80why, one step back
80 return getQueryParams(this.url, key)

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 81–85why, one step back
81 }
8283 header(name: string): string | undefined84 header(): Record<string, string>85 header(name?: string) {

perf(Request): optimize Request.prototype.header (#1204)James A Rosenbefore adoptionddc34e1

lines 86–87why, one step back
86 if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined
87

fix: empty string is a valid header value (#1056)Alex Errantbefore adoption7c6ebe8

line 88why, one step back
88 const headerData: Record<string, string | undefined> = {}

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 89–91why, one step back
89 this.raw.headers.forEach((value, key) => {
90 headerData[key] = value91 })

perf(Request): optimize Request.prototype.header (#1204)James A Rosenbefore adoptionddc34e1

line 92why, one step back
92 return headerData

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 93–94why, one step back
93 }
94

feat: Cookie Middleware and deprecate `c.req.cookie()` / `c.cookie()` (#1066)Yusuke Wadabefore adoptiona73f0d2

lines 95–103why, one step back
95 /** @deprecated
96 * Use Cookie Middleware instead of `c.req.cookie()`. The `c.req.cookie()` will be removed in v4.97 *98 * @example99 *100 * import { getCookie } from 'hono/cookie'101 * // ...102 * app.get('/', (c) => c.text(getCookie(c, 'cookie-name')))103 */

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 104why, one step back
104 cookie(key: string): string | undefined

feat: Cookie Middleware and deprecate `c.req.cookie()` / `c.cookie()` (#1066)Yusuke Wadabefore adoptiona73f0d2

lines 105–114why, one step back
105
106 /** @deprecated107 * Use Cookie Middleware instead of `c.req.cookie()`. The `c.req.cookie()` will be removed in v4.108 *109 * @example110 *111 * import { getCookie } from 'hono/cookie'112 * // ...113 * app.get('/', (c) => c.json(getCookie(c)))114 */

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 115why, one step back
115 cookie(): Cookie

feat: Cookie Middleware and deprecate `c.req.cookie()` / `c.cookie()` (#1066)Yusuke Wadabefore adoptiona73f0d2

line 116why, one step back
116

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 117–128why, one step back
117 cookie(key?: string) {
118 const cookie = this.raw.headers.get('Cookie')119 if (!cookie) return120 const obj = parse(cookie)121 if (key) {122 const value = obj[key]123 return value124 } else {125 return obj126 }127 }128

feat(parseBody): allow passing generics to `parseBody()` (#1289)Yusuke Wadabefore adoption85b5492

line 129why, one step back
129 async parseBody<T extends BodyData = BodyData>(): Promise<T> {

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

lines 130–133why, one step back
130 if (this.bodyCache.parsedBody) return this.bodyCache.parsedBody as T
131 const parsedBody = await parseBody<T>(this)132 this.bodyCache.parsedBody = parsedBody133 return parsedBody

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 134–135why, one step back
134 }
135

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

line 136why, one step back
136 private cachedBody = (key: keyof Body) => {

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 137–139why, one step back
137 const { bodyCache, raw } = this
138 const cachedBody = bodyCache[key]139 if (cachedBody) return cachedBody

fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c

lines 140–148why, one step back
140 /**
141 * If an arrayBuffer cache is exist,142 * use it for creating a text, json, and others.143 */144 if (bodyCache.arrayBuffer) {145 return (async () => {146 return await new Response(bodyCache.arrayBuffer)[key]()147 })()148 }

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 149–151why, one step back
149 return (bodyCache[key] = raw[key]())
150 }151

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

line 152why, one step back
152 json<T = any>(): Promise<T> {

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

line 153why, one step back
153 return this.cachedBody('json')

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 154–155why, one step back
154 }
155

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 156–157why, one step back
156 text(): Promise<string> {
157 return this.cachedBody('text')

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 158–159why, one step back
158 }
159

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 160–161why, one step back
160 arrayBuffer(): Promise<ArrayBuffer> {
161 return this.cachedBody('arrayBuffer')

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 162–163why, one step back
162 }
163

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 164–165why, one step back
164 blob(): Promise<Blob> {
165 return this.cachedBody('blob')

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 166–167why, one step back
166 }
167

feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc

lines 168–169why, one step back
168 formData(): Promise<FormData> {
169 return this.cachedBody('formData')

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 170–172why, one step back
170 }
171172 addValidatedData(target: keyof ValidationTargets, data: {}) {

Merge pull request #1117 from honojs/nextYusuke Wadabefore adoptionec6fa09

line 173why, one step back
173 this.vData[target] = data

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 174–175why, one step back
174 }
175

fix(req): fix inferring types in `req.valid()` (#1351)Yusuke Wadabefore adoption2faaa15

lines 176–178why, one step back
176 valid<T extends keyof I & keyof ValidationTargets>(target: T): InputToDataByTarget<I, T>
177 valid(target: keyof ValidationTargets) {178 return this.vData[target] as unknown

fix(pages/nextjs): don't use `route()` if path is not passed (#943)Yusuke Wadabefore adoption165d925

lines 179–208why, one step back
179 }
180181 get url() {182 return this.raw.url183 }184 get method() {185 return this.raw.method186 }187 get headers() {188 return this.raw.headers189 }190 get body() {191 return this.raw.body192 }193 get bodyUsed() {194 return this.raw.bodyUsed195 }196 get integrity() {197 return this.raw.integrity198 }199 get keepalive() {200 return this.raw.keepalive201 }202 get referrer() {203 return this.raw.referrer204 }205 get signal() {206 return this.raw.signal207 }208}