generated from T99/ts-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
68 lines (56 loc) · 2.53 KB
/
test.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
/*
* Created by Trevor Sears <[email protected]> (https://trevorsears.com/).
* 11:37 AM -- November 2nd, 2021
* Project: harbour
*/
import { IncomingHTTPRequest } from "../messages/incoming-http-request";
import { OutgoingHTTPResponse } from "../messages/outgoing-http-response";
/**
* The form of a function that can be used as a Middleware with a {@link MiddlewareExecutor}.
*/
export type MiddlewareFunction<T = void> = (request: IncomingHTTPRequest, response: OutgoingHTTPResponse) => T | Promise<T>;
/**
* A more generic definition of 'Middleware' that can accept either a more formal object instance of the
* {@link Middleware} class, or the more functional definition using a callback/anonymous function.
*/
export type Middlewareable = Middleware | MiddlewareFunction;
/**
* A functional interface representative of a function that should be executed as a part of the request-handling process
* by the server.
*
* @author Trevor Sears <[email protected]> (https://trevorsears.com/)
* @version v0.1.0
* @since v0.1.0
*/
export abstract class Middleware {
/**
* Executes the functionality enclosed by this Middleware, returning a Promise that resolves once this Middleware
* function has completed.
*
* @param {HTTPRequest} request An object representative of the request as made by the client.
* @param {HTTPResponse} response An object representative of the response being prepared for the client.
* @returns {Promise<void>} A Promise that resolves once the middleware function has completed.
*/
public abstract execute(request: IncomingHTTPRequest, response: OutgoingHTTPResponse): void | Promise<void>;
/**
* Creates and returns a Middleware instance from a functional middleware declaration.
*
* @param {MiddlewareFunction} func The middleware function.
* @return {Middleware} Middleware A Middleware instance functionally identical to the provided middleware function.
*/
public static fromFunction(func: MiddlewareFunction): Middleware {
return new class extends Middleware {
public execute: MiddlewareFunction = func;
}
}
/**
* Normalizes a 'middlewareable' to Middleware instances.
*
* @param {Middlewareable} middlewareable The 'middlewareable' to normalize.
* @return {Middleware} A Middleware instance functionally identical to the provided 'middlewareable'.
*/
public static normalizeMiddlewareable(middlewareable: Middlewareable): Middleware {
if (!(middlewareable instanceof Middleware)) return Middleware.fromFunction(middlewareable);
else return middlewareable;
}
}