-
-
Notifications
You must be signed in to change notification settings - Fork 655
/
apiFetch.js
182 lines (165 loc) · 3.76 KB
/
apiFetch.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
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
/* @flow */
import type { Auth, ResponseExtractionFunc } from '../types';
import { getAuthHeader, encodeAsURI, isValidUrl } from '../utils/url';
import userAgent from '../utils/userAgent';
import { networkActivityStart, networkActivityStop } from '../utils/networkActivity';
const apiVersion = 'api/v1';
const defaultResFunc: ResponseExtractionFunc = res => res;
export const fetchWithAuth = async (auth: Auth, url: string, params: Object = {}) => {
if (!isValidUrl(url)) {
throw new Error(`Invalid url ${url}`);
}
const contentType =
params.body instanceof FormData
? 'multipart/form-data'
: 'application/x-www-form-urlencoded; charset=utf-8';
const allParams = {
headers: {
'Content-Type': contentType,
'User-Agent': userAgent,
Authorization: getAuthHeader(auth.email, auth.apiKey),
},
...params,
};
return fetch(url, allParams);
};
export const apiFetch = async (auth: Auth, route: string, params: Object = {}) =>
fetchWithAuth(auth, `${auth.realm}/${apiVersion}/${route}`, params);
export const apiCall = async (
auth: Auth,
route: string,
params: Object = {},
resFunc: ResponseExtractionFunc = defaultResFunc,
isSilent: boolean = false,
) => {
try {
networkActivityStart(isSilent);
const response = await apiFetch(auth, route, params);
if (!response.ok) {
console.log('Bad response for:', { auth, route, params, response }); // eslint-disable-line
const error = new Error('API');
// $FlowFixMe
error.response = response;
// $FlowFixMe
throw error;
}
const json = await response.json();
if (json.result !== 'success') {
console.log('Bad response for:', { auth, route, params, response }); // eslint-disable-line
const error = new Error('API');
// $FlowFixMe
error.response = response;
// $FlowFixMe
error.code = json.code;
throw error;
}
return resFunc(json);
} finally {
networkActivityStop(isSilent);
}
};
export const apiGet = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
isSilent: boolean = false,
) =>
apiCall(
auth,
`${route}?${encodeAsURI(params)}`,
{
method: 'get',
},
resFunc,
isSilent,
);
export const apiPost = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
) =>
apiCall(
auth,
route,
{
method: 'post',
body: encodeAsURI(params),
},
resFunc,
);
export const apiFile = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
body: FormData,
) =>
apiCall(
auth,
route,
{
method: 'post',
body,
},
resFunc,
);
export const apiPut = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
) =>
apiCall(
auth,
route,
{
method: 'put',
body: encodeAsURI(params),
},
resFunc,
);
export const apiDelete = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
) =>
apiCall(
auth,
route,
{
method: 'delete',
body: encodeAsURI(params),
},
resFunc,
);
export const apiPatch = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
) =>
apiCall(
auth,
route,
{
method: 'patch',
body: encodeAsURI(params),
},
resFunc,
);
export const apiHead = async (
auth: Auth,
route: string,
resFunc: ResponseExtractionFunc = defaultResFunc,
params: Object = {},
) =>
apiCall(
auth,
`${route}?${encodeAsURI(params)}`,
{
method: 'head',
},
resFunc,
);