/
bridge.ts
241 lines (204 loc) · 7.29 KB
/
bridge.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
import * as clone from 'clone';
import * as fetchPonyfill from 'fetch-ponyfill';
import * as qs from 'qs';
import { Request } from '../models/request';
import { Navigation, RangeRefinement, Record, RefinementResults, Results } from '../models/response';
import { Normalizers } from '../utils';
import { Query } from './query';
const SEARCH = '/search';
const REFINEMENTS = '/refinements';
const INVALID_QUERY_ERROR = 'query was not of a recognised type';
const createTimeoutPromise = (timeout: number) =>
new Promise((resolve, reject) => {
setTimeout(() => {
reject(new BridgeTimeout(`Timed out in ${timeout} ms`));
}, timeout);
});
export interface RawRecord extends Record {
_id: string;
_u: string;
_t: string;
_snippet?: string;
}
export interface BridgeCallback {
(err?: Error, res?: Results): void;
}
export class BridgeTimeout extends Error {
/* istanbul ignore next */
constructor (err: string) {
super(err);
}
}
export class BridgeError extends Error {
/* istanbul ignore next */
constructor (statusText: string, public status: number, public data: any) {
super(statusText);
Object.setPrototypeOf(this, BridgeError.prototype);
}
get statusText() {
return this.message;
}
}
export type BridgeQuery = string | Query | Request;
export const DEFAULT_CONFIG: BridgeConfig = {
timeout: 1500
};
export abstract class AbstractBridge {
config: BridgeConfig;
fetch: fetchPonyfill = fetchPonyfill().fetch;
headers: any = {};
baseUrl: string;
errorHandler: (error: Error) => void;
protected bridgeUrl: string;
protected refinementsUrl: string;
constructor(config: BridgeConfig) {
this.config = Object.assign({}, DEFAULT_CONFIG, config);
}
search(query: BridgeQuery, callback?: BridgeCallback): Promise<Results> {
let { request, queryParams } = this.extractRequest(query);
if (request === null) return this.generateError(INVALID_QUERY_ERROR, callback);
const response = this.fireRequest(this.bridgeUrl, request, queryParams)
.then(AbstractBridge.transformRecords)
.then(AbstractBridge.transformRefinements);
return this.handleResponse(response, callback);
}
refinements(query: BridgeQuery, navigationName: string, callback?: BridgeCallback): Promise<RefinementResults> {
let { request } = this.extractRequest(query);
if (request === null) return this.generateError(INVALID_QUERY_ERROR, callback);
const refinementsRequest = { originalQuery: request, navigationName };
const response = this.fireRequest(this.refinementsUrl, refinementsRequest);
return this.handleResponse(response, callback);
}
protected abstract augmentRequest(request: any): any;
private handleResponse<T>(response: PromiseLike<T>, callback: Function) {
if (callback) {
response.then((res) => callback(undefined, res), (err) => callback(err));
} else {
return <Promise<T>>response;
}
}
private extractRequest(query: any): { request: Request; queryParams: any; } {
switch (typeof query) {
case 'string': return { request: new Query(<string>query).build(), queryParams: {} };
case 'object': return query instanceof Query
? { request: query.build(), queryParams: query.queryParams }
: { request: clone(query), queryParams: {} };
default: return { request: null, queryParams: null };
}
}
private generateError(error: string, callback: (err: Error) => void): Promise<any> {
const err = new Error(error);
if (callback) {
callback(err);
} else {
return Promise.reject(err);
}
}
// tslint:disable-next-line max-line-length
private fireRequest(url: string, body: Request | any, queryParams: any = {}): fetchPonyfill.Promise<any> {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...this.headers
},
body: JSON.stringify(AbstractBridge.normalizeRequest(this.augmentRequest(body))),
};
const params = qs.stringify(queryParams);
url = params ? `${url}?${params}` : url;
return Promise.race([this.fetch(url, options), createTimeoutPromise(this.config.timeout)])
.then((res) => {
if (res.ok) {
return res.json();
} else {
return res.json().then((err) => {
throw new BridgeError(
res.statusText,
res.status,
err
);
});
}
})
.catch((err) => {
if (this.errorHandler) {
this.errorHandler(err);
}
throw err;
});
}
static normalizeRequest(request: Request): Request {
Object.keys(Normalizers).forEach((normalize) => Normalizers[normalize](request));
return request;
}
static transform(response: any, key: string, callback: Function) {
if (response[key]) {
return Object.assign(response, { [key]: response[key].map(callback) });
} else {
return response;
}
}
static transformRecords(response: any) {
return AbstractBridge.transform(response, 'records', AbstractBridge.convertRecordFields);
}
static transformRefinements(response: any) {
const transformed = AbstractBridge.transform(response, 'availableNavigation', AbstractBridge.convertRefinement);
return AbstractBridge.transform(transformed, 'selectedNavigation', AbstractBridge.convertRefinement);
}
static convertRecordFields(record: RawRecord): Record | RawRecord {
const converted = Object.assign(record, { id: record._id, url: record._u, title: record._t });
delete converted._id;
delete converted._u;
delete converted._t;
if (record._snippet) {
converted.snippet = record._snippet;
delete converted._snippet;
}
return converted;
}
static convertRefinement(navigation: Navigation): Navigation {
if (navigation.range) {
navigation.min = Number.MAX_SAFE_INTEGER;
navigation.max = Number.MIN_SAFE_INTEGER;
navigation.refinements = navigation.refinements
.map((ref: RangeRefinement) => {
navigation.min = Math.min(navigation.min, ref.low);
navigation.max = Math.max(navigation.max, ref.high);
return ({
...ref,
high: parseFloat(<any>ref.high),
low: parseFloat(<any>ref.low)
});
});
}
return navigation;
}
}
export class CloudBridge extends AbstractBridge {
constructor(private clientKey: string, customerId: string, config: BridgeConfig = {}) {
super(config);
this.baseUrl = `https://${customerId}.groupbycloud.com:443/api/v1`;
this.bridgeUrl = this.baseUrl + SEARCH;
this.refinementsUrl = this.bridgeUrl + REFINEMENTS;
}
protected augmentRequest(request: any): any {
return Object.assign(request, { clientKey: this.clientKey });
}
}
export class BrowserBridge extends AbstractBridge {
constructor(customerId: string, https: boolean = false, config: BridgeConfig = {}) {
super(config);
const scheme = https ? 'https' : 'http';
const port = https ? ':443' : '';
this.baseUrl = config.proxyUrl || `${scheme}://${customerId}-cors.groupbycloud.com${port}/api/v1`;
this.bridgeUrl = this.baseUrl + SEARCH;
this.refinementsUrl = this.bridgeUrl + REFINEMENTS;
}
protected augmentRequest(request: any): any {
return request;
}
}
export interface BridgeConfig {
timeout?: number;
proxyUrl?: string;
}