@art-suite/art-core-ts-communication-status 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -46,7 +46,7 @@ __export(index_exports, {
|
|
|
46
46
|
serverFailure: () => serverFailure,
|
|
47
47
|
statusRegex: () => statusRegex,
|
|
48
48
|
success: () => success,
|
|
49
|
-
|
|
49
|
+
timeoutFailure: () => timeoutFailure
|
|
50
50
|
});
|
|
51
51
|
module.exports = __toCommonJS(index_exports);
|
|
52
52
|
|
|
@@ -61,9 +61,9 @@ var communicationStatuses = {
|
|
|
61
61
|
aborted: { failure: true },
|
|
62
62
|
pending: {},
|
|
63
63
|
failure: { httpStatus: 500, failure: true },
|
|
64
|
-
|
|
64
|
+
timeoutFailure: { failure: true }
|
|
65
65
|
};
|
|
66
|
-
var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|
|
|
66
|
+
var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeoutFailure)$/;
|
|
67
67
|
var success = "success";
|
|
68
68
|
var missing = "missing";
|
|
69
69
|
var clientFailure = "clientFailure";
|
|
@@ -73,7 +73,7 @@ var networkFailure = "networkFailure";
|
|
|
73
73
|
var aborted = "aborted";
|
|
74
74
|
var pending = "pending";
|
|
75
75
|
var failure = "failure";
|
|
76
|
-
var
|
|
76
|
+
var timeoutFailure = "timeoutFailure";
|
|
77
77
|
var isSuccess = (status) => status === success;
|
|
78
78
|
var isFailure = (status) => !!communicationStatuses[status]?.failure;
|
|
79
79
|
var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
|
|
@@ -84,7 +84,7 @@ var isClientFailureNotAuthorized = (status) => status === clientFailureNotAuthor
|
|
|
84
84
|
var isAborted = (status) => status === aborted;
|
|
85
85
|
var isMissing = (status) => status === missing;
|
|
86
86
|
var isPending = (status) => status === pending;
|
|
87
|
-
var isTimeout = (status) => status ===
|
|
87
|
+
var isTimeout = (status) => status === timeoutFailure;
|
|
88
88
|
var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
|
|
89
89
|
var isStatusValid = (status) => statusRegex.test(status);
|
|
90
90
|
var getCommunicationStatusDetails = (httpStatus) => {
|
|
@@ -178,6 +178,6 @@ var getHttpStatus = (status) => {
|
|
|
178
178
|
serverFailure,
|
|
179
179
|
statusRegex,
|
|
180
180
|
success,
|
|
181
|
-
|
|
181
|
+
timeoutFailure
|
|
182
182
|
});
|
|
183
183
|
//# sourceMappingURL=index.cjs.map
|
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 timeout: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeout\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/\n\n// Export status constants\n/**\n * HTTP 2xx responses\n *\n * Client Can Automatically:\n * - Process the successful response\n * - Update UI to reflect success\n *\n * Client Developer Can:\n * - Handle the successful response data\n */\nexport const success: CommunicationStatus = \"success\"\n\n/**\n * Resource not found\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const missing: CommunicationStatus = \"missing\"\n\n/**\n * Client-side errors; i.e. the client needs to change the request somehow to succeed\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n * - Prompt user to correct invalid input\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n * - validate input before sending requests\n */\nexport const clientFailure: CommunicationStatus = \"clientFailure\"\n\n/**\n * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const clientFailureNotAuthorized: CommunicationStatus = \"clientFailureNotAuthorized\"\n\n/**\n * Server-side errors; i.e. internal server errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n * - Implement automatic retry with backoff\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error\n */\nexport const serverFailure: CommunicationStatus = \"serverFailure\"\n\n/**\n * Request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request when network is available\n * - Monitor network status for recovery\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n * - implement offline-first capabilities\n */\nexport const networkFailure: CommunicationStatus = \"networkFailure\"\n\n/**\n * Request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n * - cleanup any pending state\n *\n * Client Developer Can:\n * - fix the client to not abort the request unnecessarily\n * - implement proper cleanup on abort\n */\nexport const aborted: CommunicationStatus = \"aborted\"\n\n/**\n * Request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n * - implement proper loading states\n */\nexport const pending: CommunicationStatus = \"pending\"\n\n/**\n * Any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can Automatically:\n * - Show appropriate error message to user\n * - Implement generic error handling\n * - Log errors for debugging\n *\n * Client Developer Can:\n * - Use more specific is* functions for targeted error handling\n * - Implement proper error recovery strategies\n */\nexport const failure: CommunicationStatus = \"failure\"\n\n/**\n * Request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - fix the client to not timeout the request\n * - implement proper timeout handling\n */\nexport const timeout: CommunicationStatus = \"timeout\"\n\n// Core status check functions\n/** Returns true for HTTP 2xx responses */\nexport const isSuccess = (status: CommunicationStatus) => status === success\n\n/**\n * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can:\n * - Use a different is* function for more specific checks\n */\nexport const isFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.failure\n\n/**\n * Returns true for client-side errors\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n*/\nexport const isClientFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.clientFailure\n\n/**\n * Returns true for server-side errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)\n*/\nexport const isServerFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.serverFailure\n\n/**\n * Returns true when request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n*/\nexport const isNetworkFailure = (status: CommunicationStatus) => status === networkFailure\n\n/** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */\nexport const isNonClientFailure = (status: CommunicationStatus) => isFailure(status) && !isClientFailure(status)\n\n/**\n * Returns true for unauthorized requests (not authenticated or not authorized)\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n * - 511: Network Authentication Required\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const isClientFailureNotAuthorized = (status: CommunicationStatus) => status === clientFailureNotAuthorized\n\n/**\n * Returns true when request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n *\n * Client Developer Can:\n * - fix the client to not abort the request\n */\nexport const isAborted = (status: CommunicationStatus) => status === aborted\n\n/**\n * Returns true when resource not found / not available\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n * - 501: Not Implemented\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const isMissing = (status: CommunicationStatus) => status === missing\n\n/**\n * Returns true while request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request (trigging an \"aborted\" communication status)\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n */\nexport const isPending = (status: CommunicationStatus) => status === pending\n\n/**\n * Returns true if the request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - extend the timeout duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeout\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeout\n * - aborted\n *\n * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.\n *\n * Client and Server Devs can\n * - investigate network, client and server performance and reliability issues\n */\nexport const isRetryableFailure = (status: CommunicationStatus) => isNetworkFailure(status) || isTimeout(status) || isAborted(status)\n\n/**\n * Returns true if the status is a valid communication status\n */\nexport const isStatusValid = (status: string) => statusRegex.test(status);\n\ninterface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n\n/**\n * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code\n *\n * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)\n *\n * @param httpStatus - The HTTP status code to get the communication status for\n * @returns The communication status for the given HTTP status code\n */\nexport const 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;AAA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AK,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/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,MAA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ure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "
|
|
1
|
+
type CommunicationStatus = "success" | "missing" | "clientFailure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "timeoutFailure";
|
|
2
2
|
/**
|
|
3
3
|
* RegEx returns true for all valid communication statuses
|
|
4
4
|
*/
|
|
@@ -156,10 +156,10 @@ declare const failure: CommunicationStatus;
|
|
|
156
156
|
* - try again (automatically or via user action)
|
|
157
157
|
*
|
|
158
158
|
* Client Developer Can:
|
|
159
|
-
* - fix the client to not
|
|
160
|
-
* - implement proper
|
|
159
|
+
* - fix the client to not timeoutFailure the request
|
|
160
|
+
* - implement proper timeoutFailure handling
|
|
161
161
|
*/
|
|
162
|
-
declare const
|
|
162
|
+
declare const timeoutFailure: CommunicationStatus;
|
|
163
163
|
/** Returns true for HTTP 2xx responses */
|
|
164
164
|
declare const isSuccess: (status: CommunicationStatus) => status is "success";
|
|
165
165
|
/**
|
|
@@ -294,19 +294,19 @@ declare const isPending: (status: CommunicationStatus) => status is "pending";
|
|
|
294
294
|
* - try again (automatically or via user action)
|
|
295
295
|
*
|
|
296
296
|
* Client Developer Can:
|
|
297
|
-
* - extend the
|
|
297
|
+
* - extend the timeoutFailure duration
|
|
298
298
|
*
|
|
299
299
|
* Server Developer Can:
|
|
300
300
|
* - improve server performance and reliability
|
|
301
301
|
*/
|
|
302
|
-
declare const isTimeout: (status: CommunicationStatus) => status is "
|
|
302
|
+
declare const isTimeout: (status: CommunicationStatus) => status is "timeoutFailure";
|
|
303
303
|
/**
|
|
304
304
|
* Returns true if client can safely retry the request
|
|
305
305
|
*
|
|
306
306
|
* A a clearly-retryable failure:
|
|
307
307
|
*
|
|
308
308
|
* - network failure
|
|
309
|
-
* -
|
|
309
|
+
* - timeoutFailure
|
|
310
310
|
* - aborted
|
|
311
311
|
*
|
|
312
312
|
* Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.
|
|
@@ -314,7 +314,7 @@ declare const isTimeout: (status: CommunicationStatus) => status is "timeout";
|
|
|
314
314
|
* Client and Server Devs can
|
|
315
315
|
* - investigate network, client and server performance and reliability issues
|
|
316
316
|
*/
|
|
317
|
-
declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "
|
|
317
|
+
declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "timeoutFailure";
|
|
318
318
|
/**
|
|
319
319
|
* Returns true if the status is a valid communication status
|
|
320
320
|
*/
|
|
@@ -336,4 +336,4 @@ declare const getCommunicationStatusDetails: (httpStatus?: number) => Communicat
|
|
|
336
336
|
declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatus;
|
|
337
337
|
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
338
338
|
|
|
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,
|
|
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type CommunicationStatus = "success" | "missing" | "clientFailure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "
|
|
1
|
+
type CommunicationStatus = "success" | "missing" | "clientFailure" | "clientFailureNotAuthorized" | "serverFailure" | "networkFailure" | "aborted" | "pending" | "failure" | "timeoutFailure";
|
|
2
2
|
/**
|
|
3
3
|
* RegEx returns true for all valid communication statuses
|
|
4
4
|
*/
|
|
@@ -156,10 +156,10 @@ declare const failure: CommunicationStatus;
|
|
|
156
156
|
* - try again (automatically or via user action)
|
|
157
157
|
*
|
|
158
158
|
* Client Developer Can:
|
|
159
|
-
* - fix the client to not
|
|
160
|
-
* - implement proper
|
|
159
|
+
* - fix the client to not timeoutFailure the request
|
|
160
|
+
* - implement proper timeoutFailure handling
|
|
161
161
|
*/
|
|
162
|
-
declare const
|
|
162
|
+
declare const timeoutFailure: CommunicationStatus;
|
|
163
163
|
/** Returns true for HTTP 2xx responses */
|
|
164
164
|
declare const isSuccess: (status: CommunicationStatus) => status is "success";
|
|
165
165
|
/**
|
|
@@ -294,19 +294,19 @@ declare const isPending: (status: CommunicationStatus) => status is "pending";
|
|
|
294
294
|
* - try again (automatically or via user action)
|
|
295
295
|
*
|
|
296
296
|
* Client Developer Can:
|
|
297
|
-
* - extend the
|
|
297
|
+
* - extend the timeoutFailure duration
|
|
298
298
|
*
|
|
299
299
|
* Server Developer Can:
|
|
300
300
|
* - improve server performance and reliability
|
|
301
301
|
*/
|
|
302
|
-
declare const isTimeout: (status: CommunicationStatus) => status is "
|
|
302
|
+
declare const isTimeout: (status: CommunicationStatus) => status is "timeoutFailure";
|
|
303
303
|
/**
|
|
304
304
|
* Returns true if client can safely retry the request
|
|
305
305
|
*
|
|
306
306
|
* A a clearly-retryable failure:
|
|
307
307
|
*
|
|
308
308
|
* - network failure
|
|
309
|
-
* -
|
|
309
|
+
* - timeoutFailure
|
|
310
310
|
* - aborted
|
|
311
311
|
*
|
|
312
312
|
* Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.
|
|
@@ -314,7 +314,7 @@ declare const isTimeout: (status: CommunicationStatus) => status is "timeout";
|
|
|
314
314
|
* Client and Server Devs can
|
|
315
315
|
* - investigate network, client and server performance and reliability issues
|
|
316
316
|
*/
|
|
317
|
-
declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "
|
|
317
|
+
declare const isRetryableFailure: (status: CommunicationStatus) => status is "networkFailure" | "aborted" | "timeoutFailure";
|
|
318
318
|
/**
|
|
319
319
|
* Returns true if the status is a valid communication status
|
|
320
320
|
*/
|
|
@@ -336,4 +336,4 @@ declare const getCommunicationStatusDetails: (httpStatus?: number) => Communicat
|
|
|
336
336
|
declare const getCommunicationStatus: (httpStatus?: number) => CommunicationStatus;
|
|
337
337
|
declare const getHttpStatus: (status: CommunicationStatus) => number;
|
|
338
338
|
|
|
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,
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -9,9 +9,9 @@ var communicationStatuses = {
|
|
|
9
9
|
aborted: { failure: true },
|
|
10
10
|
pending: {},
|
|
11
11
|
failure: { httpStatus: 500, failure: true },
|
|
12
|
-
|
|
12
|
+
timeoutFailure: { failure: true }
|
|
13
13
|
};
|
|
14
|
-
var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|
|
|
14
|
+
var statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeoutFailure)$/;
|
|
15
15
|
var success = "success";
|
|
16
16
|
var missing = "missing";
|
|
17
17
|
var clientFailure = "clientFailure";
|
|
@@ -21,7 +21,7 @@ var networkFailure = "networkFailure";
|
|
|
21
21
|
var aborted = "aborted";
|
|
22
22
|
var pending = "pending";
|
|
23
23
|
var failure = "failure";
|
|
24
|
-
var
|
|
24
|
+
var timeoutFailure = "timeoutFailure";
|
|
25
25
|
var isSuccess = (status) => status === success;
|
|
26
26
|
var isFailure = (status) => !!communicationStatuses[status]?.failure;
|
|
27
27
|
var isClientFailure = (status) => !!communicationStatuses[status]?.clientFailure;
|
|
@@ -32,7 +32,7 @@ var isClientFailureNotAuthorized = (status) => status === clientFailureNotAuthor
|
|
|
32
32
|
var isAborted = (status) => status === aborted;
|
|
33
33
|
var isMissing = (status) => status === missing;
|
|
34
34
|
var isPending = (status) => status === pending;
|
|
35
|
-
var isTimeout = (status) => status ===
|
|
35
|
+
var isTimeout = (status) => status === timeoutFailure;
|
|
36
36
|
var isRetryableFailure = (status) => isNetworkFailure(status) || isTimeout(status) || isAborted(status);
|
|
37
37
|
var isStatusValid = (status) => statusRegex.test(status);
|
|
38
38
|
var getCommunicationStatusDetails = (httpStatus) => {
|
|
@@ -125,6 +125,6 @@ export {
|
|
|
125
125
|
serverFailure,
|
|
126
126
|
statusRegex,
|
|
127
127
|
success,
|
|
128
|
-
|
|
128
|
+
timeoutFailure
|
|
129
129
|
};
|
|
130
130
|
//# sourceMappingURL=index.js.map
|
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 timeout: { failure: true }\n}\n\nexport type CommunicationStatus = \"success\" | \"missing\" | \"clientFailure\" | \"clientFailureNotAuthorized\" | \"serverFailure\" | \"networkFailure\" | \"aborted\" | \"pending\" | \"failure\" | \"timeout\"\n/**\n * RegEx returns true for all valid communication statuses\n */\nexport const statusRegex = /^(success|missing|clientFailure|clientFailureNotAuthorized|serverFailure|networkFailure|aborted|pending|failure|timeout)$/\n\n// Export status constants\n/**\n * HTTP 2xx responses\n *\n * Client Can Automatically:\n * - Process the successful response\n * - Update UI to reflect success\n *\n * Client Developer Can:\n * - Handle the successful response data\n */\nexport const success: CommunicationStatus = \"success\"\n\n/**\n * Resource not found\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const missing: CommunicationStatus = \"missing\"\n\n/**\n * Client-side errors; i.e. the client needs to change the request somehow to succeed\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n * - Prompt user to correct invalid input\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n * - validate input before sending requests\n */\nexport const clientFailure: CommunicationStatus = \"clientFailure\"\n\n/**\n * Unauthorized requests; i.e. client needs to change the credentials (or the grants for the current credentials) to succeed\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const clientFailureNotAuthorized: CommunicationStatus = \"clientFailureNotAuthorized\"\n\n/**\n * Server-side errors; i.e. internal server errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n * - Implement automatic retry with backoff\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error\n */\nexport const serverFailure: CommunicationStatus = \"serverFailure\"\n\n/**\n * Request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request when network is available\n * - Monitor network status for recovery\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n * - implement offline-first capabilities\n */\nexport const networkFailure: CommunicationStatus = \"networkFailure\"\n\n/**\n * Request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n * - cleanup any pending state\n *\n * Client Developer Can:\n * - fix the client to not abort the request unnecessarily\n * - implement proper cleanup on abort\n */\nexport const aborted: CommunicationStatus = \"aborted\"\n\n/**\n * Request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n * - implement proper loading states\n */\nexport const pending: CommunicationStatus = \"pending\"\n\n/**\n * Any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can Automatically:\n * - Show appropriate error message to user\n * - Implement generic error handling\n * - Log errors for debugging\n *\n * Client Developer Can:\n * - Use more specific is* functions for targeted error handling\n * - Implement proper error recovery strategies\n */\nexport const failure: CommunicationStatus = \"failure\"\n\n/**\n * Request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - fix the client to not timeout the request\n * - implement proper timeout handling\n */\nexport const timeout: CommunicationStatus = \"timeout\"\n\n// Core status check functions\n/** Returns true for HTTP 2xx responses */\nexport const isSuccess = (status: CommunicationStatus) => status === success\n\n/**\n * Returns true for any error response (HTTP 4xx/5xx) or network/abort failures\n *\n * HTTP Status Codes Covered:\n * - 4xx: Client-side errors (except 404)\n * - 5xx: Server-side errors\n * - Network failures\n * - Abort failures\n *\n * Client Can:\n * - Use a different is* function for more specific checks\n */\nexport const isFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.failure\n\n/**\n * Returns true for client-side errors\n *\n * HTTP Status Codes Covered:\n * - 400: Bad Request\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 409: Conflict\n * - 422: Unprocessable Entity\n *\n * Client Can Automatically:\n * - Notify the user that the client is experiencing issues\n *\n * Client Developer Can:\n * - use isClientFailureNotAuthorized to check for 401/403/407/451\n * - fix the request to avoid the 4xx error\n*/\nexport const isClientFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.clientFailure\n\n/**\n * Returns true for server-side errors\n *\n * HTTP Status Codes Covered:\n * - 500: Internal Server Error\n * - 502: Bad Gateway\n * - 503: Service Unavailable\n * - 504: Gateway Timeout\n *\n * Client Can Automatically:\n * - Ask the user to try again later\n * - Notify the user that the server is experiencing issues\n *\n * Client Developer: (probably) can't fix\n *\n * Server Developer Can:\n * - fix the server to avoid the 5xx error\n * - fix server infrastructure to avoid the 5xx error (e.g. Bad Gateway, Service Unavailable, Gateway Timeout)\n*/\nexport const isServerFailure = (status: CommunicationStatus) => !!communicationStatuses[status]?.serverFailure\n\n/**\n * Returns true when request fails due to network connectivity issues\n *\n * HTTP Status Codes Covered: NONE (server was not reachable)\n *\n * Client Can Automatically:\n * - Prompt the user to fix the network connection\n * - Retry the request\n *\n * Client Developer Can:\n * - fix bad network constants (like address, ports, etc.)\n*/\nexport const isNetworkFailure = (status: CommunicationStatus) => status === networkFailure\n\n/** Returns true for server errors, network failures and aborted requests; i.e. the client did nothing wrong (as far as we can tell); client can ask the user to do something OR retry the request */\nexport const isNonClientFailure = (status: CommunicationStatus) => isFailure(status) && !isClientFailure(status)\n\n/**\n * Returns true for unauthorized requests (not authenticated or not authorized)\n *\n * HTTP Status Codes Covered:\n * - 401: Unauthorized\n * - 403: Forbidden\n * - 407: Proxy Authentication Required\n * - 451: Unavailable For Legal Reasons\n * - 511: Network Authentication Required\n *\n * Client Can Automatically:\n * - refresh the request token\n * - prompt the user to re-login\n * - ask the user to contact the administrator for access\n *\n * Client and Server Developer Can:\n * - fix authorization / authentication bugs\n */\nexport const isClientFailureNotAuthorized = (status: CommunicationStatus) => status === clientFailureNotAuthorized\n\n/**\n * Returns true when request was cancelled by client\n *\n * Client Can Automatically:\n * - notify the user that the request was cancelled\n * - prompt the user to try again\n *\n * Client Developer Can:\n * - fix the client to not abort the request\n */\nexport const isAborted = (status: CommunicationStatus) => status === aborted\n\n/**\n * Returns true when resource not found / not available\n *\n * HTTP Status Codes Covered:\n * - 404: Not Found\n * - 501: Not Implemented\n *\n * Client Can Automatically:\n * - notify the user that the resource was not found\n * - prompt the user to request a different resource\n *\n * Client Developer Can:\n * - fix the bad resource paths\n */\nexport const isMissing = (status: CommunicationStatus) => status === missing\n\n/**\n * Returns true while request is in progress\n *\n * Client Can Automatically:\n * - notify the user that the request is in progress\n * - show the user progress (if available)\n * - allow the user to cancel the request (trigging an \"aborted\" communication status)\n *\n * Client Developer Can:\n * - if \"pending\" was not expected, maybe the client needs to `wait` for the request to complete?\n */\nexport const isPending = (status: CommunicationStatus) => status === pending\n\n/**\n * Returns true if the request timed out\n *\n * Client Can Automatically:\n * - notify the user that the request timed out\n * - try again (automatically or via user action)\n *\n * Client Developer Can:\n * - extend the timeout duration\n *\n * Server Developer Can:\n * - improve server performance and reliability\n */\nexport const isTimeout = (status: CommunicationStatus) => status === timeout\n\n/**\n * Returns true if client can safely retry the request\n *\n * A a clearly-retryable failure:\n *\n * - network failure\n * - timeout\n * - aborted\n *\n * Note: some serverFailures will succeed on retry, but HTTP doesn't return clear indications which ones. To be safe, the client should not retry serverFailures indiscriminately.\n *\n * Client and Server Devs can\n * - investigate network, client and server performance and reliability issues\n */\nexport const isRetryableFailure = (status: CommunicationStatus) => isNetworkFailure(status) || isTimeout(status) || isAborted(status)\n\n/**\n * Returns true if the status is a valid communication status\n */\nexport const isStatusValid = (status: string) => statusRegex.test(status);\n\ninterface CommunicationStatusDetails {\n status: CommunicationStatus\n httpStatus?: number\n message: string\n}\n\n/**\n * Returns CommunicationStatusDetails {status, httpStatus, message} given an HTTP status code\n *\n * Throws: Error if the HTTP status code is not supported (i.e. the 100 codes or non HTTP status code numbers)\n *\n * @param httpStatus - The HTTP status code to get the communication status for\n * @returns The communication status for the given HTTP status code\n */\nexport const 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,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,SAAS,EAAE,SAAS,KAAK;AAC3B;AAMO,IAAM,cAAc;AAapB,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,UAA+B;AAIrC,IAAM,YAAY,CAAC,WAAgC,WAAW;AAc9D,IAAM,YAAY,CAAC,WAAgC,CAAC,CAAC,sBAAsB,MAAM,GAAG;AAoBpF,IAAM,kBAAkB,CAAC,WAAgC,CAAC,CAAC,sBAAsB,MAAM,GAAG;AAqB1F,IAAM,kBAAkB,CAAC,WA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AA,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/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,EAAE,Y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AgB,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":[]}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@art-suite/art-core-ts-communication-status",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A TypeScript string utility library",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/art-suite/art-suite-ts"
|
|
8
|
+
"url": "git+https://github.com/art-suite/art-suite-ts.git"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"author": "Shane Delamore",
|