Skip to content

Commit 7ff8d58

Browse files
committed
Rename responseInterceptors -> responseHnadlers. Add comment to interface.
1 parent b74c685 commit 7ff8d58

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/plugins/es_ui_shared/public/request/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export {
1010
SendRequestConfig,
1111
SendRequestResponse,
1212
sendRequest,
13-
ResponseInterceptor,
13+
ResponseHandler,
1414
} from './send_request';
1515
export { UseRequestConfig, UseRequestResponse, useRequest } from './use_request';

src/plugins/es_ui_shared/public/request/send_request.test.helpers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import {
1818
export interface SendRequestHelpers {
1919
getSendRequestSpy: () => sinon.SinonStub;
2020
sendSuccessRequest: (
21-
responseInterceptors?: SendRequestConfig['responseInterceptors']
21+
responseHandlers?: SendRequestConfig['responseHandlers']
2222
) => Promise<SendRequestResponse>;
2323
getSuccessResponse: () => SendRequestResponse;
2424
sendErrorRequest: (
25-
responseInterceptors?: SendRequestConfig['responseInterceptors']
25+
responseHandlers?: SendRequestConfig['responseHandlers']
2626
) => Promise<SendRequestResponse>;
2727
getErrorResponse: () => SendRequestResponse;
2828
}
@@ -53,8 +53,8 @@ export const createSendRequestHelpers = (): SendRequestHelpers => {
5353
})
5454
)
5555
.resolves(successResponse);
56-
const sendSuccessRequest = (responseInterceptors?: SendRequestConfig['responseInterceptors']) =>
57-
sendRequest({ ...successRequest, responseInterceptors });
56+
const sendSuccessRequest = (responseHandlers?: SendRequestConfig['responseHandlers']) =>
57+
sendRequest({ ...successRequest, responseHandlers });
5858
const getSuccessResponse = () => ({ data: successResponse.data, error: null });
5959

6060
// Set up failed request helpers.
@@ -67,8 +67,8 @@ export const createSendRequestHelpers = (): SendRequestHelpers => {
6767
})
6868
)
6969
.rejects(errorResponse);
70-
const sendErrorRequest = (responseInterceptors?: SendRequestConfig['responseInterceptors']) =>
71-
sendRequest({ ...errorRequest, responseInterceptors });
70+
const sendErrorRequest = (responseHandlers?: SendRequestConfig['responseHandlers']) =>
71+
sendRequest({ ...errorRequest, responseHandlers });
7272
const getErrorResponse = () => ({
7373
data: null,
7474
error: errorResponse.response.data,

src/plugins/es_ui_shared/public/request/send_request.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ describe('sendRequest function', () => {
3434
expect(errorResponse).toEqual(getErrorResponse());
3535
});
3636

37-
it('calls responseInterceptors with successful responses', async () => {
37+
it('calls responseHandlers with successful responses', async () => {
3838
const { sendSuccessRequest, getSuccessResponse } = helpers;
39-
const successInterceptorSpy = sinon.spy();
40-
const successInterceptors = [successInterceptorSpy];
39+
const successHandlerSpy = sinon.spy();
40+
const successHandlers = [successHandlerSpy];
4141

42-
await sendSuccessRequest(successInterceptors);
43-
sinon.assert.calledOnce(successInterceptorSpy);
44-
sinon.assert.calledWith(successInterceptorSpy, getSuccessResponse());
42+
await sendSuccessRequest(successHandlers);
43+
sinon.assert.calledOnce(successHandlerSpy);
44+
sinon.assert.calledWith(successHandlerSpy, getSuccessResponse());
4545
});
4646

47-
it('calls responseInterceptors with errors', async () => {
47+
it('calls responseHandlers with errors', async () => {
4848
const { sendErrorRequest, getErrorResponse } = helpers;
49-
const errorInterceptorSpy = sinon.spy();
50-
const errorInterceptors = [errorInterceptorSpy];
49+
const errorHandlerSpy = sinon.spy();
50+
const errorHandlers = [errorHandlerSpy];
5151

5252
// For some reason sinon isn't throwing an error on rejection, as an awaited Promise normally would.
53-
await sendErrorRequest(errorInterceptors);
54-
sinon.assert.calledOnce(errorInterceptorSpy);
55-
sinon.assert.calledWith(errorInterceptorSpy, getErrorResponse());
53+
await sendErrorRequest(errorHandlers);
54+
sinon.assert.calledOnce(errorHandlerSpy);
55+
sinon.assert.calledWith(errorHandlerSpy, getErrorResponse());
5656
});
5757
});

src/plugins/es_ui_shared/public/request/send_request.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { HttpSetup, HttpFetchQuery } from '../../../../../src/core/public';
1010

11-
export type ResponseInterceptor = ({ data, error }: { data: any; error: any }) => void;
11+
export type ResponseHandler = ({ data, error }: { data: any; error: any }) => void;
1212

1313
export interface SendRequestConfig {
1414
path: string;
@@ -20,7 +20,11 @@ export interface SendRequestConfig {
2020
* HttpFetchOptions#asSystemRequest.
2121
*/
2222
asSystemRequest?: boolean;
23-
responseInterceptors?: ResponseInterceptor[];
23+
/**
24+
* Optional handlers for triggering side effects based on a request's
25+
* successful response or errror. These don't mutate responses or errors.
26+
*/
27+
responseHandlers?: ResponseHandler[];
2428
}
2529

2630
export interface SendRequestResponse<D = any, E = any> {
@@ -30,16 +34,13 @@ export interface SendRequestResponse<D = any, E = any> {
3034

3135
// Pass the response sequentially through each interceptor, allowing for
3236
// side effects to be run.
33-
const updateResponseInterceptors = (
34-
response: any,
35-
responseInterceptors: ResponseInterceptor[] = []
36-
) => {
37-
responseInterceptors.forEach((interceptor) => interceptor(response));
37+
const updateResponseHandlers = (response: any, responseHandlers: ResponseHandler[] = []) => {
38+
responseHandlers.forEach((interceptor) => interceptor(response));
3839
};
3940

4041
export const sendRequest = async <D = any, E = any>(
4142
httpClient: HttpSetup,
42-
{ path, method, body, query, asSystemRequest, responseInterceptors }: SendRequestConfig
43+
{ path, method, body, query, asSystemRequest, responseHandlers }: SendRequestConfig
4344
): Promise<SendRequestResponse<D, E>> => {
4445
try {
4546
const stringifiedBody = typeof body === 'string' ? body : JSON.stringify(body);
@@ -54,15 +55,15 @@ export const sendRequest = async <D = any, E = any>(
5455
error: null,
5556
};
5657

57-
updateResponseInterceptors(response, responseInterceptors);
58+
updateResponseHandlers(response, responseHandlers);
5859
return response;
5960
} catch (e) {
6061
const response = {
6162
data: null,
6263
error: e.response?.data ?? e.body,
6364
};
6465

65-
updateResponseInterceptors(response, responseInterceptors);
66+
updateResponseHandlers(response, responseHandlers);
6667
return response;
6768
}
6869
};

src/plugins/es_ui_shared/public/request/use_request.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const useRequest = <D = any, E = Error>(
3535
pollIntervalMs,
3636
initialData,
3737
deserializer,
38-
responseInterceptors,
38+
responseHandlers,
3939
}: UseRequestConfig
4040
): UseRequestResponse<D, E> => {
4141
const isMounted = useRef(false);
@@ -89,7 +89,7 @@ export const useRequest = <D = any, E = Error>(
8989
// Any requests that are sent in the background (without user interaction) should be flagged as "system requests". This should not be
9090
// confused with any terminology in Elasticsearch. This is a Kibana-specific construct that allows the server to differentiate between
9191
// user-initiated and requests "system"-initiated requests, for purposes like security features.
92-
const requestPayload = { ...requestBody, asSystemRequest, responseInterceptors };
92+
const requestPayload = { ...requestBody, asSystemRequest, responseHandlers };
9393
const response = await sendRequest<D, E>(httpClient, requestPayload);
9494
const { data: serializedResponseData, error: responseError } = response;
9595

@@ -115,7 +115,7 @@ export const useRequest = <D = any, E = Error>(
115115
// Setting isLoading to false also acts as a signal for scheduling the next poll request.
116116
setIsLoading(false);
117117
},
118-
[requestBody, httpClient, deserializer, clearPollInterval, responseInterceptors]
118+
[requestBody, httpClient, deserializer, clearPollInterval, responseHandlers]
119119
);
120120

121121
const scheduleRequest = useCallback(() => {

0 commit comments

Comments
 (0)