@art-suite/art-core-ts-communication-status 0.1.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.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # @art-suite/art-core-ts-communication-status
2
+
3
+ A simple, consistent library for handling communication status in client-server applications.
4
+
5
+ ## Why This Module?
6
+
7
+ HTTP status codes confound two distinct purposes: providing machine-actionable status codes and conveying semantic information about errors to humans. This dual purpose leads to complexity and confusion in client-side code that needs to handle different types of failures appropriately.
8
+
9
+ This library teases apart these concerns by focusing solely on what software can act on. It reduces the complex space of HTTP status codes and other communication states into a small set of actionable categories that make client-side code simpler and more robust.
10
+
11
+ A comprehensive set of status types (simple enumerated strings e.g. "success") that cover all possible machine-actionable communication states, including situations HTTP status codes don't address (like network failures and aborted requests).
12
+
13
+ ## Example Installation and Use (Required)
14
+
15
+ Install with npm:
16
+
17
+ ```sh
18
+ npm install @art-suite/art-core-ts-communication-status
19
+ ```
20
+
21
+ Basic usage:
22
+
23
+ ```ts
24
+ import {
25
+ success,
26
+ serverFailure,
27
+ clientFailure,
28
+ clientFailureNotAuthorized,
29
+ missing,
30
+ networkFailure,
31
+ aborted,
32
+ pending,
33
+ isSuccess,
34
+ isServerFailure,
35
+ isClientFailure,
36
+ isClientFailureNotAuthorized,
37
+ isMissing,
38
+ isNetworkFailure,
39
+ isAborted,
40
+ isPending,
41
+ } from "@art-suite/art-core-ts-communication-status";
42
+
43
+ // Handle success
44
+ if (isSuccess(status)) {
45
+ }
46
+
47
+ // Simply handle all server failures
48
+ if (isServerFailure(status)) {
49
+ // Handle server failure
50
+ }
51
+
52
+ // or Handle multiple statues as a simple switch:
53
+ switch (status) {
54
+ case success:
55
+ /*...*/ break; // Request completed successfully
56
+ case missing:
57
+ /*...*/ break; // Resource not found, ask the user to fix
58
+ default:
59
+ /*...*/ break; // handle all other failures as one
60
+ }
61
+ ```
62
+
63
+ ## Functional Overview
64
+
65
+ ### Status Types
66
+
67
+ - **Success Status:**
68
+
69
+ - `success` — Request completed successfully
70
+
71
+ - **Missing Status:**
72
+
73
+ - `missing` — Resource not found / not available (404 and 501)
74
+
75
+ - **Client-side Failures:**
76
+
77
+ - `clientFailure` — Invalid request (needs client-side fixes)
78
+ - `clientFailureNotAuthorized` — Valid request but unauthorized
79
+
80
+ - **Server-side Failures:**
81
+
82
+ - `serverFailure` — Server-side failure (including server-side infra failures like gateways)
83
+
84
+ - **Non HTTP Failure States:**
85
+ - `networkFailure` — Network connectivity issues
86
+ - `aborted` — Request was cancelled
87
+ - `pending` — Request is in progress
88
+ - `timeout` - Request timed out
89
+
90
+ ### Type Guards
91
+
92
+ - `isSuccess(status)` — Checks if status indicates success
93
+ - `isMissing(status)` — Checks if status indicates missing resource
94
+ - `isClientFailure(status)` — Checks if status indicates client failure
95
+ - `isClientFailureNotAuthorized(status)` — Checks if status indicates auth failure
96
+ - `isServerFailure(status)` — Checks if status indicates server failure
97
+ - `isNetworkFailure(status)` — Checks if status indicates network issues
98
+ - `isAborted(status)` — Checks if status indicates aborted request
99
+ - `isPending(status)` — Checks if status indicates pending request
100
+
101
+ ### HTTP Status Code Mapping
102
+
103
+ The library handles mapping HTTP status codes to these simplified states:
104
+
105
+ - 2xx → `success`
106
+ - 404, 501 → `missing`
107
+ - 401, 403, 407, 451, 511 → `clientFailureNotAuthorized`
108
+ - 4xx, 505, 530 → `clientFailure`
109
+ - 5xx → `serverFailure`
110
+
111
+ Note: HTTP redirects (3xx) are considered protocol-level signals and should be handled by the HTTP client library, not exposed to application code.
112
+
113
+ ## API Documentation Reference
114
+
115
+ For detailed information on all exported functions and their parameters, please refer to the TypeScript typings and JSDoc comments within the source code.
package/dist/index.cjs ADDED
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ aborted: () => aborted,
24
+ clientFailure: () => clientFailure,
25
+ clientFailureNotAuthorized: () => clientFailureNotAuthorized,
26
+ failure: () => failure,
27
+ getCommunicationStatus: () => getCommunicationStatus,
28
+ getHttpStatus: () => getHttpStatus,
29
+ isAborted: () => isAborted,
30
+ isClientFailure: () => isClientFailure,
31
+ isClientFailureNotAuthorized: () => isClientFailureNotAuthorized,
32
+ isFailure: () => isFailure,
33
+ isMissing: () => isMissing,
34
+ isNetworkFailure: () => isNetworkFailure,
35
+ isNonClientFailure: () => isNonClientFailure,
36
+ isPending: () => isPending,
37
+ isRetryableFailure: () => isRetryableFailure,
38
+ isServerFailure: () => isServerFailure,
39
+ isStatusValid: () => isStatusValid,
40
+ isSuccess: () => isSuccess,
41
+ isTimeout: () => isTimeout,
42
+ missing: () => missing,
43
+ networkFailure: () => networkFailure,
44
+ pending: () => pending,
45
+ serverFailure: () => serverFailure,
46
+ statusRegex: () => statusRegex,
47
+ success: () => success,
48
+ timeout: () => timeout
49
+ });
50
+ module.exports = __toCommonJS(index_exports);
51
+
52
+ // src/CommunicationStatus.ts
53
+ var communicationStatuses = {
54
+ success: { httpStatus: 200 },
55
+ missing: { httpStatus: 404, failure: true },
56
+ clientFailure: { httpStatus: 400, clientFailure: true, failure: true },
57
+ clientFailureNotAuthorized: { httpStatus: 403, clientFailure: true, failure: true },
58
+ serverFailure: { httpStatus: 500, failure: true, serverFailure: true },
59
+ networkFailure: { failure: true },
60
+ aborted: { failure: true },
61
+ pending: {},
62
+ failure: { httpStatus: 500, failure: true },
63
+ timeout: { failure: true }
64
+ };
65
+ var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/;
66
+ var success = "success";
67
+ var missing = "missing";
68
+ var clientFailure = "clientFailure";
69
+ var clientFailureNotAuthorized = "clientFailureNotAuthorized";
70
+ var serverFailure = "serverFailure";
71
+ var networkFailure = "networkFailure";
72
+ var aborted = "aborted";
73
+ var pending = "pending";
74
+ var failure = "failure";
75
+ var timeout = "timeout";
76
+ var isSuccess = (status) => status === success;
77
+ var isFailure = (status) => !!communicationStatuses[status]?.failure;
78
+ var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
79
+ var isServerFailure = (status) => !!communicationStatuses[status]?.serverFailure;
80
+ var isNetworkFailure = (status) => status === networkFailure;
81
+ var isNonClientFailure = (status) => isFailure(status) && !isClientFailure(status);
82
+ var isClientFailureNotAuthorized = (status) => status === clientFailureNotAuthorized;
83
+ var isAborted = (status) => status === aborted;
84
+ var isMissing = (status) => status === missing;
85
+ var isPending = (status) => status === pending;
86
+ var isTimeout = (status) => status === timeout;
87
+ var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
88
+ var isStatusValid = (status) => statusRegex.test(status);
89
+ var getCommunicationStatus = (httpStatus) => {
90
+ if (!httpStatus) return { status: networkFailure, message: "network failure" };
91
+ let status;
92
+ switch (Math.floor(httpStatus / 100)) {
93
+ case 2:
94
+ status = success;
95
+ break;
96
+ case 3:
97
+ status = missing;
98
+ break;
99
+ case 4:
100
+ switch (httpStatus) {
101
+ case 401:
102
+ case 403:
103
+ case 407:
104
+ case 451:
105
+ status = clientFailureNotAuthorized;
106
+ break;
107
+ case 404:
108
+ status = missing;
109
+ break;
110
+ default:
111
+ status = clientFailure;
112
+ }
113
+ break;
114
+ case 5:
115
+ switch (httpStatus) {
116
+ case 502:
117
+ case 503:
118
+ case 504:
119
+ status = networkFailure;
120
+ break;
121
+ case 511:
122
+ status = clientFailureNotAuthorized;
123
+ break;
124
+ case 501:
125
+ status = missing;
126
+ break;
127
+ // 501 Not Implemented - i.e. it "does not exist" currently - i.e. missing
128
+ case 505:
129
+ // HTTP Version Not Supported - client should change the request
130
+ case 530:
131
+ status = clientFailure;
132
+ break;
133
+ default:
134
+ status = serverFailure;
135
+ break;
136
+ }
137
+ break;
138
+ }
139
+ if (!status) throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);
140
+ return {
141
+ status,
142
+ httpStatus,
143
+ message: `${status} (${httpStatus})`
144
+ };
145
+ };
146
+ var getHttpStatus = (status) => {
147
+ const httpStatus = communicationStatuses[status]?.httpStatus;
148
+ if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`);
149
+ return httpStatus;
150
+ };
151
+ // Annotate the CommonJS export names for ESM import in node:
152
+ 0 && (module.exports = {
153
+ aborted,
154
+ clientFailure,
155
+ clientFailureNotAuthorized,
156
+ failure,
157
+ getCommunicationStatus,
158
+ getHttpStatus,
159
+ isAborted,
160
+ isClientFailure,
161
+ isClientFailureNotAuthorized,
162
+ isFailure,
163
+ isMissing,
164
+ isNetworkFailure,
165
+ isNonClientFailure,
166
+ isPending,
167
+ isRetryableFailure,
168
+ isServerFailure,
169
+ isStatusValid,
170
+ isSuccess,
171
+ isTimeout,
172
+ missing,
173
+ networkFailure,
174
+ pending,
175
+ serverFailure,
176
+ statusRegex,
177
+ success,
178
+ timeout
179
+ });
180
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/CommunicationStatus.ts"],"sourcesContent":["export * from \"./CommunicationStatus\"","interface CommunicationStatusInfo {\n httpStatus?: number\n failure?: boolean\n clientFailure?: boolean\n serverFailure?: boolean\n}\n\ninterface CommunicationStatuses {\n [key: string]: CommunicationStatusInfo\n}\n\nconst communicationStatuses: CommunicationStatuses = {\n success: { httpStatus: 200 },\n missing: { httpStatus: 404, failure: true },\n clientFailure: { httpStatus: 400, clientFailure: true, failure: true },\n clientFailureNotAuthorized: { httpStatus: 403, clientFailure: true, failure: true },\n serverFailure: { httpStatus: 500, failure: true, serverFailure: true },\n networkFailure: { failure: true },\n aborted: { failure: true },\n pending: {},\n failure: { httpStatus: 500, failure: true },\n timeout: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeout\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/\n\n// Export status constants\n/**\n * HTTP 2xx responses\n *\n * Client Can Automatically:\n * - Process the successful response\n * - Update UI to reflect success\n *\n * Client Developer Can:\n * - Handle the successful response data\n */\nexport const success: CommunicationStatus = \"success\"\n\n/**\n * Resource not found\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const missing: CommunicationStatus = \"missing\"\n\n/**\n * Client-side errors; i.e. the client needs to change the request somehow to succeed\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n * - Prompt user to correct invalid input\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n * - validate input before sending requests\n */\nexport const clientFailure: CommunicationStatus = \"clientFailure\"\n\n/**\n * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const clientFailureNotAuthorized: CommunicationStatus = \"clientFailureNotAuthorized\"\n\n/**\n * Server-side errors; i.e. internal server errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n * - Implement automatic retry with backoff\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error\n */\nexport const serverFailure: CommunicationStatus = \"serverFailure\"\n\n/**\n * Request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request when network is available\n * - Monitor network status for recovery\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n * - implement offline-first capabilities\n */\nexport const networkFailure: CommunicationStatus = \"networkFailure\"\n\n/**\n * Request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n * - cleanup any pending state\n *\n * Client Developer Can:\n * - fix the client to not abort the request unnecessarily\n * - implement proper cleanup on abort\n */\nexport const aborted: CommunicationStatus = \"aborted\"\n\n/**\n * Request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n * - implement proper loading states\n */\nexport const pending: CommunicationStatus = \"pending\"\n\n/**\n * Any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can Automatically:\n * - Show appropriate error message to user\n * - Implement generic error handling\n * - Log errors for debugging\n *\n * Client Developer Can:\n * - Use more specific is* functions for targeted error handling\n * - Implement proper error recovery strategies\n */\nexport const failure: CommunicationStatus = \"failure\"\n\n/**\n * Request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - fix the client to not timeout the request\n * - implement proper timeout handling\n */\nexport const timeout: CommunicationStatus = \"timeout\"\n\n// Core status check functions\n/** Returns true for HTTP 2xx responses */\nexport const isSuccess = (status: CommunicationStatus) => status === success\n\n/**\n * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can:\n * - Use a different is* function for more specific checks\n */\nexport const isFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.failure\n\n/**\n * Returns true for client-side errors\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n*/\nexport const isClientFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.clientFailure\n\n/**\n * Returns true for server-side errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)\n*/\nexport const isServerFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.serverFailure\n\n/**\n * Returns true when request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n*/\nexport const isNetworkFailure = (status: CommunicationStatus) => status === networkFailure\n\n/** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */\nexport const isNonClientFailure = (status: CommunicationStatus) => isFailure(status) && !isClientFailure(status)\n\n/**\n * Returns true for unauthorized requests (not authenticated or not authorized)\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n * - 511: Network Authentication Required\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const isClientFailureNotAuthorized = (status: CommunicationStatus) => status === clientFailureNotAuthorized\n\n/**\n * Returns true when request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n *\n * Client Developer Can:\n * - fix the client to not abort the request\n */\nexport const isAborted = (status: CommunicationStatus) => status === aborted\n\n/**\n * Returns true when resource not found / not available\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n * - 501: Not Implemented\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const isMissing = (status: CommunicationStatus) => status === missing\n\n/**\n * Returns true while request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request (trigging an \"aborted\" communication status)\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n */\nexport const isPending = (status: CommunicationStatus) => status === pending\n\n/**\n * Returns true if the request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - extend the timeout duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeout\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeout\n * - aborted\n *\n * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.\n *\n * Client and Server Devs can\n * - investigate network, client and server performance and reliability issues\n */\nexport const isRetryableFailure = (status: CommunicationStatus) => isNetworkFailure(status) || isTimeout(status) || isAborted(status)\n\n/**\n * Returns true if the status is a valid communication status\n */\nexport const isStatusValid = (status: string) => statusRegex.test(status);\n\ninterface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n\n/**\n * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code\n *\n * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)\n *\n * @param httpStatus - The HTTP status code to get the communication status for\n * @returns The communication status for the given HTTP status code\n */\nexport const getCommunicationStatus = (httpStatus?: number): CommunicationStatusDetails => {\n if (!httpStatus) return { status: networkFailure, message: \"network failure\" }\n\n let status: CommunicationStatus | undefined\n\n switch (Math.floor(httpStatus / 100)) {\n case 2: status = success; break\n case 3: status = missing; break\n case 4:\n switch (httpStatus) {\n case 401:\n case 403:\n case 407:\n case 451: status = clientFailureNotAuthorized; break\n case 404: status = missing; break\n default: status = clientFailure\n }\n break\n case 5:\n switch (httpStatus) {\n case 502:\n case 503:\n case 504: status = networkFailure; break\n case 511: status = clientFailureNotAuthorized; break\n case 501: status = missing; break // 501 Not Implemented - i.e. it \"does not exist\" currently - i.e. missing\n case 505: // HTTP Version Not Supported - client should change the request\n case 530: status = clientFailure; break\n default: status = serverFailure; break\n }\n break\n }\n\n if (!status) throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`)\n\n return {\n status,\n httpStatus,\n message: `${status} (${httpStatus})`\n }\n}\n\nexport const getHttpStatus = (status: CommunicationStatus): number => {\n const httpStatus = communicationStatuses[status]?.httpStatus\n if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`)\n return httpStatus\n}"],"mappings":"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","names":[]}
@@ -0,0 +1,338 @@
1
+ type CommunicationStatus = "success" | "missing" | "clientFailure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "timeout";
2
+ /**
3
+ * RegEx returns true for all valid communication statuses
4
+ */
5
+ declare const statusRegex: RegExp;
6
+ /**
7
+ * HTTP 2xx responses
8
+ *
9
+ * Client Can Automatically:
10
+ * - Process the successful response
11
+ * - Update UI to reflect success
12
+ *
13
+ * Client Developer Can:
14
+ * - Handle the successful response data
15
+ */
16
+ declare const success: CommunicationStatus;
17
+ /**
18
+ * Resource not found
19
+ *
20
+ * HTTP Status Codes Covered:
21
+ * - 404: Not Found
22
+ *
23
+ * Client Can Automatically:
24
+ * - notify the user that the resource was not found
25
+ * - prompt the user to request a different resource
26
+ *
27
+ * Client Developer Can:
28
+ * - fix the bad resource paths
29
+ */
30
+ declare const missing: CommunicationStatus;
31
+ /**
32
+ * Client-side errors; i.e. the client needs to change the request somehow to succeed
33
+ *
34
+ * HTTP Status Codes Covered:
35
+ * - 400: Bad Request
36
+ * - 401: Unauthorized
37
+ * - 403: Forbidden
38
+ * - 407: Proxy Authentication Required
39
+ * - 409: Conflict
40
+ * - 422: Unprocessable Entity
41
+ *
42
+ * Client Can Automatically:
43
+ * - Notify the user that the client is experiencing issues
44
+ * - Prompt user to correct invalid input
45
+ *
46
+ * Client Developer Can:
47
+ * - use isClientFailureNotAuthorized to check for 401/403/407/451
48
+ * - fix the request to avoid the 4xx error
49
+ * - validate input before sending requests
50
+ */
51
+ declare const clientFailure: CommunicationStatus;
52
+ /**
53
+ * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed
54
+ *
55
+ * HTTP Status Codes Covered:
56
+ * - 401: Unauthorized
57
+ * - 403: Forbidden
58
+ * - 407: Proxy Authentication Required
59
+ * - 451: Unavailable For Legal Reasons
60
+ *
61
+ * Client Can Automatically:
62
+ * - refresh the request token
63
+ * - prompt the user to re-login
64
+ * - ask the user to contact the administrator for access
65
+ *
66
+ * Client and Server Developer Can:
67
+ * - fix authorization / authentication bugs
68
+ */
69
+ declare const clientFailureNotAuthorized: CommunicationStatus;
70
+ /**
71
+ * Server-side errors; i.e. internal server errors
72
+ *
73
+ * HTTP Status Codes Covered:
74
+ * - 500: Internal Server Error
75
+ * - 502: Bad Gateway
76
+ * - 503: Service Unavailable
77
+ * - 504: Gateway Timeout
78
+ *
79
+ * Client Can Automatically:
80
+ * - Ask the user to try again later
81
+ * - Notify the user that the server is experiencing issues
82
+ * - Implement automatic retry with backoff
83
+ *
84
+ * Client Developer: (probably) can't fix
85
+ *
86
+ * Server Developer Can:
87
+ * - fix the server to avoid the 5xx error
88
+ * - fix server infrastructure to avoid the 5xx error
89
+ */
90
+ declare const serverFailure: CommunicationStatus;
91
+ /**
92
+ * Request fails due to network connectivity issues
93
+ *
94
+ * HTTP Status Codes Covered: NONE (server was not reachable)
95
+ *
96
+ * Client Can Automatically:
97
+ * - Prompt the user to fix the network connection
98
+ * - Retry the request when network is available
99
+ * - Monitor network status for recovery
100
+ *
101
+ * Client Developer Can:
102
+ * - fix bad network constants (like address, ports, etc.)
103
+ * - implement offline-first capabilities
104
+ */
105
+ declare const networkFailure: CommunicationStatus;
106
+ /**
107
+ * Request was cancelled by client
108
+ *
109
+ * Client Can Automatically:
110
+ * - notify the user that the request was cancelled
111
+ * - prompt the user to try again
112
+ * - cleanup any pending state
113
+ *
114
+ * Client Developer Can:
115
+ * - fix the client to not abort the request unnecessarily
116
+ * - implement proper cleanup on abort
117
+ */
118
+ declare const aborted: CommunicationStatus;
119
+ /**
120
+ * Request is in progress
121
+ *
122
+ * Client Can Automatically:
123
+ * - notify the user that the request is in progress
124
+ * - show the user progress (if available)
125
+ * - allow the user to cancel the request
126
+ *
127
+ * Client Developer Can:
128
+ * - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
129
+ * - implement proper loading states
130
+ */
131
+ declare const pending: CommunicationStatus;
132
+ /**
133
+ * Any error response (HTTP 4xx/5xx) or network/abort failures
134
+ *
135
+ * HTTP Status Codes Covered:
136
+ * - 4xx: Client-side errors (except 404)
137
+ * - 5xx: Server-side errors
138
+ * - Network failures
139
+ * - Abort failures
140
+ *
141
+ * Client Can Automatically:
142
+ * - Show appropriate error message to user
143
+ * - Implement generic error handling
144
+ * - Log errors for debugging
145
+ *
146
+ * Client Developer Can:
147
+ * - Use more specific is* functions for targeted error handling
148
+ * - Implement proper error recovery strategies
149
+ */
150
+ declare const failure: CommunicationStatus;
151
+ /**
152
+ * Request timed out
153
+ *
154
+ * Client Can Automatically:
155
+ * - notify the user that the request timed out
156
+ * - try again (automatically or via user action)
157
+ *
158
+ * Client Developer Can:
159
+ * - fix the client to not timeout the request
160
+ * - implement proper timeout handling
161
+ */
162
+ declare const timeout: CommunicationStatus;
163
+ /** Returns true for HTTP 2xx responses */
164
+ declare const isSuccess: (status: CommunicationStatus) => status is "success";
165
+ /**
166
+ * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures
167
+ *
168
+ * HTTP Status Codes Covered:
169
+ * - 4xx: Client-side errors (except 404)
170
+ * - 5xx: Server-side errors
171
+ * - Network failures
172
+ * - Abort failures
173
+ *
174
+ * Client Can:
175
+ * - Use a different is* function for more specific checks
176
+ */
177
+ declare const isFailure: (status: CommunicationStatus) => boolean;
178
+ /**
179
+ * Returns true for client-side errors
180
+ *
181
+ * HTTP Status Codes Covered:
182
+ * - 400: Bad Request
183
+ * - 401: Unauthorized
184
+ * - 403: Forbidden
185
+ * - 407: Proxy Authentication Required
186
+ * - 409: Conflict
187
+ * - 422: Unprocessable Entity
188
+ *
189
+ * Client Can Automatically:
190
+ * - Notify the user that the client is experiencing issues
191
+ *
192
+ * Client Developer Can:
193
+ * - use isClientFailureNotAuthorized to check for 401/403/407/451
194
+ * - fix the request to avoid the 4xx error
195
+ */
196
+ declare const isClientFailure: (status: CommunicationStatus) => boolean;
197
+ /**
198
+ * Returns true for server-side errors
199
+ *
200
+ * HTTP Status Codes Covered:
201
+ * - 500: Internal Server Error
202
+ * - 502: Bad Gateway
203
+ * - 503: Service Unavailable
204
+ * - 504: Gateway Timeout
205
+ *
206
+ * Client Can Automatically:
207
+ * - Ask the user to try again later
208
+ * - Notify the user that the server is experiencing issues
209
+ *
210
+ * Client Developer: (probably) can't fix
211
+ *
212
+ * Server Developer Can:
213
+ * - fix the server to avoid the 5xx error
214
+ * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)
215
+ */
216
+ declare const isServerFailure: (status: CommunicationStatus) => boolean;
217
+ /**
218
+ * Returns true when request fails due to network connectivity issues
219
+ *
220
+ * HTTP Status Codes Covered: NONE (server was not reachable)
221
+ *
222
+ * Client Can Automatically:
223
+ * - Prompt the user to fix the network connection
224
+ * - Retry the request
225
+ *
226
+ * Client Developer Can:
227
+ * - fix bad network constants (like address, ports, etc.)
228
+ */
229
+ declare const isNetworkFailure: (status: CommunicationStatus) => status is "networkFailure";
230
+ /** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */
231
+ declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
232
+ /**
233
+ * Returns true for unauthorized requests (not authenticated or not authorized)
234
+ *
235
+ * HTTP Status Codes Covered:
236
+ * - 401: Unauthorized
237
+ * - 403: Forbidden
238
+ * - 407: Proxy Authentication Required
239
+ * - 451: Unavailable For Legal Reasons
240
+ * - 511: Network Authentication Required
241
+ *
242
+ * Client Can Automatically:
243
+ * - refresh the request token
244
+ * - prompt the user to re-login
245
+ * - ask the user to contact the administrator for access
246
+ *
247
+ * Client and Server Developer Can:
248
+ * - fix authorization / authentication bugs
249
+ */
250
+ declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => status is "clientFailureNotAuthorized";
251
+ /**
252
+ * Returns true when request was cancelled by client
253
+ *
254
+ * Client Can Automatically:
255
+ * - notify the user that the request was cancelled
256
+ * - prompt the user to try again
257
+ *
258
+ * Client Developer Can:
259
+ * - fix the client to not abort the request
260
+ */
261
+ declare const isAborted: (status: CommunicationStatus) => status is "aborted";
262
+ /**
263
+ * Returns true when resource not found / not available
264
+ *
265
+ * HTTP Status Codes Covered:
266
+ * - 404: Not Found
267
+ * - 501: Not Implemented
268
+ *
269
+ * Client Can Automatically:
270
+ * - notify the user that the resource was not found
271
+ * - prompt the user to request a different resource
272
+ *
273
+ * Client Developer Can:
274
+ * - fix the bad resource paths
275
+ */
276
+ declare const isMissing: (status: CommunicationStatus) => status is "missing";
277
+ /**
278
+ * Returns true while request is in progress
279
+ *
280
+ * Client Can Automatically:
281
+ * - notify the user that the request is in progress
282
+ * - show the user progress (if available)
283
+ * - allow the user to cancel the request (trigging an "aborted" communication status)
284
+ *
285
+ * Client Developer Can:
286
+ * - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
287
+ */
288
+ declare const isPending: (status: CommunicationStatus) => status is "pending";
289
+ /**
290
+ * Returns true if the request timed out
291
+ *
292
+ * Client Can Automatically:
293
+ * - notify the user that the request timed out
294
+ * - try again (automatically or via user action)
295
+ *
296
+ * Client Developer Can:
297
+ * - extend the timeout duration
298
+ *
299
+ * Server Developer Can:
300
+ * - improve server performance and reliability
301
+ */
302
+ declare const isTimeout: (status: CommunicationStatus) => status is "timeout";
303
+ /**
304
+ * Returns true if client can safely retry the request
305
+ *
306
+ * A a clearly-retryable failure:
307
+ *
308
+ * - network failure
309
+ * - timeout
310
+ * - aborted
311
+ *
312
+ * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.
313
+ *
314
+ * Client and Server Devs can
315
+ * - investigate network, client and server performance and reliability issues
316
+ */
317
+ declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "timeout";
318
+ /**
319
+ * Returns true if the status is a valid communication status
320
+ */
321
+ declare const isStatusValid: (status: string) => boolean;
322
+ interface CommunicationStatusDetails {
323
+ status: CommunicationStatus;
324
+ httpStatus?: number;
325
+ message: string;
326
+ }
327
+ /**
328
+ * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code
329
+ *
330
+ * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)
331
+ *
332
+ * @param httpStatus - The HTTP status code to get the communication status for
333
+ * @returns The communication status for the given HTTP status code
334
+ */
335
+ declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatusDetails;
336
+ declare const getHttpStatus: (status: CommunicationStatus) => number;
337
+
338
+ export { type CommunicationStatus, aborted, clientFailure, clientFailureNotAuthorized, failure, getCommunicationStatus, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeout };
@@ -0,0 +1,338 @@
1
+ type CommunicationStatus = "success" | "missing" | "clientFailure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "timeout";
2
+ /**
3
+ * RegEx returns true for all valid communication statuses
4
+ */
5
+ declare const statusRegex: RegExp;
6
+ /**
7
+ * HTTP 2xx responses
8
+ *
9
+ * Client Can Automatically:
10
+ * - Process the successful response
11
+ * - Update UI to reflect success
12
+ *
13
+ * Client Developer Can:
14
+ * - Handle the successful response data
15
+ */
16
+ declare const success: CommunicationStatus;
17
+ /**
18
+ * Resource not found
19
+ *
20
+ * HTTP Status Codes Covered:
21
+ * - 404: Not Found
22
+ *
23
+ * Client Can Automatically:
24
+ * - notify the user that the resource was not found
25
+ * - prompt the user to request a different resource
26
+ *
27
+ * Client Developer Can:
28
+ * - fix the bad resource paths
29
+ */
30
+ declare const missing: CommunicationStatus;
31
+ /**
32
+ * Client-side errors; i.e. the client needs to change the request somehow to succeed
33
+ *
34
+ * HTTP Status Codes Covered:
35
+ * - 400: Bad Request
36
+ * - 401: Unauthorized
37
+ * - 403: Forbidden
38
+ * - 407: Proxy Authentication Required
39
+ * - 409: Conflict
40
+ * - 422: Unprocessable Entity
41
+ *
42
+ * Client Can Automatically:
43
+ * - Notify the user that the client is experiencing issues
44
+ * - Prompt user to correct invalid input
45
+ *
46
+ * Client Developer Can:
47
+ * - use isClientFailureNotAuthorized to check for 401/403/407/451
48
+ * - fix the request to avoid the 4xx error
49
+ * - validate input before sending requests
50
+ */
51
+ declare const clientFailure: CommunicationStatus;
52
+ /**
53
+ * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed
54
+ *
55
+ * HTTP Status Codes Covered:
56
+ * - 401: Unauthorized
57
+ * - 403: Forbidden
58
+ * - 407: Proxy Authentication Required
59
+ * - 451: Unavailable For Legal Reasons
60
+ *
61
+ * Client Can Automatically:
62
+ * - refresh the request token
63
+ * - prompt the user to re-login
64
+ * - ask the user to contact the administrator for access
65
+ *
66
+ * Client and Server Developer Can:
67
+ * - fix authorization / authentication bugs
68
+ */
69
+ declare const clientFailureNotAuthorized: CommunicationStatus;
70
+ /**
71
+ * Server-side errors; i.e. internal server errors
72
+ *
73
+ * HTTP Status Codes Covered:
74
+ * - 500: Internal Server Error
75
+ * - 502: Bad Gateway
76
+ * - 503: Service Unavailable
77
+ * - 504: Gateway Timeout
78
+ *
79
+ * Client Can Automatically:
80
+ * - Ask the user to try again later
81
+ * - Notify the user that the server is experiencing issues
82
+ * - Implement automatic retry with backoff
83
+ *
84
+ * Client Developer: (probably) can't fix
85
+ *
86
+ * Server Developer Can:
87
+ * - fix the server to avoid the 5xx error
88
+ * - fix server infrastructure to avoid the 5xx error
89
+ */
90
+ declare const serverFailure: CommunicationStatus;
91
+ /**
92
+ * Request fails due to network connectivity issues
93
+ *
94
+ * HTTP Status Codes Covered: NONE (server was not reachable)
95
+ *
96
+ * Client Can Automatically:
97
+ * - Prompt the user to fix the network connection
98
+ * - Retry the request when network is available
99
+ * - Monitor network status for recovery
100
+ *
101
+ * Client Developer Can:
102
+ * - fix bad network constants (like address, ports, etc.)
103
+ * - implement offline-first capabilities
104
+ */
105
+ declare const networkFailure: CommunicationStatus;
106
+ /**
107
+ * Request was cancelled by client
108
+ *
109
+ * Client Can Automatically:
110
+ * - notify the user that the request was cancelled
111
+ * - prompt the user to try again
112
+ * - cleanup any pending state
113
+ *
114
+ * Client Developer Can:
115
+ * - fix the client to not abort the request unnecessarily
116
+ * - implement proper cleanup on abort
117
+ */
118
+ declare const aborted: CommunicationStatus;
119
+ /**
120
+ * Request is in progress
121
+ *
122
+ * Client Can Automatically:
123
+ * - notify the user that the request is in progress
124
+ * - show the user progress (if available)
125
+ * - allow the user to cancel the request
126
+ *
127
+ * Client Developer Can:
128
+ * - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
129
+ * - implement proper loading states
130
+ */
131
+ declare const pending: CommunicationStatus;
132
+ /**
133
+ * Any error response (HTTP 4xx/5xx) or network/abort failures
134
+ *
135
+ * HTTP Status Codes Covered:
136
+ * - 4xx: Client-side errors (except 404)
137
+ * - 5xx: Server-side errors
138
+ * - Network failures
139
+ * - Abort failures
140
+ *
141
+ * Client Can Automatically:
142
+ * - Show appropriate error message to user
143
+ * - Implement generic error handling
144
+ * - Log errors for debugging
145
+ *
146
+ * Client Developer Can:
147
+ * - Use more specific is* functions for targeted error handling
148
+ * - Implement proper error recovery strategies
149
+ */
150
+ declare const failure: CommunicationStatus;
151
+ /**
152
+ * Request timed out
153
+ *
154
+ * Client Can Automatically:
155
+ * - notify the user that the request timed out
156
+ * - try again (automatically or via user action)
157
+ *
158
+ * Client Developer Can:
159
+ * - fix the client to not timeout the request
160
+ * - implement proper timeout handling
161
+ */
162
+ declare const timeout: CommunicationStatus;
163
+ /** Returns true for HTTP 2xx responses */
164
+ declare const isSuccess: (status: CommunicationStatus) => status is "success";
165
+ /**
166
+ * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures
167
+ *
168
+ * HTTP Status Codes Covered:
169
+ * - 4xx: Client-side errors (except 404)
170
+ * - 5xx: Server-side errors
171
+ * - Network failures
172
+ * - Abort failures
173
+ *
174
+ * Client Can:
175
+ * - Use a different is* function for more specific checks
176
+ */
177
+ declare const isFailure: (status: CommunicationStatus) => boolean;
178
+ /**
179
+ * Returns true for client-side errors
180
+ *
181
+ * HTTP Status Codes Covered:
182
+ * - 400: Bad Request
183
+ * - 401: Unauthorized
184
+ * - 403: Forbidden
185
+ * - 407: Proxy Authentication Required
186
+ * - 409: Conflict
187
+ * - 422: Unprocessable Entity
188
+ *
189
+ * Client Can Automatically:
190
+ * - Notify the user that the client is experiencing issues
191
+ *
192
+ * Client Developer Can:
193
+ * - use isClientFailureNotAuthorized to check for 401/403/407/451
194
+ * - fix the request to avoid the 4xx error
195
+ */
196
+ declare const isClientFailure: (status: CommunicationStatus) => boolean;
197
+ /**
198
+ * Returns true for server-side errors
199
+ *
200
+ * HTTP Status Codes Covered:
201
+ * - 500: Internal Server Error
202
+ * - 502: Bad Gateway
203
+ * - 503: Service Unavailable
204
+ * - 504: Gateway Timeout
205
+ *
206
+ * Client Can Automatically:
207
+ * - Ask the user to try again later
208
+ * - Notify the user that the server is experiencing issues
209
+ *
210
+ * Client Developer: (probably) can't fix
211
+ *
212
+ * Server Developer Can:
213
+ * - fix the server to avoid the 5xx error
214
+ * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)
215
+ */
216
+ declare const isServerFailure: (status: CommunicationStatus) => boolean;
217
+ /**
218
+ * Returns true when request fails due to network connectivity issues
219
+ *
220
+ * HTTP Status Codes Covered: NONE (server was not reachable)
221
+ *
222
+ * Client Can Automatically:
223
+ * - Prompt the user to fix the network connection
224
+ * - Retry the request
225
+ *
226
+ * Client Developer Can:
227
+ * - fix bad network constants (like address, ports, etc.)
228
+ */
229
+ declare const isNetworkFailure: (status: CommunicationStatus) => status is "networkFailure";
230
+ /** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */
231
+ declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
232
+ /**
233
+ * Returns true for unauthorized requests (not authenticated or not authorized)
234
+ *
235
+ * HTTP Status Codes Covered:
236
+ * - 401: Unauthorized
237
+ * - 403: Forbidden
238
+ * - 407: Proxy Authentication Required
239
+ * - 451: Unavailable For Legal Reasons
240
+ * - 511: Network Authentication Required
241
+ *
242
+ * Client Can Automatically:
243
+ * - refresh the request token
244
+ * - prompt the user to re-login
245
+ * - ask the user to contact the administrator for access
246
+ *
247
+ * Client and Server Developer Can:
248
+ * - fix authorization / authentication bugs
249
+ */
250
+ declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => status is "clientFailureNotAuthorized";
251
+ /**
252
+ * Returns true when request was cancelled by client
253
+ *
254
+ * Client Can Automatically:
255
+ * - notify the user that the request was cancelled
256
+ * - prompt the user to try again
257
+ *
258
+ * Client Developer Can:
259
+ * - fix the client to not abort the request
260
+ */
261
+ declare const isAborted: (status: CommunicationStatus) => status is "aborted";
262
+ /**
263
+ * Returns true when resource not found / not available
264
+ *
265
+ * HTTP Status Codes Covered:
266
+ * - 404: Not Found
267
+ * - 501: Not Implemented
268
+ *
269
+ * Client Can Automatically:
270
+ * - notify the user that the resource was not found
271
+ * - prompt the user to request a different resource
272
+ *
273
+ * Client Developer Can:
274
+ * - fix the bad resource paths
275
+ */
276
+ declare const isMissing: (status: CommunicationStatus) => status is "missing";
277
+ /**
278
+ * Returns true while request is in progress
279
+ *
280
+ * Client Can Automatically:
281
+ * - notify the user that the request is in progress
282
+ * - show the user progress (if available)
283
+ * - allow the user to cancel the request (trigging an "aborted" communication status)
284
+ *
285
+ * Client Developer Can:
286
+ * - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
287
+ */
288
+ declare const isPending: (status: CommunicationStatus) => status is "pending";
289
+ /**
290
+ * Returns true if the request timed out
291
+ *
292
+ * Client Can Automatically:
293
+ * - notify the user that the request timed out
294
+ * - try again (automatically or via user action)
295
+ *
296
+ * Client Developer Can:
297
+ * - extend the timeout duration
298
+ *
299
+ * Server Developer Can:
300
+ * - improve server performance and reliability
301
+ */
302
+ declare const isTimeout: (status: CommunicationStatus) => status is "timeout";
303
+ /**
304
+ * Returns true if client can safely retry the request
305
+ *
306
+ * A a clearly-retryable failure:
307
+ *
308
+ * - network failure
309
+ * - timeout
310
+ * - aborted
311
+ *
312
+ * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.
313
+ *
314
+ * Client and Server Devs can
315
+ * - investigate network, client and server performance and reliability issues
316
+ */
317
+ declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "timeout";
318
+ /**
319
+ * Returns true if the status is a valid communication status
320
+ */
321
+ declare const isStatusValid: (status: string) => boolean;
322
+ interface CommunicationStatusDetails {
323
+ status: CommunicationStatus;
324
+ httpStatus?: number;
325
+ message: string;
326
+ }
327
+ /**
328
+ * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code
329
+ *
330
+ * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)
331
+ *
332
+ * @param httpStatus - The HTTP status code to get the communication status for
333
+ * @returns The communication status for the given HTTP status code
334
+ */
335
+ declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatusDetails;
336
+ declare const getHttpStatus: (status: CommunicationStatus) => number;
337
+
338
+ export { type CommunicationStatus, aborted, clientFailure, clientFailureNotAuthorized, failure, getCommunicationStatus, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeout };
package/dist/index.js ADDED
@@ -0,0 +1,128 @@
1
+ // src/CommunicationStatus.ts
2
+ var communicationStatuses = {
3
+ success: { httpStatus: 200 },
4
+ missing: { httpStatus: 404, failure: true },
5
+ clientFailure: { httpStatus: 400, clientFailure: true, failure: true },
6
+ clientFailureNotAuthorized: { httpStatus: 403, clientFailure: true, failure: true },
7
+ serverFailure: { httpStatus: 500, failure: true, serverFailure: true },
8
+ networkFailure: { failure: true },
9
+ aborted: { failure: true },
10
+ pending: {},
11
+ failure: { httpStatus: 500, failure: true },
12
+ timeout: { failure: true }
13
+ };
14
+ var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/;
15
+ var success = "success";
16
+ var missing = "missing";
17
+ var clientFailure = "clientFailure";
18
+ var clientFailureNotAuthorized = "clientFailureNotAuthorized";
19
+ var serverFailure = "serverFailure";
20
+ var networkFailure = "networkFailure";
21
+ var aborted = "aborted";
22
+ var pending = "pending";
23
+ var failure = "failure";
24
+ var timeout = "timeout";
25
+ var isSuccess = (status) => status === success;
26
+ var isFailure = (status) => !!communicationStatuses[status]?.failure;
27
+ var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
28
+ var isServerFailure = (status) => !!communicationStatuses[status]?.serverFailure;
29
+ var isNetworkFailure = (status) => status === networkFailure;
30
+ var isNonClientFailure = (status) => isFailure(status) && !isClientFailure(status);
31
+ var isClientFailureNotAuthorized = (status) => status === clientFailureNotAuthorized;
32
+ var isAborted = (status) => status === aborted;
33
+ var isMissing = (status) => status === missing;
34
+ var isPending = (status) => status === pending;
35
+ var isTimeout = (status) => status === timeout;
36
+ var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
37
+ var isStatusValid = (status) => statusRegex.test(status);
38
+ var getCommunicationStatus = (httpStatus) => {
39
+ if (!httpStatus) return { status: networkFailure, message: "network failure" };
40
+ let status;
41
+ switch (Math.floor(httpStatus / 100)) {
42
+ case 2:
43
+ status = success;
44
+ break;
45
+ case 3:
46
+ status = missing;
47
+ break;
48
+ case 4:
49
+ switch (httpStatus) {
50
+ case 401:
51
+ case 403:
52
+ case 407:
53
+ case 451:
54
+ status = clientFailureNotAuthorized;
55
+ break;
56
+ case 404:
57
+ status = missing;
58
+ break;
59
+ default:
60
+ status = clientFailure;
61
+ }
62
+ break;
63
+ case 5:
64
+ switch (httpStatus) {
65
+ case 502:
66
+ case 503:
67
+ case 504:
68
+ status = networkFailure;
69
+ break;
70
+ case 511:
71
+ status = clientFailureNotAuthorized;
72
+ break;
73
+ case 501:
74
+ status = missing;
75
+ break;
76
+ // 501 Not Implemented - i.e. it "does not exist" currently - i.e. missing
77
+ case 505:
78
+ // HTTP Version Not Supported - client should change the request
79
+ case 530:
80
+ status = clientFailure;
81
+ break;
82
+ default:
83
+ status = serverFailure;
84
+ break;
85
+ }
86
+ break;
87
+ }
88
+ if (!status) throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);
89
+ return {
90
+ status,
91
+ httpStatus,
92
+ message: `${status} (${httpStatus})`
93
+ };
94
+ };
95
+ var getHttpStatus = (status) => {
96
+ const httpStatus = communicationStatuses[status]?.httpStatus;
97
+ if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`);
98
+ return httpStatus;
99
+ };
100
+ export {
101
+ aborted,
102
+ clientFailure,
103
+ clientFailureNotAuthorized,
104
+ failure,
105
+ getCommunicationStatus,
106
+ getHttpStatus,
107
+ isAborted,
108
+ isClientFailure,
109
+ isClientFailureNotAuthorized,
110
+ isFailure,
111
+ isMissing,
112
+ isNetworkFailure,
113
+ isNonClientFailure,
114
+ isPending,
115
+ isRetryableFailure,
116
+ isServerFailure,
117
+ isStatusValid,
118
+ isSuccess,
119
+ isTimeout,
120
+ missing,
121
+ networkFailure,
122
+ pending,
123
+ serverFailure,
124
+ statusRegex,
125
+ success,
126
+ timeout
127
+ };
128
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/CommunicationStatus.ts"],"sourcesContent":["interface CommunicationStatusInfo {\n httpStatus?: number\n failure?: boolean\n clientFailure?: boolean\n serverFailure?: boolean\n}\n\ninterface CommunicationStatuses {\n [key: string]: CommunicationStatusInfo\n}\n\nconst communicationStatuses: CommunicationStatuses = {\n success: { httpStatus: 200 },\n missing: { httpStatus: 404, failure: true },\n clientFailure: { httpStatus: 400, clientFailure: true, failure: true },\n clientFailureNotAuthorized: { httpStatus: 403, clientFailure: true, failure: true },\n serverFailure: { httpStatus: 500, failure: true, serverFailure: true },\n networkFailure: { failure: true },\n aborted: { failure: true },\n pending: {},\n failure: { httpStatus: 500, failure: true },\n timeout: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeout\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/\n\n// Export status constants\n/**\n * HTTP 2xx responses\n *\n * Client Can Automatically:\n * - Process the successful response\n * - Update UI to reflect success\n *\n * Client Developer Can:\n * - Handle the successful response data\n */\nexport const success: CommunicationStatus = \"success\"\n\n/**\n * Resource not found\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const missing: CommunicationStatus = \"missing\"\n\n/**\n * Client-side errors; i.e. the client needs to change the request somehow to succeed\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n * - Prompt user to correct invalid input\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n * - validate input before sending requests\n */\nexport const clientFailure: CommunicationStatus = \"clientFailure\"\n\n/**\n * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const clientFailureNotAuthorized: CommunicationStatus = \"clientFailureNotAuthorized\"\n\n/**\n * Server-side errors; i.e. internal server errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n * - Implement automatic retry with backoff\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error\n */\nexport const serverFailure: CommunicationStatus = \"serverFailure\"\n\n/**\n * Request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request when network is available\n * - Monitor network status for recovery\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n * - implement offline-first capabilities\n */\nexport const networkFailure: CommunicationStatus = \"networkFailure\"\n\n/**\n * Request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n * - cleanup any pending state\n *\n * Client Developer Can:\n * - fix the client to not abort the request unnecessarily\n * - implement proper cleanup on abort\n */\nexport const aborted: CommunicationStatus = \"aborted\"\n\n/**\n * Request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n * - implement proper loading states\n */\nexport const pending: CommunicationStatus = \"pending\"\n\n/**\n * Any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can Automatically:\n * - Show appropriate error message to user\n * - Implement generic error handling\n * - Log errors for debugging\n *\n * Client Developer Can:\n * - Use more specific is* functions for targeted error handling\n * - Implement proper error recovery strategies\n */\nexport const failure: CommunicationStatus = \"failure\"\n\n/**\n * Request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - fix the client to not timeout the request\n * - implement proper timeout handling\n */\nexport const timeout: CommunicationStatus = \"timeout\"\n\n// Core status check functions\n/** Returns true for HTTP 2xx responses */\nexport const isSuccess = (status: CommunicationStatus) => status === success\n\n/**\n * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can:\n * - Use a different is* function for more specific checks\n */\nexport const isFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.failure\n\n/**\n * Returns true for client-side errors\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n*/\nexport const isClientFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.clientFailure\n\n/**\n * Returns true for server-side errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)\n*/\nexport const isServerFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.serverFailure\n\n/**\n * Returns true when request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n*/\nexport const isNetworkFailure = (status: CommunicationStatus) => status === networkFailure\n\n/** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */\nexport const isNonClientFailure = (status: CommunicationStatus) => isFailure(status) && !isClientFailure(status)\n\n/**\n * Returns true for unauthorized requests (not authenticated or not authorized)\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n * - 511: Network Authentication Required\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const isClientFailureNotAuthorized = (status: CommunicationStatus) => status === clientFailureNotAuthorized\n\n/**\n * Returns true when request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n *\n * Client Developer Can:\n * - fix the client to not abort the request\n */\nexport const isAborted = (status: CommunicationStatus) => status === aborted\n\n/**\n * Returns true when resource not found / not available\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n * - 501: Not Implemented\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const isMissing = (status: CommunicationStatus) => status === missing\n\n/**\n * Returns true while request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request (trigging an \"aborted\" communication status)\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n */\nexport const isPending = (status: CommunicationStatus) => status === pending\n\n/**\n * Returns true if the request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - extend the timeout duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeout\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeout\n * - aborted\n *\n * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.\n *\n * Client and Server Devs can\n * - investigate network, client and server performance and reliability issues\n */\nexport const isRetryableFailure = (status: CommunicationStatus) => isNetworkFailure(status) || isTimeout(status) || isAborted(status)\n\n/**\n * Returns true if the status is a valid communication status\n */\nexport const isStatusValid = (status: string) => statusRegex.test(status);\n\ninterface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n\n/**\n * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code\n *\n * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)\n *\n * @param httpStatus - The HTTP status code to get the communication status for\n * @returns The communication status for the given HTTP status code\n */\nexport const getCommunicationStatus = (httpStatus?: number): CommunicationStatusDetails => {\n if (!httpStatus) return { status: networkFailure, message: \"network failure\" }\n\n let status: CommunicationStatus | undefined\n\n switch (Math.floor(httpStatus / 100)) {\n case 2: status = success; break\n case 3: status = missing; break\n case 4:\n switch (httpStatus) {\n case 401:\n case 403:\n case 407:\n case 451: status = clientFailureNotAuthorized; break\n case 404: status = missing; break\n default: status = clientFailure\n }\n break\n case 5:\n switch (httpStatus) {\n case 502:\n case 503:\n case 504: status = networkFailure; break\n case 511: status = clientFailureNotAuthorized; break\n case 501: status = missing; break // 501 Not Implemented - i.e. it \"does not exist\" currently - i.e. missing\n case 505: // HTTP Version Not Supported - client should change the request\n case 530: status = clientFailure; break\n default: status = serverFailure; break\n }\n break\n }\n\n if (!status) throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`)\n\n return {\n status,\n httpStatus,\n message: `${status} (${httpStatus})`\n }\n}\n\nexport const getHttpStatus = (status: CommunicationStatus): number => {\n const httpStatus = communicationStatuses[status]?.httpStatus\n if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`)\n return httpStatus\n}"],"mappings":"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","names":[]}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@art-suite/art-core-ts-communication-status",
3
+ "version": "0.1.0",
4
+ "description": "A TypeScript string utility library",
5
+ "keywords": [],
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/art-suite/art-suite-ts"
9
+ },
10
+ "license": "MIT",
11
+ "author": "Shane Delamore",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "main": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "scripts": {
26
+ "prebuild": "npm run clean",
27
+ "build": "npx sort-package-json;tsup src/index.ts --format esm,cjs --dts --sourcemap",
28
+ "build:clean": "npm run clean && npm run build",
29
+ "clean": "rm -rf dist",
30
+ "prepublishOnly": "npm run build",
31
+ "test": "vitest run",
32
+ "test:coverage": "vitest run --coverage",
33
+ "test:watch": "vitest"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^20.11.24",
37
+ "typescript": "^5.3.3",
38
+ "vitest": "^3.1.3"
39
+ }
40
+ }