-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
75 lines (66 loc) · 1.92 KB
/
mod.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
import { createErreurStore, type TErreurStore } from "@dldc/erreur";
// I: Input
// O: Output
// T: transformed value (returned by next())
export type INext<I, T> = (input: I) => T;
export type IMiddleware<I, O, T extends O = O> = (
input: I,
next: INext<I, T>,
) => O;
export type IMiddlewares<I, O, T extends O = O> = Array<IMiddleware<I, O, T>>;
export function composeAdvanced<I, O, T extends O = O>(
transform: (output: O) => T,
middlewares: Array<IMiddleware<I, O, T> | null>,
): IMiddleware<I, O, T> {
const resolved = middlewares.filter((v): v is IMiddleware<I, O, T> =>
v !== null
);
resolved.forEach((middle, index) => {
if (typeof middle !== "function") {
throw createInvalidMiddlewareError(
middle,
`Not a function at index ${index}`,
);
}
});
return function (ctx, next): O {
return dispatch(0, ctx);
function dispatch(index: number, input: I): O {
const middle = resolved[index];
if (!middle) {
return next(input);
}
return middle(input, (ctx) => transform(dispatch(index + 1, ctx)));
}
};
}
export function compose<I, O>(
...middlewares: Array<IMiddleware<I, O, O> | null>
): IMiddleware<I, O, O> {
return composeAdvanced((v) => v, middlewares);
}
export type TInvalidMiddlewareErreurData = {
middleware: unknown;
infos: string;
};
const InvalidMiddlewareErreurInternal: TErreurStore<
TInvalidMiddlewareErreurData
> = createErreurStore<
TInvalidMiddlewareErreurData
>();
/**
* Use this to detect when an error comes from the compose function
*
* ```ts
* InvalidMiddlewareErreur.has(err)
* InvalidMiddlewareErreur.get(err)
* ```
*/
export const InvalidMiddlewareErreur =
InvalidMiddlewareErreurInternal.asReadonly;
function createInvalidMiddlewareError(middleware: unknown, infos: string) {
return InvalidMiddlewareErreurInternal.setAndThrow(
`Invalid middleware: ${infos}`,
{ middleware, infos },
);
}