Skip to content
Myr

feat: check response type (#94)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: check response type * refactor * refactor: add types

authored by Yusuke Wada · committed by GitHub · parent a72660b · Browse files · 9 files+33 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+33 added−18 removed

  • tests:+10 added−2 removed
  • other:+23 added−16 removed

9 files+33 added−18 removed

modified:examples/basic/index.js+1 added
modified:src/middleware/mustache/mustache.ts+7 added-3 removed
modified:src/utils/buffer.ts+2 added-2 removed
38 unmodified lines
39 throw e40 }41}4243export const sha256 = async (a: string): Promise<string> => {43export const sha256 = async (a: string | object | boolean): Promise<string> => {44 if (crypto && crypto.subtle) {45 const buffer = await crypto.subtle.digest(46 {47 name: 'SHA-256',
13 unmodified lines
61 throw e62 }63}6465export const timingSafeEqual = async (a: any, b: any) => {65export const timingSafeEqual = async (a: string | object | boolean, b: string | object | boolean) => {66 const sa = await sha256(a)67 const sb = await sha256(b)68 return sa === sb && a === b69}
modified:src/utils/cloudflare.ts-2 removed
1import { getMimeType } from './mime'21declare const __STATIC_CONTENT: KVNamespace, __STATIC_CONTENT_MANIFEST: string23export const getContentFromKVAsset = async (path: string): Promise<ArrayBuffer> => {4 let ASSET_MANIFEST: { [key: string]: string }
52 unmodified lines
modified:src/compose.ts+3 added-3 removed
modified:src/hono.ts+9 added-5 removed
151 unmodified lines
152 }153 }154155 const wrappedHandler = async (context: Context, next: Function) => {156 context.res = await handler(context)156 const res = await handler(context)157 if (!(res instanceof Response)) {158 throw new TypeError('response must be a instace of Response')159 }160 context.res = res161 await next()162 }163164 middleware.push(wrappedHandler)165162 const composed = compose(middleware)166 const composed = compose<Context>(middleware)167 const c = new Context(request, { env: env, event: event, res: null })168 await composed(c)169170 return c.res171 }172173 async handleEvent(event: FetchEvent): Promise<Response> {170 return this.dispatch(event.request, {}, event).catch((err) => {174 return this.dispatch(event.request, {}, event).catch((err: Error) => {175 return this.onError(err)176 })177 }178179 async fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {176 return this.dispatch(request, env, event).catch((err) => {180 return this.dispatch(request, env, event).catch((err: Error) => {181 return this.onError(err)182 })183 }184
2 unmodified lines
187 event.respondWith(this.handleEvent(event))188 })189 }190187 onError(err: any) {191 onError(err: Error) {192 console.error(`${err}`)193 const message = 'Internal Server Error'194 return new Response(message, {195 status: 500,
16 unmodified lines
modified:src/node.ts+1 added-1 removed
19 unmodified lines
20 handler: T21 children: { [key: string]: Node<T> }22 middlewares: []2324 constructor(method?: string, handler?: any, children?: { [key: string]: Node<T> }) {24 constructor(method?: string, handler?: T, children?: { [key: string]: Node<T> }) {25 this.children = children || {}26 this.method = {}27 if (method && handler) {28 this.method[method] = handler
98 unmodified lines
modified:test/compose.test.ts+1 added-1 removed
30 unmodified lines
31 const response = {}3233 it('Request', async () => {34 const c: C = { req: request, res: response }35 const composed = compose(middleware)35 const composed = compose<C>(middleware)36 await composed(c)37 expect(c.req['log']).not.toBeNull()38 expect(c.req['log']).toBe('log message')39 })
8 unmodified lines
modified:test/hono.test.ts+9 added-1 removed
255 unmodified lines
256 app.get('/error', () => {257 throw 'This is Error'258 })259260 app.use('*', async (c, next) => {260 app.use('/error', async (c, next) => {261 try {262 await next()263 } catch (err) {264 c.res = new Response('Custom Error Message', { status: 500 })
7 unmodified lines
272 expect(res.status).toBe(500)273 expect(await res.text()).toBe('Custom Error Message')274 expect(res.headers.get('debug')).toBe('This is Error')275 })276277 // eslint-disable-next-line @typescript-eslint/ban-ts-comment278 // @ts-ignore279 app.get('/text', () => 'text')280 it('If return not Reponse object', async () => {281 const req = new Request('https://example.com/text')282 expect(app.dispatch(req)).rejects.toThrowError(TypeError)283 })284})285286describe('Request methods with custom middleware', () => {287 const app = new Hono()
38 unmodified lines