Skip to content

Commit

Permalink
clean up request logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Nov 6, 2019
1 parent 1be3a10 commit e31dba7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
31 changes: 18 additions & 13 deletions src/plugins/es_ui_shared/public/request/np_ready_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,11 +48,10 @@ export interface UseRequestResponse {

export const sendRequest = async (
httpClient: HttpServiceBase,
path: string,
requestOptions: SendRequestOptions
{ path, method, body }: SendRequestConfig
): Promise<SendRequestResponse> => {
try {
const response = await httpClient.fetch(path, requestOptions);
const response = await httpClient[method](path, { body });

return { data: response, error: null };
} catch (e) {
Expand All @@ -68,7 +66,8 @@ export const useRequest = (
httpClient: HttpServiceBase,
{
path,
requestOptions,
method,
body,
pollIntervalMs,
initialData,
deserializer = (data: any): any => data,
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 12 additions & 15 deletions x-pack/legacy/plugins/index_management/public/app/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Template['name']>) {
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;

Expand All @@ -218,14 +214,13 @@ export async function deleteTemplates(names: Array<Template['name']>) {
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),
});
Expand All @@ -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),
});
Expand All @@ -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',
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {
SendRequestOptions,
SendRequestConfig,
SendRequestResponse,
UseRequestConfig,
sendRequest as _sendRequest,
Expand All @@ -14,11 +14,8 @@ import {

import { httpService } from './http';

export const sendRequest = (
path: string,
options: SendRequestOptions
): Promise<SendRequestResponse> => {
return _sendRequest(httpService.httpClient, path, options);
export const sendRequest = (config: SendRequestConfig): Promise<SendRequestResponse> => {
return _sendRequest(httpService.httpClient, config);
};

export const useRequest = (config: UseRequestConfig) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

export {
SendRequestOptions,
SendRequestConfig,
SendRequestResponse,
UseRequestConfig,
sendRequest,
Expand Down

0 comments on commit e31dba7

Please sign in to comment.