added api setup

This commit is contained in:
ari committed 2021-12-30 12:37:30 -05:00
1 parent 787716ccba
commit 4a08956e1a
21 files changed
+12292 -12527

No files matched your search

-17
View File
@@ -1,17 +0,0 @@
import { AutomationData } from "types/automations";
import { API } from "./base";
export const makeAutomationAPI = (api: API) => ({
async list(offset: number, limit: number) {
return await api.makeCall<AutomationData[]>({
path: "/automations/list",
data: {
offset, limit,
}
});
}
})
export type AutomationAPI = ReturnType<typeof makeAutomationAPI>;
-33
View File
@@ -1,33 +0,0 @@
export interface API {
makeCall: <Res = {}, Req = {}>(req: APIRequest<Req>) => Promise<APIResponse<Res>>;
}
export type APIRequest<D> = {
path: string;
data: D;
};
export type APIResponse<D> = {
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(),
}
}
})
-5
View File
@@ -1,5 +0,0 @@
import { makeAutomationAPI, AutomationAPI } from './automations';
import { makeRemoteAPI } from "./base";
const api = makeRemoteAPI("/api/v1");
export const automationAPI: AutomationAPI = makeAutomationAPI(api);