-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
polymorphics.ts
78 lines (71 loc) · 1.52 KB
/
polymorphics.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
/**
* Event-like interface that's usable in browser and node.
*
* Note: Here we mean the kind of events handled by event listeners, not our `Event` type.
*
* Property availability taken from https://developer.mozilla.org/en-US/docs/Web/API/Event#browser_compatibility
*/
export interface PolymorphicEvent {
[key: string]: unknown;
readonly type: string;
readonly target?: unknown;
readonly currentTarget?: unknown;
}
/** A `Request` type compatible with Node, Express, browser, etc., because everything is optional */
export type PolymorphicRequest = BaseRequest &
BrowserRequest &
NodeRequest &
ExpressRequest &
KoaRequest &
NextjsRequest;
type BaseRequest = {
method?: string;
url?: string;
};
type BrowserRequest = BaseRequest;
type NodeRequest = BaseRequest & {
headers?: {
[key: string]: string | string[] | undefined;
};
protocol?: string;
socket?: {
encrypted?: boolean;
remoteAddress?: string;
};
};
type KoaRequest = NodeRequest & {
host?: string;
hostname?: string;
ip?: string;
originalUrl?: string;
};
type NextjsRequest = NodeRequest & {
cookies?: {
[key: string]: string;
};
query?: {
[key: string]: any;
};
};
type ExpressRequest = NodeRequest & {
baseUrl?: string;
body?: string | { [key: string]: any };
host?: string;
hostname?: string;
ip?: string;
originalUrl?: string;
route?: {
path: string;
stack: [
{
name: string;
},
];
};
query?: {
[key: string]: any;
};
user?: {
[key: string]: any;
};
};