-
Notifications
You must be signed in to change notification settings - Fork 351
/
upload-request.js
63 lines (56 loc) · 1.84 KB
/
upload-request.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
import { getBaseURL, httpHeaderSafeJson } from './utils';
function parseBodyToType(res) {
const clone = res.clone();
return new Promise((resolve) => {
res.json()
.then(data => resolve(data))
.catch(() => clone.text().then(data => resolve(data)));
}).then(data => [res, data]);
}
export function uploadRequest(fetch) {
return function uploadRequestWithFetch(path, args, auth, host, client, options) {
return client.checkAndRefreshAccessToken()
.then(() => {
if (auth !== 'user') {
throw new Error(`Unexpected auth type: ${auth}`);
}
const { contents } = args;
delete args.contents;
const fetchOptions = {
body: contents,
method: 'POST',
headers: {
Authorization: `Bearer ${client.getAccessToken()}`,
'Content-Type': 'application/octet-stream',
'Dropbox-API-Arg': httpHeaderSafeJson(args),
},
};
if (options) {
if (options.selectUser) {
fetchOptions.headers['Dropbox-API-Select-User'] = options.selectUser;
}
if (options.selectAdmin) {
fetchOptions.headers['Dropbox-API-Select-Admin'] = options.selectAdmin;
}
if (options.pathRoot) {
fetchOptions.headers['Dropbox-API-Path-Root'] = options.pathRoot;
}
}
return fetchOptions;
})
.then(fetchOptions => fetch(getBaseURL(host) + path, fetchOptions))
.then(res => parseBodyToType(res))
.then(([res, data]) => {
// maintaining existing API for error codes not equal to 200 range
if (!res.ok) {
// eslint-disable-next-line no-throw-literal
throw {
error: data,
response: res,
status: res.status,
};
}
return data;
});
};
}