Skip to content

API 概览

Fetcher 生态系统在多个包中暴露 API,每个包处理 HTTP 客户端生命周期中的特定关注点。本页提供所有公共 API 接口的摘要及快速导航链接。

包 API 摘要

npm 名称主要导出描述
Fetcher@ahoo-wang/fetcherFetcherNamedFetcherFetcherRegistrarInterceptorManager带拦截器管道的核心 HTTP 客户端
Decorator@ahoo-wang/fetcher-decorator@api@get@post@put@del@patch@path@query@header@body通过装饰器实现声明式 API 客户端
React Hooks@ahoo-wang/fetcher-reactuseFetcheruseQueryuseExecutePromisecreateQueryApiHooks用于数据获取的 React Hooks
EventStream@ahoo-wang/fetcher-eventstreamtoJsonServerSentEventStreamEventStreamResultExtractorSSE 和 LLM 流式支持
EventBus@ahoo-wang/fetcher-eventbusEventBusTypedEventBusParallelTypedEventBusSerialTypedEventBus类型安全的事件总线
OpenAPI@ahoo-wang/fetcher-openapiOpenAPI 3.x 的 TypeScript 接口规范类型定义
Storage@ahoo-wang/fetcher-storage存储抽象浏览器存储封装
CoSec@ahoo-wang/fetcher-cosec安全拦截器认证与授权
Wow@ahoo-wang/fetcher-wowCQRS 命令/查询客户端DDD + 事件溯源支持
Viewer@ahoo-wang/fetcher-viewerReact + Ant Design 组件API 文档查看器
Generator@ahoo-wang/fetcher-generatorfetcher-generator CLIOpenAPI 到 TypeScript 代码生成

导入模式

核心 Fetcher

typescript
import { Fetcher, NamedFetcher, fetcherRegistrar } from '@ahoo-wang/fetcher';
import { HttpMethod, ResultExtractors } from '@ahoo-wang/fetcher';
import type { FetchRequest, FetchExchange, FetcherOptions } from '@ahoo-wang/fetcher';

Decorator

typescript
import 'reflect-metadata'; // Required before any decorator usage
import { api, get, post, put, del, patch } from '@ahoo-wang/fetcher-decorator';
import { path, query, header, body, request, attribute } from '@ahoo-wang/fetcher-decorator';
import { autoGeneratedError } from '@ahoo-wang/fetcher-decorator';

React Hooks

typescript
import { useFetcher, useFetcherQuery } from '@ahoo-wang/fetcher-react';
import { useQuery, useExecutePromise, usePromiseState } from '@ahoo-wang/fetcher-react';
import { createQueryApiHooks } from '@ahoo-wang/fetcher-react';

EventStream(副作用导入)

typescript
// Importing this module patches Response.prototype with eventStream() and jsonEventStream()
import '@ahoo-wang/fetcher-eventstream';

EventBus

typescript
import { EventBus, ParallelTypedEventBus, SerialTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
import type { EventHandler } from '@ahoo-wang/fetcher-eventbus';

架构图

mermaid
graph TB
    subgraph sg_1 ["Application Layer"]
        REACT["@ahoo-wang/fetcher-react"]
        VIEWER["@ahoo-wang/fetcher-viewer"]
        GENERATOR["@ahoo-wang/fetcher-generator"]
    end

    subgraph sg_2 ["Service Layer"]
        DECO["@ahoo-wang/fetcher-decorator"]
        WOW["@ahoo-wang/fetcher-wow"]
        OPENAI["@ahoo-wang/fetcher-openai"]
        COSEC["@ahoo-wang/fetcher-cosec"]
    end

    subgraph sg_3 ["Core Layer"]
        FETCHER["@ahoo-wang/fetcher"]
        ESTREAM["@ahoo-wang/fetcher-eventstream"]
        EBUS["@ahoo-wang/fetcher-eventbus"]
        STORAGE["@ahoo-wang/fetcher-storage"]
        OPENAPI["@ahoo-wang/fetcher-openapi"]
    end

    REACT --> FETCHER
    REACT --> ESTREAM
    REACT --> EBUS
    REACT --> STORAGE
    REACT --> WOW
    REACT --> COSEC
    VIEWER --> REACT
    VIEWER --> OPENAPI
    DECO --> FETCHER
    WOW --> FETCHER
    WOW --> ESTREAM
    WOW --> DECO
    OPENAI --> FETCHER
    OPENAI --> ESTREAM
    OPENAI --> DECO
    COSEC --> FETCHER
    COSEC --> EBUS
    COSEC --> STORAGE
    ESTREAM --> FETCHER
    EBUS --> FETCHER
    STORAGE --> EBUS

    style FETCHER fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style DECO fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style REACT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style ESTREAM fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style EBUS fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style STORAGE fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OPENAPI fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style VIEWER fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style GENERATOR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style WOW fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OPENAI fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style COSEC fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

请求生命周期

mermaid
sequenceDiagram
autonumber

    participant App as Application
    participant Fetcher as Fetcher
    participant IM as InterceptorManager
    participant ReqInt as Request Interceptors
    participant ResInt as Response Interceptors
    participant ErrInt as Error Interceptors

    App->>Fetcher: fetch(url, request, options)
    Fetcher->>Fetcher: resolveExchange()
    Fetcher->>IM: exchange(fetchExchange)
    IM->>ReqInt: intercept(exchange)
    ReqInt-->>IM: modified exchange
    IM->>ResInt: intercept(exchange)
    ResInt-->>IM: validated exchange
    IM-->>Fetcher: processed exchange
    Fetcher->>Fetcher: extractResult()
    Fetcher-->>App: result
    Note over IM,ErrInt: On error: error interceptors run
    IM->>ErrInt: intercept(exchange)
    ErrInt-->>IM: handled or re-thrown

拦截器管道详解

mermaid
graph LR
    subgraph sg_1 ["Request Phase"]
        R1["RequestBodyInterceptor<br>(order: MIN_SAFE + STEP)"]
        R2["UrlResolveInterceptor<br>(near MAX_SAFE)"]
        R3["FetchInterceptor<br>(MAX_SAFE - STEP)"]
    end

    subgraph sg_2 ["Response Phase"]
        S1["ValidateStatusInterceptor<br>(MAX_SAFE - STEP)"]
        S2["Custom Response<br>Interceptors"]
    end

    subgraph sg_3 ["Error Phase"]
        E1["Custom Error<br>Interceptors"]
    end

    R1 --> R2 --> R3
    R3 -->|"success"| S1
    S1 --> S2
    R3 -->|"error"| E1
    S1 -->|"validation error"| E1

    style R1 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style R2 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style R3 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style S1 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style S2 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style E1 fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

相关页面

基于 Apache License 2.0 发布。