Skip to content

Commit

Permalink
fix(resource): allow empty string as query param value (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdostie authored Oct 6, 2023
1 parent d9b5336 commit 4b39e9e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 178 deletions.
10 changes: 6 additions & 4 deletions src/resources/Resource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import API from '../APICore.js';
import queryString from '#query-string';

const defaultOptions: queryString.StringifyOptions = {skipEmptyString: true, skipNull: true, sort: false};

class Resource {
static baseUrl: string;

Expand All @@ -9,15 +11,15 @@ class Resource {
protected serverlessApi: API,
) {}

protected buildPath(route: string, parameters?: any): string {
return route + this.convertObjectToQueryString(parameters);
protected buildPath(route: string, parameters?: any, options?: queryString.StringifyOptions): string {
return route + this.convertObjectToQueryString(parameters, options);
}

private convertObjectToQueryString(parameters: any): string {
private convertObjectToQueryString(parameters: any, userOptions?: queryString.StringifyOptions): string {
if (!parameters) {
return '';
} else {
const requestURL = queryString.stringify(parameters, {skipEmptyString: true, skipNull: true, sort: false});
const requestURL = queryString.stringify(parameters, {...defaultOptions, ...userOptions});
return requestURL.length ? `?${requestURL}` : '';
}
}
Expand Down
45 changes: 33 additions & 12 deletions src/resources/Search/Search.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {ListFieldValuesBodyQueryParams, PostSearchQuerySuggestBodyParams} from './index.js';
import API from '../../APICore.js';
import Ressource from '../Resource.js';
import {
SingleItemParameters,
ItemPreviewHtmlParameters,
RestFacetSearchParameters,
RestFacetSearchResponse,
RestQueryParams,
RestQueryResult,
RestTokenParams,
SearchListFieldsParams,
SearchListFieldsResponse,
SearchResponse,
SingleItemParameters,
TokenModel,
RestFacetSearchParameters,
RestFacetSearchResponse,
} from './SearchInterfaces.js';
import {ListFieldValuesBodyQueryParams, PostSearchQuerySuggestBodyParams} from './index.js';

export default class Search extends Ressource {
static baseUrl = `/rest/search/v2`;
Expand Down Expand Up @@ -87,12 +87,29 @@ export default class Search extends Ressource {
/**
* Get an item's preview in HTML format
*/
previewHTML(params: ItemPreviewHtmlParameters) {
return this.api.get<string>(
previewHTML({
enableNavigation,
findNext,
findPrevious,
organizationId,
page,
requestedOutputSize,
uniqueId,
viewAllContent,
...body
}: ItemPreviewHtmlParameters) {
return this.api.post<string>(
this.buildPath(`${Search.baseUrl}/html`, {
...params,
organizationId: params.organizationId ?? this.api.organizationId,
enableNavigation,
findNext,
findPrevious,
page,
requestedOutputSize,
uniqueId,
viewAllContent,
organizationId: organizationId ?? this.api.organizationId,
}),
body,
{responseBodyFormat: 'text'},
);
}
Expand All @@ -102,10 +119,14 @@ export default class Search extends Ressource {
*/
getDocument(params: SingleItemParameters) {
return this.api.get<RestQueryResult>(
this.buildPath(`${Search.baseUrl}/document`, {
...params,
organizationId: params.organizationId ?? this.api.organizationId,
}),
this.buildPath(
`${Search.baseUrl}/document`,
{
...params,
organizationId: params.organizationId ?? this.api.organizationId,
},
{skipEmptyString: false}, // otherwise we cannot use the empty pipeline (`pipeline=`)
),
);
}

Expand Down
Loading

0 comments on commit 4b39e9e

Please sign in to comment.