fix(types): improve custom handler (#641)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent c14dd5a · Browse files · 3 files·+21 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+21 added−11 removed
- tests:+10 added−2 removed
- other:+11 added−9 removed
Provenance
- Commit
44eb4bb- Tree
32db5bb- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
c14dd5a
3 files changed+21 added−11 removed
modified:src/context.ts+4 added-4 removed
9 unmodified lines10 11 export interface Context<12 P extends string = string,13 E extends Partial<Environment> = Environment,14- S extends Partial<Schema> = Schema14+ S extends Partial<Schema> | unknown = Schema15 > {16- req: Request<P, SchemaToProp<S>>16+ req: Request<P, S extends Schema ? SchemaToProp<S> : any>17 env: E['Bindings']18 event: FetchEvent19 executionCtx: ExecutionContext20 finalized: boolean29 unmodified lines50 E extends Partial<Environment> = Environment,51 S extends Partial<Schema> = Schema52 > implements Context<P, E, S>53 {54- req: Request<P, SchemaToProp<S>>54+ req: Request<P, S extends Schema ? SchemaToProp<S> : any>55 env: E['Bindings']56 finalized: boolean57 error: Error | undefined = undefined58 12 unmodified lines71 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,72 notFoundHandler: NotFoundHandler<P, E, S> = () => new Response()73 ) {74 this._executionCtx = executionCtx75- this.req = req as Request<P, SchemaToProp<S>>75+ this.req = req as Request<P, S extends Schema ? SchemaToProp<S> : any>76 this.env = env || ({} as Bindings)77 78 this.notFoundHandler = notFoundHandler79 this.finalized = false154 unmodified linesmodified:src/types.test.ts+10 added-2 removed
24 unmodified lines252627282930313233342 unmodified lines3738394039414243444546475 unmodified lines5354555657585960616255 unmodified lines11811912012111612212312412512612712820 unmodified lines24 unmodified lines const url = 'http://localhost/'
test('No arguments', async () => { const handler: Handler = (c) => { const data = c.req.valid() type verifySchema = Expect<Equal<typeof data['foo'], any>> return c.text('Hi') } app.get('/', handler) const res = await app.request(url)2 unmodified lines
test('Path', async () => { const handler: Handler<'id'> = (c) => { const id = c.req.param('id') type verify = Expect<Equal<typeof id, string>> type verifyPath = Expect<Equal<typeof id, string>> const data = c.req.valid() type verifySchema = Expect<Equal<typeof data['foo'], any>> return c.text('Hi') } app.get('/', handler) const res = await app.request(url)5 unmodified lines const id = c.req.param('id') type verifyPath = Expect<Equal<typeof id, string>> const foo = c.get('foo') type verifyEnv = Expect<Equal<typeof foo, string>> const data = c.req.valid() type verifySchema = Expect<Equal<typeof data['foo'], any>> return c.text('Hi') } app.get('/', handler) const res = await app.request(url)55 unmodified lines test('Complex', async () => { const app = new Hono<Env>() const handler: Handler<Env> = (c) => { const foo = c.get('foo') type verify = Expect<Equal<typeof foo, string>> type verifyEnv = Expect<Equal<typeof foo, string>> const data = c.req.valid() type verifySchema = Expect<Equal<typeof data['foo'], any>> return c.text(foo) } app.get( '/',20 unmodified linesmodified:src/types.ts+7 added-5 removed
50 unmodified lines51 52 // This is not used for internally53 // Will be used by users as `Handler`54 export interface CustomHandler<55- P extends string | Partial<Environment> | Partial<Schema> = string,55+ P extends string | Partial<Environment> | Schema = string,56 E = Partial<Environment> | Partial<Schema>,57 S = Partial<Schema>58 > {59 (9 unmodified lines69 ? P70 : P extends Partial<Schema>71 ? Partial<Environment>72 : E extends Partial<Environment>73- ? E73+ ? E extends Partial<Schema>74+ ? Environment75+ : E76 : E extends Partial<Schema>77 ? Partial<Environment>76- : never,78+ : Environment,79 P extends Partial<Schema>80 ? P81 : E extends Partial<Schema>82 ? P extends Partial<Environment>83 ? E extends Partial<Environment>82- ? never84+ ? unknown85 : E84- : never86+ : unknown87 : S extends Partial<Schema>88 ? S89 : never90 >,3 unmodified lines