@fluid-topics/ft-wc-utils 1.2.46 → 1.2.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,9 +28,9 @@ export declare class SearchPlaceConverter {
28
28
  constructor(baseUrl: string, predefinedSorts?: Record<string, Array<FtSearchSortCriterion>>, defaultPerPage?: number, allLanguagesAllowed?: boolean, defaultContentLocale?: string);
29
29
  serialize(request: EnrichedFtSearchRequest, searchPage?: string): string;
30
30
  serializeToCurrentPageIfPossible(request: EnrichedFtSearchRequest, searchPage?: string): string;
31
- toURLSearchParams(request: EnrichedFtSearchRequest, omitContentLocaleIfDefault?: boolean): URLSearchParams;
31
+ toURLSearchParams(request: EnrichedFtSearchRequest, omitContentLocaleIfDefault?: boolean): string;
32
32
  parse(url: string): EnrichedFtSearchRequest;
33
- fromURLSearchParams(params: URLSearchParams): EnrichedFtSearchRequest;
33
+ fromURLSearchParams(strParams: string): EnrichedFtSearchRequest;
34
34
  private parseCompatFilters;
35
35
  private splitUnknownFilter;
36
36
  private parseValueFilters;
@@ -52,10 +52,9 @@ export class SearchPlaceConverter {
52
52
  return this.serializeToCurrentPageIfPossible(request, searchPage !== null && searchPage !== void 0 ? searchPage : "/search");
53
53
  }
54
54
  serializeToCurrentPageIfPossible(request, searchPage) {
55
- const queryParams = this.toURLSearchParams(request).toString();
56
55
  const useCurrenLocation = searchPage == null || searchPage.trim().length === 0;
57
56
  const url = new URL(useCurrenLocation ? window.location.href : `${this.baseUrl.replace(/\/+$/, "")}${searchPage}`);
58
- url.search = queryParams;
57
+ url.search = this.toURLSearchParams(request);
59
58
  return url.toString();
60
59
  }
61
60
  toURLSearchParams(request, omitContentLocaleIfDefault = false) {
@@ -70,12 +69,7 @@ export class SearchPlaceConverter {
70
69
  params.append(SearchPlaceQueryParams.LOCALE, this.allLanguagesAllowed ? "all" : this.defaultContentLocale);
71
70
  }
72
71
  if (((_a = request.query) === null || _a === void 0 ? void 0 : _a.length) > 0) {
73
- // Explicitly encode query. This leads to the serialized query to be encoded twice (e.g. "one two" is
74
- // encoded into "one%2520two" rather than "one+two" or "one%20two").
75
- // This is necessary as it is decoded twice when loading the search page. Encoding only once causes
76
- // errors if the query contains a '%' character, as the second decoding treats it as the start of an
77
- // encoded character.
78
- params.append(SearchPlaceQueryParams.QUERY, encodeURIComponent(request.query));
72
+ params.append(SearchPlaceQueryParams.QUERY, request.query);
79
73
  }
80
74
  if (request.scope && request.scope !== "DEFAULT") {
81
75
  params.append(SearchPlaceQueryParams.SCOPE, request.scope.toLowerCase());
@@ -92,7 +86,7 @@ export class SearchPlaceConverter {
92
86
  var _a, _b;
93
87
  const values = ((_b = (_a = f.valueFilter) === null || _a === void 0 ? void 0 : _a.values) !== null && _b !== void 0 ? _b : [])
94
88
  .map(value => this.escapeFilters(value))
95
- .map(value => encodeURIComponent(quote(unquote(value))))
89
+ .map(value => quote(unquote(value)))
96
90
  .join("_");
97
91
  return `${f.key}~${values}`;
98
92
  }).join("*");
@@ -117,17 +111,25 @@ export class SearchPlaceConverter {
117
111
  if (((_k = request.paging) === null || _k === void 0 ? void 0 : _k.perPage) && ((_m = (_l = request.paging) === null || _l === void 0 ? void 0 : _l.perPage) !== null && _m !== void 0 ? _m : this.defaultPerPage) != this.defaultPerPage) {
118
112
  params.append(SearchPlaceQueryParams.PER_PAGE, String(request.paging.perPage));
119
113
  }
120
- return params;
114
+ // GWT needs the query string to be encoded twice 🙄
115
+ return encodeURI(params.toString()).replace(/#/g, "%23");
121
116
  }
122
117
  parse(url) {
123
118
  var _a;
124
119
  const urlSplit = url.split("?");
125
- const fromParams = this.fromURLSearchParams(new URLSearchParams(urlSplit.slice(1).join("?")));
120
+ const fromParams = this.fromURLSearchParams(urlSplit.slice(1).join("?"));
126
121
  const maybeScopeFromPath = urlSplit[0].split("/search/")[1];
127
122
  return maybeScopeFromPath == null ? fromParams : { ...fromParams, scope: (_a = this.scopeMapping[maybeScopeFromPath]) !== null && _a !== void 0 ? _a : fromParams.scope };
128
123
  }
129
- fromURLSearchParams(params) {
124
+ fromURLSearchParams(strParams) {
130
125
  var _a, _b, _c, _d, _e, _f, _g, _h;
126
+ try {
127
+ strParams = decodeURI(strParams).replace(/%23/g, "#");
128
+ }
129
+ catch (e) {
130
+ console.warn("Could not decode search request query params, fallback on raw value", e);
131
+ }
132
+ const params = new URLSearchParams(strParams);
131
133
  const contentLocale = params.get(SearchPlaceQueryParams.LOCALE) === "all" ? undefined : params.get(SearchPlaceQueryParams.LOCALE);
132
134
  const virtualField = ((_a = params.get(SearchPlaceQueryParams.VIRTUAL_FIELD)) !== null && _a !== void 0 ? _a : "EVERYWHERE").toUpperCase();
133
135
  const sortType = ((_b = params.get(SearchPlaceQueryParams.SORT)) !== null && _b !== void 0 ? _b : "DEFAULT").toUpperCase();
@@ -141,7 +143,7 @@ export class SearchPlaceConverter {
141
143
  }
142
144
  return {
143
145
  contentLocale: contentLocale !== null && contentLocale !== void 0 ? contentLocale : (this.allLanguagesAllowed ? undefined : this.defaultContentLocale),
144
- query: decodeURIComponent((_e = params.get(SearchPlaceQueryParams.QUERY)) !== null && _e !== void 0 ? _e : ""),
146
+ query: (_e = params.get(SearchPlaceQueryParams.QUERY)) !== null && _e !== void 0 ? _e : "",
145
147
  scope: (_f = this.scopeMapping[scope]) !== null && _f !== void 0 ? _f : "DEFAULT",
146
148
  virtualField: isFtVirtualField(virtualField) ? virtualField : "EVERYWHERE",
147
149
  sort: (_h = (_g = this.predefinedSorts[sortType]) !== null && _g !== void 0 ? _g : this.predefinedSorts["DEFAULT"]) !== null && _h !== void 0 ? _h : [],
@@ -174,7 +176,7 @@ export class SearchPlaceConverter {
174
176
  ];
175
177
  }
176
178
  splitUnknownFilter(s) {
177
- return this.superEscapeFilters(decodeURIComponent(s))
179
+ return this.superEscapeFilters(s)
178
180
  .split("*")
179
181
  .map(oneFilter => oneFilter.split("~"));
180
182
  }