@defjs/core

中文文档
0.6.0发布于 2026/11/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 9544

Define and execute typed HTTP, SSE, and WebSocket APIs.

原始使用文档
文档目录5 节
  1. Install
  2. Quick start
  3. Rules that matter
  4. Documentation
  5. License

@defjs/core

Design reusable HTTP, SSE, and WebSocket APIs as data, then execute them with an application-owned client.

def means define. An SDK author exports operation definitions and data contracts. The consuming application supplies the endpoint, credentials, interceptors, and transport. Importing a definition starts no request and binds no client.

Install

bun add @defjs/core

The package is ESM and declares no runtime dependencies. Declarations target ES2022 with ESNext.Disposable, DOM, and DOM.Iterable; those entries supply types only, not polyfills. Defjs is pre-1.0, so use the documentation for the exact version you installed.

Quick start

// users-sdk.ts — SDK definition
import { defineRequest, struct } from "@defjs/core";

export const getUser = defineRequest({
  method: "GET",
  path: "/users/:id",
  input: struct.object({ id: struct.number() }),
  builder: (input) => ({ path: input }),
  output: struct.object({ id: struct.number(), name: struct.string() }),
  error: struct.object({ message: struct.string() }),
});
// app.ts — application-owned client
import { createClient, withEndpoint } from "@defjs/core";
import { getUser } from "./users-sdk.ts";

const client = createClient(withEndpoint("https://api.example.com"));

async function main() {
  const [error, user] = await getUser(client, { id: 1 });
  if (error) {
    console.error(String(error), error.code);
    return;
  }
  console.log(user.name);
}

void main();

Calling getUser(client, { id: 1 }) validates, builds, and sends the request. client({ signal }) returns a Client value for one call and does not perform I/O. Return before reading the success value when error is set. Once published, import getUser from your SDK package instead of the local file — see Build and publish an SDK.

Rules that matter

  • Calling an operation with a client performs I/O. A definition does not capture a client. There is no global client.
  • Per-call options are the seven RequestOptions fields: signal, onUploadProgress, onDownloadProgress, beforeConnect, protocols, heartbeat, and reconnect. client(options) returns a non-callable client value that copies those fields into a fresh object; fields set to undefined are omitted. Nested heartbeat, protocols, and reconnect values are kept by reference, so treat them as immutable after passing them. Pass AbortSignal.timeout(ms) as signal when a call needs a deadline. That reason is NET_TIMEOUT; a manual abort is NET_ABORTED. A getter that throws does so from client(options), not as a fault. SSE and WebSocket have no startup-only deadline: the signal aborts the whole connection.
  • input declares parameters; a synchronous, pure builder places them on path, query, headers, and body. Every definition with input requires an explicit builder.
  • Structs validate at runtime. output is the 2xx body, error every other status; omit a side and that body is never read.
  • Failures are returned as Fault instances in an error-first tuple — HTTP_STATUS, REQ_*, NET_*, RES_*, EXT_*, CAP_*, ENV_UNSUPPORTED. String(error) gives the message; error.code and the cause chain stay structured for logs.
  • A timeout or cancellation does not prove the server missed a write. Keep an application idempotency contract before replaying writes.
  • Returned SSE and WebSocket handles are AsyncDisposable; WebSocket closed is the logical terminal result.
  • Inject a Fetch-compatible function with withHTTPHandle(...) to test a client without a server — see Test with a handle.

Documentation

License

MIT.

版本与发布记录

@defjs/core 共发布 32 个版本,首次发布于 2024/08/03;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.0latest2026/11/0210 天前
0.5.02026/11/0110 天前
0.4.02026/10/2913 天前
0.4.0-beta.1beta2026/10/101 个月前
0.4.0-beta.02026/09/261 个月前
0.3.32025/03/191.6 年前

其他发布通道:test → 0.3.1,beta → 0.4.0-beta.1。

关于 @defjs/core 的常见问题

@defjs/core 是什么?

Define and execute typed HTTP, SSE, and WebSocket APIs.

如何安装 @defjs/core?

在项目目录运行 npm install @defjs/core,或使用 pnpm add / yarn add / bun add。

@defjs/core 支持 TypeScript 吗?

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

@defjs/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@defjs/core 可以商用吗?

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

@defjs/core 的下载量有多少?

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

@defjs/core 还在维护吗?

最新版本 0.6.0 发布于 2026/11/02(10 天前);至今共发布 32 个版本,其中过去 12 个月发布了 5 个;首次发布于 2024/08/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。