queryString.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. export function appendExcludeTagValuesCondition(
  48. query: QueryValue,
  49. key: string,
  50. values: string[]
  51. ): string {
  52. let currentQuery = Array.isArray(query) ? query.pop() : isString(query) ? query : '';
  53. const filteredValuesCondition = `[${values
  54. .map(value => {
  55. if (typeof value === 'string' && /[\s"]/g.test(value)) {
  56. value = `"${escapeDoubleQuotes(value)}"`;
  57. }
  58. return value;
  59. })
  60. .join(', ')}]`;
  61. if (currentQuery) {
  62. currentQuery += ` !${key}:${filteredValuesCondition}`;
  63. } else {
  64. currentQuery = `!${key}:${filteredValuesCondition}`;
  65. }
  66. return currentQuery;
  67. }
  68. // This function has multiple signatures to help with typing in callers.
  69. export function decodeScalar(value: QueryValue): string | undefined;
  70. export function decodeScalar(value: QueryValue, fallback: string): string;
  71. export function decodeScalar(value: QueryValue, fallback?: string): string | undefined {
  72. if (!value) {
  73. return fallback;
  74. }
  75. const unwrapped =
  76. Array.isArray(value) && value.length > 0
  77. ? value[0]
  78. : isString(value)
  79. ? value
  80. : fallback;
  81. return isString(unwrapped) ? unwrapped : fallback;
  82. }
  83. export function decodeList(value: string[] | string | undefined | null): string[] {
  84. if (!value) {
  85. return [];
  86. }
  87. return Array.isArray(value) ? value : isString(value) ? [value] : [];
  88. }
  89. // This function has multiple signatures to help with typing in callers.
  90. export function decodeInteger(value: QueryValue): number | undefined;
  91. export function decodeInteger(value: QueryValue, fallback: number): number;
  92. export function decodeInteger(value: QueryValue, fallback?: number): number | undefined {
  93. const unwrapped = decodeScalar(value);
  94. if (unwrapped === undefined) {
  95. return fallback;
  96. }
  97. const parsed = parseInt(unwrapped, 10);
  98. if (isFinite(parsed)) {
  99. return parsed;
  100. }
  101. return fallback;
  102. }
  103. const queryString = {
  104. decodeInteger,
  105. decodeList,
  106. decodeScalar,
  107. formatQueryString,
  108. addQueryParamsToExistingUrl,
  109. appendTagCondition,
  110. appendExcludeTagValuesCondition,
  111. };
  112. export default queryString;