/
search.ts
141 lines (129 loc) · 4.45 KB
/
search.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
/*!
* 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 { SearchThingsResponse } from '../../model/response';
import { CountOptions, DefaultSearchOptions, RequestOptions, SearchOptions } from '../../options/request.options';
import { ContentType } from '../constants/content-type';
import { Header } from '../constants/header';
import { HttpVerb } from '../constants/http-verb';
import { RequestSender, RequestSenderFactory } from '../request-factory/request-sender';
export interface SearchHandle {
/**
* Searches for Things that match the restrictions set in options.
*
* @param options - Options to use for the search.
* @returns The search result
*/
search(options?: SearchOptions): Promise<SearchThingsResponse>;
/**
* Searches for Things that match the restrictions set in options using a http post request.
*
* @param options - Options to use for the search.
* @returns The search result
*/
postSearch(options?: SearchOptions): Promise<SearchThingsResponse>;
/**
* Counts the Things that match the restrictions set in options.
*
* @param options - Options to use for the search.
* @returns The count
*/
count(options?: CountOptions): Promise<number>;
/**
* Counts the Things that match the restrictions set in options using a http post request.
*
* @param options - Options to use for the search.
* @returns The count
*/
postCount(options?: CountOptions): Promise<number>;
}
/**
* Handle to send Search requests.
*/
export class DefaultSearchHandle implements SearchHandle {
private constructor(readonly requestFactory: RequestSender) {}
/**
* returns an instance of SearchHandle using the provided RequestSender.
*
* @param builder - The builder for the RequestSender to work with.
* @returns The SearchHandle
*/
public static getInstance(builder: RequestSenderFactory): DefaultSearchHandle {
return new DefaultSearchHandle(builder.buildInstance('search/things'));
}
/**
* Searches for Things that match the restrictions set in options.
*
* @param options - Options to use for the search.
* @returns The search result
*/
search(options?: SearchOptions): Promise<SearchThingsResponse> {
return this.requestFactory.fetchJsonRequest({
verb: HttpVerb.GET,
parser: SearchThingsResponse.fromObject,
requestOptions: options
});
}
/**
* Searches for Things that match the restrictions set in options using a http post request.
*
* @param options - Options to use for the search.
* @returns The search result
*/
postSearch(options?: SearchOptions): Promise<SearchThingsResponse> {
return this.requestFactory.fetchFormRequest({
verb: HttpVerb.POST,
parser: SearchThingsResponse.fromObject,
payload: options?.getOptions(),
requestOptions: this.getFormRequestOptions(options)
});
}
/**
* Counts the Things that match the restrictions set in options.
*
* @param options - Options to use for the search.
* @returns The count
*/
count(options?: CountOptions): Promise<number> {
return this.requestFactory.fetchJsonRequest({
verb: HttpVerb.GET,
parser: Number,
path: 'count',
requestOptions: options
});
}
/**
* Counts the Things that match the restrictions set in options using a http post request.
*
* @param options - Options to use for the search.
* @returns The count
*/
postCount(options?: CountOptions): Promise<number> {
return this.requestFactory.fetchFormRequest({
verb: HttpVerb.POST,
parser: Number,
path: 'count',
payload: options?.getOptions(),
requestOptions: this.getFormRequestOptions(options)
});
}
private getFormRequestOptions(options?: SearchOptions | CountOptions): RequestOptions | undefined {
const requestOptions = DefaultSearchOptions.getInstance();
options?.getHeaders().forEach((value, key) => {
requestOptions.addHeader(key, value);
});
if (!requestOptions?.getHeaders().has(Header.CONTENT_TYPE)) {
requestOptions?.addHeader(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED);
}
return requestOptions;
}
}