2022-11-03 07:53:41 +01:00
|
|
|
import { Context } from './context.ts'
|
2023-01-16 14:57:47 +01:00
|
|
|
import type { Env, NotFoundHandler, ErrorHandler } from './types.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.
|
2023-01-16 14:57:47 +01:00
|
|
|
export const compose = <C extends ComposeContext, E extends Env = Env>(
|
2022-09-02 15:55:01 +02:00
|
|
|
middleware: Function[],
|
2023-02-09 11:49:27 +01:00
|
|
|
onError?: ErrorHandler<E>,
|
|
|
|
onNotFound?: NotFoundHandler<E>
|
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-11-03 07:53:41 +01:00
|
|
|
if (context instanceof Context && 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) {
|
2022-11-03 07:53:41 +01:00
|
|
|
if (err instanceof Error && context instanceof Context && onError) {
|
2022-10-02 08:44:54 +02:00
|
|
|
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-12-28 03:25:48 +01:00
|
|
|
if (res !== undefined && 'response' in res) {
|
|
|
|
res = res['response']
|
|
|
|
}
|
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) => {
|
2023-01-08 10:45:46 +01:00
|
|
|
if (res !== undefined && 'response' in res) {
|
|
|
|
res = res['response']
|
|
|
|
}
|
2022-10-04 02:01:31 +02:00
|
|
|
if (res && context.finalized === false) {
|
|
|
|
context.res = res
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2022-11-03 07:53:41 +01:00
|
|
|
if (err instanceof Error && context instanceof Context && onError) {
|
2022-10-04 02:01:31 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|