This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (85 loc) · 3.02 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
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
const fetch = require("node-fetch");
const FormData = require("form-data");
const fs = require("fs");
const path = require("path");
const util = require("util");
let URL = null
const format = util.format;
util.format = (...args) => {
const str = format.call(util, ...args);
if (str.length > 10000) return str.slice(0, 10000) + "...";
return str;
};
async function postFormData(pathUrl, opts = {}, mediaPath, filename, contentType = "image/jpg") {
const form = new FormData();
mediaPath = Array.isArray(mediaPath) ? mediaPath : [mediaPath]
filename = Array.isArray(filename) ? filename : [filename]
if (Array.isArray(filename) && Array.isArray(mediaPath)) {
if (filename.length != mediaPath.length)
console.warn('Names array length should equal paths array length');
else {
for (let i = 0; i < filename.length; i++) {
form.append(filename[i], fs.createReadStream(path.join(__dirname, mediaPath[i])), {
filename: filename[i],
contentType
});
}
const headers = form.getHeaders();
if (opts.token) {
headers.authorization = "Bearer " + opts.token;
}
const response = await fetch(URL + pathUrl, Object.assign({
method: "POST", body: form, headers: headers
}, opts));
contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return { body: await response.json(), status: response.status, headers: response.headers };
} else {
return { body: await response.text(), status: response.status, headers: response.headers };
}
}
}
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function _fetchy(method, url, body, opts) {
opts = opts || {};
const headers = opts.headers || {};
if (method == "GET")
body = undefined;
if (body != null) {
headers["Content-Type"] = "application/json";
}
if (opts.token) {
headers.authorization = "Bearer " + opts.token;
}
const response = await fetch(url, Object.assign({
method, headers, body: typeof body === 'object' ? JSON.stringify(body) : body
}, opts));
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const body = await response.json()
return { body, status: response.status, headers: response.headers };
} else {
return { body: await response.text(), status: response.status, headers: response.headers };
}
}
const fetchy = (method, path, body, opts) => _fetchy(method, URL + path, body, opts);
module.exports = function (host) {
URL = host
return {
postFormData,
util,
fetchy_util: {
get: fetchy.bind(global, "GET"),
put: fetchy.bind(global, "PUT"),
post: fetchy.bind(global, "POST"),
del: fetchy.bind(global, "DELETE")
},
_fetchy: {
get: _fetchy.bind(global, "GET"),
put: _fetchy.bind(global, "PUT"),
post: _fetchy.bind(global, "POST"),
del: _fetchy.bind(global, "DELETE")
}
}
};