-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompose.ts
141 lines (129 loc) · 3.03 KB
/
compose.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Middleware as BaseMiddleware } from "./types.d.ts";
export function compose<T1, U1, T2, U2>(
middleware: [BaseMiddleware<T1, U1>, BaseMiddleware<T2, U2>],
): BaseMiddleware<T1 & T2, U1 & U2>;
export function compose<T1, U1, T2, U2, T3, U3>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
],
): BaseMiddleware<T1 & T2 & T3, U1 & U2 & U3>;
export function compose<T1, U1, T2, U2, T3, U3, T4, U4>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
BaseMiddleware<T4, U4>,
],
): BaseMiddleware<T1 & T2 & T3 & T4, U1 & U2 & U3 & U4>;
export function compose<T1, U1, T2, U2, T3, U3, T4, U4, T5, U5>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
BaseMiddleware<T4, U4>,
BaseMiddleware<T5, U5>,
],
): BaseMiddleware<T1 & T2 & T3 & T4 & T5, U1 & U2 & U3 & U4 & U5>;
export function compose<T1, U1, T2, U2, T3, U3, T4, U4, T5, U5, T6, U6>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
BaseMiddleware<T4, U4>,
BaseMiddleware<T5, U5>,
BaseMiddleware<T6, U6>,
],
): BaseMiddleware<T1 & T2 & T3 & T4 & T5 & T6, U1 & U2 & U3 & U4 & U5 & U6>;
export function compose<
T1,
U1,
T2,
U2,
T3,
U3,
T4,
U4,
T5,
U5,
T6,
U6,
T7,
U7,
>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
BaseMiddleware<T4, U4>,
BaseMiddleware<T5, U5>,
BaseMiddleware<T6, U6>,
BaseMiddleware<T7, U7>,
],
): BaseMiddleware<
T1 & T2 & T3 & T4 & T5 & T6 & T7,
U1 & U2 & U3 & U4 & U5 & U6 & U7
>;
export function compose<
T1,
U1,
T2,
U2,
T3,
U3,
T4,
U4,
T5,
U5,
T6,
U6,
T7,
U7,
T8,
U8,
>(
middleware: [
BaseMiddleware<T1, U1>,
BaseMiddleware<T2, U2>,
BaseMiddleware<T3, U3>,
BaseMiddleware<T4, U4>,
BaseMiddleware<T5, U5>,
BaseMiddleware<T6, U6>,
BaseMiddleware<T7, U7>,
BaseMiddleware<T8, U8>,
],
): BaseMiddleware<
T1 & T2 & T3 & T4 & T5 & T6 & T7 & T8,
U1 & U2 & U3 & U4 & U5 & U6 & U7 & U8
>;
export function compose<T>(
middleware: Array<Middleware<T>>,
): ComposedMiddleware<T>;
export function compose(middleware: Function[]) {
return function (context: any, next?: Next) {
// last called middleware #
let index = -1;
function dispatch(i: number): Promise<any> {
if (i <= index) {
return Promise.reject(new Error("next() called multiple times"));
}
index = i;
let fn = middleware[i];
if (i === middleware.length && next) fn = next;
if (!fn) return Promise.resolve();
try {
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
} catch (err) {
return Promise.reject(err);
}
}
return dispatch(0);
};
}
export type Next = () => Promise<any>;
export type Middleware<T> = (context: T, next: Next) => any;
export type ComposedMiddleware<T> = (
context: T,
next?: Next,
) => Promise<void>;