fix(types): correct types for `app.notFound` / `app.onError` (#512)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #511
authored by Yusuke Wada · committed by GitHub · parent 5a642ed · Browse files · 6 files·+26 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+26 added−26 removed
- other:+26 added−26 removed
Provenance
- Commit
058e311- Tree
604981e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
5a642ed
6 files changed+26 added−26 removed
modified:deno_dist/compose.ts+3 added-3 removed
122345678910101112121314151639 unmodified linesimport { HonoContext } from './context.ts'import type { NotFoundHandler } from './hono.ts'import type { Environment, NotFoundHandler } from './hono.ts'
interface ComposeContext { finalized: boolean res: any}
// Based on the code in the MIT licensed `koa-compose` package.export const compose = <C extends ComposeContext>(export const compose = <C extends ComposeContext, E extends Partial<Environment> = Environment>( middleware: Function[], onNotFound?: NotFoundHandler onNotFound?: NotFoundHandler<E>) => { const middlewareLength = middleware.length return (context: C, next?: Function) => { let index = -139 unmodified linesmodified:deno_dist/context.ts+2 added-2 removed
57 unmodified lines58 private _prettySpace: number = 259 private _map: Record<string, any> | undefined60 private _headers: Record<string, string> | undefined61 private _res: Response | undefined62- private notFoundHandler: NotFoundHandler62+ private notFoundHandler: NotFoundHandler<E>63 64 constructor(65 req: Request,66 env: E['Bindings'] | undefined = undefined,67 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,68- notFoundHandler: NotFoundHandler = () => new Response()68+ notFoundHandler: NotFoundHandler<E> = () => new Response()69 ) {70 this._executionCtx = executionCtx71 this.req = req72 this.env = env || ({} as Bindings)120 unmodified linesmodified:deno_dist/hono.ts+8 added-8 removed
122 unmodified lines123 124 Object.assign(this, init)125 }126 127- private notFoundHandler: NotFoundHandler = (c: Context) => {127+ private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {128 const message = defaultNotFoundMessage129 return c.text(message, 404)130 }131 132- private errorHandler: ErrorHandler = (err: Error, c: Context) => {132+ private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {133 console.error(`${err.stack || err.message}`)134 const message = 'Internal Server Error'135 return c.text(message, 500)136 }28 unmodified lines165 })166 return this167 }168 169- onError(handler: ErrorHandler): Hono<E, P> {169+ onError(handler: ErrorHandler<E>): Hono<E, P> {170 this.errorHandler = handler171 return this172 }173 174- notFound(handler: NotFoundHandler): Hono<E, P> {174+ notFound(handler: NotFoundHandler<E>): Hono<E, P> {175 this.notFoundHandler = handler176 return this177 }178 32 unmodified lines211 if (res) {212 const awaited = res instanceof Promise ? await res : res213 if (awaited) return awaited214 }215- return this.notFoundHandler(c as Context)215+ return this.notFoundHandler(c)216 } catch (err) {217 if (err instanceof Error) {218- return this.errorHandler(err, c as Context)218+ return this.errorHandler(err, c)219 }220 throw err221 }222 }223 224 const handlers = result ? result.handlers : [this.notFoundHandler]225 226- const composed = compose<HonoContext<string, E>>(handlers, this.notFoundHandler)226+ const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)227 let context: HonoContext<string, E>228 try {229 const tmp = composed(c)230 context = tmp instanceof Promise ? await tmp : tmp3 unmodified lines234 )235 }236 } catch (err) {237 if (err instanceof Error) {238- return this.errorHandler(err, c as Context)238+ return this.errorHandler(err, c)239 }240 throw err241 }242 16 unmodified linesmodified:src/compose.ts+3 added-3 removed
122345678910101112121314151639 unmodified linesimport { HonoContext } from './context'import type { NotFoundHandler } from './hono'import type { Environment, NotFoundHandler } from './hono'
interface ComposeContext { finalized: boolean res: any}
// Based on the code in the MIT licensed `koa-compose` package.export const compose = <C extends ComposeContext>(export const compose = <C extends ComposeContext, E extends Partial<Environment> = Environment>( middleware: Function[], onNotFound?: NotFoundHandler onNotFound?: NotFoundHandler<E>) => { const middlewareLength = middleware.length return (context: C, next?: Function) => { let index = -139 unmodified linesmodified:src/context.ts+2 added-2 removed
57 unmodified lines58 private _prettySpace: number = 259 private _map: Record<string, any> | undefined60 private _headers: Record<string, string> | undefined61 private _res: Response | undefined62- private notFoundHandler: NotFoundHandler62+ private notFoundHandler: NotFoundHandler<E>63 64 constructor(65 req: Request,66 env: E['Bindings'] | undefined = undefined,67 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,68- notFoundHandler: NotFoundHandler = () => new Response()68+ notFoundHandler: NotFoundHandler<E> = () => new Response()69 ) {70 this._executionCtx = executionCtx71 this.req = req72 this.env = env || ({} as Bindings)120 unmodified linesmodified:src/hono.ts+8 added-8 removed
122 unmodified lines123 124 Object.assign(this, init)125 }126 127- private notFoundHandler: NotFoundHandler = (c: Context) => {127+ private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {128 const message = defaultNotFoundMessage129 return c.text(message, 404)130 }131 132- private errorHandler: ErrorHandler = (err: Error, c: Context) => {132+ private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {133 console.error(`${err.stack || err.message}`)134 const message = 'Internal Server Error'135 return c.text(message, 500)136 }28 unmodified lines165 })166 return this167 }168 169- onError(handler: ErrorHandler): Hono<E, P> {169+ onError(handler: ErrorHandler<E>): Hono<E, P> {170 this.errorHandler = handler171 return this172 }173 174- notFound(handler: NotFoundHandler): Hono<E, P> {174+ notFound(handler: NotFoundHandler<E>): Hono<E, P> {175 this.notFoundHandler = handler176 return this177 }178 32 unmodified lines211 if (res) {212 const awaited = res instanceof Promise ? await res : res213 if (awaited) return awaited214 }215- return this.notFoundHandler(c as Context)215+ return this.notFoundHandler(c)216 } catch (err) {217 if (err instanceof Error) {218- return this.errorHandler(err, c as Context)218+ return this.errorHandler(err, c)219 }220 throw err221 }222 }223 224 const handlers = result ? result.handlers : [this.notFoundHandler]225 226- const composed = compose<HonoContext<string, E>>(handlers, this.notFoundHandler)226+ const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)227 let context: HonoContext<string, E>228 try {229 const tmp = composed(c)230 context = tmp instanceof Promise ? await tmp : tmp3 unmodified lines234 )235 }236 } catch (err) {237 if (err instanceof Error) {238- return this.errorHandler(err, c as Context)238+ return this.errorHandler(err, c)239 }240 throw err241 }242 16 unmodified lines