-
Notifications
You must be signed in to change notification settings - Fork 235
/
middleware.ts
95 lines (84 loc) · 2.92 KB
/
middleware.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
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
/**
* The internal middleware interfaces and abstractions used by oak.
*
* Outside of the {@linkcode Middleware} interface, items are not generally
* used by end users, but {@linkcode compose} can be used for advanced use
* cases.
*
* @module
*/
// deno-lint-ignore-file
import type { State } from "./application.ts";
import type { Context } from "./context.ts";
/** A function for chaining middleware. */
export type Next = () => Promise<unknown>;
/** Middleware are functions which are chained together to deal with
* requests. */
export interface Middleware<
S extends State = Record<string, any>,
T extends Context = Context<S>,
> {
(context: T, next: Next): Promise<unknown> | unknown;
}
/** Middleware objects allow encapsulation of middleware along with the ability
* to initialize the middleware upon listen. */
export interface MiddlewareObject<
S extends State = Record<string, any>,
T extends Context<S> = Context<S>,
> {
/** Optional function for delayed initialization which will be called when
* the application starts listening. */
init?: () => Promise<unknown> | unknown;
/** The method to be called to handle the request. */
handleRequest(context: T, next: Next): Promise<unknown> | unknown;
}
/** Type that represents {@linkcode Middleware} or
* {@linkcode MiddlewareObject}. */
export type MiddlewareOrMiddlewareObject<
S extends State = Record<string, any>,
T extends Context = Context<S>,
> = Middleware<S, T> | MiddlewareObject<S, T>;
/** A type guard that returns true if the value is
* {@linkcode MiddlewareObject}. */
export function isMiddlewareObject<
S extends State = Record<string, any>,
T extends Context = Context<S>,
>(value: MiddlewareOrMiddlewareObject<S, T>): value is MiddlewareObject<S, T> {
return value && typeof value === "object" && "handleRequest" in value;
}
/** Compose multiple middleware functions into a single middleware function. */
export function compose<
S extends State = Record<string, any>,
T extends Context = Context<S>,
>(
middleware: MiddlewareOrMiddlewareObject<S, T>[],
): (context: T, next?: Next) => Promise<unknown> {
return function composedMiddleware(
context: T,
next?: Next,
): Promise<unknown> {
let index = -1;
async function dispatch(i: number): Promise<void> {
if (i <= index) {
throw new Error("next() called multiple times.");
}
index = i;
let m: MiddlewareOrMiddlewareObject<S, T> | undefined = middleware[i];
let fn: Middleware<S, T> | undefined;
if (typeof m === "function") {
fn = m;
} else if (m && typeof m.handleRequest === "function") {
fn = (m as MiddlewareObject).handleRequest.bind(m);
}
if (i === middleware.length) {
fn = next;
}
if (!fn) {
return;
}
await fn(context, dispatch.bind(null, i + 1));
}
return dispatch(0);
};
}