-
Notifications
You must be signed in to change notification settings - Fork 23
/
request.options.ts
469 lines (391 loc) · 12.3 KB
/
request.options.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import { Filter } from './filter.options';
export interface RequestOptions {
/**
* Provides the specified URL options.
*
* @returns The string to attach to the URL
*/
getOptions(): Map<string, string>;
/**
* Provides the specified header options.
*
* @returns The headers
*/
getHeaders(): Map<string, string>;
}
export interface AddRequestOptions<T extends AddRequestOptions<T>> extends RequestOptions {
/**
* Adds a URl option.
*
* @param id - The ID of the option to add.
* @param value - The body of the option to add.
*/
addRequestParameter(id: string, value: string): T;
/**
* Adds a header option.
*
* @param name - The name of the header to add.
* @param value - The body of the header to add.
*/
addHeader(name: string, value: string): T;
}
/**
* Option provider for options. Options will be replaced if methods are called multiple times.
*/
export abstract class AbstractRequestOptions<T extends AbstractRequestOptions<T>> implements AddRequestOptions<T> {
private readonly options: Map<string, string>;
private readonly headers: Map<string, string>;
protected constructor() {
this.options = new Map<string, string>();
this.headers = new Map<string, string>();
}
getOptions(): Map<string, string> {
return new Map(this.options);
}
getHeaders(): Map<string, string> {
return new Map(this.headers);
}
addRequestParameter(id: string, value: string): T {
this.options.set(id, value);
return this as unknown as T;
}
addHeader(name: string, value: string): T {
this.headers.set(name, value);
return this as unknown as T;
}
}
export interface HasMatch<T extends HasMatch<T>> {
/**
* Sets an If-Match option.
*
* @param tag - The tags to match.
* @returns This Options instance with the added option
*/
ifMatch(...tag: string[]): T;
/**
* Shortcut for If-Match: *
* @returns This Options instance with the added option
*/
ifMatchAny(): T;
/**
* Sets an If-None-Match option.
*
* @param tag - The tags to match.
* @returns This Options instance with the added option
*/
ifNoneMatch(...tag: string[]): T;
/**
* Shortcut for If-None-Match: *
* @returns This Options instance with the added option
*/
ifNoneMatchAny(): T;
}
export interface HasFields<T extends HasFields<T>> {
/**
* Sets a fields option.
*
* @param fields - The fields to return.
* @returns This Options instance with the added option
*/
withFields(...fields: string[]): T;
}
export interface HasFilterAndNamespace<T extends HasFilterAndNamespace<T>> {
/**
* Sets a filter option.
*
* @param rawFilterString - The string to filter by.
* @returns This Options instance with the added option
*/
withRawFilter(rawFilterString: string): T;
/**
* Sets a filter option.
*
* @param filter - The instance of Filter to use.
* @returns This Options instance with the added option
*/
withFilter(filter: Filter): T;
/**
* Sets a namespaces option.
*
* @param namespaces - The namespaces to set.
* @returns This Options instance with the added option
*/
withNamespaces(...namespaces: string[]): T;
}
export interface RequestOptionsWithMatchOptions<T extends RequestOptionsWithMatchOptions<T>>
extends AddRequestOptions<RequestOptionsWithMatchOptions<T>>,
HasMatch<RequestOptionsWithMatchOptions<T>> { }
export abstract class AbstractRequestOptionsWithMatchOptions<T extends AbstractRequestOptionsWithMatchOptions<T>>
extends AbstractRequestOptions<AbstractRequestOptionsWithMatchOptions<T>>
implements RequestOptionsWithMatchOptions<AbstractRequestOptionsWithMatchOptions<T>> {
public ifMatch(...tags: string[]): T {
this.addHeader('If-Match', tags.join(', '));
return this as unknown as T;
}
public ifNoneMatch(...tags: string[]): T {
this.addHeader('If-None-Match', tags.join(', '));
return this as unknown as T;
}
ifMatchAny(): T {
this.addHeader('If-Match', '*');
return this as unknown as T;
}
ifNoneMatchAny(): T {
this.addHeader('If-None-Match', '*');
return this as unknown as T;
}
}
/**
* Option provider for If-Match / If-None-Match headers
*/
export interface MatchOptions extends RequestOptionsWithMatchOptions<MatchOptions> {}
export class DefaultMatchOptions extends AbstractRequestOptionsWithMatchOptions<DefaultMatchOptions> implements MatchOptions {
private constructor() {
super();
}
/**
* Provides an instance of MatchOptions.
*
* @returns The MatchOptions
*/
public static getInstance(): DefaultMatchOptions {
return new DefaultMatchOptions();
}
}
/**
* Option provider for some get requests
*/
export interface FieldsOptions extends RequestOptionsWithMatchOptions<FieldsOptions>, HasFields<FieldsOptions> {
withFields(...fields: string[]): FieldsOptions;
}
export class DefaultFieldsOptions extends AbstractRequestOptionsWithMatchOptions<DefaultFieldsOptions> implements FieldsOptions {
private constructor() {
super();
}
/**
* Provides an instance of FieldsOptions.
*
* @returns The FieldsOptions
*/
public static getInstance(): DefaultFieldsOptions {
return new DefaultFieldsOptions();
}
public withFields(...fields: string[]): DefaultFieldsOptions {
super.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
}
/**
* Option provider for count requests
*/
export interface CountOptions extends AddRequestOptions<CountOptions>, HasFilterAndNamespace<CountOptions> {
}
export class DefaultCountOptions extends AbstractRequestOptions<DefaultCountOptions> implements CountOptions {
protected constructor() {
super();
}
/**
* Provides an instance of DefaultCountOptions.
*
* @returns The DefaultCountOptions
*/
public static getInstance(): DefaultCountOptions {
return new DefaultCountOptions();
}
public withRawFilter(rawFilterString: string): DefaultCountOptions {
this.addRequestParameter('filter', encodeURIComponent(rawFilterString));
return this;
}
public withFilter(filter: Filter): DefaultCountOptions {
this.addRequestParameter('filter', encodeURIComponent(filter.toString()));
return this;
}
public withNamespaces(...namespaces: string[]): DefaultCountOptions {
this.addRequestParameter('namespaces', encodeURIComponent(namespaces.join()));
return this;
}
}
/**
* Option provider for search requests
*/
export interface SearchOptions extends AddRequestOptions<SearchOptions>, HasFilterAndNamespace<SearchOptions>, HasFields<SearchOptions> {
/**
* Sets a limit option.
*
* @param offset - The index to start at.
* @param count - The number of things to return.
* @returns The instance of SearchOptions with the added option
*/
withLimit(offset: number, count: number): SearchOptions;
/**
* Sets a sort option.
*
* @param sortOperation - The string to sort by.
* @returns The instance of SearchOptions with the added option
*/
withSort(sortOperation: string): SearchOptions;
}
export class DefaultSearchOptions extends AbstractRequestOptions<DefaultSearchOptions>
implements SearchOptions {
private sort: string;
private limit: string;
private constructor() {
super();
this.sort = '';
this.limit = '';
}
/**
* Provides an instance of DefaultSearchOptions.
*
* @returns The DefaultSearchOptions
*/
public static getInstance(): DefaultSearchOptions {
return new DefaultSearchOptions();
}
public withNamespaces(...namespaces: string[]): DefaultSearchOptions {
this.addRequestParameter('namespaces', encodeURIComponent(namespaces.join()));
return this;
}
public withFields(...fields: string[]): DefaultSearchOptions {
this.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
public withRawFilter(rawFilterString: string): DefaultSearchOptions {
this.addRequestParameter('filter', encodeURIComponent(rawFilterString));
return this;
}
public withFilter(filter: Filter): DefaultSearchOptions {
this.addRequestParameter('filter', encodeURIComponent(filter.toString()));
return this;
}
public withLimit(offset: number, count: number): DefaultSearchOptions {
this.limit = `limit(${offset},${count})`;
return this.setOption();
}
public withSort(sortOperation: string): DefaultSearchOptions {
this.sort = `sort(${sortOperation})`;
return this.setOption();
}
/**
* Constructs the 'option' option out of the values of limit and sort.
*
* @returns The instance of DefaultSearchOptions with the constructed option
*/
private setOption(): DefaultSearchOptions {
let parameter: string;
if (this.sort === '') {
parameter = this.limit;
} else if (this.limit === '') {
parameter = this.sort;
} else {
parameter = `${this.limit},${this.sort}`;
}
this.addRequestParameter('option', encodeURIComponent(parameter));
return this;
}
}
/**
* Option provider for get Things requests
*/
export interface GetThingsOptions extends AddRequestOptions<GetThingsOptions>, HasFields<GetThingsOptions> {
setThingIds(ids: string[]): GetThingsOptions;
}
export class DefaultGetThingsOptions extends AbstractRequestOptions<DefaultGetThingsOptions> implements GetThingsOptions {
private constructor() {
super();
}
/**
* Provides an instance of GetThingsOptions.
*
* @returns The GetThingsOptions
*/
public static getInstance(): DefaultGetThingsOptions {
return new DefaultGetThingsOptions();
}
public withFields(...fields: string[]): DefaultGetThingsOptions {
super.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
public setThingIds(ids: string[]): DefaultGetThingsOptions {
super.addRequestParameter('ids', encodeURIComponent(ids.join()));
return this;
}
}
/**
* Option provider for Messages requests
*/
export interface MessagesOptions extends AddRequestOptions<MessagesOptions> {
/**
* Sets a timeout option.
*
* @param timeout - The timeout to use.
* @returns The instance of MessagesOptions with the added option
*/
withTimeout(timeout: number): MessagesOptions;
}
export class DefaultMessagesOptions extends AbstractRequestOptions<DefaultMessagesOptions> implements MessagesOptions {
private constructor() {
super();
}
/**
* Provides an instance of MessagesOptions.
*
* @returns The MessagesOptions
*/
public static getInstance(): DefaultMessagesOptions {
return new DefaultMessagesOptions();
}
public withTimeout(timeout: number): DefaultMessagesOptions {
this.addRequestParameter('timeout', timeout.toString());
return this;
}
}
/**
* Option provider for post Connection requests
*/
export interface PostConnectionOptions extends AddRequestOptions<PostConnectionOptions> {
/**
* Sets a dry-run option to test a Connection.
*
* @param dryRun - If the connection should only be tested, but not already created.
* @returns The instance of DefaultPostConnectionOptions with the added option
*/
asDryRun(dryRun: boolean): DefaultPostConnectionOptions;
}
export class DefaultPostConnectionOptions extends AbstractRequestOptions<DefaultPostConnectionOptions> implements PostConnectionOptions {
private constructor() {
super();
}
/**
* Provides an instance of DefaultPostConnectionOptions.
*
* @returns The DefaultPostConnectionOptions
*/
public static getInstance(): DefaultPostConnectionOptions {
return new DefaultPostConnectionOptions();
}
/**
* Gets an instance of DefaultPostConnectionOptions with the dry-run parameter set to true to test Connections.
*
* @returns The instance of MessagesOptions
*/
public static getDryRunInstance(): DefaultPostConnectionOptions {
return new DefaultPostConnectionOptions().asDryRun(true);
}
public asDryRun(dryRun: boolean): DefaultPostConnectionOptions {
this.addRequestParameter('dry-run', String(dryRun));
return this;
}
}