-
Notifications
You must be signed in to change notification settings - Fork 127
/
index.js
52 lines (40 loc) · 1.18 KB
/
index.js
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Client4} from 'mattermost-redux/client';
import {ClientError} from 'mattermost-redux/client/client4';
export const doFetch = async (url, options) => {
const {data} = await doFetchWithResponse(url, options);
return data;
};
export const doFetchWithResponse = async (url, options = {}) => {
const response = await fetch(url, Client4.getOptions(options));
let data;
if (response.ok) {
data = await response.json();
return {
response,
data,
};
}
data = await response.text();
throw new ClientError(Client4.url, {
message: data || '',
status_code: response.status,
url,
});
};
export function buildQueryString(parameters) {
const keys = Object.keys(parameters);
if (keys.length === 0) {
return '';
}
let query = '?';
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
query += key + '=' + encodeURIComponent(parameters[key]);
if (i < keys.length - 1) {
query += '&';
}
}
return query;
}