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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import {Temporal} from "@js-temporal/polyfill";
import {clear_session_data} from "$shared/lib/session";
import {resolve_references} from "$shared/lib/helpers";
import {replace} from "svelte-spa-router";
import type {IInternalFetchResponse} from "$shared/lib/models/IInternalFetchResponse";
import type {IInternalFetchRequest} from "$shared/lib/models/IInternalFetchRequest";
export async function http_post(url: string, body?: object|string, timeout = -1, skip_401_check = false, abort_signal: AbortSignal = undefined): Promise<IInternalFetchResponse> {
const init = {
method: "post",
} as RequestInit;
if (abort_signal) {
init.signal = abort_signal;
}
if (body) {
init.headers = {
"Content-Type": "application/json;charset=UTF-8",
};
init.body = JSON.stringify(body);
}
const response = await internal_fetch({url, init, timeout});
const result = {} as IInternalFetchResponse;
if (!skip_401_check && await is_401(response)) return result;
result.ok = response.ok;
result.status = response.status;
result.http_response = response;
if (response.status !== 204) {
try {
const ct = response.headers.get("Content-Type")?.toString() ?? "";
if (ct.startsWith("application/json")) {
const data = await response.json();
result.data = resolve_references(data);
} else if (ct.startsWith("text/plain")) {
const text = await response.text();
result.data = text as string;
}
} catch {
// Ignored
}
}
return result;
}
export async function http_get(url: string, timeout = -1, skip_401_check = false, abort_signal: AbortSignal = undefined): Promise<IInternalFetchResponse> {
const init = {
method: "get",
} as RequestInit;
if (abort_signal) {
init.signal = abort_signal;
}
const response = await internal_fetch({url, init, timeout});
const result = {} as IInternalFetchResponse;
if (!skip_401_check && await is_401(response)) return result;
result.ok = response.ok;
result.status = response.status;
result.http_response = response;
if (response.status !== 204) {
try {
const ct = response.headers.get("Content-Type")?.toString() ?? "";
if (ct.startsWith("application/json")) {
const data = await response.json();
result.data = resolve_references(data);
} else if (ct.startsWith("text/plain")) {
const text = await response.text();
result.data = text as string;
}
} catch {
// Ignored
}
}
return result;
}
export async function http_delete(url: string, body?: object|string, timeout = -1, skip_401_check = false, abort_signal: AbortSignal = undefined): Promise<IInternalFetchResponse> {
const init = {
method: "delete",
} as RequestInit;
if (abort_signal) {
init.signal = abort_signal;
}
if (body) {
init.headers = {
"Content-Type": "application/json;charset=UTF-8",
};
init.body = JSON.stringify(body);
}
const response = await internal_fetch({url, init, timeout});
const result = {} as IInternalFetchResponse;
if (!skip_401_check && await is_401(response)) return result;
result.ok = response.ok;
result.status = response.status;
result.http_response = response;
if (response.status !== 204) {
try {
const ct = response.headers.get("Content-Type")?.toString() ?? "";
if (ct.startsWith("application/json")) {
const data = await response.json();
result.data = resolve_references(data);
} else if (ct.startsWith("text/plain")) {
const text = await response.text();
result.data = text as string;
}
} catch (error) {
// ignored
}
}
return result;
}
async function internal_fetch(request: IInternalFetchRequest): Promise<Response> {
if (!request.init) request.init = {};
request.init.credentials = "include";
request.init.headers = {
"X-TimeZone": Temporal.Now.timeZone().id,
...request.init.headers
};
const fetch_request = new Request(request.url, request.init);
let response: any;
try {
if (request.timeout > 500) {
response = await Promise.race([
fetch(fetch_request),
new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), request.timeout))
]);
} else {
response = await fetch(fetch_request);
}
} catch (error) {
if (error.message === "Timeout") {
console.error("Request timed out");
} else if (error.message === "Network request failed") {
console.error("No internet connection");
} else {
throw error; // rethrow other unexpected errors
}
}
return response;
}
async function is_401(response: Response): Promise<boolean> {
if (response.status === 401) {
clear_session_data();
await replace("/login");
return true;
}
return false;
}
|