@idearium/fetch
Advanced tools
Comparing version 2.0.3 to 2.0.4-beta.3
16
index.js
@@ -23,7 +23,17 @@ 'use strict'; | ||
const fetchApi = async (url, { headers, ...options } = {}) => { | ||
const opts = { ...options }; | ||
const fetchApi = async (url, opts = {}) => { | ||
const headers = {}; | ||
opts.headers = { 'content-type': 'application/json', ...headers }; | ||
if (opts?.headers) { | ||
Object.keys(opts.headers) | ||
.map((key) => key.toLowerCase()) | ||
.forEach((key) => (headers[key] = opts.headers[key])); | ||
} | ||
if (!headers['content-type']) { | ||
headers['content-type'] = 'application/json'; | ||
} | ||
opts.headers = headers; | ||
return parseResponse(await fetch(url, opts)); | ||
@@ -30,0 +40,0 @@ }; |
{ | ||
"name": "@idearium/fetch", | ||
"version": "2.0.3", | ||
"version": "2.0.4-beta.3", | ||
"description": "A package to make fetching trivial.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ 'use strict'; | ||
const testUrl = 'http://www.idearium.io/'; | ||
const testUrl = 'https://www.idearium.io/'; | ||
const headers = { 'content-type': 'application/json' }; | ||
@@ -29,3 +29,3 @@ | ||
result: { pass: true }, | ||
status: 200 | ||
status: 200, | ||
}); | ||
@@ -40,3 +40,3 @@ }); | ||
headers, | ||
status: 400 | ||
status: 400, | ||
}); | ||
@@ -47,3 +47,3 @@ | ||
result: { pass: false }, | ||
status: 400 | ||
status: 400, | ||
}); | ||
@@ -71,6 +71,4 @@ }); | ||
const headers = { 'content-type': 'application/json' }; | ||
fetchMock.mock(testUrl, [], { | ||
headers | ||
headers, | ||
}); | ||
@@ -81,3 +79,3 @@ | ||
await expect(fetchMock).toHaveBeenCalledWith(testUrl, { | ||
headers | ||
headers, | ||
}); | ||
@@ -91,3 +89,3 @@ }); | ||
headers, | ||
method: 'POST' | ||
method: 'POST', | ||
}); | ||
@@ -99,3 +97,3 @@ | ||
headers, | ||
method: 'POST' | ||
method: 'POST', | ||
}); | ||
@@ -111,3 +109,3 @@ }); | ||
credentials, | ||
headers | ||
headers, | ||
}); | ||
@@ -119,3 +117,3 @@ | ||
credentials, | ||
headers | ||
headers, | ||
}); | ||
@@ -132,3 +130,3 @@ }); | ||
credentials, | ||
headers | ||
headers, | ||
}; | ||
@@ -151,4 +149,24 @@ | ||
result: {}, | ||
status: 400 | ||
status: 400, | ||
}); | ||
}); | ||
it('does not duplicate the content type header', async () => { | ||
expect.assertions(2); | ||
fetchMock.mock(testUrl, [], { | ||
headers, | ||
}); | ||
await fetchApi(testUrl, { | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
await expect(fetchMock).toHaveBeenCalledWith(testUrl, { | ||
headers: { 'content-type': 'application/json' }, | ||
}); | ||
await expect(fetchMock).not.toHaveBeenCalledWith(testUrl, { | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5627
149
2