Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
developit authored Apr 29, 2022
2 parents 22018ac + a3b7a14 commit e7595db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/index.js'
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function create(defaults) {
data = f(data, options.headers) || data;
});

if (data && typeof data === 'object' && typeof data.append !== 'function') {
if (data && typeof data === 'object' && typeof data.append !== 'function' && typeof data.text !== 'function') {
data = JSON.stringify(data);
customHeaders['content-type'] = 'application/json';
}
Expand Down Expand Up @@ -200,7 +200,7 @@ function create(defaults) {
const fetchFunc = options.fetch || fetch;

return fetchFunc(url, {
method: _method || options.method,
method: (_method || options.method || 'get').toUpperCase(),
body: data,
headers: deepMerge(options.headers, customHeaders, true),
credentials: options.withCredentials ? 'include' : 'same-origin'
Expand Down
26 changes: 22 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('redaxios', () => {
expect(window.fetch).toHaveBeenCalledWith(
'http://foo/bar',
jasmine.objectContaining({
method: 'get',
method: 'GET',
headers: {},
body: undefined
})
Expand All @@ -112,7 +112,7 @@ describe('redaxios', () => {
expect(window.fetch).toHaveBeenCalledWith(
'/foo/bar',
jasmine.objectContaining({
method: 'get',
method: 'GET',
headers: {},
body: undefined
})
Expand Down Expand Up @@ -203,7 +203,25 @@ describe('redaxios', () => {
expect(fetchMock).toHaveBeenCalledWith(
'/foo',
jasmine.objectContaining({
method: 'post',
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: '{"hello":"world"}'
})
);
expect(res.status).toEqual(200);
expect(res.data).toEqual('yep');
});

it('should issue PATCH requests (with JSON body)', async () => {
const res = await axios.patch('/foo', {
hello: 'world'
});
expect(fetchMock).toHaveBeenCalledWith(
'/foo',
jasmine.objectContaining({
method: 'PATCH',
headers: {
'content-type': 'application/json'
},
Expand Down Expand Up @@ -235,7 +253,7 @@ describe('redaxios', () => {
expect(fetchMock).toHaveBeenCalledWith(
'/foo',
jasmine.objectContaining({
method: 'post',
method: 'POST',
headers: {
'content-type': 'multipart/form-data'
},
Expand Down

0 comments on commit e7595db

Please sign in to comment.