From 787716ccbacca9a7f4b0d0c4502c8b0b30234c5a Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 30 Dec 2021 12:37:17 -0500 Subject: [PATCH] added api setup --- webapp/src/services/automations.ts | 17 +++++++++++++++ webapp/src/services/base.ts | 33 ++++++++++++++++++++++++++++++ webapp/src/services/index.ts | 5 +++++ 3 files changed, 55 insertions(+) create mode 100644 webapp/src/services/automations.ts create mode 100644 webapp/src/services/base.ts create mode 100644 webapp/src/services/index.ts 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);