3bb6ffddfd96a4e6917a79d1eda84321d1e3b721f5c4c667eb1373e57658950cecd94523481a2261fe0b1b3f0e1b224175a7dbaf442b3cc2b235a4527489b8 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. var utils = require('../utils');
  3. var AxiosURLSearchParams = require('../helpers/AxiosURLSearchParams');
  4. function encode(val) {
  5. return encodeURIComponent(val).
  6. replace(/%3A/gi, ':').
  7. replace(/%24/g, '$').
  8. replace(/%2C/gi, ',').
  9. replace(/%20/g, '+').
  10. replace(/%5B/gi, '[').
  11. replace(/%5D/gi, ']');
  12. }
  13. /**
  14. * Build a URL by appending params to the end
  15. *
  16. * @param {string} url The base of the url (e.g., http://www.google.com)
  17. * @param {object} [params] The params to be appended
  18. * @param {?object} options
  19. * @returns {string} The formatted url
  20. */
  21. module.exports = function buildURL(url, params, options) {
  22. /*eslint no-param-reassign:0*/
  23. if (!params) {
  24. return url;
  25. }
  26. var hashmarkIndex = url.indexOf('#');
  27. if (hashmarkIndex !== -1) {
  28. url = url.slice(0, hashmarkIndex);
  29. }
  30. var _encode = options && options.encode || encode;
  31. var serializeFn = options && options.serialize;
  32. var serializedParams;
  33. if (serializeFn) {
  34. serializedParams = serializeFn(params, options);
  35. } else {
  36. serializedParams = utils.isURLSearchParams(params) ?
  37. params.toString() :
  38. new AxiosURLSearchParams(params, options).toString(_encode);
  39. }
  40. if (serializedParams) {
  41. url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
  42. }
  43. return url;
  44. };