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

fix(req): fix inferring types in req.valid() (#1351)

* fix(req): fix inferring types in `req.valid()`

* denoify
This commit is contained in:
Yusuke Wada 2023-08-19 17:48:49 +09:00 committed by GitHub
parent dc0696f2d1
commit 2faaa15ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 26 deletions

View File

@ -155,18 +155,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
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) {
return this.vData[target] as unknown
}
valid<T extends keyof I & keyof ValidationTargets>(target: T): InputToDataByTarget<I, T>
valid(target: keyof ValidationTargets) {
return this.vData[target] as unknown
}
get url() {

View File

@ -44,14 +44,14 @@ describe('req.addValidatedData() and req.data()', () => {
}
test('add data - json', () => {
const req = new HonoRequest(rawRequest)
const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)
req.addValidatedData('json', payload)
const data = req.valid('json')
expect(data).toEqual(payload)
})
test('replace data - json', () => {
const req = new HonoRequest(rawRequest)
const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)
req.addValidatedData('json', payload)
req.addValidatedData('json', {
tag: ['sport', 'music'],

View File

@ -155,18 +155,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
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) {
return this.vData[target] as unknown
}
valid<T extends keyof I & keyof ValidationTargets>(target: T): InputToDataByTarget<I, T>
valid(target: keyof ValidationTargets) {
return this.vData[target] as unknown
}
get url() {