2022-07-10 16:44:23 +02:00
|
|
|
import { HonoContext } from './context.ts'
|
2022-10-31 16:07:56 +01:00
|
|
|
import type { Environment, NotFoundHandler, ErrorHandler } from './types.ts'
|
2022-10-23 01:10:00 +02:00
|
|
|
import type { Schema } from './validator/schema.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-09-02 15:55:01 +02:00
|
|
|
interface ComposeContext {
|
|
|
|
finalized: boolean
|
2022-10-23 01:10:00 +02:00
|
|
|
res: unknown
|
2022-09-02 15:55:01 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
// Based on the code in the MIT licensed `koa-compose` package.
|
2022-10-23 01:10:00 +02:00
|
|
|
export const compose = <
|
|
|
|
C extends ComposeContext,
|
|
|
|
P extends string = string,
|
|
|
|
E extends Partial<Environment> = Environment,
|
|
|
|
D extends Partial<Schema> = Schema
|
|
|
|
>(
|
2022-09-02 15:55:01 +02:00
|
|
|
middleware: Function[],
|
2022-10-23 01:10:00 +02:00
|
|
|
onNotFound?: NotFoundHandler<P, E, D>,
|
|
|
|
onError?: ErrorHandler<P, E, D>
|
2022-09-02 15:55:01 +02:00
|
|
|
) => {
|
2022-08-09 03:51:08 +02:00
|
|
|
const middlewareLength = middleware.length
|
2022-07-17 02:13:44 +02:00
|
|
|
return (context: C, next?: Function) => {
|
2022-07-02 08:09:45 +02:00
|
|
|
let index = -1
|
|
|
|
return dispatch(0)
|
2022-08-15 01:35:57 +02:00
|
|
|
|
2022-09-02 15:55:01 +02:00
|
|
|
function dispatch(i: number): C | Promise<C> {
|
2022-07-02 08:09:45 +02:00
|
|
|
if (i <= index) {
|
2022-08-15 01:35:57 +02:00
|
|
|
throw new Error('next() called multiple times')
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
let handler = middleware[i]
|
|
|
|
index = i
|
2022-08-09 03:51:08 +02:00
|
|
|
if (i === middlewareLength && next) handler = next
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-09-02 15:55:01 +02:00
|
|
|
let res
|
2022-10-02 08:44:54 +02:00
|
|
|
let isError = false
|
2022-09-02 15:55:01 +02:00
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
if (!handler) {
|
2022-07-10 16:44:23 +02:00
|
|
|
if (context instanceof HonoContext && context.finalized === false && onNotFound) {
|
2022-09-02 15:55:01 +02:00
|
|
|
res = onNotFound(context)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
2022-09-02 15:55:01 +02:00
|
|
|
} else {
|
2022-10-02 08:44:54 +02:00
|
|
|
try {
|
|
|
|
res = handler(context, () => {
|
|
|
|
const dispatchRes = dispatch(i + 1)
|
|
|
|
return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes)
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
|
|
context.error = err
|
|
|
|
res = onError(err, context)
|
|
|
|
isError = true
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 15:55:01 +02:00
|
|
|
if (!(res instanceof Promise)) {
|
2022-10-02 08:44:54 +02:00
|
|
|
if (res && (context.finalized === false || isError)) {
|
2022-09-02 15:55:01 +02:00
|
|
|
context.res = res
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
} else {
|
2022-10-04 02:01:31 +02:00
|
|
|
return res
|
|
|
|
.then((res) => {
|
|
|
|
if (res && context.finalized === false) {
|
|
|
|
context.res = res
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (err instanceof Error && context instanceof HonoContext && onError) {
|
|
|
|
context.error = err
|
|
|
|
context.res = onError(err, context)
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
2022-08-15 01:35:57 +02:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|