-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestBuilder.ts
234 lines (190 loc) · 5.61 KB
/
requestBuilder.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {
HolMetadata,
HolMetadataKey,
HolRequest,
} from './model'
import { BodyEncoder } from './codec'
export type QueryParamPrimitiveValue = string | number | boolean | undefined | null
export type QueryParamValue = QueryParamPrimitiveValue | Array<QueryParamPrimitiveValue>
export type QueryParam = [string, QueryParamPrimitiveValue]
export type QueryParams = URLSearchParams | Array<QueryParam> | ReadonlyArray<QueryParam> | { [name: string]: QueryParamValue }
export interface UrlBuilder {
from(url: URL | string): void
protocol(protocol: string): void
host(host: string): void
port(port: number): void
path(path: string): void
addQueryParam(name: string, value: string): void
addQueryParams(params: QueryParams): void
fragment(fragment: string | undefined): void
}
class SimpleUrlBuilder implements UrlBuilder {
private url: URL
private hasHost: boolean = false
constructor(baseUrl?: URL | string) {
if (baseUrl) {
this.url = new URL(baseUrl)
this.hasHost = true
} else {
this.url = new URL('https://localhost')
}
}
from(url: URL | string) {
this.url = new URL(url)
this.hasHost = true
}
protocol(protocol: string) {
if (protocol.endsWith(':')) {
this.url.protocol = protocol
} else {
this.url.protocol = `${protocol}:`
}
}
host(host: string) {
this.url.host = host
this.hasHost = true
}
port(port: number | undefined) {
if (typeof port === 'number') {
this.url.port = `${port}`
} else {
this.url.port = ''
}
}
path(path: string) {
this.url.pathname = path
}
addQueryParam(name: string, value: string) {
this.url.searchParams.append(name, value)
}
addQueryParams(params: QueryParams): void {
if (params instanceof URLSearchParams) {
for (const [name, value] of params.entries()) {
this.addQueryParam(name, value)
}
} else if (Array.isArray(params)) {
for (const [name, value] of params) {
this.addQueryParam(name, `${value}`)
}
} else {
for (const [name, value] of Object.entries(params)) {
if (value === null || value === undefined) {
continue
}
if (Array.isArray(value)) {
for (const entry of value) {
if (entry === null || entry === undefined) {
continue
}
this.addQueryParam(name, `${entry}`)
}
} else {
this.addQueryParam(name, `${value}`);
}
}
}
}
fragment(fragment: string | undefined) {
if (!fragment) {
this.url.hash = ''
} else {
this.url.hash = `#${fragment}`
}
}
build(): URL {
if (!this.hasHost) {
throw new Error("No host has been configured!")
}
return this.url
}
}
export interface RequestBuilder {
buildUrl(build: (builder: UrlBuilder) => void): void
buildUrlFrom(from: URL | string, build?: (builder: UrlBuilder) => void): void
method(method: string): void
addHeader(name: string, value: string): void
addMetadata<T>(key: HolMetadataKey<T>, value: T): void
body(encoder: BodyEncoder): void
rawBody(body: BodyInit): void
abortOn(signal: AbortSignal): void
requestInit(init: RequestInit): void
}
function mergeHeaders(init: HeadersInit | undefined, additionalHeaders: Headers): Headers {
const headers = new Headers(init)
for (let [name, value] of additionalHeaders) {
headers.append(name, value);
}
return headers
}
function linkAbortSignals(a: AbortSignal | undefined | null, b: AbortSignal | undefined | null): AbortSignal | null {
if (a && b) {
const combined = new AbortController()
a.addEventListener('abort', () => combined.abort(a.reason))
b.addEventListener('abort', () => combined.abort(b.reason))
return combined.signal
} else if (a) {
return a
} else if (b) {
return b
} else {
return null
}
}
export class SimpleRequestBuilder implements RequestBuilder {
private urlBuilder: SimpleUrlBuilder | undefined = undefined
private methodValue: string | undefined = undefined
private init: RequestInit | undefined = undefined
private headers = new Headers()
private metadata = new HolMetadata()
private bodyValue: BodyInit | null = null
private abortSignal: AbortSignal | undefined = undefined
buildUrl(build: (builder: UrlBuilder) => void) {
this.urlBuilder = new SimpleUrlBuilder()
build(this.urlBuilder)
}
buildUrlFrom(from: URL | string, build?: (builder: UrlBuilder) => void) {
this.urlBuilder = new SimpleUrlBuilder(from)
if (build) {
build(this.urlBuilder)
}
}
method(method: string) {
this.methodValue = method
}
addHeader(name: string, value: string) {
this.headers.append(name, value)
}
addMetadata<T>(key: HolMetadataKey<T>, value: T) {
this.metadata.put(key, value)
}
body(body: BodyEncoder) {
body(this)
}
rawBody(body: BodyInit) {
this.bodyValue = body
}
requestInit(init: RequestInit) {
this.init = init
}
abortOn(signal: AbortSignal) {
this.abortSignal = signal;
}
build(): HolRequest {
if (!this.urlBuilder) {
throw new Error("No URL has been configured!")
}
let mergedInit = {
...this.init,
method: this.methodValue,
headers: mergeHeaders(this.init?.headers, this.headers),
signal: linkAbortSignals(this.init?.signal, this.abortSignal),
body: this.bodyValue,
}
return new HolRequest(this.urlBuilder.build(), mergedInit, this.metadata)
}
}
export function buildRequest(block: (builder: RequestBuilder) => void): HolRequest {
const builder = new SimpleRequestBuilder()
block(builder)
return builder.build()
}