diff --git a/src/plugins/es_ui_shared/public/request/np_ready_request.ts b/src/plugins/es_ui_shared/public/request/np_ready_request.ts index 138e31ff53ca50..ec0db8c8e969e3 100644 --- a/src/plugins/es_ui_shared/public/request/np_ready_request.ts +++ b/src/plugins/es_ui_shared/public/request/np_ready_request.ts @@ -21,19 +21,18 @@ import { useEffect, useState, useRef } from 'react'; import { HttpServiceBase } from '../../../../../src/core/public'; +export interface SendRequestConfig { + path: string; + method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'; + body?: any; +} + export interface SendRequestResponse { data: any; error: Error | null; } -export interface SendRequestOptions { - method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'; - body?: any; -} - -export interface UseRequestConfig { - path: string; - requestOptions: SendRequestOptions; +export interface UseRequestConfig extends SendRequestConfig { pollIntervalMs?: number; initialData?: any; deserializer?: (data: any) => any; @@ -49,11 +48,10 @@ export interface UseRequestResponse { export const sendRequest = async ( httpClient: HttpServiceBase, - path: string, - requestOptions: SendRequestOptions + { path, method, body }: SendRequestConfig ): Promise => { try { - const response = await httpClient.fetch(path, requestOptions); + const response = await httpClient[method](path, { body }); return { data: response, error: null }; } catch (e) { @@ -68,7 +66,8 @@ export const useRequest = ( httpClient: HttpServiceBase, { path, - requestOptions, + method, + body, pollIntervalMs, initialData, deserializer = (data: any): any => data, @@ -107,7 +106,13 @@ export const useRequest = ( // "old" error/data or loading state when a new request is in-flight. setIsLoading(true); - const response = await sendRequest(httpClient, path, requestOptions); + const requestBody = { + path, + method, + body, + }; + + const response = await sendRequest(httpClient, requestBody); const { data: serializedResponseData, error: responseError } = response; const responseData = deserializer(serializedResponseData); diff --git a/x-pack/legacy/plugins/index_management/public/app/services/api.ts b/x-pack/legacy/plugins/index_management/public/app/services/api.ts index 4da5818695ad6e..1b6c604d6c71f6 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/api.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/api.ts @@ -194,19 +194,15 @@ export async function loadIndexData(type: string, indexName: string) { export function loadIndexTemplates() { return useRequest({ path: `${API_BASE_PATH}/templates`, - requestOptions: { - method: 'get', - }, + method: 'get', }); } export async function deleteTemplates(names: Array) { - const result = sendRequest( - `${API_BASE_PATH}/templates/${names.map(name => encodeURIComponent(name)).join(',')}`, - { - method: 'delete', - } - ); + const result = sendRequest({ + path: `${API_BASE_PATH}/templates/${names.map(name => encodeURIComponent(name)).join(',')}`, + method: 'delete', + }); const uimActionType = names.length > 1 ? UIM_TEMPLATE_DELETE_MANY : UIM_TEMPLATE_DELETE; @@ -218,14 +214,13 @@ export async function deleteTemplates(names: Array) { export function loadIndexTemplate(name: Template['name']) { return useRequest({ path: `${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, - requestOptions: { - method: 'get', - }, + method: 'get', }); } export async function saveTemplate(template: Template, isClone?: boolean) { - const result = sendRequest(`${API_BASE_PATH}/templates`, { + const result = await sendRequest({ + path: `${API_BASE_PATH}/templates`, method: 'put', body: JSON.stringify(template), }); @@ -239,7 +234,8 @@ export async function saveTemplate(template: Template, isClone?: boolean) { export async function updateTemplate(template: Template) { const { name } = template; - const result = sendRequest(`${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, { + const result = await sendRequest({ + path: `${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, method: 'put', body: JSON.stringify(template), }); @@ -250,7 +246,8 @@ export async function updateTemplate(template: Template) { } export async function loadTemplateToClone(name: Template['name']) { - return sendRequest(`${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, { + return await sendRequest({ + path: `${API_BASE_PATH}/templates/${encodeURIComponent(name)}`, method: 'get', }); } diff --git a/x-pack/legacy/plugins/index_management/public/app/services/use_request.ts b/x-pack/legacy/plugins/index_management/public/app/services/use_request.ts index d4292e3ea52784..8ede3d196911cc 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/use_request.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/use_request.ts @@ -5,7 +5,7 @@ */ import { - SendRequestOptions, + SendRequestConfig, SendRequestResponse, UseRequestConfig, sendRequest as _sendRequest, @@ -14,11 +14,8 @@ import { import { httpService } from './http'; -export const sendRequest = ( - path: string, - options: SendRequestOptions -): Promise => { - return _sendRequest(httpService.httpClient, path, options); +export const sendRequest = (config: SendRequestConfig): Promise => { + return _sendRequest(httpService.httpClient, config); }; export const useRequest = (config: UseRequestConfig) => { diff --git a/x-pack/legacy/plugins/index_management/public/shared_imports.ts b/x-pack/legacy/plugins/index_management/public/shared_imports.ts index a9a97acefc14fb..cbc4dde7448ff0 100644 --- a/x-pack/legacy/plugins/index_management/public/shared_imports.ts +++ b/x-pack/legacy/plugins/index_management/public/shared_imports.ts @@ -5,7 +5,7 @@ */ export { - SendRequestOptions, + SendRequestConfig, SendRequestResponse, UseRequestConfig, sendRequest,