Skip to content

Commit

Permalink
fix(resource): allow empty string as query param value
Browse files Browse the repository at this point in the history
  • Loading branch information
gdostie committed Sep 29, 2023
1 parent 3a71f44 commit de4752d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 173 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
40 changes: 33 additions & 7 deletions src/resources/Search/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,33 @@ export default class Search extends Ressource {
* Get an item's preview in HTML format
*/
previewHTML(params: ItemPreviewHtmlParameters) {
return this.api.get<string>(
const {
enableNavigation,
findNext,
findPrevious,
organizationId,
page,
requestedOutputSize,
uniqueId,
viewAllContent,
...body
} = params;
const queryParams = {
enableNavigation,
findNext,
findPrevious,
organizationId,
page,
requestedOutputSize,
uniqueId,
viewAllContent,
};
return this.api.post<string>(
this.buildPath(`${Search.baseUrl}/html`, {
...params,
organizationId: params.organizationId ?? this.api.organizationId,
...queryParams,
organizationId: queryParams.organizationId ?? this.api.organizationId,
}),
body,
{responseBodyFormat: 'text'},
);
}
Expand All @@ -102,10 +124,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 de4752d

Please sign in to comment.