added api setup
This commit is contained in:
1 parent
fa88b3840c
commit
787716ccba
3 files changed
+55
No files matched your search
@@ -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<AutomationData[]>({
|
||||
path: "/automations/list",
|
||||
data: {
|
||||
offset, limit,
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
export type AutomationAPI = ReturnType<typeof makeAutomationAPI>;
|
||||
@@ -0,0 +1,33 @@
|
||||
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(),
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
import { makeAutomationAPI, AutomationAPI } from './automations';
|
||||
import { makeRemoteAPI } from "./base";
|
||||
|
||||
const api = makeRemoteAPI("/api/v1");
|
||||
export const automationAPI: AutomationAPI = makeAutomationAPI(api);
|
||||
Reference in new issue
Block a user