refactor: compose (#121)
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 32686f4 · Browse files · 2 files·+33 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+33 added−24 removed
- tests:+12 added−2 removed
- other:+21 added−22 removed
Provenance
- Commit
6561c5e- Tree
42e58e4- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
32686f4
2 files changed+33 added−24 removed
modified:src/compose.test.ts+12 added-2 removed
11 unmodified lines1213141516161718192022212223242526272829303132333435363738394041424344454647484950515253545556575846 unmodified lines11 unmodified lines const a = async (c: C, next: Function) => { c.req['log'] = 'log' await next() } middleware.push(a)
const b = async (c: C, next: Function) => { await next() c.res['headers'] = 'custom-header' } middleware.push(b)
const c = async (c: C, next: Function) => { c.req['xxx'] = 'yyy' await next() c.res['zzz'] = c.req['xxx'] }
const handler = async (c: C, next: Function) => { c.req['log'] = `${c.req.log} message` await next() c.res = { message: 'new response' } }
middleware.push(a) middleware.push(b) middleware.push(c) middleware.push(handler)
it('Request', async () => { const c: C = { req: {}, res: {} } const composed = compose<C>(middleware) const context = await composed(c) expect(context.req['log']).not.toBeNull() expect(context.req['log']).toBe('log message') expect(context.req['xxx']).toBe('yyy') }) it('Response', async () => { const c: C = { req: {}, res: {} } const composed = compose<C>(middleware) const context = await composed(c) expect(context.res['headers']).not.toBeNull() expect(context.res['headers']).toBe('custom-header') expect(context.res['message']).toBe('new response') expect(context.res['zzz']).toBe('yyy') })})
describe('compose with Context', () => {46 unmodified linesmodified:src/compose.ts+21 added-22 removed
123456567891091011121314151617121314151617181920212223242519202122232425262726272829282930303132import { Context } from './context'import type { ErrorHandler } from './hono'
// Based on the code in the MIT licensed `koa-compose` package.export const compose = <T>(middleware: Function[], onError?: ErrorHandler) => { return function (context: T, next?: Function) {export const compose = <C>(middleware: Function[], onError?: ErrorHandler) => { return function (context: C) { let index = -1 return dispatch(0) async function dispatch(i: number): Promise<T> { if (i <= index) return Promise.reject(new Error('next() called multiple times')) async function dispatch(i: number): Promise<C> { if (i === middleware.length) { return context } if (i <= index) { return Promise.reject(new Error('next() called multiple times')) } const handler = middleware[i] index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return context try { return Promise.resolve(fn(context, dispatch.bind(null, i + 1))) .then(() => { return Promise.resolve(handler(context, dispatch.bind(null, i + 1))) .then(() => { return context }) .catch((err) => { if (onError && context instanceof Context) { context.res = onError(err, context) return context }) .catch((err) => { if (onError && context instanceof Context) { context.res = onError(err, context) return context } else { throw err } }) } else { throw err } }) } catch (err) { return Promise.reject(err) } } }}