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

+27
View File
@@ -0,0 +1,27 @@
import { APIRequest, APIResponse } from "./types";
export interface API {
makeCall: <Res = any, Req = any>(req: APIRequest<Req>) => Promise<APIResponse<Res>>;
}
export const makeRemoteAPI = (baseURL: string): API => ({
async makeCall({
path,
method = "POST",
data={}
}) {
const reply = await fetch(baseURL + "/" + path, {
method,
body: JSON.stringify(data),
});
if (reply.status === 200) {
return {
ok: true,
data: await reply.json(),
}
}
return {
ok: false,
error: await reply.text(),
}
}
})