@art-suite/art-core-ts-communication-status

中文文档
0.12.1发布于 2026/06/19MITTypeScriptESM + CJS零依赖近 30 天下载 3240

A TypeScript string utility library

原始使用文档
文档目录10 节
  1. What This Package Is For
  2. The Action Categories
  3. Why More Statuses Than Action Categories?
  4. Why This Module?
  5. Example Installation and Use (Required)
  6. Functional Overview
  7. Status Types
  8. Type Guards
  9. HTTP Status Code Mapping
  10. API Documentation Reference

@art-suite/art-core-ts-communication-status

When writing HTTP APIs, error handling doesn't need to be such a pain!

What This Package Is For

Any remote call — HTTP or otherwise — can fail in dozens of distinct ways: a wide range of HTTP status codes, network errors, timeouts, user cancellations, disabled features. But the client only has a handful of meaningful ways to respond to those failures. This package's job is to collapse the full space of possible outcomes into the small set of actions a client can actually take.

The status names are just labels; the action category is the point. Everything else is semantics for the programmer to debug.

The Action Categories

StatusWhat the client should do
successProcess the response
pendingShow progress; optionally allow cancel
networkFailure, timeoutFailureRetry with backoff — transport-level problem (includes rate-limit, gateway timeouts, DNS/TCP failures). Always safe to auto-retry as long as you back off.
serverFailureImplementer decides — server-side logic failure. The operation may or may not have completed. For reads or idempotent ops: retry with backoff. For non-idempotent writes: query the API to check what happened, or surface to the user.
missing, clientFailureDon't retry — the request itself is wrong; fix it before trying again
clientFailureNotAuthorizedPrompt the user — auth is missing, expired, or denied; re-login or refresh token
abortedDon't retry — the client deliberately cancelled; honor that
disabledRequest is intentionally turned off; re-enable to proceed

Use the corresponding is* guards (isRetryableFailure, isServerFailure, isClientFailure, isClientFailureNotAuthorized, etc.) to drive client behavior off these categories directly, without ever inspecting the raw HTTP code.

Why More Statuses Than Action Categories?

Strictly speaking, you only need a handful of action categories — yet this library exposes roughly 50% more statuses than that. The extra granularity exists so the client can log, alert, and escalate failures appropriately even when the action is identical.

For example, missing (e.g. 404) and clientFailure (e.g. 400) both fall under "don't retry — fix the request," but you almost certainly want to treat them differently in logs:

  • missing is often expected — a user typed a bad URL, a record was deleted — and usually warrants an info log at most
  • clientFailure typically means the code sent a malformed request, which is a bug worth warn or error
  • serverFailure usually deserves louder alerting than networkFailure — one points at a server bug, the other at flaky transport
  • clientFailureNotAuthorized may want its own metric for security/audit purposes, separate from generic client errors

Think of each status as action category + severity hint. Use the action category to decide what the code does; use the specific status to decide how loud to be about it.

Why This Module?

HTTP status codes confound two distinct purposes: telling the client what to do, and conveying semantic detail about why something failed. That dual purpose makes client code more complex than it needs to be. And not every failure is even covered by HTTP — network failures, timeouts, and client-side aborts have no status code at all.

This library teases those concerns apart by focusing solely on what software can act on. The full space of HTTP status codes plus non-HTTP failure modes is reduced to a small set of actionable categories, so client-side code can be simpler, more robust, and safer (e.g. never retry-looping a rate-limited endpoint).

Example Installation and Use (Required)

Install with npm:

npm install @art-suite/art-core-ts-communication-status

Basic usage:

import {
  isSuccess,
  isServerFailure,
  success, // === "success"
  missing, // === "missing"
} from "@art-suite/art-core-ts-communication-status";

// Handle success
if (isSuccess(status)) {...}

// Simply handle all server failures
if (isServerFailure(status)) {...}

// or Handle multiple statues as a simple switch:
switch (status) {
  case success:
    /*...*/ break; // Request completed successfully
  case missing:
    /*...*/ break; // Resource not found, ask the user to fix
  default:
    /*...*/ break; // handle all other failures as one
}

Functional Overview

Status Types

Statuses are strings, or you can import constants with the identical name:

  • Success:

    • success — hurray! (2xx)
  • Missing:

    • missing — resource doesn't exist (3xx redirects, 404, 410). Note this is about data, not about request shape — see clientFailure for unsupported methods/capabilities.
  • Client-side Failures (fix the request or prompt the user):

    • clientFailure — request is wrong (most 4xx, plus 501, 505)
    • clientFailureNotAuthorized — auth missing/expired/denied (401, 403, 407, 451, 511)
  • Server-side Logic Failure (idempotency matters):

    • serverFailure — operation may or may not have completed (500, 506-508, 510, 525, 526, 530)
  • Transport-level Failures (always safe to retry with backoff):

    • networkFailure — rate-limit, gateway down, DNS/TCP/TLS failure (423, 425, 429, 502, 503, 521, 523, 527, plus non-HTTP transport failures)
    • timeoutFailure — request exceeded a deadline (408, 504, 522, 524, 598, 599, plus client-side timeouts)
  • Liminal Statuses (not errors, not success):

    • aborted — request was deliberately cancelled by the client
    • pending — request is in progress
    • disabled — request was intentionally turned off (httpStatus 0)

Type Guards

  • isSuccess(status) — succeeded
  • isMissing(status) — resource doesn't exist
  • isClientFailure(status) — request itself is wrong (includes auth failures)
  • isClientFailureNotAuthorized(status) — specifically an auth failure
  • isServerFailure(status) — server logic failure (idempotency matters)
  • isNetworkFailure(status) — transport-level failure
  • isTimeout(status) — timeout
  • isAborted(status) — client cancelled
  • isPending(status) — in progress
  • isDisabled(status) — intentionally off
  • isFailure(status) — any failure of any kind
  • isNonClientFailure(status) — a failure that wasn't the client's fault
  • isRetryableFailure(status) — safe to auto-retry with backoff (networkFailure + timeoutFailure only)

HTTP Status Code Mapping

The library maps HTTP status codes to these simplified states. Notice how it does NOT respect HTTP's leading-digit grouping — that's the whole point: HTTP scatters transport problems, timeouts, and rate-limits across both the 4xx and 5xx ranges, so "treat all 5xx the same" leads to broken retry logic.

HTTP codesCommunicationStatus
0disabled
2xxsuccess
3xx, 404, 410missing
401, 403, 407, 451, 511clientFailureNotAuthorized
408, 504, 522, 524, 598, 599timeoutFailure
423, 425, 429, 502, 503, 521, 523, 527networkFailure
501, 505, and most other 4xxclientFailure
500, 506–508, 510, 525, 526, 530, and other 5xxserverFailure

Note: HTTP redirects (3xx) are protocol-level signals and should be handled (followed) by the HTTP client library. They're mapped to missing only as a defensive fallback if one ever reaches application code.

API Documentation Reference

For detailed information on all exported functions and their parameters, please refer to the TypeScript typings and JSDoc comments within the source code.

版本与发布记录

@art-suite/art-core-ts-communication-status 共发布 25 个版本,首次发布于 2025/05/22;过去 12 个月发布了 13 个版本。

版本发布日期距今
0.12.1latest2026/06/194 个月前
0.12.02026/05/205 个月前
0.11.02026/03/277 个月前
0.10.12026/02/258 个月前
0.10.02026/02/208 个月前
0.9.02026/01/149 个月前

关于 @art-suite/art-core-ts-communication-status 的常见问题

@art-suite/art-core-ts-communication-status 是什么?

A TypeScript string utility library

如何安装 @art-suite/art-core-ts-communication-status?

在项目目录运行 npm install @art-suite/art-core-ts-communication-status,或使用 pnpm add / yarn add / bun add。

@art-suite/art-core-ts-communication-status 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@art-suite/art-core-ts-communication-status 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@art-suite/art-core-ts-communication-status 可以商用吗?

@art-suite/art-core-ts-communication-status 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@art-suite/art-core-ts-communication-status 的下载量有多少?

根据 npm 官方下载统计,@art-suite/art-core-ts-communication-status 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,240 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@art-suite/art-core-ts-communication-status 还在维护吗?

最新版本 0.12.1 发布于 2026/06/19(4 个月前);至今共发布 25 个版本,其中过去 12 个月发布了 13 个;首次发布于 2025/05/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。