-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-request.ts
160 lines (133 loc) · 3.33 KB
/
http-request.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { HttpMethod } from '../enum';
import { HttpContext, HttpContextToken } from './http-context';
export interface HttpRequestProps<HttpRequestBodyT> {
baseUrl: string;
body?: HttpRequestBodyT;
credentials?: RequestCredentials;
headers?: HeadersInit;
maxAge?: number;
method: HttpMethod;
queryParams?: Record<string, string>;
relativeUrl: string;
signal?: AbortSignal;
context?: HttpContext;
}
export class HttpRequest<HttpRequestBodyT> implements HttpRequestProps<HttpRequestBodyT> {
/**
* The base url of the remote call. The subpath is
* relative to the base url.
*/
private _baseUrl: string;
/**
* The relative url of the request to be concatenated
* to the base url.
*/
private _relativeUrl: string;
/**
* The request body.
*/
private _body?: HttpRequestBodyT;
/**
* The request credentials.
*/
private _credentials?: RequestCredentials;
/**
* The request headers.
*/
private _headers?: HeadersInit;
/**
* The request max age (in seconds).
*/
private _maxAge: number;
/**
* The request http method.
*/
private _method: HttpMethod;
/**
* The request query params.
*/
private _queryParams?: Record<string, string>;
/**
* The request abort signal.
*/
private _signal?: AbortSignal;
/**
* The request context storing arbitrary user defined data.
*/
private _context?: HttpContext;
constructor(requestOpts: HttpRequestProps<HttpRequestBodyT>) {
const {
baseUrl,
body,
credentials,
headers,
maxAge,
method,
queryParams,
relativeUrl,
signal,
context,
} = requestOpts;
this._baseUrl = baseUrl;
this._body = body || undefined;
this._credentials = credentials;
this._headers = headers;
this._maxAge = maxAge || 0;
this._method = method;
this._queryParams = queryParams;
this._relativeUrl = relativeUrl;
this._signal = signal;
this._context = context;
}
get baseUrl(): string {
return this._baseUrl;
}
get body(): HttpRequestBodyT | undefined {
return this._body;
}
get credentials(): RequestCredentials | undefined {
return this._credentials;
}
get headers(): HeadersInit | undefined {
return this._headers;
}
get maxAge(): number | undefined {
return Number.isInteger(this._maxAge) ? this._maxAge : undefined;
}
get method(): HttpMethod {
return this._method;
}
get queryParams(): Record<string, string> | undefined {
return this._queryParams;
}
get relativeUrl(): string {
return this._relativeUrl;
}
get signal(): AbortSignal | undefined {
return this._signal;
}
get url(): string {
return `${this.baseUrl}/${this.relativeUrl}`;
}
get serializedQueryParams(): string {
if (!this.queryParams) {
return '';
}
const urlSearchParams = new URLSearchParams(this.queryParams);
return urlSearchParams.toString();
}
get urlWithParams(): string {
if (!this.serializedQueryParams) {
return this.url;
}
return `${this.url}?${this.serializedQueryParams}`;
}
get context(): HttpContext | undefined {
return this._context;
}
getContextValue<ContextTokenValueT>(
token: HttpContextToken<ContextTokenValueT>
): ContextTokenValueT | undefined {
return this.context ? this.context.get(token) : undefined;
}
}