elbe-ui 0.4.2 → 0.4.5

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,30 +1,23 @@
1
- export interface PostArgs {
1
+ export interface GetArgs {
2
2
  path?: {
3
3
  [key: string]: string | number | boolean | undefined;
4
4
  };
5
5
  query?: {
6
6
  [key: string]: string | number | boolean | undefined;
7
7
  };
8
+ }
9
+ export interface PostArgs extends GetArgs {
8
10
  body?: any;
9
11
  }
10
12
  /**
11
13
  * ApiService is a simple wrapper around fetch that handles JSON serialization and error handling.
12
14
  * to use it, you must first call `ApiService.init(apiURL)` with the base URL of your API.
13
15
  */
14
- export declare class ApiService {
16
+ export declare class ApiWorker {
15
17
  private apiURL;
16
- private static _i;
17
- static get i(): ApiService;
18
- private constructor();
19
- static init(apiURL: string): void;
18
+ constructor(apiURL: string);
20
19
  private _fetch;
21
- get(path: string, args?: PostArgs): Promise<any>;
20
+ get(path: string, args?: GetArgs): Promise<any>;
22
21
  post(path: string, args: PostArgs): Promise<any>;
23
- delete(path: string, args: PostArgs): Promise<any>;
24
- }
25
- export interface ApiError {
26
- code: number;
27
- message: string;
28
- data?: any;
22
+ delete(path: string, args: GetArgs): Promise<any>;
29
23
  }
30
- export declare function ifApiError(e: any): ApiError | null;
@@ -7,28 +7,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { httpErrorFromCode, rethrow } from "./error";
10
11
  const _noArgs = {};
11
12
  /**
12
13
  * ApiService is a simple wrapper around fetch that handles JSON serialization and error handling.
13
14
  * to use it, you must first call `ApiService.init(apiURL)` with the base URL of your API.
14
15
  */
15
- export class ApiService {
16
- static get i() {
17
- if (!ApiService._i)
18
- throw "ApiService not initialized. Call ApiService.init(apiURL)";
19
- return ApiService._i;
20
- }
16
+ export class ApiWorker {
21
17
  constructor(apiURL) {
22
18
  this.apiURL = apiURL;
23
19
  }
24
- static init(apiURL) {
25
- if (ApiService._i)
26
- throw "ApiService already initialized";
27
- ApiService._i = new ApiService(apiURL);
28
- }
29
20
  _fetch(p_1, method_1, _a) {
30
21
  return __awaiter(this, arguments, void 0, function* (p, method, { path, query, body }) {
31
- var _b;
32
22
  try {
33
23
  p = path
34
24
  ? p.replace(/:([a-zA-Z0-9_]+)/g, (m, p1) => {
@@ -61,14 +51,10 @@ export class ApiService {
61
51
  catch (e) {
62
52
  data = yield response.text();
63
53
  }
64
- throw {
65
- code: response.status,
66
- message: (_b = data.message) !== null && _b !== void 0 ? _b : "undefined error",
67
- data,
68
- };
54
+ throw httpErrorFromCode(response.status);
69
55
  }
70
56
  catch (e) {
71
- rethrow(e, 0, "unknown error");
57
+ rethrow(e);
72
58
  }
73
59
  });
74
60
  }
@@ -88,15 +74,4 @@ export class ApiService {
88
74
  });
89
75
  }
90
76
  }
91
- ApiService._i = null;
92
- function rethrow(e, code, message) {
93
- // if e implements the apiError interface, rethrow it:
94
- if (e && e.code !== null && e.message !== null)
95
- throw e;
96
- throw { code, message, data: e };
97
- }
98
- export function ifApiError(e) {
99
- if (e && e.code !== null && e.message !== null)
100
- return e;
101
- return null;
102
- }
77
+ const errors = {};
@@ -0,0 +1,24 @@
1
+ import { Maybe } from "..";
2
+ import { IconChild } from "../ui/components/button/icon_button";
3
+ import { L10nInlinePlain } from "../ui/util/l10n/l10n";
4
+ /**
5
+ * a generic error for providing reasonable error messages
6
+ * to both the user and the developer
7
+ */
8
+ export type ElbeError = {
9
+ code: string | number;
10
+ message: string | L10nInlinePlain;
11
+ description?: Maybe<string | L10nInlinePlain>;
12
+ icon?: Maybe<IconChild>;
13
+ details?: Maybe<any>;
14
+ cause?: Maybe<ElbeError | any>;
15
+ };
16
+ export declare function isElbeError(e: any): e is ElbeError;
17
+ export declare function maybeElbeError(e: any): ElbeError | null;
18
+ export declare function toElbeError(e: any): ElbeError;
19
+ export declare function httpErrorFromCode(code: number, e?: any): ElbeError;
20
+ /**
21
+ * takes an object and throws it as an ElbeError.
22
+ * @param e the error to throw
23
+ */
24
+ export declare function rethrow(e: any, base?: ElbeError): void;
@@ -0,0 +1,29 @@
1
+ import { errors } from "..";
2
+ export function isElbeError(e) {
3
+ return (e &&
4
+ (typeof e.code === "string" || typeof e.code === "number") &&
5
+ (typeof e.message === "string" || typeof e.message === "object"));
6
+ }
7
+ export function maybeElbeError(e) {
8
+ if (isElbeError(e))
9
+ return e;
10
+ return null;
11
+ }
12
+ export function toElbeError(e) {
13
+ var _a;
14
+ return ((_a = maybeElbeError(e)) !== null && _a !== void 0 ? _a : Object.assign(Object.assign({}, errors.unknown), { details: e }));
15
+ }
16
+ export function httpErrorFromCode(code, e) {
17
+ var _a;
18
+ const err = (_a = Object.values(errors.http).find((e) => e.code.toString().split("_")[1] === code.toString())) !== null && _a !== void 0 ? _a : errors.unknown;
19
+ return Object.assign(Object.assign({}, err), { details: e });
20
+ }
21
+ /**
22
+ * takes an object and throws it as an ElbeError.
23
+ * @param e the error to throw
24
+ */
25
+ export function rethrow(e, base = errors.unknown) {
26
+ if (isElbeError(e))
27
+ throw e;
28
+ throw Object.assign(Object.assign({}, base), { message: e instanceof Error ? e.message : base.message, details: e });
29
+ }
@@ -0,0 +1,100 @@
1
+ import { ElbeError } from "..";
2
+ export declare const errors: {
3
+ unknown: ElbeError;
4
+ http: {
5
+ badRequest: ElbeError;
6
+ unauthorized: ElbeError;
7
+ paymentRequired: ElbeError;
8
+ forbidden: ElbeError;
9
+ notFound: ElbeError;
10
+ methodNotAllowed: ElbeError;
11
+ notAcceptable: {
12
+ code: string;
13
+ message: {
14
+ de: string;
15
+ en: string;
16
+ es: string;
17
+ fr: string;
18
+ it: string;
19
+ pt: string;
20
+ };
21
+ description: {
22
+ de: string;
23
+ en: string;
24
+ es: string;
25
+ fr: string;
26
+ it: string;
27
+ pt: string;
28
+ };
29
+ };
30
+ proxyAuthenticationRequired: {
31
+ code: string;
32
+ message: {
33
+ de: string;
34
+ en: string;
35
+ es: string;
36
+ fr: string;
37
+ it: string;
38
+ pt: string;
39
+ };
40
+ description: {
41
+ de: string;
42
+ en: string;
43
+ es: string;
44
+ fr: string;
45
+ it: string;
46
+ pt: string;
47
+ };
48
+ };
49
+ requestTimeout: {
50
+ code: string;
51
+ message: {
52
+ de: string;
53
+ en: string;
54
+ es: string;
55
+ fr: string;
56
+ it: string;
57
+ pt: string;
58
+ };
59
+ description: {
60
+ de: string;
61
+ en: string;
62
+ es: string;
63
+ fr: string;
64
+ it: string;
65
+ pt: string;
66
+ };
67
+ };
68
+ conflict: ElbeError;
69
+ gone: ElbeError;
70
+ lengthRequired: ElbeError;
71
+ preconditionFailed: ElbeError;
72
+ contentTooLarge: ElbeError;
73
+ uriTooLong: ElbeError;
74
+ unsupportedMediaType: ElbeError;
75
+ rangeNotSatisfiable: ElbeError;
76
+ expectationFailed: ElbeError;
77
+ imATeapot: ElbeError;
78
+ misdirectedRequest: ElbeError;
79
+ unprocessableContent: ElbeError;
80
+ locked: ElbeError;
81
+ failedDependency: ElbeError;
82
+ tooEarly: ElbeError;
83
+ upgradeRequired: ElbeError;
84
+ preconditionRequired: ElbeError;
85
+ tooManyRequests: ElbeError;
86
+ requestHeaderFieldsTooLarge: ElbeError;
87
+ unavailableForLegalReasons: ElbeError;
88
+ internalServerError: ElbeError;
89
+ notImplemented: ElbeError;
90
+ badGateway: ElbeError;
91
+ serviceUnavailable: ElbeError;
92
+ gatewayTimeout: ElbeError;
93
+ httpVersionNotSupported: ElbeError;
94
+ variantAlsoNegotiates: ElbeError;
95
+ insufficientStorage: ElbeError;
96
+ loopDetected: ElbeError;
97
+ notExtended: ElbeError;
98
+ networkAuthenticationRequired: ElbeError;
99
+ };
100
+ };