-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfetchy.js
executable file
·54 lines (45 loc) · 1.72 KB
/
fetchy.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
const fetch = require('node-fetch')
const nodeUrl = require('url')
const bodyAdapter = (body) => {
if(Object.hasOwnProperty.call(body, 'value') && !Object.hasOwnProperty.call(body, 'status')) {
body.status = 0
}
return body
}
async function _fetchy(slowTime, method, timeout, url, body, opts) {
opts = opts || {}
const headers = opts.headers || {}
// if method GET body should be undefined
if(method == "GET") body = undefined
// content type default: application/json
if(body != null) {headers["Content-Type"] = "application/json"}
// slowTime, if pause required before every request, default is 0
if(slowTime) {await (() => new Promise(res => setTimeout(res, slowTime)))()}
const response = await fetch(url, {
method, headers, timeout,
body: typeof body === 'object' ? JSON.stringify(body) : body
})
const contentType = response.headers.get("content-type")
if(contentType && contentType.includes("application/json")) {
if(response.status > 300) {
throw new Error(`
Something went wrong with request, pleace check driver server connection ${await response.text()}`)
}
const body = bodyAdapter(await response.json())
return {body, headers: response.headers}
} else {
if(response.status > 300) {
console.log(body, url)
throw new Error(`
Something went wrong with request, pleace check driver server connection ${await response.text()}`
)
}
return {body: await response.text(), headers: response.headers}
}
}
const fetchy = (slowTime, method, URL, timeout, path, body, opts) => {
return _fetchy(slowTime, method, timeout, nodeUrl.resolve(URL, path), body, opts)
}
module.exports = (slowTime) => {
return fetchy.bind(fetch, slowTime)
}