-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (65 loc) · 1.8 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
'use strict'
require('isomorphic-fetch')
const http = require('http')
const createError = require('http-errors')
const https = require('https')
const pRetry = require('p-retry')
function tryToParseJson(str) {
try {
return JSON.parse(str)
} catch (err) {
return str
}
}
function parseBody(res) {
if (res.status === 204) {
return Promise.resolve()
}
if (res.headers.get('content-type')?.includes('application/json')) {
return res.json()
}
return res.text().then(tryToParseJson)
}
function fetchPlusPlus(url, options) {
const {
queryString = {},
retries = 0,
...fetchOptions
} = options ?? { method: 'GET' }
let finalUrl = url
if (Object.keys(queryString).length > 0) {
// arrays output in query string is different
// depending on if it is passed in the constructor or using .append() method
// AWS works better with the output from .append()
const searchParams = new URLSearchParams()
Object.entries(queryString).forEach(([key, value]) =>
Array.isArray(value)
? value.forEach(v => searchParams.append(key, v))
: searchParams.append(key, value)
)
finalUrl = `${url}?${searchParams}`
}
if (finalUrl.startsWith('http://')) {
fetchOptions.agent = new http.Agent()
} else if (fetchOptions.insecure) {
fetchOptions.agent = new https.Agent({
rejectUnauthorized: false
})
}
return pRetry(
() =>
fetch(finalUrl, fetchOptions)
.then(res => Promise.all([res, parseBody(res)]))
.then(function ([res, body]) {
if (!res.ok && !fetchOptions.ignoreError) {
throw createError(res.status, body)
}
return body
}),
{
retries,
...(fetchOptions.signal ? { signal: fetchOptions.signal } : {})
}
)
}
module.exports = fetchPlusPlus