queryString.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import isString from 'lodash/isString';
  2. import * as qs from 'query-string';
  3. import {escapeDoubleQuotes} from 'sentry/utils';
  4. // remove leading and trailing whitespace and remove double spaces
  5. export function formatQueryString(query: string): string {
  6. return query.trim().replace(/\s+/g, ' ');
  7. }
  8. export function addQueryParamsToExistingUrl(
  9. origUrl: string,
  10. queryParams: object
  11. ): string {
  12. let url;
  13. try {
  14. url = new URL(origUrl);
  15. } catch {
  16. return '';
  17. }
  18. const searchEntries = url.searchParams.entries();
  19. // Order the query params alphabetically.
  20. // Otherwise ``queryString`` orders them randomly and it's impossible to test.
  21. const params = JSON.parse(JSON.stringify(queryParams));
  22. const query = {...Object.fromEntries(searchEntries), ...params};
  23. return `${url.protocol}//${url.host}${url.pathname}?${qs.stringify(query)}`;
  24. }
  25. type QueryValue = string | string[] | undefined | null;
  26. /**
  27. * Append a tag key:value to a query string.
  28. *
  29. * Handles spacing and quoting if necessary.
  30. */
  31. export function appendTagCondition(
  32. query: QueryValue,
  33. key: string,
  34. value: null | string
  35. ): string {
  36. let currentQuery = Array.isArray(query) ? query.pop() : isString(query) ? query : '';
  37. if (typeof value === 'string' && /[:\s\(\)\\"]/g.test(value)) {
  38. value = `"${escapeDoubleQuotes(value)}"`;
  39. }
  40. if (currentQuery) {
  41. currentQuery += ` ${key}:${value}`;
  42. } else {
  43. currentQuery = `${key}:${value}`;
  44. }
  45. return currentQuery;
  46. }
  47. // This function has multiple signatures to help with typing in callers.
  48. export function decodeScalar(value: QueryValue): string | undefined;
  49. export function decodeScalar(value: QueryValue, fallback: string): string;
  50. export function decodeScalar(value: QueryValue, fallback?: string): string | undefined {
  51. if (!value) {
  52. return fallback;
  53. }
  54. const unwrapped =
  55. Array.isArray(value) && value.length > 0
  56. ? value[0]
  57. : isString(value)
  58. ? value
  59. : fallback;
  60. return isString(unwrapped) ? unwrapped : fallback;
  61. }
  62. export function decodeList(value: string[] | string | undefined | null): string[] {
  63. if (!value) {
  64. return [];
  65. }
  66. return Array.isArray(value) ? value : isString(value) ? [value] : [];
  67. }
  68. export function decodeInteger(value: QueryValue, fallback?: number): number | undefined {
  69. const unwrapped = decodeScalar(value);
  70. if (unwrapped === undefined) {
  71. return fallback;
  72. }
  73. const parsed = parseInt(unwrapped, 10);
  74. if (isFinite(parsed)) {
  75. return parsed;
  76. }
  77. return fallback;
  78. }
  79. export default {
  80. decodeInteger,
  81. decodeList,
  82. decodeScalar,
  83. formatQueryString,
  84. addQueryParamsToExistingUrl,
  85. appendTagCondition,
  86. };