From 7ff8d58d35b06c09e726d6d1a3cf0d8c28757427 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Tue, 5 Oct 2021 20:45:46 -0700 Subject: [PATCH] Rename responseInterceptors -> responseHnadlers. Add comment to interface. --- .../es_ui_shared/public/request/index.ts | 2 +- .../request/send_request.test.helpers.ts | 12 +++++----- .../public/request/send_request.test.ts | 24 +++++++++---------- .../public/request/send_request.ts | 21 ++++++++-------- .../public/request/use_request.ts | 6 ++--- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/plugins/es_ui_shared/public/request/index.ts b/src/plugins/es_ui_shared/public/request/index.ts index 02544720a847bd..c3c20ce262544f 100644 --- a/src/plugins/es_ui_shared/public/request/index.ts +++ b/src/plugins/es_ui_shared/public/request/index.ts @@ -10,6 +10,6 @@ export { SendRequestConfig, SendRequestResponse, sendRequest, - ResponseInterceptor, + ResponseHandler, } from './send_request'; export { UseRequestConfig, UseRequestResponse, useRequest } from './use_request'; diff --git a/src/plugins/es_ui_shared/public/request/send_request.test.helpers.ts b/src/plugins/es_ui_shared/public/request/send_request.test.helpers.ts index 4e47835ff2cf51..90c742d7dfb2e5 100644 --- a/src/plugins/es_ui_shared/public/request/send_request.test.helpers.ts +++ b/src/plugins/es_ui_shared/public/request/send_request.test.helpers.ts @@ -18,11 +18,11 @@ import { export interface SendRequestHelpers { getSendRequestSpy: () => sinon.SinonStub; sendSuccessRequest: ( - responseInterceptors?: SendRequestConfig['responseInterceptors'] + responseHandlers?: SendRequestConfig['responseHandlers'] ) => Promise; getSuccessResponse: () => SendRequestResponse; sendErrorRequest: ( - responseInterceptors?: SendRequestConfig['responseInterceptors'] + responseHandlers?: SendRequestConfig['responseHandlers'] ) => Promise; getErrorResponse: () => SendRequestResponse; } @@ -53,8 +53,8 @@ export const createSendRequestHelpers = (): SendRequestHelpers => { }) ) .resolves(successResponse); - const sendSuccessRequest = (responseInterceptors?: SendRequestConfig['responseInterceptors']) => - sendRequest({ ...successRequest, responseInterceptors }); + const sendSuccessRequest = (responseHandlers?: SendRequestConfig['responseHandlers']) => + sendRequest({ ...successRequest, responseHandlers }); const getSuccessResponse = () => ({ data: successResponse.data, error: null }); // Set up failed request helpers. @@ -67,8 +67,8 @@ export const createSendRequestHelpers = (): SendRequestHelpers => { }) ) .rejects(errorResponse); - const sendErrorRequest = (responseInterceptors?: SendRequestConfig['responseInterceptors']) => - sendRequest({ ...errorRequest, responseInterceptors }); + const sendErrorRequest = (responseHandlers?: SendRequestConfig['responseHandlers']) => + sendRequest({ ...errorRequest, responseHandlers }); const getErrorResponse = () => ({ data: null, error: errorResponse.response.data, diff --git a/src/plugins/es_ui_shared/public/request/send_request.test.ts b/src/plugins/es_ui_shared/public/request/send_request.test.ts index 247cc419bba669..7174ac51b64c86 100644 --- a/src/plugins/es_ui_shared/public/request/send_request.test.ts +++ b/src/plugins/es_ui_shared/public/request/send_request.test.ts @@ -34,24 +34,24 @@ describe('sendRequest function', () => { expect(errorResponse).toEqual(getErrorResponse()); }); - it('calls responseInterceptors with successful responses', async () => { + it('calls responseHandlers with successful responses', async () => { const { sendSuccessRequest, getSuccessResponse } = helpers; - const successInterceptorSpy = sinon.spy(); - const successInterceptors = [successInterceptorSpy]; + const successHandlerSpy = sinon.spy(); + const successHandlers = [successHandlerSpy]; - await sendSuccessRequest(successInterceptors); - sinon.assert.calledOnce(successInterceptorSpy); - sinon.assert.calledWith(successInterceptorSpy, getSuccessResponse()); + await sendSuccessRequest(successHandlers); + sinon.assert.calledOnce(successHandlerSpy); + sinon.assert.calledWith(successHandlerSpy, getSuccessResponse()); }); - it('calls responseInterceptors with errors', async () => { + it('calls responseHandlers with errors', async () => { const { sendErrorRequest, getErrorResponse } = helpers; - const errorInterceptorSpy = sinon.spy(); - const errorInterceptors = [errorInterceptorSpy]; + const errorHandlerSpy = sinon.spy(); + const errorHandlers = [errorHandlerSpy]; // For some reason sinon isn't throwing an error on rejection, as an awaited Promise normally would. - await sendErrorRequest(errorInterceptors); - sinon.assert.calledOnce(errorInterceptorSpy); - sinon.assert.calledWith(errorInterceptorSpy, getErrorResponse()); + await sendErrorRequest(errorHandlers); + sinon.assert.calledOnce(errorHandlerSpy); + sinon.assert.calledWith(errorHandlerSpy, getErrorResponse()); }); }); diff --git a/src/plugins/es_ui_shared/public/request/send_request.ts b/src/plugins/es_ui_shared/public/request/send_request.ts index 33d481359cf9e1..51d81bb1bbebd8 100644 --- a/src/plugins/es_ui_shared/public/request/send_request.ts +++ b/src/plugins/es_ui_shared/public/request/send_request.ts @@ -8,7 +8,7 @@ import { HttpSetup, HttpFetchQuery } from '../../../../../src/core/public'; -export type ResponseInterceptor = ({ data, error }: { data: any; error: any }) => void; +export type ResponseHandler = ({ data, error }: { data: any; error: any }) => void; export interface SendRequestConfig { path: string; @@ -20,7 +20,11 @@ export interface SendRequestConfig { * HttpFetchOptions#asSystemRequest. */ asSystemRequest?: boolean; - responseInterceptors?: ResponseInterceptor[]; + /** + * Optional handlers for triggering side effects based on a request's + * successful response or errror. These don't mutate responses or errors. + */ + responseHandlers?: ResponseHandler[]; } export interface SendRequestResponse { @@ -30,16 +34,13 @@ export interface SendRequestResponse { // Pass the response sequentially through each interceptor, allowing for // side effects to be run. -const updateResponseInterceptors = ( - response: any, - responseInterceptors: ResponseInterceptor[] = [] -) => { - responseInterceptors.forEach((interceptor) => interceptor(response)); +const updateResponseHandlers = (response: any, responseHandlers: ResponseHandler[] = []) => { + responseHandlers.forEach((interceptor) => interceptor(response)); }; export const sendRequest = async ( httpClient: HttpSetup, - { path, method, body, query, asSystemRequest, responseInterceptors }: SendRequestConfig + { path, method, body, query, asSystemRequest, responseHandlers }: SendRequestConfig ): Promise> => { try { const stringifiedBody = typeof body === 'string' ? body : JSON.stringify(body); @@ -54,7 +55,7 @@ export const sendRequest = async ( error: null, }; - updateResponseInterceptors(response, responseInterceptors); + updateResponseHandlers(response, responseHandlers); return response; } catch (e) { const response = { @@ -62,7 +63,7 @@ export const sendRequest = async ( error: e.response?.data ?? e.body, }; - updateResponseInterceptors(response, responseInterceptors); + updateResponseHandlers(response, responseHandlers); return response; } }; diff --git a/src/plugins/es_ui_shared/public/request/use_request.ts b/src/plugins/es_ui_shared/public/request/use_request.ts index 8bac129af134c3..03643b5012df0b 100644 --- a/src/plugins/es_ui_shared/public/request/use_request.ts +++ b/src/plugins/es_ui_shared/public/request/use_request.ts @@ -35,7 +35,7 @@ export const useRequest = ( pollIntervalMs, initialData, deserializer, - responseInterceptors, + responseHandlers, }: UseRequestConfig ): UseRequestResponse => { const isMounted = useRef(false); @@ -89,7 +89,7 @@ export const useRequest = ( // Any requests that are sent in the background (without user interaction) should be flagged as "system requests". This should not be // confused with any terminology in Elasticsearch. This is a Kibana-specific construct that allows the server to differentiate between // user-initiated and requests "system"-initiated requests, for purposes like security features. - const requestPayload = { ...requestBody, asSystemRequest, responseInterceptors }; + const requestPayload = { ...requestBody, asSystemRequest, responseHandlers }; const response = await sendRequest(httpClient, requestPayload); const { data: serializedResponseData, error: responseError } = response; @@ -115,7 +115,7 @@ export const useRequest = ( // Setting isLoading to false also acts as a signal for scheduling the next poll request. setIsLoading(false); }, - [requestBody, httpClient, deserializer, clearPollInterval, responseInterceptors] + [requestBody, httpClient, deserializer, clearPollInterval, responseHandlers] ); const scheduleRequest = useCallback(() => {