@cedx/base 0.7.0 → 0.9.0

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.
Files changed (47) hide show
  1. package/ReadMe.md +1 -1
  2. package/lib/FileExtensions.js +1 -1
  3. package/lib/Hosting/Environment.d.ts +22 -0
  4. package/lib/Hosting/Environment.d.ts.map +1 -0
  5. package/lib/Hosting/Environment.js +17 -0
  6. package/lib/Hosting/HostEnvironment.d.ts +61 -0
  7. package/lib/Hosting/HostEnvironment.d.ts.map +1 -0
  8. package/lib/Hosting/HostEnvironment.js +56 -0
  9. package/lib/UI/Components/LoadingIndicator.d.ts +8 -0
  10. package/lib/UI/Components/LoadingIndicator.d.ts.map +1 -1
  11. package/lib/UI/Components/LoadingIndicator.js +16 -4
  12. package/lib/UI/Components/OfflineIndicator.d.ts +7 -2
  13. package/lib/UI/Components/OfflineIndicator.d.ts.map +1 -1
  14. package/lib/UI/Components/OfflineIndicator.js +16 -6
  15. package/lib/UI/Components/TabActivator.d.ts +45 -0
  16. package/lib/UI/Components/TabActivator.d.ts.map +1 -0
  17. package/lib/UI/Components/TabActivator.js +67 -0
  18. package/lib/UI/Components/ThemeDropdown.d.ts +0 -8
  19. package/lib/UI/Components/ThemeDropdown.d.ts.map +1 -1
  20. package/lib/UI/Components/ThemeDropdown.js +10 -20
  21. package/lib/UI/FormExtensions.d.ts +33 -0
  22. package/lib/UI/FormExtensions.d.ts.map +1 -0
  23. package/lib/UI/FormExtensions.js +50 -0
  24. package/lib/UI/StorageArea.d.ts +18 -0
  25. package/lib/UI/StorageArea.d.ts.map +1 -0
  26. package/lib/UI/StorageArea.js +13 -0
  27. package/package.json +2 -5
  28. package/src/Client/FileExtensions.ts +1 -1
  29. package/src/Client/Hosting/Environment.ts +25 -0
  30. package/src/Client/Hosting/HostEnvironment.ts +86 -0
  31. package/src/Client/Hosting/tsconfig.json +13 -0
  32. package/src/Client/UI/Components/LoadingIndicator.ts +18 -4
  33. package/src/Client/UI/Components/OfflineIndicator.ts +19 -6
  34. package/src/Client/UI/Components/TabActivator.ts +87 -0
  35. package/src/Client/UI/Components/ThemeDropdown.ts +9 -21
  36. package/src/Client/UI/FormExtensions.ts +55 -0
  37. package/src/Client/UI/StorageArea.ts +20 -0
  38. package/src/Client/UI/tsconfig.json +1 -4
  39. package/src/Client/tsconfig.json +1 -0
  40. package/lib/Net/Http/HttpClient.d.ts +0 -83
  41. package/lib/Net/Http/HttpClient.d.ts.map +0 -1
  42. package/lib/Net/Http/HttpClient.js +0 -104
  43. package/lib/Net/Http/HttpRequestError.d.ts +0 -33
  44. package/lib/Net/Http/HttpRequestError.d.ts.map +0 -1
  45. package/lib/Net/Http/HttpRequestError.js +0 -66
  46. package/src/Client/Net/Http/HttpClient.ts +0 -145
  47. package/src/Client/Net/Http/HttpRequestError.ts +0 -75
@@ -1,104 +0,0 @@
1
- import { MediaType } from "../Mime/MediaType.js";
2
- import { HttpMethod } from "./HttpMethod.js";
3
- import { HttpRequestError } from "./HttpRequestError.js";
4
- /**
5
- * Performs HTTP requests.
6
- */
7
- export class HttpClient {
8
- /**
9
- * The base URL of the remote service.
10
- */
11
- baseAddress;
12
- /**
13
- * The function returning the component used as loading indicator.
14
- */
15
- #loadingIndicator;
16
- /**
17
- * Creates a new HTTP client.
18
- * @param options An object providing values to initialize this instance.
19
- */
20
- constructor(options = {}) {
21
- const url = options.baseUrl ? (options.baseUrl instanceof URL ? options.baseUrl.href : options.baseUrl) : document.baseURI;
22
- this.baseAddress = new URL(url.endsWith("/") ? url : `${url}/`);
23
- this.#loadingIndicator = options.loadingIndicator ?? (() => document.body.querySelector("loading-indicator, .loading-indicator"));
24
- }
25
- /**
26
- * Performs a DELETE request.
27
- * @param url The URL of the resource to fetch.
28
- * @param options The request options.
29
- * @returns The server response.
30
- */
31
- delete(url, options) {
32
- return this.#fetch(HttpMethod.Delete, url, null, options);
33
- }
34
- /**
35
- * Performs a GET request.
36
- * @param url The URL of the resource to fetch.
37
- * @param options The request options.
38
- * @returns The server response.
39
- */
40
- get(url, options) {
41
- return this.#fetch(HttpMethod.Get, url, null, options);
42
- }
43
- /**
44
- * Performs a PATCH request.
45
- * @param url The URL of the resource to fetch.
46
- * @param body The request body.
47
- * @param options The request options.
48
- * @returns The server response.
49
- */
50
- patch(url, body, options) {
51
- return this.#fetch(HttpMethod.Patch, url, body, options);
52
- }
53
- /**
54
- * Performs a POST request.
55
- * @param url The URL of the resource to fetch.
56
- * @param body The request body.
57
- * @param options The request options.
58
- * @returns The server response.
59
- */
60
- post(url, body, options) {
61
- return this.#fetch(HttpMethod.Post, url, body, options);
62
- }
63
- /**
64
- * Performs a PUT request.
65
- * @param url The URL of the resource to fetch.
66
- * @param body The request body.
67
- * @param options The request options.
68
- * @returns The server response.
69
- */
70
- put(url, body, options) {
71
- return this.#fetch(HttpMethod.Put, url, body, options);
72
- }
73
- /**
74
- * Performs a custom HTTP request.
75
- * @param method The HTTP method.
76
- * @param url The URL of the resource to fetch.
77
- * @param body The request body.
78
- * @param options The request options.
79
- * @returns The server response.
80
- */
81
- async #fetch(method, url = "", body = null, options = {}) {
82
- const headers = new Headers(options.headers);
83
- if (!headers.has("Accept"))
84
- headers.set("Accept", MediaType.Application.Json);
85
- if (body && !(body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams)) {
86
- if (typeof body != "string")
87
- body = JSON.stringify(body);
88
- if (!headers.has("Content-Type"))
89
- headers.set("Content-Type", MediaType.Application.Json);
90
- }
91
- const loadingIndicator = this.#loadingIndicator();
92
- try {
93
- loadingIndicator?.start();
94
- const request = new Request(new URL(url, this.baseAddress), { ...options, method, headers, body });
95
- const response = await fetch(request);
96
- if (!response.ok)
97
- throw new HttpRequestError(response);
98
- return response;
99
- }
100
- finally {
101
- loadingIndicator?.stop();
102
- }
103
- }
104
- }
@@ -1,33 +0,0 @@
1
- import { StatusCode } from "./StatusCode.js";
2
- /**
3
- * An error thrown by the HTTP client.
4
- */
5
- export declare class HttpRequestError extends globalThis.Error {
6
- #private;
7
- /**
8
- * Creates a new HTTP error.
9
- * @param response The HTTP response.
10
- */
11
- constructor(response: Response);
12
- /**
13
- * The HTTP response.
14
- */
15
- get cause(): Response;
16
- /**
17
- * Value indicating whether the HTTP status code is between 400 and 499.
18
- */
19
- get isClientError(): boolean;
20
- /**
21
- * Value indicating whether the HTTP status code is between 500 and 599.
22
- */
23
- get isServerError(): boolean;
24
- /**
25
- * The HTTP status code.
26
- */
27
- get statusCode(): StatusCode;
28
- /**
29
- * The validation errors.
30
- */
31
- get validationErrors(): Promise<Map<string, string>>;
32
- }
33
- //# sourceMappingURL=HttpRequestError.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"HttpRequestError.d.ts","sourceRoot":"","sources":["../../../src/Client/Net/Http/HttpRequestError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAC;AAE3C;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,UAAU,CAAC,KAAK;;IAOrD;;;OAGG;gBACS,QAAQ,EAAE,QAAQ;IAK9B;;OAEG;IACH,IAAa,KAAK,IAAI,QAAQ,CAE7B;IAED;;OAEG;IACH,IAAI,aAAa,IAAI,OAAO,CAG3B;IAED;;OAEG;IACH,IAAI,aAAa,IAAI,OAAO,CAG3B;IAED;;OAEG;IACH,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED;;OAEG;IACH,IAAI,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAInD;CAgBD"}
@@ -1,66 +0,0 @@
1
- import { StatusCode } from "./StatusCode.js";
2
- /**
3
- * An error thrown by the HTTP client.
4
- */
5
- export class HttpRequestError extends globalThis.Error {
6
- /**
7
- * The validation errors.
8
- */
9
- #validationErrors = null;
10
- /**
11
- * Creates a new HTTP error.
12
- * @param response The HTTP response.
13
- */
14
- constructor(response) {
15
- super(`${response.status} ${response.statusText}`, { cause: response });
16
- this.name = "HttpRequestError";
17
- }
18
- /**
19
- * The HTTP response.
20
- */
21
- get cause() {
22
- return super.cause;
23
- }
24
- /**
25
- * Value indicating whether the HTTP status code is between 400 and 499.
26
- */
27
- get isClientError() {
28
- const { statusCode } = this;
29
- return statusCode >= 400 && statusCode < 500;
30
- }
31
- /**
32
- * Value indicating whether the HTTP status code is between 500 and 599.
33
- */
34
- get isServerError() {
35
- const { statusCode } = this;
36
- return statusCode >= 500 && statusCode < 600;
37
- }
38
- /**
39
- * The HTTP status code.
40
- */
41
- get statusCode() {
42
- return this.cause.status;
43
- }
44
- /**
45
- * The validation errors.
46
- */
47
- get validationErrors() {
48
- return this.#validationErrors
49
- ? Promise.resolve(this.#validationErrors)
50
- : this.#parseValidationErrors().then(errors => this.#validationErrors = errors);
51
- }
52
- /**
53
- * Parses the validation errors returned in the body of the specified response.
54
- * @returns The validation errors provided by the response body.
55
- */
56
- async #parseValidationErrors() {
57
- try {
58
- const statuses = [StatusCode.BadRequest, StatusCode.UnprocessableContent];
59
- const ignoreBody = this.cause.bodyUsed || !statuses.includes(this.statusCode);
60
- return new Map(ignoreBody ? [] : Object.entries(await this.cause.json()));
61
- }
62
- catch {
63
- return new Map;
64
- }
65
- }
66
- }
@@ -1,145 +0,0 @@
1
- import {MediaType} from "../Mime/MediaType.js";
2
- import {HttpMethod} from "./HttpMethod.js";
3
- import {HttpRequestError} from "./HttpRequestError.js";
4
-
5
- /**
6
- * Performs HTTP requests.
7
- */
8
- export class HttpClient {
9
-
10
- /**
11
- * The base URL of the remote service.
12
- */
13
- readonly baseAddress: URL;
14
-
15
- /**
16
- * The function returning the component used as loading indicator.
17
- */
18
- readonly #loadingIndicator: () => ILoadingIndicator|null;
19
-
20
- /**
21
- * Creates a new HTTP client.
22
- * @param options An object providing values to initialize this instance.
23
- */
24
- constructor(options: HttpClientOptions = {}) {
25
- const url = options.baseUrl ? (options.baseUrl instanceof URL ? options.baseUrl.href : options.baseUrl) : document.baseURI;
26
- this.baseAddress = new URL(url.endsWith("/") ? url : `${url}/`);
27
- this.#loadingIndicator = options.loadingIndicator ?? (() => document.body.querySelector("loading-indicator, .loading-indicator") as ILoadingIndicator|null);
28
- }
29
-
30
- /**
31
- * Performs a DELETE request.
32
- * @param url The URL of the resource to fetch.
33
- * @param options The request options.
34
- * @returns The server response.
35
- */
36
- delete(url?: string|URL, options?: RequestInit): Promise<Response> {
37
- return this.#fetch(HttpMethod.Delete, url, null, options);
38
- }
39
-
40
- /**
41
- * Performs a GET request.
42
- * @param url The URL of the resource to fetch.
43
- * @param options The request options.
44
- * @returns The server response.
45
- */
46
- get(url?: string|URL, options?: RequestInit): Promise<Response> {
47
- return this.#fetch(HttpMethod.Get, url, null, options);
48
- }
49
-
50
- /**
51
- * Performs a PATCH request.
52
- * @param url The URL of the resource to fetch.
53
- * @param body The request body.
54
- * @param options The request options.
55
- * @returns The server response.
56
- */
57
- patch(url?: string|URL, body?: unknown, options?: RequestInit): Promise<Response> {
58
- return this.#fetch(HttpMethod.Patch, url, body, options);
59
- }
60
-
61
- /**
62
- * Performs a POST request.
63
- * @param url The URL of the resource to fetch.
64
- * @param body The request body.
65
- * @param options The request options.
66
- * @returns The server response.
67
- */
68
- post(url?: string|URL, body?: unknown, options?: RequestInit): Promise<Response> {
69
- return this.#fetch(HttpMethod.Post, url, body, options);
70
- }
71
-
72
- /**
73
- * Performs a PUT request.
74
- * @param url The URL of the resource to fetch.
75
- * @param body The request body.
76
- * @param options The request options.
77
- * @returns The server response.
78
- */
79
- put(url?: string|URL, body?: unknown, options?: RequestInit): Promise<Response> {
80
- return this.#fetch(HttpMethod.Put, url, body, options);
81
- }
82
-
83
- /**
84
- * Performs a custom HTTP request.
85
- * @param method The HTTP method.
86
- * @param url The URL of the resource to fetch.
87
- * @param body The request body.
88
- * @param options The request options.
89
- * @returns The server response.
90
- */
91
- async #fetch(method: string, url: string|URL = "", body: unknown = null, options: RequestInit = {}): Promise<Response> {
92
- const headers = new Headers(options.headers);
93
- if (!headers.has("Accept")) headers.set("Accept", MediaType.Application.Json);
94
-
95
- if (body && !(body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams)) {
96
- if (typeof body != "string") body = JSON.stringify(body);
97
- if (!headers.has("Content-Type")) headers.set("Content-Type", MediaType.Application.Json);
98
- }
99
-
100
- const loadingIndicator = this.#loadingIndicator();
101
- try {
102
- loadingIndicator?.start();
103
- const request = new Request(new URL(url, this.baseAddress), {...options, method, headers, body} as RequestInit);
104
- const response = await fetch(request);
105
- if (!response.ok) throw new HttpRequestError(response);
106
- return response;
107
- }
108
- finally {
109
- loadingIndicator?.stop();
110
- }
111
- }
112
- }
113
-
114
- /**
115
- * Defines the options of a {@link HttpClient} instance.
116
- */
117
- export type HttpClientOptions = Partial<{
118
-
119
- /**
120
- * The base URL of the remote service.
121
- */
122
- baseUrl: string|URL;
123
-
124
- /**
125
- * The function returning the component used as loading indicator.
126
- */
127
- loadingIndicator: () => ILoadingIndicator|null;
128
- }>;
129
-
130
- /**
131
- * A component that shows up when an HTTP request starts, and hides when all concurrent HTTP requests are completed.
132
- */
133
- export interface ILoadingIndicator {
134
-
135
- /**
136
- * Starts the loading indicator.
137
- */
138
- start: () => void;
139
-
140
- /**
141
- * Stops the loading indicator.
142
- * @param options Value indicating whether to force the loading indicator to stop.
143
- */
144
- stop: (options?: {force?: boolean}) => void;
145
- }
@@ -1,75 +0,0 @@
1
- import {StatusCode} from "./StatusCode.js";
2
-
3
- /**
4
- * An error thrown by the HTTP client.
5
- */
6
- export class HttpRequestError extends globalThis.Error {
7
-
8
- /**
9
- * The validation errors.
10
- */
11
- #validationErrors: Map<string, string>|null = null;
12
-
13
- /**
14
- * Creates a new HTTP error.
15
- * @param response The HTTP response.
16
- */
17
- constructor(response: Response) {
18
- super(`${response.status} ${response.statusText}`, {cause: response});
19
- this.name = "HttpRequestError";
20
- }
21
-
22
- /**
23
- * The HTTP response.
24
- */
25
- override get cause(): Response {
26
- return super.cause as Response;
27
- }
28
-
29
- /**
30
- * Value indicating whether the HTTP status code is between 400 and 499.
31
- */
32
- get isClientError(): boolean {
33
- const {statusCode} = this;
34
- return statusCode >= 400 && statusCode < 500;
35
- }
36
-
37
- /**
38
- * Value indicating whether the HTTP status code is between 500 and 599.
39
- */
40
- get isServerError(): boolean {
41
- const {statusCode} = this;
42
- return statusCode >= 500 && statusCode < 600;
43
- }
44
-
45
- /**
46
- * The HTTP status code.
47
- */
48
- get statusCode(): StatusCode {
49
- return this.cause.status as StatusCode;
50
- }
51
-
52
- /**
53
- * The validation errors.
54
- */
55
- get validationErrors(): Promise<Map<string, string>> {
56
- return this.#validationErrors
57
- ? Promise.resolve(this.#validationErrors)
58
- : this.#parseValidationErrors().then(errors => this.#validationErrors = errors);
59
- }
60
-
61
- /**
62
- * Parses the validation errors returned in the body of the specified response.
63
- * @returns The validation errors provided by the response body.
64
- */
65
- async #parseValidationErrors(): Promise<Map<string, string>> {
66
- try {
67
- const statuses: StatusCode[] = [StatusCode.BadRequest, StatusCode.UnprocessableContent];
68
- const ignoreBody = this.cause.bodyUsed || !statuses.includes(this.statusCode);
69
- return new Map(ignoreBody ? [] : Object.entries(await this.cause.json() as Record<string, string>));
70
- }
71
- catch {
72
- return new Map;
73
- }
74
- }
75
- }