(
route,
opts = {
method: 'get',
body: undefined,
followRedirects: false,
followAllRedirects: false,
headers: {},
}
)
| 3 | import { omitBy, isUndefined } from 'lodash-es' |
| 4 | |
| 5 | export async function get( |
| 6 | route, |
| 7 | opts = { |
| 8 | method: 'get', |
| 9 | body: undefined, |
| 10 | followRedirects: false, |
| 11 | followAllRedirects: false, |
| 12 | headers: {}, |
| 13 | } |
| 14 | ) { |
| 15 | const method = opts.method || 'get' |
| 16 | const fn = got[method] |
| 17 | if (!fn || typeof fn !== 'function') throw new Error(`No method function for '${method}'`) |
| 18 | const absURL = `http://localhost:4000${route}` |
| 19 | const xopts = omitBy( |
| 20 | { |
| 21 | body: opts.body, |
| 22 | headers: opts.headers, |
| 23 | retry: { limit: 0 }, |
| 24 | throwHttpErrors: false, |
| 25 | followRedirect: opts.followAllRedirects || opts.followRedirects, |
| 26 | }, |
| 27 | isUndefined |
| 28 | ) |
| 29 | const res = await fn(absURL, xopts) |
| 30 | // follow all redirects, or just follow one |
| 31 | if (opts.followAllRedirects && [301, 302].includes(res.status)) { |
| 32 | // res = await get(res.headers.location, opts) |
| 33 | throw new Error('A') |
| 34 | } else if (opts.followRedirects && [301, 302].includes(res.status)) { |
| 35 | // res = await get(res.headers.location) |
| 36 | throw new Error('B') |
| 37 | } |
| 38 | |
| 39 | const text = res.body |
| 40 | const status = res.statusCode |
| 41 | const headers = res.headers |
| 42 | return { |
| 43 | text, |
| 44 | status, |
| 45 | statusCode: status, // Legacy |
| 46 | headers, |
| 47 | header: headers, // Legacy |
| 48 | url: res.url, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | export async function head(route, opts = { followRedirects: false }) { |
| 53 | const res = await get(route, { method: 'head', followRedirects: opts.followRedirects }) |
no outgoing calls
no test coverage detected