@art-suite/art-core-ts-communication-status 0.3.9 → 0.4.2
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/dist/index.cjs +73 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -26
- package/dist/index.d.ts +40 -26
- package/dist/index.js +69 -38
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,9 +23,13 @@ __export(index_exports, {
|
|
|
23
23
|
aborted: () => aborted,
|
|
24
24
|
clientFailure: () => clientFailure,
|
|
25
25
|
clientFailureNotAuthorized: () => clientFailureNotAuthorized,
|
|
26
|
+
communicationStatuses: () => communicationStatuses,
|
|
26
27
|
failure: () => failure,
|
|
27
28
|
getCommunicationStatus: () => getCommunicationStatus,
|
|
28
29
|
getCommunicationStatusDetails: () => getCommunicationStatusDetails,
|
|
30
|
+
getCommunicationStatusFromHttpStatus: () => getCommunicationStatusFromHttpStatus,
|
|
31
|
+
getCommunicationStatusFromHttpStatusOrUndefined: () => getCommunicationStatusFromHttpStatusOrUndefined,
|
|
32
|
+
getCommunicationStatusOrUndefined: () => getCommunicationStatusOrUndefined,
|
|
29
33
|
getHttpStatus: () => getHttpStatus,
|
|
30
34
|
isAborted: () => isAborted,
|
|
31
35
|
isClientFailure: () => isClientFailure,
|
|
@@ -50,7 +54,19 @@ __export(index_exports, {
|
|
|
50
54
|
});
|
|
51
55
|
module.exports = __toCommonJS(index_exports);
|
|
52
56
|
|
|
53
|
-
// src/
|
|
57
|
+
// src/CommunicationStatusConsts.ts
|
|
58
|
+
var success = "success";
|
|
59
|
+
var missing = "missing";
|
|
60
|
+
var clientFailure = "clientFailure";
|
|
61
|
+
var clientFailureNotAuthorized = "clientFailureNotAuthorized";
|
|
62
|
+
var serverFailure = "serverFailure";
|
|
63
|
+
var networkFailure = "networkFailure";
|
|
64
|
+
var aborted = "aborted";
|
|
65
|
+
var pending = "pending";
|
|
66
|
+
var failure = "failure";
|
|
67
|
+
var timeoutFailure = "timeoutFailure";
|
|
68
|
+
|
|
69
|
+
// src/CommunicationStatusTypes.ts
|
|
54
70
|
var communicationStatuses = {
|
|
55
71
|
success: { httpStatus: 200 },
|
|
56
72
|
missing: { httpStatus: 404, failure: true },
|
|
@@ -63,17 +79,9 @@ var communicationStatuses = {
|
|
|
63
79
|
failure: { httpStatus: 500, failure: true },
|
|
64
80
|
timeoutFailure: { failure: true }
|
|
65
81
|
};
|
|
66
|
-
var statusRegex =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var clientFailure = "clientFailure";
|
|
70
|
-
var clientFailureNotAuthorized = "clientFailureNotAuthorized";
|
|
71
|
-
var serverFailure = "serverFailure";
|
|
72
|
-
var networkFailure = "networkFailure";
|
|
73
|
-
var aborted = "aborted";
|
|
74
|
-
var pending = "pending";
|
|
75
|
-
var failure = "failure";
|
|
76
|
-
var timeoutFailure = "timeoutFailure";
|
|
82
|
+
var statusRegex = new RegExp(`^(${Object.keys(communicationStatuses).join("|")})$`);
|
|
83
|
+
|
|
84
|
+
// src/CommunicationStatusTests.ts
|
|
77
85
|
var isSuccess = (status) => status === success;
|
|
78
86
|
var isFailure = (status) => !!communicationStatuses[status]?.failure;
|
|
79
87
|
var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
|
|
@@ -87,64 +95,87 @@ var isPending = (status) => status === pending;
|
|
|
87
95
|
var isTimeout = (status) => status === timeoutFailure;
|
|
88
96
|
var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
|
|
89
97
|
var isStatusValid = (status) => statusRegex.test(status);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
|
|
99
|
+
// src/CommunicationStatus.ts
|
|
100
|
+
var getCommunicationStatusFromHttpStatusOrUndefined = (httpStatus) => {
|
|
93
101
|
switch (Math.floor(httpStatus / 100)) {
|
|
94
102
|
case 2:
|
|
95
|
-
|
|
96
|
-
break;
|
|
103
|
+
return success;
|
|
97
104
|
case 3:
|
|
98
|
-
|
|
99
|
-
break;
|
|
105
|
+
return missing;
|
|
100
106
|
case 4:
|
|
101
107
|
switch (httpStatus) {
|
|
102
108
|
case 401:
|
|
103
109
|
case 403:
|
|
104
110
|
case 407:
|
|
105
111
|
case 451:
|
|
106
|
-
|
|
107
|
-
break;
|
|
112
|
+
return clientFailureNotAuthorized;
|
|
108
113
|
case 404:
|
|
109
|
-
|
|
110
|
-
break;
|
|
114
|
+
return missing;
|
|
111
115
|
default:
|
|
112
|
-
|
|
116
|
+
return clientFailure;
|
|
113
117
|
}
|
|
114
|
-
break;
|
|
115
118
|
case 5:
|
|
116
119
|
switch (httpStatus) {
|
|
117
120
|
case 502:
|
|
118
121
|
case 503:
|
|
119
122
|
case 504:
|
|
120
|
-
|
|
121
|
-
break;
|
|
123
|
+
return networkFailure;
|
|
122
124
|
case 511:
|
|
123
|
-
|
|
124
|
-
break;
|
|
125
|
+
return clientFailureNotAuthorized;
|
|
125
126
|
case 501:
|
|
126
|
-
|
|
127
|
-
break;
|
|
127
|
+
return missing;
|
|
128
128
|
// 501 Not Implemented - i.e. it "does not exist" currently - i.e. missing
|
|
129
129
|
case 505:
|
|
130
130
|
// HTTP Version Not Supported - client should change the request
|
|
131
131
|
case 530:
|
|
132
|
-
|
|
133
|
-
break;
|
|
132
|
+
return clientFailure;
|
|
134
133
|
default:
|
|
135
|
-
|
|
136
|
-
break;
|
|
134
|
+
return serverFailure;
|
|
137
135
|
}
|
|
138
|
-
break;
|
|
139
136
|
}
|
|
140
|
-
|
|
137
|
+
return void 0;
|
|
138
|
+
};
|
|
139
|
+
var getCommunicationStatusFromHttpStatus = (httpStatus) => {
|
|
140
|
+
const status = getCommunicationStatusFromHttpStatusOrUndefined(httpStatus);
|
|
141
|
+
if (!status) {
|
|
142
|
+
throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);
|
|
143
|
+
}
|
|
144
|
+
return status;
|
|
145
|
+
};
|
|
146
|
+
var getCommunicationStatusDetails = (httpStatus) => {
|
|
147
|
+
if (!httpStatus) return { status: networkFailure, message: "network failure" };
|
|
148
|
+
const status = getCommunicationStatusFromHttpStatus(httpStatus);
|
|
141
149
|
return {
|
|
142
150
|
status,
|
|
143
151
|
httpStatus,
|
|
144
152
|
message: `${status} (${httpStatus})`
|
|
145
153
|
};
|
|
146
154
|
};
|
|
147
|
-
var
|
|
155
|
+
var getCommunicationStatusOrUndefined = (status) => {
|
|
156
|
+
if (status == null) return void 0;
|
|
157
|
+
if (typeof status === "string") {
|
|
158
|
+
if (!isStatusValid(status)) {
|
|
159
|
+
return void 0;
|
|
160
|
+
}
|
|
161
|
+
return status;
|
|
162
|
+
}
|
|
163
|
+
if (typeof status === "number") {
|
|
164
|
+
return getCommunicationStatusFromHttpStatusOrUndefined(status);
|
|
165
|
+
}
|
|
166
|
+
return void 0;
|
|
167
|
+
};
|
|
168
|
+
var getCommunicationStatus = (status) => {
|
|
169
|
+
if (status == null) return void 0;
|
|
170
|
+
if (typeof status === "number") {
|
|
171
|
+
return getCommunicationStatusFromHttpStatus(status);
|
|
172
|
+
}
|
|
173
|
+
const communicationStatus = getCommunicationStatusOrUndefined(status);
|
|
174
|
+
if (!communicationStatus) {
|
|
175
|
+
throw new Error(`${status} is not a valid CommunicationStatus.`);
|
|
176
|
+
}
|
|
177
|
+
return communicationStatus;
|
|
178
|
+
};
|
|
148
179
|
var getHttpStatus = (status) => {
|
|
149
180
|
const httpStatus = communicationStatuses[status]?.httpStatus;
|
|
150
181
|
if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`);
|
|
@@ -155,9 +186,13 @@ var getHttpStatus = (status) => {
|
|
|
155
186
|
aborted,
|
|
156
187
|
clientFailure,
|
|
157
188
|
clientFailureNotAuthorized,
|
|
189
|
+
communicationStatuses,
|
|
158
190
|
failure,
|
|
159
191
|
getCommunicationStatus,
|
|
160
192
|
getCommunicationStatusDetails,
|
|
193
|
+
getCommunicationStatusFromHttpStatus,
|
|
194
|
+
getCommunicationStatusFromHttpStatusOrUndefined,
|
|
195
|
+
getCommunicationStatusOrUndefined,
|
|
161
196
|
getHttpStatus,
|
|
162
197
|
isAborted,
|
|
163
198
|
isClientFailure,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +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 timeoutFailure: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeoutFailure\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeoutFailure)$/\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 timeoutFailure the request\n * - implement proper timeoutFailure handling\n */\nexport const timeoutFailure: CommunicationStatus = \"timeoutFailure\"\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 timeoutFailure duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeoutFailure\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeoutFailure\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 getCommunicationStatusDetails = (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 getCommunicationStatus = (httpStatus?: number): CommunicationStatus =>\n getCommunicationStatusDetails(httpStatus).status\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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWA,IAAM,wBAA+C;AAAA,EACnD,SAAS,EAAE,YAAY,IAAI;AAAA,EAC3B,SAAS,EAAE,YAAY,KAAK,SAAS,KAAK;AAAA,EAC1C,eAAe,EAAE,YAAY,KAAK,eAAe,MAAM,SAAS,KAAK;AAAA,EACrE,4BAA4B,EAAE,YAAY,KAAK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oD;AAChG,MAAI,CAAC,WAAY,QAAO,EAAE,QAAQ,gBAAgB,SAAS,kBAAkB;AAE7E,MAAI;AAEJ,UAAQ,KAAK,MAAM,aAAa,GAAG,GAAG;AAAA,IACpC,KAAK;AAAG,eAAS;AAAS;AAAA,IAC1B,KAAK;AAAG,eAAS;AAAS;AAAA,IAC1B,KAAK;AACH,cAAQ,YAAY;AAAA,QAClB,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAK,mBAAS;AAA4B;AAAA,QAC/C,KAAK;AAAK,mBAAS;AAAS;AAAA,QAC5B;AAAS,mBAAS;AAAA,MACpB;AACA;AAAA,IACF,KAAK;AACH,cAAQ,YAAY;AAAA,QAClB,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAK,mBAAS;AAAgB;AAAA,QACnC,KAAK;AAAK,mBAAS;AAA4B;AAAA,QAC/C,KAAK;AAAK,mBAAS;AAAS;AAAA;AAAA,QAC5B,KAAK;AAAA;AAAA,QACL,KAAK;AAAK,mBAAS;AAAe;AAAA,QAClC;AAAS,mBAAS;AAAe;AAAA,MACnC;AACA;AAAA,EACJ;AAEA,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,cAAc,UAAU,0CAA0C;AAE/F,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,SAAS,GAAG,MAAM,KAAK,UAAU;AAAA,EACnC;AACF;AAEO,IAAM,yBAAyB,CAAC,eACrC,8BAA8B,UAAU,EAAE;AAErC,IAAM,gBAAgB,CAAC,WAAwC;AACpE,QAAM,aAAa,sBAAsB,MAAM,GAAG;AAClD,MAAI,CAAC,WAAY,OAAM,IAAI,MAAM,oCAAoC,MAAM,GAAG;AAC9E,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/CommunicationStatusConsts.ts","../src/CommunicationStatusTypes.ts","../src/CommunicationStatusTests.ts","../src/CommunicationStatus.ts"],"sourcesContent":["export * from \"./CommunicationStatus\";\nexport * from \"./CommunicationStatusConsts\";\nexport * from \"./CommunicationStatusTests\";\nexport * from \"./CommunicationStatusTypes\";\n","// Export status constants\nimport type { CommunicationStatus } from './CommunicationStatusTypes'\n\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 timeoutFailure the request\n * - implement proper timeoutFailure handling\n */\nexport const timeoutFailure: CommunicationStatus = \"timeoutFailure\"\n","interface CommunicationStatusInfo {\n httpStatus?: number\n failure?: boolean\n clientFailure?: boolean\n serverFailure?: boolean\n}\n\nexport const communicationStatuses: Record<string, CommunicationStatusInfo> = {\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 timeoutFailure: { failure: true }\n}\n\nexport type CommunicationStatuses = typeof communicationStatuses\nexport type CommunicationStatus = keyof CommunicationStatuses\n\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = new RegExp(`^(${Object.keys(communicationStatuses).join('|')})$`)\n\nexport interface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n","import { communicationStatuses, CommunicationStatus, statusRegex } from './CommunicationStatusTypes'\nimport { success, missing, clientFailure, clientFailureNotAuthorized, serverFailure, networkFailure, aborted, pending, timeoutFailure } from './CommunicationStatusConsts'\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 timeoutFailure duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeoutFailure\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeoutFailure\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","import { clientFailure, clientFailureNotAuthorized, missing, networkFailure, serverFailure, success } from './CommunicationStatusConsts';\nimport { isStatusValid } from './CommunicationStatusTests';\nimport { CommunicationStatus, CommunicationStatusDetails, communicationStatuses } from './CommunicationStatusTypes';\n\nexport const getCommunicationStatusFromHttpStatusOrUndefined = (httpStatus: number): CommunicationStatus | undefined => {\n switch (Math.floor(httpStatus / 100)) {\n case 2: return success;\n case 3: return missing;\n case 4:\n switch (httpStatus) {\n case 401:\n case 403:\n case 407:\n case 451: return clientFailureNotAuthorized;\n case 404: return missing;\n default: return clientFailure;\n }\n case 5:\n switch (httpStatus) {\n case 502:\n case 503:\n case 504: return networkFailure;\n case 511: return clientFailureNotAuthorized;\n case 501: return missing; // 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: return clientFailure;\n default: return serverFailure;\n }\n }\n return undefined;\n};\n\nexport const getCommunicationStatusFromHttpStatus = (httpStatus: number): CommunicationStatus => {\n const status = getCommunicationStatusFromHttpStatusOrUndefined(httpStatus);\n if (!status) {\n throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);\n }\n return status;\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 getCommunicationStatusDetails = (httpStatus?: number): CommunicationStatusDetails => {\n if (!httpStatus) return { status: networkFailure, message: \"network failure\" };\n\n const status = getCommunicationStatusFromHttpStatus(httpStatus);\n\n return {\n status,\n httpStatus,\n message: `${status} (${httpStatus})`\n };\n};\n\nexport const getCommunicationStatusOrUndefined = <T extends number | CommunicationStatus | null | undefined>(\n status: T\n): T extends null | undefined ? undefined : (CommunicationStatus | undefined) => {\n if (status == null) return undefined as any;\n if (typeof status === 'string') {\n if (!isStatusValid(status)) {\n return undefined;\n }\n return status as any;\n }\n if (typeof status === 'number') {\n return getCommunicationStatusFromHttpStatusOrUndefined(status) as any;\n }\n return undefined;\n};\n\nexport const getCommunicationStatus = <T extends number | CommunicationStatus | null | undefined>(\n status: T\n): T extends null | undefined ? undefined : CommunicationStatus => {\n if (status == null) return undefined as any;\n if (typeof status === 'number') {\n return getCommunicationStatusFromHttpStatus(status) as any;\n }\n const communicationStatus: CommunicationStatus | undefined = getCommunicationStatusOrUndefined(status);\n if (!communicationStatus) {\n throw new Error(`${status} is not a valid CommunicationStatus.`);\n }\n return communicationStatus as any;\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};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaO,IAAM,UAA+B;AAerC,IAAM,UAA+B;AAsBrC,IAAM,gBAAqC;AAmB3C,IAAM,6BAAkD;AAsBxD,IAAM,gBAAqC;AAgB3C,IAAM,iBAAsC;AAc5C,IAAM,UAA+B;AAcrC,IAAM,UAA+B;AAoBrC,IAAM,UAA+B;AAarC,IAAM,iBAAsC;;;ACjK5C,IAAM,wBAAiE;AAAA,EAC5E,SAAS,EAAE,YAAY,IAAI;AAAA,EAC3B,SAAS,EAAE,YAAY,KAAK,SAAS,KAAK;AAAA,EAC1C,eAAe,EAAE,YAAY,KAAK,eAAe,MAAM,SAAS,KAAK;AAAA,EACrE,4BAA4B,EAAE,YAAY,KAAK,eAAe,MAAM,SAAS,KAAK;AAAA,EAClF,eAAe,EAAE,YAAY,KAAK,SAAS,MAAM,eAAe,KAAK;AAAA,EACrE,gBAAgB,EAAE,SAAS,KAAK;AAAA,EAChC,SAAS,EAAE,SAAS,KAAK;AAAA,EACzB,SAAS,CAAC;AAAA,EACV,SAAS,EAAE,YAAY,KAAK,SAAS,KAAK;AAAA,EAC1C,gBAAgB,EAAE,SAAS,KAAK;AAClC;AAQO,IAAM,cAAc,IAAI,OAAO,KAAK,OAAO,KAAK,qBAAqB,EAAE,KAAK,GAAG,CA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|
1
|
+
interface CommunicationStatusInfo {
|
|
2
|
+
httpStatus?: number;
|
|
3
|
+
failure?: boolean;
|
|
4
|
+
clientFailure?: boolean;
|
|
5
|
+
serverFailure?: boolean;
|
|
6
|
+
}
|
|
7
|
+
declare const communicationStatuses: Record<string, CommunicationStatusInfo>;
|
|
8
|
+
type CommunicationStatuses = typeof communicationStatuses;
|
|
9
|
+
type CommunicationStatus = keyof CommunicationStatuses;
|
|
2
10
|
/**
|
|
3
11
|
* RegEx returns true for all valid communication statuses
|
|
4
12
|
*/
|
|
5
13
|
declare const statusRegex: RegExp;
|
|
14
|
+
interface CommunicationStatusDetails {
|
|
15
|
+
status: CommunicationStatus;
|
|
16
|
+
httpStatus?: number;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare const getCommunicationStatusFromHttpStatusOrUndefined: (httpStatus: number) => CommunicationStatus | undefined;
|
|
21
|
+
declare const getCommunicationStatusFromHttpStatus: (httpStatus: number) => CommunicationStatus;
|
|
22
|
+
/**
|
|
23
|
+
* Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code
|
|
24
|
+
*
|
|
25
|
+
* Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)
|
|
26
|
+
*
|
|
27
|
+
* @param httpStatus - The HTTP status code to get the communication status for
|
|
28
|
+
* @returns The communication status for the given HTTP status code
|
|
29
|
+
*/
|
|
30
|
+
declare const getCommunicationStatusDetails: (httpStatus?: number) => CommunicationStatusDetails;
|
|
31
|
+
declare const getCommunicationStatusOrUndefined: <T extends number | CommunicationStatus | null | undefined>(status: T) => T extends null | undefined ? undefined : (CommunicationStatus | undefined);
|
|
32
|
+
declare const getCommunicationStatus: <T extends number | CommunicationStatus | null | undefined>(status: T) => T extends null | undefined ? undefined : CommunicationStatus;
|
|
33
|
+
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
34
|
+
|
|
6
35
|
/**
|
|
7
36
|
* HTTP 2xx responses
|
|
8
37
|
*
|
|
@@ -160,8 +189,9 @@ declare const failure: CommunicationStatus;
|
|
|
160
189
|
* - implement proper timeoutFailure handling
|
|
161
190
|
*/
|
|
162
191
|
declare const timeoutFailure: CommunicationStatus;
|
|
192
|
+
|
|
163
193
|
/** Returns true for HTTP 2xx responses */
|
|
164
|
-
declare const isSuccess: (status: CommunicationStatus) =>
|
|
194
|
+
declare const isSuccess: (status: CommunicationStatus) => boolean;
|
|
165
195
|
/**
|
|
166
196
|
* Returns true for any error response (HTTP 4xx/5xx) or network/abort failures
|
|
167
197
|
*
|
|
@@ -226,7 +256,7 @@ declare const isServerFailure: (status: CommunicationStatus) => boolean;
|
|
|
226
256
|
* Client Developer Can:
|
|
227
257
|
* - fix bad network constants (like address, ports, etc.)
|
|
228
258
|
*/
|
|
229
|
-
declare const isNetworkFailure: (status: CommunicationStatus) =>
|
|
259
|
+
declare const isNetworkFailure: (status: CommunicationStatus) => boolean;
|
|
230
260
|
/** 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
261
|
declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
|
|
232
262
|
/**
|
|
@@ -247,7 +277,7 @@ declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
|
|
|
247
277
|
* Client and Server Developer Can:
|
|
248
278
|
* - fix authorization / authentication bugs
|
|
249
279
|
*/
|
|
250
|
-
declare const isClientFailureNotAuthorized: (status: CommunicationStatus) =>
|
|
280
|
+
declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => boolean;
|
|
251
281
|
/**
|
|
252
282
|
* Returns true when request was cancelled by client
|
|
253
283
|
*
|
|
@@ -258,7 +288,7 @@ declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => sta
|
|
|
258
288
|
* Client Developer Can:
|
|
259
289
|
* - fix the client to not abort the request
|
|
260
290
|
*/
|
|
261
|
-
declare const isAborted: (status: CommunicationStatus) =>
|
|
291
|
+
declare const isAborted: (status: CommunicationStatus) => boolean;
|
|
262
292
|
/**
|
|
263
293
|
* Returns true when resource not found / not available
|
|
264
294
|
*
|
|
@@ -273,7 +303,7 @@ declare const isAborted: (status: CommunicationStatus) => status is "aborted";
|
|
|
273
303
|
* Client Developer Can:
|
|
274
304
|
* - fix the bad resource paths
|
|
275
305
|
*/
|
|
276
|
-
declare const isMissing: (status: CommunicationStatus) =>
|
|
306
|
+
declare const isMissing: (status: CommunicationStatus) => boolean;
|
|
277
307
|
/**
|
|
278
308
|
* Returns true while request is in progress
|
|
279
309
|
*
|
|
@@ -285,7 +315,7 @@ declare const isMissing: (status: CommunicationStatus) => status is "missing";
|
|
|
285
315
|
* Client Developer Can:
|
|
286
316
|
* - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
|
|
287
317
|
*/
|
|
288
|
-
declare const isPending: (status: CommunicationStatus) =>
|
|
318
|
+
declare const isPending: (status: CommunicationStatus) => boolean;
|
|
289
319
|
/**
|
|
290
320
|
* Returns true if the request timed out
|
|
291
321
|
*
|
|
@@ -299,7 +329,7 @@ declare const isPending: (status: CommunicationStatus) => status is "pending";
|
|
|
299
329
|
* Server Developer Can:
|
|
300
330
|
* - improve server performance and reliability
|
|
301
331
|
*/
|
|
302
|
-
declare const isTimeout: (status: CommunicationStatus) =>
|
|
332
|
+
declare const isTimeout: (status: CommunicationStatus) => boolean;
|
|
303
333
|
/**
|
|
304
334
|
* Returns true if client can safely retry the request
|
|
305
335
|
*
|
|
@@ -314,26 +344,10 @@ declare const isTimeout: (status: CommunicationStatus) => status is "timeoutFail
|
|
|
314
344
|
* Client and Server Devs can
|
|
315
345
|
* - investigate network, client and server performance and reliability issues
|
|
316
346
|
*/
|
|
317
|
-
declare const isRetryableFailure: (status: CommunicationStatus) =>
|
|
347
|
+
declare const isRetryableFailure: (status: CommunicationStatus) => boolean;
|
|
318
348
|
/**
|
|
319
349
|
* Returns true if the status is a valid communication status
|
|
320
350
|
*/
|
|
321
351
|
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 getCommunicationStatusDetails: (httpStatus?: number) => CommunicationStatusDetails;
|
|
336
|
-
declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatus;
|
|
337
|
-
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
338
352
|
|
|
339
|
-
export { type CommunicationStatus, aborted, clientFailure, clientFailureNotAuthorized, failure, getCommunicationStatus, getCommunicationStatusDetails, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeoutFailure };
|
|
353
|
+
export { type CommunicationStatus, type CommunicationStatusDetails, type CommunicationStatuses, aborted, clientFailure, clientFailureNotAuthorized, communicationStatuses, failure, getCommunicationStatus, getCommunicationStatusDetails, getCommunicationStatusFromHttpStatus, getCommunicationStatusFromHttpStatusOrUndefined, getCommunicationStatusOrUndefined, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeoutFailure };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
interface CommunicationStatusInfo {
|
|
2
|
+
httpStatus?: number;
|
|
3
|
+
failure?: boolean;
|
|
4
|
+
clientFailure?: boolean;
|
|
5
|
+
serverFailure?: boolean;
|
|
6
|
+
}
|
|
7
|
+
declare const communicationStatuses: Record<string, CommunicationStatusInfo>;
|
|
8
|
+
type CommunicationStatuses = typeof communicationStatuses;
|
|
9
|
+
type CommunicationStatus = keyof CommunicationStatuses;
|
|
2
10
|
/**
|
|
3
11
|
* RegEx returns true for all valid communication statuses
|
|
4
12
|
*/
|
|
5
13
|
declare const statusRegex: RegExp;
|
|
14
|
+
interface CommunicationStatusDetails {
|
|
15
|
+
status: CommunicationStatus;
|
|
16
|
+
httpStatus?: number;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare const getCommunicationStatusFromHttpStatusOrUndefined: (httpStatus: number) => CommunicationStatus | undefined;
|
|
21
|
+
declare const getCommunicationStatusFromHttpStatus: (httpStatus: number) => CommunicationStatus;
|
|
22
|
+
/**
|
|
23
|
+
* Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code
|
|
24
|
+
*
|
|
25
|
+
* Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)
|
|
26
|
+
*
|
|
27
|
+
* @param httpStatus - The HTTP status code to get the communication status for
|
|
28
|
+
* @returns The communication status for the given HTTP status code
|
|
29
|
+
*/
|
|
30
|
+
declare const getCommunicationStatusDetails: (httpStatus?: number) => CommunicationStatusDetails;
|
|
31
|
+
declare const getCommunicationStatusOrUndefined: <T extends number | CommunicationStatus | null | undefined>(status: T) => T extends null | undefined ? undefined : (CommunicationStatus | undefined);
|
|
32
|
+
declare const getCommunicationStatus: <T extends number | CommunicationStatus | null | undefined>(status: T) => T extends null | undefined ? undefined : CommunicationStatus;
|
|
33
|
+
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
34
|
+
|
|
6
35
|
/**
|
|
7
36
|
* HTTP 2xx responses
|
|
8
37
|
*
|
|
@@ -160,8 +189,9 @@ declare const failure: CommunicationStatus;
|
|
|
160
189
|
* - implement proper timeoutFailure handling
|
|
161
190
|
*/
|
|
162
191
|
declare const timeoutFailure: CommunicationStatus;
|
|
192
|
+
|
|
163
193
|
/** Returns true for HTTP 2xx responses */
|
|
164
|
-
declare const isSuccess: (status: CommunicationStatus) =>
|
|
194
|
+
declare const isSuccess: (status: CommunicationStatus) => boolean;
|
|
165
195
|
/**
|
|
166
196
|
* Returns true for any error response (HTTP 4xx/5xx) or network/abort failures
|
|
167
197
|
*
|
|
@@ -226,7 +256,7 @@ declare const isServerFailure: (status: CommunicationStatus) => boolean;
|
|
|
226
256
|
* Client Developer Can:
|
|
227
257
|
* - fix bad network constants (like address, ports, etc.)
|
|
228
258
|
*/
|
|
229
|
-
declare const isNetworkFailure: (status: CommunicationStatus) =>
|
|
259
|
+
declare const isNetworkFailure: (status: CommunicationStatus) => boolean;
|
|
230
260
|
/** 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
261
|
declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
|
|
232
262
|
/**
|
|
@@ -247,7 +277,7 @@ declare const isNonClientFailure: (status: CommunicationStatus) => boolean;
|
|
|
247
277
|
* Client and Server Developer Can:
|
|
248
278
|
* - fix authorization / authentication bugs
|
|
249
279
|
*/
|
|
250
|
-
declare const isClientFailureNotAuthorized: (status: CommunicationStatus) =>
|
|
280
|
+
declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => boolean;
|
|
251
281
|
/**
|
|
252
282
|
* Returns true when request was cancelled by client
|
|
253
283
|
*
|
|
@@ -258,7 +288,7 @@ declare const isClientFailureNotAuthorized: (status: CommunicationStatus) => sta
|
|
|
258
288
|
* Client Developer Can:
|
|
259
289
|
* - fix the client to not abort the request
|
|
260
290
|
*/
|
|
261
|
-
declare const isAborted: (status: CommunicationStatus) =>
|
|
291
|
+
declare const isAborted: (status: CommunicationStatus) => boolean;
|
|
262
292
|
/**
|
|
263
293
|
* Returns true when resource not found / not available
|
|
264
294
|
*
|
|
@@ -273,7 +303,7 @@ declare const isAborted: (status: CommunicationStatus) => status is "aborted";
|
|
|
273
303
|
* Client Developer Can:
|
|
274
304
|
* - fix the bad resource paths
|
|
275
305
|
*/
|
|
276
|
-
declare const isMissing: (status: CommunicationStatus) =>
|
|
306
|
+
declare const isMissing: (status: CommunicationStatus) => boolean;
|
|
277
307
|
/**
|
|
278
308
|
* Returns true while request is in progress
|
|
279
309
|
*
|
|
@@ -285,7 +315,7 @@ declare const isMissing: (status: CommunicationStatus) => status is "missing";
|
|
|
285
315
|
* Client Developer Can:
|
|
286
316
|
* - if "pending" was not expected, maybe the client needs to `wait` for the request to complete?
|
|
287
317
|
*/
|
|
288
|
-
declare const isPending: (status: CommunicationStatus) =>
|
|
318
|
+
declare const isPending: (status: CommunicationStatus) => boolean;
|
|
289
319
|
/**
|
|
290
320
|
* Returns true if the request timed out
|
|
291
321
|
*
|
|
@@ -299,7 +329,7 @@ declare const isPending: (status: CommunicationStatus) => status is "pending";
|
|
|
299
329
|
* Server Developer Can:
|
|
300
330
|
* - improve server performance and reliability
|
|
301
331
|
*/
|
|
302
|
-
declare const isTimeout: (status: CommunicationStatus) =>
|
|
332
|
+
declare const isTimeout: (status: CommunicationStatus) => boolean;
|
|
303
333
|
/**
|
|
304
334
|
* Returns true if client can safely retry the request
|
|
305
335
|
*
|
|
@@ -314,26 +344,10 @@ declare const isTimeout: (status: CommunicationStatus) => status is "timeoutFail
|
|
|
314
344
|
* Client and Server Devs can
|
|
315
345
|
* - investigate network, client and server performance and reliability issues
|
|
316
346
|
*/
|
|
317
|
-
declare const isRetryableFailure: (status: CommunicationStatus) =>
|
|
347
|
+
declare const isRetryableFailure: (status: CommunicationStatus) => boolean;
|
|
318
348
|
/**
|
|
319
349
|
* Returns true if the status is a valid communication status
|
|
320
350
|
*/
|
|
321
351
|
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 getCommunicationStatusDetails: (httpStatus?: number) => CommunicationStatusDetails;
|
|
336
|
-
declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatus;
|
|
337
|
-
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
338
352
|
|
|
339
|
-
export { type CommunicationStatus, aborted, clientFailure, clientFailureNotAuthorized, failure, getCommunicationStatus, getCommunicationStatusDetails, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeoutFailure };
|
|
353
|
+
export { type CommunicationStatus, type CommunicationStatusDetails, type CommunicationStatuses, aborted, clientFailure, clientFailureNotAuthorized, communicationStatuses, failure, getCommunicationStatus, getCommunicationStatusDetails, getCommunicationStatusFromHttpStatus, getCommunicationStatusFromHttpStatusOrUndefined, getCommunicationStatusOrUndefined, getHttpStatus, isAborted, isClientFailure, isClientFailureNotAuthorized, isFailure, isMissing, isNetworkFailure, isNonClientFailure, isPending, isRetryableFailure, isServerFailure, isStatusValid, isSuccess, isTimeout, missing, networkFailure, pending, serverFailure, statusRegex, success, timeoutFailure };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/CommunicationStatusConsts.ts
|
|
2
|
+
var success = "success";
|
|
3
|
+
var missing = "missing";
|
|
4
|
+
var clientFailure = "clientFailure";
|
|
5
|
+
var clientFailureNotAuthorized = "clientFailureNotAuthorized";
|
|
6
|
+
var serverFailure = "serverFailure";
|
|
7
|
+
var networkFailure = "networkFailure";
|
|
8
|
+
var aborted = "aborted";
|
|
9
|
+
var pending = "pending";
|
|
10
|
+
var failure = "failure";
|
|
11
|
+
var timeoutFailure = "timeoutFailure";
|
|
12
|
+
|
|
13
|
+
// src/CommunicationStatusTypes.ts
|
|
2
14
|
var communicationStatuses = {
|
|
3
15
|
success: { httpStatus: 200 },
|
|
4
16
|
missing: { httpStatus: 404, failure: true },
|
|
@@ -11,17 +23,9 @@ var communicationStatuses = {
|
|
|
11
23
|
failure: { httpStatus: 500, failure: true },
|
|
12
24
|
timeoutFailure: { failure: true }
|
|
13
25
|
};
|
|
14
|
-
var statusRegex =
|
|
15
|
-
|
|
16
|
-
|
|
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 timeoutFailure = "timeoutFailure";
|
|
26
|
+
var statusRegex = new RegExp(`^(${Object.keys(communicationStatuses).join("|")})$`);
|
|
27
|
+
|
|
28
|
+
// src/CommunicationStatusTests.ts
|
|
25
29
|
var isSuccess = (status) => status === success;
|
|
26
30
|
var isFailure = (status) => !!communicationStatuses[status]?.failure;
|
|
27
31
|
var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
|
|
@@ -35,64 +39,87 @@ var isPending = (status) => status === pending;
|
|
|
35
39
|
var isTimeout = (status) => status === timeoutFailure;
|
|
36
40
|
var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
|
|
37
41
|
var isStatusValid = (status) => statusRegex.test(status);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
|
|
43
|
+
// src/CommunicationStatus.ts
|
|
44
|
+
var getCommunicationStatusFromHttpStatusOrUndefined = (httpStatus) => {
|
|
41
45
|
switch (Math.floor(httpStatus / 100)) {
|
|
42
46
|
case 2:
|
|
43
|
-
|
|
44
|
-
break;
|
|
47
|
+
return success;
|
|
45
48
|
case 3:
|
|
46
|
-
|
|
47
|
-
break;
|
|
49
|
+
return missing;
|
|
48
50
|
case 4:
|
|
49
51
|
switch (httpStatus) {
|
|
50
52
|
case 401:
|
|
51
53
|
case 403:
|
|
52
54
|
case 407:
|
|
53
55
|
case 451:
|
|
54
|
-
|
|
55
|
-
break;
|
|
56
|
+
return clientFailureNotAuthorized;
|
|
56
57
|
case 404:
|
|
57
|
-
|
|
58
|
-
break;
|
|
58
|
+
return missing;
|
|
59
59
|
default:
|
|
60
|
-
|
|
60
|
+
return clientFailure;
|
|
61
61
|
}
|
|
62
|
-
break;
|
|
63
62
|
case 5:
|
|
64
63
|
switch (httpStatus) {
|
|
65
64
|
case 502:
|
|
66
65
|
case 503:
|
|
67
66
|
case 504:
|
|
68
|
-
|
|
69
|
-
break;
|
|
67
|
+
return networkFailure;
|
|
70
68
|
case 511:
|
|
71
|
-
|
|
72
|
-
break;
|
|
69
|
+
return clientFailureNotAuthorized;
|
|
73
70
|
case 501:
|
|
74
|
-
|
|
75
|
-
break;
|
|
71
|
+
return missing;
|
|
76
72
|
// 501 Not Implemented - i.e. it "does not exist" currently - i.e. missing
|
|
77
73
|
case 505:
|
|
78
74
|
// HTTP Version Not Supported - client should change the request
|
|
79
75
|
case 530:
|
|
80
|
-
|
|
81
|
-
break;
|
|
76
|
+
return clientFailure;
|
|
82
77
|
default:
|
|
83
|
-
|
|
84
|
-
break;
|
|
78
|
+
return serverFailure;
|
|
85
79
|
}
|
|
86
|
-
break;
|
|
87
80
|
}
|
|
88
|
-
|
|
81
|
+
return void 0;
|
|
82
|
+
};
|
|
83
|
+
var getCommunicationStatusFromHttpStatus = (httpStatus) => {
|
|
84
|
+
const status = getCommunicationStatusFromHttpStatusOrUndefined(httpStatus);
|
|
85
|
+
if (!status) {
|
|
86
|
+
throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);
|
|
87
|
+
}
|
|
88
|
+
return status;
|
|
89
|
+
};
|
|
90
|
+
var getCommunicationStatusDetails = (httpStatus) => {
|
|
91
|
+
if (!httpStatus) return { status: networkFailure, message: "network failure" };
|
|
92
|
+
const status = getCommunicationStatusFromHttpStatus(httpStatus);
|
|
89
93
|
return {
|
|
90
94
|
status,
|
|
91
95
|
httpStatus,
|
|
92
96
|
message: `${status} (${httpStatus})`
|
|
93
97
|
};
|
|
94
98
|
};
|
|
95
|
-
var
|
|
99
|
+
var getCommunicationStatusOrUndefined = (status) => {
|
|
100
|
+
if (status == null) return void 0;
|
|
101
|
+
if (typeof status === "string") {
|
|
102
|
+
if (!isStatusValid(status)) {
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
105
|
+
return status;
|
|
106
|
+
}
|
|
107
|
+
if (typeof status === "number") {
|
|
108
|
+
return getCommunicationStatusFromHttpStatusOrUndefined(status);
|
|
109
|
+
}
|
|
110
|
+
return void 0;
|
|
111
|
+
};
|
|
112
|
+
var getCommunicationStatus = (status) => {
|
|
113
|
+
if (status == null) return void 0;
|
|
114
|
+
if (typeof status === "number") {
|
|
115
|
+
return getCommunicationStatusFromHttpStatus(status);
|
|
116
|
+
}
|
|
117
|
+
const communicationStatus = getCommunicationStatusOrUndefined(status);
|
|
118
|
+
if (!communicationStatus) {
|
|
119
|
+
throw new Error(`${status} is not a valid CommunicationStatus.`);
|
|
120
|
+
}
|
|
121
|
+
return communicationStatus;
|
|
122
|
+
};
|
|
96
123
|
var getHttpStatus = (status) => {
|
|
97
124
|
const httpStatus = communicationStatuses[status]?.httpStatus;
|
|
98
125
|
if (!httpStatus) throw new Error(`There is no valid HttpStatus for ${status}.`);
|
|
@@ -102,9 +129,13 @@ export {
|
|
|
102
129
|
aborted,
|
|
103
130
|
clientFailure,
|
|
104
131
|
clientFailureNotAuthorized,
|
|
132
|
+
communicationStatuses,
|
|
105
133
|
failure,
|
|
106
134
|
getCommunicationStatus,
|
|
107
135
|
getCommunicationStatusDetails,
|
|
136
|
+
getCommunicationStatusFromHttpStatus,
|
|
137
|
+
getCommunicationStatusFromHttpStatusOrUndefined,
|
|
138
|
+
getCommunicationStatusOrUndefined,
|
|
108
139
|
getHttpStatus,
|
|
109
140
|
isAborted,
|
|
110
141
|
isClientFailure,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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 timeoutFailure: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeoutFailure\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeoutFailure)$/\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 timeoutFailure the request\n * - implement proper timeoutFailure handling\n */\nexport const timeoutFailure: CommunicationStatus = \"timeoutFailure\"\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 timeoutFailure duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeoutFailure\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeoutFailure\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 getCommunicationStatusDetails = (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 getCommunicationStatus = (httpStatus?: number): CommunicationStatus =>\n getCommunicationStatusDetails(httpStatus).status\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":";AAWA,IAAM,wBAA+C;AAAA,EACnD,SAAS,EAAE,YAAY,IAAI;AAAA,EAC3B,SAAS,EAAE,YAAY,KAAK,SAAS,KAAK;AAAA,EAC1C,eAAe,EAAE,YAAY,KAAK,eAAe,MAAM,SAAS,KAAK;AAAA,EACrE,4BAA4B,EAAE,YAAY,KAAK,eAAe,MAAM,SAAS,KAAK;AAAA,EAClF,eAAe,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Q,gBAAgB,SAAS,kBAAkB;AAE7E,MAAI;AAEJ,UAAQ,KAAK,MAAM,aAAa,GAAG,GAAG;AAAA,IACpC,KAAK;AAAG,eAAS;AAAS;AAAA,IAC1B,KAAK;AAAG,eAAS;AAAS;AAAA,IAC1B,KAAK;AACH,cAAQ,YAAY;AAAA,QAClB,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAK,mBAAS;AAA4B;AAAA,QAC/C,KAAK;AAAK,mBAAS;AAAS;AAAA,QAC5B;AAAS,mBAAS;AAAA,MACpB;AACA;AAAA,IACF,KAAK;AACH,cAAQ,YAAY;AAAA,QAClB,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAK,mBAAS;AAAgB;AAAA,QACnC,KAAK;AAAK,mBAAS;AAA4B;AAAA,QAC/C,KAAK;AAAK,mBAAS;AAAS;AAAA;AAAA,QAC5B,KAAK;AAAA;AAAA,QACL,KAAK;AAAK,mBAAS;AAAe;AAAA,QAClC;AAAS,mBAAS;AAAe;AAAA,MACnC;AACA;AAAA,EACJ;AAEA,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,cAAc,UAAU,0CAA0C;AAE/F,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,SAAS,GAAG,MAAM,KAAK,UAAU;AAAA,EACnC;AACF;AAEO,IAAM,yBAAyB,CAAC,eACrC,8BAA8B,UAAU,EAAE;AAErC,IAAM,gBAAgB,CAAC,WAAwC;AACpE,QAAM,aAAa,sBAAsB,MAAM,GAAG;AAClD,MAAI,CAAC,WAAY,OAAM,IAAI,MAAM,oCAAoC,MAAM,GAAG;AAC9E,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/CommunicationStatusConsts.ts","../src/CommunicationStatusTypes.ts","../src/CommunicationStatusTests.ts","../src/CommunicationStatus.ts"],"sourcesContent":["// Export status constants\nimport type { CommunicationStatus } from './CommunicationStatusTypes'\n\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 timeoutFailure the request\n * - implement proper timeoutFailure handling\n */\nexport const timeoutFailure: CommunicationStatus = \"timeoutFailure\"\n","interface CommunicationStatusInfo {\n httpStatus?: number\n failure?: boolean\n clientFailure?: boolean\n serverFailure?: boolean\n}\n\nexport const communicationStatuses: Record<string, CommunicationStatusInfo> = {\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 timeoutFailure: { failure: true }\n}\n\nexport type CommunicationStatuses = typeof communicationStatuses\nexport type CommunicationStatus = keyof CommunicationStatuses\n\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = new RegExp(`^(${Object.keys(communicationStatuses).join('|')})$`)\n\nexport interface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n","import { communicationStatuses, CommunicationStatus, statusRegex } from './CommunicationStatusTypes'\nimport { success, missing, clientFailure, clientFailureNotAuthorized, serverFailure, networkFailure, aborted, pending, timeoutFailure } from './CommunicationStatusConsts'\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 timeoutFailure duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeoutFailure\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeoutFailure\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","import { clientFailure, clientFailureNotAuthorized, missing, networkFailure, serverFailure, success } from './CommunicationStatusConsts';\nimport { isStatusValid } from './CommunicationStatusTests';\nimport { CommunicationStatus, CommunicationStatusDetails, communicationStatuses } from './CommunicationStatusTypes';\n\nexport const getCommunicationStatusFromHttpStatusOrUndefined = (httpStatus: number): CommunicationStatus | undefined => {\n switch (Math.floor(httpStatus / 100)) {\n case 2: return success;\n case 3: return missing;\n case 4:\n switch (httpStatus) {\n case 401:\n case 403:\n case 407:\n case 451: return clientFailureNotAuthorized;\n case 404: return missing;\n default: return clientFailure;\n }\n case 5:\n switch (httpStatus) {\n case 502:\n case 503:\n case 504: return networkFailure;\n case 511: return clientFailureNotAuthorized;\n case 501: return missing; // 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: return clientFailure;\n default: return serverFailure;\n }\n }\n return undefined;\n};\n\nexport const getCommunicationStatusFromHttpStatus = (httpStatus: number): CommunicationStatus => {\n const status = getCommunicationStatusFromHttpStatusOrUndefined(httpStatus);\n if (!status) {\n throw new Error(`httpStatus ${httpStatus} is not a supported CommunicationStatus.`);\n }\n return status;\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 getCommunicationStatusDetails = (httpStatus?: number): CommunicationStatusDetails => {\n if (!httpStatus) return { status: networkFailure, message: \"network failure\" };\n\n const status = getCommunicationStatusFromHttpStatus(httpStatus);\n\n return {\n status,\n httpStatus,\n message: `${status} (${httpStatus})`\n };\n};\n\nexport const getCommunicationStatusOrUndefined = <T extends number | CommunicationStatus | null | undefined>(\n status: T\n): T extends null | undefined ? undefined : (CommunicationStatus | undefined) => {\n if (status == null) return undefined as any;\n if (typeof status === 'string') {\n if (!isStatusValid(status)) {\n return undefined;\n }\n return status as any;\n }\n if (typeof status === 'number') {\n return getCommunicationStatusFromHttpStatusOrUndefined(status) as any;\n }\n return undefined;\n};\n\nexport const getCommunicationStatus = <T extends number | CommunicationStatus | null | undefined>(\n status: T\n): T extends null | undefined ? undefined : CommunicationStatus => {\n if (status == null) return undefined as any;\n if (typeof status === 'number') {\n return getCommunicationStatusFromHttpStatus(status) as any;\n }\n const communicationStatus: CommunicationStatus | undefined = getCommunicationStatusOrUndefined(status);\n if (!communicationStatus) {\n throw new Error(`${status} is not a valid CommunicationStatus.`);\n }\n return communicationStatus as any;\n};\n\nexport const getHttpStatus = (status: CommunicationStatus): number => {\n const httpStatus = communicationStatuses[status]?.httpStatus;\n if (!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