diff --git a/webapp/src/services/automations.ts b/webapp/src/services/automations.ts new file mode 100644 index 0000000..e0954a1 --- /dev/null +++ b/webapp/src/services/automations.ts @@ -0,0 +1,17 @@ +import { AutomationData } from "types/automations"; +import { API } from "./base"; + + +export const makeAutomationAPI = (api: API) => ({ + async list(offset: number, limit: number) { + return await api.makeCall({ + path: "/automations/list", + data: { + offset, limit, + } + }); + } +}) + + +export type AutomationAPI = ReturnType; diff --git a/webapp/src/services/base.ts b/webapp/src/services/base.ts new file mode 100644 index 0000000..ec17fbd --- /dev/null +++ b/webapp/src/services/base.ts @@ -0,0 +1,33 @@ +export interface API { + makeCall: (req: APIRequest) => Promise>; +} +export type APIRequest = { + path: string; + data: D; +}; +export type APIResponse = { + ok: true; + data: D; +} | { + ok: false; + error: string; +} + +export const makeRemoteAPI = (baseURL: string): API => ({ + async makeCall (req) { + const reply = await fetch(baseURL + "/" + req.path, { + method: "POST", + body: JSON.stringify(req.data), + }); + if (reply.status === 200) { + return { + ok: true, + data: await reply.json(), + } + } + return { + ok: false, + error: await reply.text(), + } + } +}) diff --git a/webapp/src/services/index.ts b/webapp/src/services/index.ts new file mode 100644 index 0000000..4c48807 --- /dev/null +++ b/webapp/src/services/index.ts @@ -0,0 +1,5 @@ +import { makeAutomationAPI, AutomationAPI } from './automations'; +import { makeRemoteAPI } from "./base"; + +const api = makeRemoteAPI("/api/v1"); +export const automationAPI: AutomationAPI = makeAutomationAPI(api);