Skip to content

Commit a7c8255

Browse files
committed
size optimizations for param serialization
1 parent 0747987 commit a7c8255

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

src/index.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @property {Headers} [headers] Request headers
2020
* @property {FormData|string|object} [body] a body, optionally encoded, to send
2121
* @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType="text"] An encoding to use for the response
22+
* @property {Record<string,any>|URLSearchParams} [params] querystring parameters
23+
* @property {(params: Options['params']) => string} [paramsSerializer] custom function to stringify querystring parameters
2224
* @property {boolean} [withCredentials] Send the request with credentials like cookies
2325
* @property {string} [auth] Authorization header value to send with the request
2426
* @property {string} [xsrfCookieName] Pass an Cross-site Request Forgery prevention cookie value as a header defined by `xsrfHeaderName`
@@ -78,31 +80,19 @@ export default (function create(/** @type {Options} */ defaults) {
7880
return (url, data, config) => redaxios(url, Object.assign({ method, data }, config));
7981
}
8082

81-
/**
82-
* Builds request url based on query string parameters
83-
* @private
84-
* @param {string} url
85-
* @param {object|URLSearchParams} [params]
86-
* @param {function} [serializer]
87-
* @returns {string}
88-
*/
89-
function buildUrl(url, params, serializer) {
90-
if (!params) {
91-
return url;
92-
}
93-
94-
let serializedParams;
95-
if (serializer) {
96-
serializedParams = serializer(params);
97-
} else if (params instanceof URLSearchParams) {
98-
serializedParams = params.toString();
99-
} else {
100-
serializedParams = (new URLSearchParams(params)).toString();
101-
}
102-
103-
const divider = url.indexOf('?') === -1 ? '?' : '&';
104-
return url + divider + serializedParams;
105-
}
83+
// /**
84+
// * Builds request url based on query string parameters
85+
// * @private
86+
// * @param {string} url
87+
// * @param {Record<string,string>|URLSearchParams} [params]
88+
// * @param {function} [serializer]
89+
// * @returns {string}
90+
// */
91+
// function buildUrl(url, params, serializer) {
92+
// const serializedParams = serializer ? serializer(params) : new URLSearchParams(params);
93+
// const divider = ~url.indexOf('?') ? '&' : '?';
94+
// return url + divider + serializedParams;
95+
// }
10696

10797
/**
10898
* @public
@@ -188,12 +178,21 @@ export default (function create(/** @type {Options} */ defaults) {
188178
url = config.url;
189179
}
190180

181+
const userConfig = /** @type {Options} */ (config || {});
182+
183+
const response = /** @type {Response<any>} */ ({ config: userConfig });
184+
191185
/**
192186
* @type {Options}
193187
*/
194-
const options = deepMerge(defaults, config || {});
188+
const options = deepMerge(defaults, userConfig);
189+
195190
let data = options.data;
196-
url = buildUrl(url, options.params, options.paramsSerializer);
191+
192+
/**
193+
* @type {{'Content-Type':'application/json';Authorization: string} & Headers}
194+
*/
195+
const customHeaders = {};
197196

198197
if (options.transformRequest) {
199198
for (let i = 0; i < options.transformRequest.length; i++) {
@@ -204,12 +203,6 @@ export default (function create(/** @type {Options} */ defaults) {
204203
}
205204
}
206205

207-
const fetchFunc = options.fetch || fetch;
208-
/**
209-
* @type {{'Content-Type':'application/json';Authorization: string} & Headers}
210-
*/
211-
const customHeaders = {};
212-
213206
if (data && typeof data === 'object' && typeof data.append !== 'function') {
214207
data = JSON.stringify(data);
215208
customHeaders['Content-Type'] = 'application/json';
@@ -233,9 +226,15 @@ export default (function create(/** @type {Options} */ defaults) {
233226
url = new URL(url, options.baseURL) + '';
234227
}
235228

236-
/** @type {Response<any>} */
237-
const response = {};
238-
response.config = /** @type {Options} */ (config);
229+
if (options.params) {
230+
const divider = ~url.indexOf('?') ? '&' : '?';
231+
const query = options.paramsSerializer
232+
? options.paramsSerializer(options.params)
233+
: new URLSearchParams(options.params);
234+
url += divider + query;
235+
}
236+
237+
const fetchFunc = options.fetch || fetch;
239238

240239
return fetchFunc(url, {
241240
method: options.method,

test/index.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ describe('redaxios', () => {
176176
expect(window.fetch).toHaveBeenCalledTimes(1);
177177
expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.any(Object));
178178

179-
let params = { a: 1, b: true };
179+
let params;
180+
181+
params = { a: 1, b: true };
180182
axios.get('/foo', { params });
181183
expect(window.fetch).toHaveBeenCalledTimes(2);
182184
expect(window.fetch).toHaveBeenCalledWith('/foo?a=1&b=true', jasmine.any(Object));
@@ -190,7 +192,7 @@ describe('redaxios', () => {
190192
expect(window.fetch).toHaveBeenCalledTimes(4);
191193
expect(window.fetch).toHaveBeenCalledWith('/foo?d=test', jasmine.any(Object));
192194

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

0 commit comments

Comments
 (0)