@effectionx/fetch

中文文档
0.2.1发布于 2026/08/08MITTypeScriptES Module构建来源已验证近 30 天下载 4.7万

Effection-native fetch with structured concurrency and streaming response support

原始使用文档
文档目录12 节
  1. Installation
  2. Usage
  3. Fluent API
  4. Traditional API
  5. Streaming response bodies
  6. Concurrent requests
  7. Validate JSON while parsing
  8. Handle non-2xx responses
  9. API
  10. fetch(input, init?)
  11. FetchOperation
  12. FetchResponse

fetch

Effection-native fetch with structured concurrency and streaming response support.


Installation

npm install @effectionx/fetch effection

Usage

import { main } from "effection";
import { fetch } from "@effectionx/fetch";

await main(function* () {
  let users = yield* fetch("https://api.example.com/users").json();
  console.log(users);
});

Fluent API

Chain methods directly on fetch() for concise one-liners:

// JSON
let data = yield* fetch("https://api.example.com/users").json();

// Text
let html = yield* fetch("https://example.com").text();

// With validation - throws HttpError on non-2xx
let data = yield* fetch("https://api.example.com/users").expect().json();

Traditional API

You can also get the response first, then consume the body:

let response = yield* fetch("https://api.example.com/users");
let data = yield* response.json();

Streaming response bodies

import { each } from "effection";
import { fetch } from "@effectionx/fetch";

function* example() {
  for (let chunk of yield* each(fetch("https://example.com/large-file.bin").body())) {
    console.log(chunk.length);
    yield* each.next();
  }
}

Concurrent requests

import { all } from "effection";
import { fetch } from "@effectionx/fetch";

function* fetchMultiple() {
  let [users, posts, comments] = yield* all([
    fetch("https://api.example.com/users").json(),
    fetch("https://api.example.com/posts").json(),
    fetch("https://api.example.com/comments").json(),
  ]);

  return { users, posts, comments };
}

Validate JSON while parsing

import { fetch } from "@effectionx/fetch";

interface User {
  id: string;
  name: string;
}

function parseUser(value: unknown): User {
  if (
    typeof value === "object" &&
    value !== null &&
    "id" in value &&
    "name" in value
  ) {
    return value as User;
  }

  throw new Error("invalid user payload");
}

function* getUser() {
  return yield* fetch("https://api.example.com/user").json(parseUser);
}

Handle non-2xx responses

import { HttpError, fetch } from "@effectionx/fetch";

function* getUser(id: string) {
  try {
    return yield* fetch(`https://api.example.com/users/${id}`).expect().json();
  } catch (error) {
    if (error instanceof HttpError) {
      console.error(error.status, error.statusText);
    }
    throw error;
  }
}

API

fetch(input, init?)

Returns a FetchOperation that supports both fluent chaining and traditional usage.

  • input - URL string, URL object, or Request object
  • init - Optional FetchInit options (same as RequestInit but without signal)

Cancellation is handled automatically via Effection's structured concurrency. When the scope exits, an in-flight request is aborted. A native fetch that has already settled is left intact. The signal option is intentionally omitted since Effection manages cancellation for you.

FetchOperation

Chainable fetch operation returned by fetch().

  • json<T>(), json<T>(parse) - parse response as JSON
  • text() - get response as text
  • arrayBuffer() - get response as ArrayBuffer
  • blob() - get response as Blob
  • formData() - get response as FormData
  • body() - stream response body as Stream<Uint8Array, void>
  • expect() - returns a new FetchOperation that throws HttpError on non-2xx

Can also be yielded directly to get a FetchResponse:

let response = yield* fetch("https://api.example.com/users");

FetchResponse

Effection wrapper around native Response with operation-based body readers.

  • json<T>(), json<T>(parse)
  • text()
  • arrayBuffer()
  • blob()
  • formData()
  • body(): Stream<Uint8Array, void>
  • expect() - throws HttpError for non-2xx responses
  • raw - access the underlying native Response

版本与发布记录

@effectionx/fetch 共发布 6 个版本,首次发布于 2026/02/16;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.1latest2026/08/083 个月前
0.2.02026/04/156 个月前
0.1.32026/03/237 个月前
0.1.22026/02/248 个月前
0.1.12026/02/248 个月前
0.1.02026/02/168 个月前

关于 @effectionx/fetch 的常见问题

@effectionx/fetch 是什么?

Effection-native fetch with structured concurrency and streaming response support

如何安装 @effectionx/fetch?

在项目目录运行 npm install @effectionx/fetch,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effection(^3 || ^4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effectionx/fetch 支持 TypeScript 吗?

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

@effectionx/fetch 支持 ESM 还是 CommonJS?

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

@effectionx/fetch 可以商用吗?

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

@effectionx/fetch 的下载量有多少?

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

@effectionx/fetch 还在维护吗?

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