kibana/examples/routing_example/public/services.ts
Brandon Kobel 170a2956c8
Updating the License (#88343)
* Updating the Licenses, except for applying eslint, building

* Applying ESLint rules,building @kbn/pm, regenerating api docs
2021-01-19 17:52:56 -08:00

68 lines
2.1 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/
import { CoreStart, HttpFetchError } from 'kibana/public';
import {
RANDOM_NUMBER_ROUTE_PATH,
RANDOM_NUMBER_BETWEEN_ROUTE_PATH,
POST_MESSAGE_ROUTE_PATH,
INTERNAL_GET_MESSAGE_BY_ID_ROUTE,
} from '../common';
export interface Services {
fetchRandomNumber: () => Promise<number | HttpFetchError>;
fetchRandomNumberBetween: (max: number) => Promise<number | HttpFetchError>;
postMessage: (message: string, id: string) => Promise<undefined | HttpFetchError>;
getMessageById: (id: string) => Promise<string | HttpFetchError>;
addSuccessToast: (message: string) => void;
}
export function getServices(core: CoreStart): Services {
return {
addSuccessToast: (message: string) => core.notifications.toasts.addSuccess(message),
fetchRandomNumber: async () => {
try {
const response = await core.http.fetch<{ randomNumber: number }>(RANDOM_NUMBER_ROUTE_PATH);
return response.randomNumber;
} catch (e) {
return e;
}
},
fetchRandomNumberBetween: async (max: number) => {
try {
const response = await core.http.fetch<{ randomNumber: number }>(
RANDOM_NUMBER_BETWEEN_ROUTE_PATH,
{ query: { max } }
);
return response.randomNumber;
} catch (e) {
return e;
}
},
postMessage: async (message: string, id: string) => {
try {
await core.http.post(`${POST_MESSAGE_ROUTE_PATH}/${id}`, {
body: JSON.stringify({ message }),
});
} catch (e) {
return e;
}
},
getMessageById: async (id: string) => {
try {
const response = await core.http.get<{ message: string }>(
`${INTERNAL_GET_MESSAGE_BY_ID_ROUTE}/${id}`
);
return response.message;
} catch (e) {
return e;
}
},
};
}