Rebuild JS artifacts
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Api / Result.js
1 define(["require", "exports", "../Core", "./Error"], function (require, exports, Core_1, Error_1) {
2 "use strict";
3 Object.defineProperty(exports, "__esModule", { value: true });
4 exports.apiResultFromStatusNotOk = exports.apiResultFromError = exports.apiResultFromValue = void 0;
5 function apiResultFromValue(value) {
6 return {
7 ok: true,
8 value,
9 unwrap() {
10 return value;
11 },
12 };
13 }
14 exports.apiResultFromValue = apiResultFromValue;
15 function apiResultFromError(error) {
16 return {
17 ok: false,
18 error,
19 unwrap() {
20 throw error;
21 },
22 };
23 }
24 exports.apiResultFromError = apiResultFromError;
25 async function apiResultFromStatusNotOk(e) {
26 const { response } = e;
27 if (response === undefined) {
28 // Aborted requests do not have a return value.
29 throw e;
30 }
31 const contentType = response.headers.get("content-type");
32 if (!contentType || !contentType.includes("application/json")) {
33 throw e;
34 }
35 let json;
36 try {
37 json = await response.json();
38 }
39 catch {
40 throw e;
41 }
42 if ((0, Core_1.isPlainObject)(json) &&
43 Object.hasOwn(json, "type") &&
44 (json.type === "api_error" || json.type === "invalid_request_error") &&
45 typeof json.code === "string" &&
46 typeof json.message === "string" &&
47 typeof json.param === "string") {
48 return apiResultFromError(new Error_1.ApiError(json.type, json.code, json.message, json.param, response.status));
49 }
50 throw e;
51 }
52 exports.apiResultFromStatusNotOk = apiResultFromStatusNotOk;
53 });