Skip to content

Commit

Permalink
improve api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Feb 10, 2021
1 parent c0d88bc commit 2cccde7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
46 changes: 32 additions & 14 deletions src/plugins/data/public/search/search_interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,23 @@ describe('SearchInterceptor', () => {
});

describe('Search session', () => {
const setup = ({
isRestore = false,
isStored = false,
sessionId,
}: {
isRestore?: boolean;
isStored?: boolean;
sessionId: string;
}) => {
const setup = (
opts: {
isRestore?: boolean;
isStored?: boolean;
sessionId: string;
} | null
) => {
const sessionServiceMock = searchMock.session as jest.Mocked<ISessionService>;
sessionServiceMock.getSearchOptions.mockImplementation(() => ({
sessionId,
isRestore,
isStored,
}));
sessionServiceMock.getSearchOptions.mockImplementation(() =>
opts
? {
sessionId: opts.sessionId,
isRestore: opts.isRestore ?? false,
isStored: opts.isStored ?? false,
}
: null
);
fetchMock.mockResolvedValue({ result: 200 });
};

Expand Down Expand Up @@ -142,6 +144,22 @@ describe('SearchInterceptor', () => {
(searchMock.session as jest.Mocked<ISessionService>).getSearchOptions
).toHaveBeenCalledWith(sessionId);
});

test("doesn't forward sessionId if search options return null", async () => {
const sessionId = 'sid';
setup(null);

await searchInterceptor.search(mockRequest, { sessionId }).toPromise();
expect(fetchMock.mock.calls[0][0]).toEqual(
expect.not.objectContaining({
options: { sessionId },
})
);

expect(
(searchMock.session as jest.Mocked<ISessionService>).getSearchOptions
).toHaveBeenCalledWith(sessionId);
});
});

describe('Should throw typed errors', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/search/search_interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ export class SearchInterceptor {
request: IKibanaSearchRequest,
options?: ISearchOptions
): Promise<IKibanaSearchResponse> {
const { abortSignal, ...requestOptions } = options || {};
const { abortSignal, sessionId, ...requestOptions } = options || {};

return this.batchedFetch(
{
request,
options: {
...requestOptions,
...(options?.sessionId && this.deps.session.getSearchOptions(options.sessionId)),
...this.deps.session.getSearchOptions(sessionId),
},
},
abortSignal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ describe('Session service', () => {
isRestore: true,
sessionId,
});

expect(sessionService.getSearchOptions(undefined)).toBeNull();
});
test('isCurrentSession', () => {
expect(sessionService.isCurrentSession()).toBeFalsy();
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data/public/search/session/session_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,12 @@ export class SessionService {
* @param sessionId
*/
public getSearchOptions(
sessionId: string
sessionId?: string
): Required<Pick<ISearchOptions, 'sessionId' | 'isRestore' | 'isStored'>> | null {
if (!sessionId) {
return null;
}

// in case user doesn't have permissions to search session, do not forward sessionId to the server
// because user most likely also doesn't have access to `search-session` SO
if (!this.hasAccessToSearchSessions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ export function getTimelionRequestHandler({
});

try {
const searchSessionOptions =
searchSessionId && dataSearch.session.getSearchOptions(searchSessionId);
const searchSessionOptions = dataSearch.session.getSearchOptions(searchSessionId);
return await http.post('/api/timelion/run', {
body: JSON.stringify({
sheet: [expression],
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/vis_type_timeseries/public/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export const metricsRequestHandler = async ({
});

try {
const searchSessionOptions =
searchSessionId && dataSearch.session.getSearchOptions(searchSessionId);
const searchSessionOptions = dataSearch.session.getSearchOptions(searchSessionId);
return await getCoreStart().http.post(ROUTES.VIS_DATA, {
body: JSON.stringify({
timerange: {
Expand Down

0 comments on commit 2cccde7

Please sign in to comment.