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:
parent
dc0696f2d1
commit
2faaa15ea1
@ -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() {
|
||||
|
@ -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'],
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user