-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
129 lines (116 loc) · 3.63 KB
/
index.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
import type { DataFunctionArgs, AppData } from '@remix-run/server-runtime';
import type { Authenticator } from 'remix-auth';
type Next = () => any;
export interface Ctx extends DataFunctionArgs {
response: Promise<Response> | Response | Promise<AppData> | AppData;
}
export interface AuthCtx<U = any> extends Ctx {
user: U;
}
type Middleware<CurCtx extends Ctx = Ctx> = (ctx: CurCtx, next: Next) => any;
type MiddlewareCo<CurCtx extends Ctx = Ctx> =
| Middleware<CurCtx>
| Middleware<CurCtx>[];
export function compose<CurCtx extends Ctx = Ctx>(
middleware: Middleware<CurCtx>[],
) {
if (!Array.isArray(middleware)) {
throw new TypeError('Middleware stack must be an array!');
}
for (const fn of middleware) {
if (typeof fn !== 'function') {
throw new TypeError('Middleware must be composed of functions!');
}
}
return async function composer(context: CurCtx, next?: Next) {
// last called middleware #
let index = -1;
await dispatch(0);
async function dispatch(i: number) {
if (i <= index) {
throw new Error('next() called multiple times');
}
index = i;
let fn: any = middleware[i];
if (i === middleware.length) fn = next;
if (!fn) return;
await fn(context, dispatch.bind(null, i + 1));
}
};
}
async function defaultMiddleware<CurCtx extends Ctx = Ctx>(
_: CurCtx,
next: Next,
) {
await next();
}
const compileName = (request: Request): string =>
`${request.url} [${request.method}]`;
export function createMiddleware<CurCtx extends Ctx = Ctx>() {
const middleware: Middleware<CurCtx>[] = [];
const middlewareMap: { [key: string]: Middleware<CurCtx>[] } = {};
async function middlewareFn(
props: DataFunctionArgs,
md: MiddlewareCo<CurCtx>,
) {
const name = compileName(props.request);
if (Array.isArray(md)) {
middlewareMap[name] = md;
} else {
middlewareMap[name] = [md];
}
const ctx: Ctx = { ...props, response: {} };
const fn = compose(middleware);
await fn(ctx as any);
return ctx.response;
}
return {
use: (md: Middleware<CurCtx>) => {
middleware.push(md);
},
response: (resp: CurCtx['response']) => async (ctx: CurCtx, next: Next) => {
ctx.response = resp;
await next();
},
routes: () => async (ctx: CurCtx, next: Next) => {
const name = compileName(ctx.request);
const match = middlewareMap[name];
if (!match) {
await next();
return;
}
const md = compose(match);
await md(ctx, next);
},
run: (
props: DataFunctionArgs,
md: MiddlewareCo<CurCtx> = defaultMiddleware,
): Promise<Response> | Promise<AppData> => middlewareFn(props, md),
};
}
export function isAuthenticated<U>(
auth: Authenticator<U>,
options?: { successRedirect?: never; failureRedirect?: never },
): Middleware<AuthCtx<U | null>>;
export function isAuthenticated<U>(
auth: Authenticator<U>,
options: { successRedirect: string; failureRedirect?: never },
): Middleware<AuthCtx<null>>;
export function isAuthenticated<U>(
auth: Authenticator<U>,
options: { successRedirect?: never; failureRedirect: string },
): Middleware<AuthCtx<U>>;
export function isAuthenticated<U = any>(
auth: Authenticator<U>,
props:
| { successRedirect?: never; failureRedirect?: never }
| { successRedirect: string; failureRedirect?: never }
| { successRedirect?: never; failureRedirect: string } = {},
): Middleware<AuthCtx<U | null>> {
async function isAuthMdw(ctx: AuthCtx, next: Next) {
const user = await auth.isAuthenticated(ctx.request, props as any);
ctx.user = user;
await next();
}
return isAuthMdw;
}