-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
37 lines (32 loc) · 860 Bytes
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { call, select } from "redux-saga/effects";
export interface CallOptions {
body?: any;
anonymous?: boolean;
method?: string;
}
export function* apiCall(path: string, options: CallOptions = {}) {
let url = path;
options = {
body: null,
anonymous: false,
method: "GET",
...options,
};
let headers = {
Accept: "application/json",
};
if (!options.anonymous) {
const token = yield select();
headers["Authorization"] = "Bearer ".concat(token);
}
if (options.method !== "GET" && !(options.body instanceof FormData)) {
options.body = JSON.stringify(options.body);
headers["Content-type"] = "application/json";
}
const reqOptions: RequestInit = {
body: options.body,
headers: new Headers(headers),
method: options.method,
};
return yield call(window.fetch, url, reqOptions);
}