-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelpers.js
42 lines (36 loc) · 1007 Bytes
/
helpers.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
import axios from "axios";
const GATEWAY_DEFAULT_TIMEOUT = 60000;
async function downloadFromGateway(cid, options) {
if (typeof options.endpoint !== "string") {
throw new Error(`Default Gateway must be set`);
}
const downloadHeaders = {};
if (options.token) {
downloadHeaders["x-filebase-gateway-token"] = options.token;
}
const downloadResponse = await axios.request({
method: "GET",
baseURL: options.endpoint,
url: `/ipfs/${cid}`,
headers: downloadHeaders,
type: "stream",
timeout: options?.timeout || GATEWAY_DEFAULT_TIMEOUT,
});
return downloadResponse.data;
}
function apiErrorHandler(err) {
if (
err?.response &&
err?.response?.status &&
(err.response.status.toString()[0] === "4" ||
err.response.status.toString()[0] === "5")
) {
throw new Error(
err.response.data.error?.details ||
err.response.data.error?.reason ||
err,
);
}
throw err;
}
export { downloadFromGateway, apiErrorHandler };