@omerlo/omerlo-webkit 0.0.53 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
import { initAssocs, parseAssocs } from './assocs';
|
|
2
2
|
export async function parseApiResponse(response, parser) {
|
|
3
|
-
const
|
|
4
|
-
let data = null, meta = null;
|
|
3
|
+
const text = await response.text();
|
|
5
4
|
if (response.ok) {
|
|
5
|
+
const payload = JSON.parse(text);
|
|
6
6
|
let assocs = initAssocs(payload.assocs);
|
|
7
7
|
assocs = parseAssocs(assocs);
|
|
8
|
-
meta = payload.meta;
|
|
9
|
-
data = parser(payload.data, assocs);
|
|
8
|
+
const meta = payload.meta;
|
|
9
|
+
const data = parser(payload.data, assocs);
|
|
10
|
+
const errors = payload.errors || [];
|
|
11
|
+
return { ok: true, status: response.status, parser, meta, data, errors };
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
// Response not OK - try to parse JSON for API errors, fallback to generic error
|
|
14
|
+
let errors = [];
|
|
15
|
+
try {
|
|
16
|
+
const payload = JSON.parse(text);
|
|
17
|
+
errors = payload.errors || [];
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
console.error('[OmerloWebkit - parseApiResponse] Non-OK response with non-JSON body', {
|
|
21
|
+
url: response.url,
|
|
22
|
+
status: response.status,
|
|
23
|
+
statusText: response.statusText,
|
|
24
|
+
contentType: response.headers.get('content-type'),
|
|
25
|
+
body: text.slice(0, 1000)
|
|
26
|
+
});
|
|
27
|
+
errors = [{ message: `${response.status} ${response.statusText}`, body: text.slice(0, 500) }];
|
|
28
|
+
}
|
|
29
|
+
return { ok: false, status: response.status, parser, meta: null, data: null, errors };
|
|
13
30
|
}
|
|
14
31
|
export function parseMany(parser) {
|
|
15
32
|
return (response, assocs) => {
|