1const jsonInit = (method, json, headers) => ({
2 method,
3 headers: {"content-type": "application/json", ...headers},
4 body: JSON.stringify(json)
5});
6
7export const postJson = async (url, json, {headers, activity} = {}) => {
8 try {
9 const res = await fetch(url, jsonInit("POST", json, headers));
10 activity?.(res.ok ? undefined : true);
11 return res;
12 } catch {
13 activity?.(true);
14 return null;
15 }
16};
17
18export const postText = async (url, body, {headers, activity} = {}) => {
19 try {
20 const res = await fetch(url, {
21 method: "POST",
22 headers: {"content-type": "text/plain; charset=utf-8", ...headers},
23 body
24 });
25 activity?.(res.ok ? undefined : true);
26 return res;
27 } catch {
28 activity?.(true);
29 return null;
30 }
31};
32
33export const getJson = async (url, {headers, signal} = {}) => {
34 const res = await fetch(url, {headers, signal});
35 if (!res.ok) throw new Error(`HTTP ${res.status}`);
36 return res.json();
37};
38