2022-11-03 06:53:41 +00:00
|
|
|
import { Context } from './context.ts'
|
2023-01-16 13:57:47 +00:00
|
|
|
import type { Env, NotFoundHandler, ErrorHandler } from './types.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-02 13:55:01 +00:00
|
|
|
interface ComposeContext {
|
|
|
|
finalized: boolean
|
2022-10-22 23:10:00 +00:00
|
|
|
res: unknown
|
2022-09-02 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
// Based on the code in the MIT licensed `koa-compose` package.
|
2023-01-16 13:57:47 +00:00
|
|
|
export const compose = <C extends ComposeContext, E extends Env = Env>(
|
2022-09-02 13:55:01 +00:00
|
|
|
middleware: Function[],
|
2023-02-09 10:49:27 +00:00
|
|
|
onError?: ErrorHandler<E>,
|
|
|
|
onNotFound?: NotFoundHandler<E>
|
2022-09-02 13:55:01 +00:00
|
|
|
) => {
|
2022-08-09 01:51:08 +00:00
|
|
|
const middlewareLength = middleware.length
|
2022-07-17 00:13:44 +00:00
|
|
|
return (context: C, next?: Function) => {
|
2022-07-02 06:09:45 +00:00
|
|
|
let index = -1
|
|
|
|
return dispatch(0)
|
2022-08-14 23:35:57 +00:00
|
|
|
|
2022-09-02 13:55:01 +00:00
|
|
|
function dispatch(i: number): C | Promise<C> {
|
2022-07-02 06:09:45 +00:00
|
|
|
if (i <= index) {
|
2022-08-14 23:35:57 +00:00
|
|
|
throw new Error('next() called multiple times')
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
let handler = middleware[i]
|
|
|
|
index = i
|
2022-08-09 01:51:08 +00:00
|
|
|
if (i === middlewareLength && next) handler = next
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-02 13:55:01 +00:00
|
|
|
let res
|
2022-10-02 06:44:54 +00:00
|
|
|
let isError = false
|
2022-09-02 13:55:01 +00:00
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
if (!handler) {
|
2022-11-03 06:53:41 +00:00
|
|
|
if (context instanceof Context && context.finalized === false && onNotFound) {
|
2022-09-02 13:55:01 +00:00
|
|
|
res = onNotFound(context)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-09-02 13:55:01 +00:00
|
|
|
} else {
|
2022-10-02 06:44:54 +00:00
|
|
|
try {
|
|
|
|
res = handler(context, () => {
|
|
|
|
const dispatchRes = dispatch(i + 1)
|
|
|
|
return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes)
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2022-11-03 06:53:41 +00:00
|
|
|
if (err instanceof Error && context instanceof Context && onError) {
|
2022-10-02 06:44:54 +00:00
|
|
|
context.error = err
|
|
|
|
res = onError(err, context)
|
|
|
|
isError = true
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 13:55:01 +00:00
|
|
|
if (!(res instanceof Promise)) {
|
2022-12-28 02:25:48 +00:00
|
|
|
if (res !== undefined && 'response' in res) {
|
|
|
|
res = res['response']
|
|
|
|
}
|
2022-10-02 06:44:54 +00:00
|
|
|
if (res && (context.finalized === false || isError)) {
|
2022-09-02 13:55:01 +00:00
|
|
|
context.res = res
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
} else {
|
2022-10-04 00:01:31 +00:00
|
|
|
return res
|
|
|
|
.then((res) => {
|
2023-01-08 09:45:46 +00:00
|
|
|
if (res !== undefined && 'response' in res) {
|
|
|
|
res = res['response']
|
|
|
|
}
|
2022-10-04 00:01:31 +00:00
|
|
|
if (res && context.finalized === false) {
|
|
|
|
context.res = res
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
})
|
2023-05-13 23:16:28 +00:00
|
|
|
.catch(async (err) => {
|
2022-11-03 06:53:41 +00:00
|
|
|
if (err instanceof Error && context instanceof Context && onError) {
|
2022-10-04 00:01:31 +00:00
|
|
|
context.error = err
|
2023-05-13 23:16:28 +00:00
|
|
|
context.res = await onError(err, context)
|
2022-10-04 00:01:31 +00:00
|
|
|
return context
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
2022-08-14 23:35:57 +00:00
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|