@idearium/fetch
Advanced tools
Comparing version 3.0.0-beta.3 to 3.0.0-beta.4
37
index.js
'use strict'; | ||
const parseBody = async (response, opts) => { | ||
const decoder = new TextDecoder('utf-8', { fatal: true }); | ||
const body = []; | ||
const isObjectMode = opts?.objectMode || false; | ||
const parseBody = async (response) => { | ||
const decoder = new TextDecoderStream('utf-8', { fatal: true }); | ||
let body = ''; | ||
for await (const chunk of response.body) { | ||
const value = decoder.decode(chunk); | ||
if (value.match(/\n/)) { | ||
body.push(...value.split('\n').filter((val) => val.length)); | ||
} | ||
if (!value.match(/\n/)) { | ||
body.push(value); | ||
} | ||
for await (const chunk of response.body.pipeThrough(decoder)) { | ||
body += chunk; | ||
} | ||
if (isObjectMode) { | ||
return body.length > 1 | ||
? body.map((chunk) => JSON.parse(chunk)) | ||
: JSON.parse(body[0]); | ||
} | ||
if ( | ||
@@ -30,9 +15,9 @@ response.headers.get('Content-Type').includes('application/json') && | ||
) { | ||
return JSON.parse(body.join('')); | ||
return JSON.parse(body); | ||
} | ||
return body.join(''); | ||
return body; | ||
}; | ||
const parseResponse = async (response, opts) => { | ||
const parseResponse = async (response) => { | ||
if ( | ||
@@ -46,3 +31,3 @@ response.headers && | ||
) { | ||
response.result = await parseBody(response, opts); | ||
response.result = await parseBody(response); | ||
} | ||
@@ -53,3 +38,3 @@ | ||
const fetchApi = async (url, fetchOptions = {}, opts = {}) => { | ||
const fetchApi = async (url, fetchOptions = {}) => { | ||
const headers = {}; | ||
@@ -71,5 +56,5 @@ | ||
return await parseResponse(response, opts); | ||
return await parseResponse(response); | ||
}; | ||
module.exports = fetchApi; |
@@ -19,3 +19,3 @@ { | ||
}, | ||
"version": "3.0.0-beta.3" | ||
"version": "3.0.0-beta.4" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
11862
321