upfetch
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseupfetch
upfetch
Use when you need a reusable fetch client with request-scoped defaults, automatic request/response shaping, runtime validation, retries, and lifecycle hooks.
up-fetch当你需要一款支持请求作用域默认值、自动请求/响应构造、运行时校验、重试以及生命周期钩子的可复用fetch客户端时,即可使用。
up-fetchMental model
核心思路
- creates the reusable client.
up(fetchFn, getDefaultOptions?) - runs on every request.
getDefaultOptions(input, options, ctx) - performs one request.
upfetch(input, options?, ctx?) - Keep high-level; load the relevant file under
SKILL.mdfor details.references/
- 用于创建可复用客户端。
up(fetchFn, getDefaultOptions?) - 每次发起请求时都会执行。
getDefaultOptions(input, options, ctx) - 用于发起单次请求。
upfetch(input, options?, ctx?) - 请查看了解概览;如需详情请加载
SKILL.md目录下的对应文件。references/
Minimum pattern
最简使用示例
ts
import { up } from 'up-fetch'
import { z } from 'zod'
export const upfetch = up(fetch, () => ({
baseUrl: 'https://api.example.com',
headers: {
Authorization: readToken() ? `Bearer ${readToken()}` : undefined,
},
timeout: 5000,
}))
const user = await upfetch('/users/1', {
schema: z.object({
id: z.number(),
name: z.string(),
}),
})ts
import { up } from 'up-fetch'
import { z } from 'zod'
export const upfetch = up(fetch, () => ({
baseUrl: 'https://api.example.com',
headers: {
Authorization: readToken() ? `Bearer ${readToken()}` : undefined,
},
timeout: 5000,
}))
const user = await upfetch('/users/1', {
schema: z.object({
id: z.number(),
name: z.string(),
}),
})Workflow
使用流程
- Start with client setup and dynamic defaults.
- If the request needs auth, params, body shaping, or merge semantics, read auth and request shaping.
- If the response contract matters, read validation, parsing, and errors.
- If retries, timeouts, or hook timing matter, read retries, timeouts, and lifecycle.
- If streaming or runtime quirks matter, read streaming and runtime caveats.
- 首先参考客户端配置与动态默认值。
- 如果请求需要身份验证、参数、请求体构造或合并规则,请阅读身份验证与请求构造。
- 如果需要遵循响应契约,请阅读校验、解析与错误处理。
- 如果需要配置重试、超时或钩子触发时机,请阅读重试、超时与生命周期。
- 如果涉及流式处理或运行时特殊问题,请阅读流式处理与运行时注意事项。
High-value rules
最佳实践规则
- Pass a function as the second argument to , not a plain object.
up() - Read auth and other mutable defaults inside that function so values stay fresh.
- Use and
paramsinstead of hand-serializing query strings or JSON.body - Use when you need runtime trust; TypeScript generics alone do not validate payloads.
schema - If you want error-as-value behavior, set before relying on
reject: () => false.parseResponse - Prefer over imported
globalThis.fetch.undici.fetch
- 给的第二个参数传递函数,而不是普通对象。
up() - 在该函数内部读取身份验证信息以及其他可变默认值,保证取值是最新的。
- 请使用和
params,不要手动序列化查询字符串或JSON。body - 需要运行时数据可靠性时请使用;仅靠TypeScript泛型无法校验载荷。
schema - 如果你需要“错误作为值”的行为,请在使用前设置
parseResponse。reject: () => false - 优先使用,而非导入的
globalThis.fetch。undici.fetch
References
参考文档
- Client setup and dynamic defaults
- Auth and request shaping
- Validation, parsing, and errors
- Retries, timeouts, and lifecycle
- Streaming and runtime caveats
- 客户端配置与动态默认值
- 身份验证与请求构造
- 校验、解析与错误处理
- 重试、超时与生命周期
- 流式处理与运行时注意事项