Skip to content

Commit

Permalink
Add query parameters serialization(#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkhismatulin committed May 23, 2020
1 parent 28aa074 commit 2e67d29
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ export default (function create(defaults) {
return (url, data, config) => redaxios(url, Object.assign({ method, data }, config));
}

/**
* Builds request url based on query string parameters
* @private
* @param {string} url
* @param {object|URLSearchParams} [params]
* @param {function} [serializer]
* @returns {string}
*/
function buildUrl(url, params, serializer) {
if (!params) {
return url;
}

let serializedParams;
if (serializer) {
serializedParams = serializer(params);
} else if (params instanceof URLSearchParams) {
serializedParams = params.toString();
} else {
serializedParams = (new URLSearchParams(params)).toString();
}

const divider = url.indexOf('?') === -1 ? '?' : '&';
return url + divider + serializedParams;
}

/**
* @public
* @type {((config?: Options) => Promise<Response>) | ((url: string, config?: Options) => Promise<Response>)}
Expand Down Expand Up @@ -133,6 +159,7 @@ export default (function create(defaults) {
}
const options = deepMerge(defaults, config || {});
let data = options.data;
url = buildUrl(url, options.params, options.paramsSerializer);

if (options.transformRequest) {
for (let i = 0; i < options.transformRequest.length; i++) {
Expand All @@ -142,7 +169,7 @@ export default (function create(defaults) {
}
}
}

const fetchFunc = options.fetch || fetch;
const customHeaders = {};

Expand Down
30 changes: 30 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ describe('redaxios', () => {
expect(res.status).toEqual(200);
expect(JSON.parse(res.data)).toEqual({ hello: 'world' });
});

it('should support params and paramsSerializer options', async () => {
const oldFetch = window.fetch;
window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve());

axios.get('/foo');
expect(window.fetch).toHaveBeenCalledTimes(1);
expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.any(Object));

let params = { a: 1, b: true };
axios.get('/foo', { params });
expect(window.fetch).toHaveBeenCalledTimes(2);
expect(window.fetch).toHaveBeenCalledWith('/foo?a=1&b=true', jasmine.any(Object));

axios.get('/foo?c=42', { params });
expect(window.fetch).toHaveBeenCalledTimes(3);
expect(window.fetch).toHaveBeenCalledWith('/foo?c=42&a=1&b=true', jasmine.any(Object));

params = new URLSearchParams({ d: 'test' });
axios.get('/foo', { params });
expect(window.fetch).toHaveBeenCalledTimes(4);
expect(window.fetch).toHaveBeenCalledWith('/foo?d=test', jasmine.any(Object));

const paramsSerializer = params => 'e=iamthelaw';
axios.get('/foo', { params, paramsSerializer });
expect(window.fetch).toHaveBeenCalledTimes(5);
expect(window.fetch).toHaveBeenCalledWith('/foo?e=iamthelaw', jasmine.any(Object));

window.fetch = oldFetch;
});
});

0 comments on commit 2e67d29

Please sign in to comment.