Skip to content

Commit

Permalink
PR feedback: Update custom params arg to take an object instead of a …
Browse files Browse the repository at this point in the history
…query string
  • Loading branch information
cee-chen committed Aug 27, 2020
1 parent 74a4149 commit f6a709d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ describe('EnterpriseSearchRequestHandler', () => {
it('passes custom params set by the handler, which override request params', async () => {
const requestHandler = enterpriseSearchRequestHandler.createRequest({
path: '/api/example',
params: '?some=custom&params=true',
params: { someQuery: true },
});
await makeAPICall(requestHandler, { query: { overriden: true } });
await makeAPICall(requestHandler, { query: { someQuery: false } });

EnterpriseSearchAPI.shouldHaveBeenCalledWith(
'http://localhost:3002/api/example?some=custom&params=true'
'http://localhost:3002/api/example?someQuery=true'
);
});
});
Expand Down Expand Up @@ -165,6 +165,11 @@ describe('EnterpriseSearchRequestHandler', () => {
});
});
});

it('has a helper for checking empty objects', async () => {
expect(enterpriseSearchRequestHandler.isEmptyObj({})).toEqual(true);
expect(enterpriseSearchRequestHandler.isEmptyObj({ empty: false })).toEqual(false);
});
});

const makeAPICall = (handler: Function, params = {}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface IConstructorDependencies {
}
interface IRequestParams<ResponseBody> {
path: string;
params?: string;
params?: object;
hasValidData?: (body?: ResponseBody) => boolean;
}
export interface IEnterpriseSearchRequestHandler {
Expand All @@ -47,7 +47,7 @@ export class EnterpriseSearchRequestHandler {

createRequest<ResponseBody>({
path,
params,
params = {},
hasValidData = () => true,
}: IRequestParams<ResponseBody>) {
return async (
Expand All @@ -57,13 +57,16 @@ export class EnterpriseSearchRequestHandler {
) => {
try {
// Set up API URL
params = params ?? (request.query ? `?${querystring.stringify(request.query)}` : '');
const url = encodeURI(this.enterpriseSearchUrl + path + params);
const queryParams = { ...request.query, ...params };
const queryString = !this.isEmptyObj(queryParams)
? `?${querystring.stringify(queryParams)}`
: '';
const url = encodeURI(this.enterpriseSearchUrl + path + queryString);

// Set up API options
const { method } = request.route;
const headers = { Authorization: request.headers.authorization as string };
const body = Object.keys(request.body as object).length
const body = !this.isEmptyObj(request.body as object)
? JSON.stringify(request.body)
: undefined;

Expand Down Expand Up @@ -93,4 +96,9 @@ export class EnterpriseSearchRequestHandler {
}
};
}

// Small helper
isEmptyObj(obj: object) {
return Object.keys(obj).length === 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('engine routes', () => {

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

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/engines/collection',
params: encodeURI('?type=meta&page[current]=99&page[size]=10'),
params: { type: 'meta', 'page[current]': 99, 'page[size]': 10 },
hasValidData: expect.any(Function),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

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

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

return enterpriseSearchRequestHandler.createRequest({
path: '/as/engines/collection',
params: `?${querystring.stringify({
params: {
type,
'page[current]': pageIndex,
'page[size]': ENGINES_PAGE_SIZE,
})}`,
},
hasValidData: (body?: IEnginesResponse) =>
Array.isArray(body?.results) && typeof body?.meta?.page?.total_results === 'number',
})(context, request, response);
Expand Down

0 comments on commit f6a709d

Please sign in to comment.