Skip to content

Commit f6a709d

Browse files
committed
PR feedback: Update custom params arg to take an object instead of a query string
1 parent 74a4149 commit f6a709d

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

x-pack/plugins/enterprise_search/server/lib/enterprise_search_request_handler.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ describe('EnterpriseSearchRequestHandler', () => {
8686
it('passes custom params set by the handler, which override request params', async () => {
8787
const requestHandler = enterpriseSearchRequestHandler.createRequest({
8888
path: '/api/example',
89-
params: '?some=custom&params=true',
89+
params: { someQuery: true },
9090
});
91-
await makeAPICall(requestHandler, { query: { overriden: true } });
91+
await makeAPICall(requestHandler, { query: { someQuery: false } });
9292

9393
EnterpriseSearchAPI.shouldHaveBeenCalledWith(
94-
'http://localhost:3002/api/example?some=custom&params=true'
94+
'http://localhost:3002/api/example?someQuery=true'
9595
);
9696
});
9797
});
@@ -165,6 +165,11 @@ describe('EnterpriseSearchRequestHandler', () => {
165165
});
166166
});
167167
});
168+
169+
it('has a helper for checking empty objects', async () => {
170+
expect(enterpriseSearchRequestHandler.isEmptyObj({})).toEqual(true);
171+
expect(enterpriseSearchRequestHandler.isEmptyObj({ empty: false })).toEqual(false);
172+
});
168173
});
169174

170175
const makeAPICall = (handler: Function, params = {}) => {

x-pack/plugins/enterprise_search/server/lib/enterprise_search_request_handler.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IConstructorDependencies {
2121
}
2222
interface IRequestParams<ResponseBody> {
2323
path: string;
24-
params?: string;
24+
params?: object;
2525
hasValidData?: (body?: ResponseBody) => boolean;
2626
}
2727
export interface IEnterpriseSearchRequestHandler {
@@ -47,7 +47,7 @@ export class EnterpriseSearchRequestHandler {
4747

4848
createRequest<ResponseBody>({
4949
path,
50-
params,
50+
params = {},
5151
hasValidData = () => true,
5252
}: IRequestParams<ResponseBody>) {
5353
return async (
@@ -57,13 +57,16 @@ export class EnterpriseSearchRequestHandler {
5757
) => {
5858
try {
5959
// Set up API URL
60-
params = params ?? (request.query ? `?${querystring.stringify(request.query)}` : '');
61-
const url = encodeURI(this.enterpriseSearchUrl + path + params);
60+
const queryParams = { ...request.query, ...params };
61+
const queryString = !this.isEmptyObj(queryParams)
62+
? `?${querystring.stringify(queryParams)}`
63+
: '';
64+
const url = encodeURI(this.enterpriseSearchUrl + path + queryString);
6265

6366
// Set up API options
6467
const { method } = request.route;
6568
const headers = { Authorization: request.headers.authorization as string };
66-
const body = Object.keys(request.body as object).length
69+
const body = !this.isEmptyObj(request.body as object)
6770
? JSON.stringify(request.body)
6871
: undefined;
6972

@@ -93,4 +96,9 @@ export class EnterpriseSearchRequestHandler {
9396
}
9497
};
9598
}
99+
100+
// Small helper
101+
isEmptyObj(obj: object) {
102+
return Object.keys(obj).length === 0;
103+
}
96104
}

x-pack/plugins/enterprise_search/server/routes/app_search/engines.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('engine routes', () => {
3838

3939
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
4040
path: '/as/engines/collection',
41-
params: encodeURI('?type=indexed&page[current]=1&page[size]=10'),
41+
params: { type: 'indexed', 'page[current]': 1, 'page[size]': 10 },
4242
hasValidData: expect.any(Function),
4343
});
4444
});
@@ -48,7 +48,7 @@ describe('engine routes', () => {
4848

4949
expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
5050
path: '/as/engines/collection',
51-
params: encodeURI('?type=meta&page[current]=99&page[size]=10'),
51+
params: { type: 'meta', 'page[current]': 99, 'page[size]': 10 },
5252
hasValidData: expect.any(Function),
5353
});
5454
});

x-pack/plugins/enterprise_search/server/routes/app_search/engines.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import querystring from 'querystring';
87
import { schema } from '@kbn/config-schema';
98

109
import { IRouteDependencies } from '../../plugin';
@@ -34,11 +33,11 @@ export function registerEnginesRoute({
3433

3534
return enterpriseSearchRequestHandler.createRequest({
3635
path: '/as/engines/collection',
37-
params: `?${querystring.stringify({
36+
params: {
3837
type,
3938
'page[current]': pageIndex,
4039
'page[size]': ENGINES_PAGE_SIZE,
41-
})}`,
40+
},
4241
hasValidData: (body?: IEnginesResponse) =>
4342
Array.isArray(body?.results) && typeof body?.meta?.page?.total_results === 'number',
4443
})(context, request, response);

0 commit comments

Comments
 (0)