added api setup
This commit is contained in:
1 parent
787716ccba
commit
4a08956e1a
21 files changed
+12292
-12527
No files matched your search
@@ -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(),
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user