upfetch
Use up-fetch when you need a reusable fetch client with request-scoped defaults, automatic request/response shaping, runtime validation, retries, and lifecycle hooks.
Mental model
up(fetchFn, getDefaultOptions?) creates the reusable client.
getDefaultOptions(input, options, ctx) runs on every request.
upfetch(input, options?, ctx?) performs one request.
- Keep
SKILL.md high-level; load the relevant file under references/ for details.
Minimum pattern
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
up(), not a plain object.
- Read auth and other mutable defaults inside that function so values stay fresh.
- Use
params and body instead of hand-serializing query strings or JSON.
- Use
schema when you need runtime trust; TypeScript generics alone do not validate payloads.
- If you want error-as-value behavior, set
reject: () => false before relying on parseResponse.
- Prefer
globalThis.fetch over imported undici.fetch.
References
- Client setup and dynamic defaults
- Auth and request shaping
- Validation, parsing, and errors
- Retries, timeouts, and lifecycle
- Streaming and runtime caveats
1---
2name: upfetch
3description: >
4 Load this skill for any up-fetch task: `up(fetch, getDefaultOptions?)`,
5 `upfetch(url, options?)`. Covers dynamic defaults, auth, request shaping,
6 validation, error handling, lifecycle timing, and runtime caveats.
7type: core
8library: up-fetch
9library_version: '2.6.0'
10sources:
11 - 'L-Blondy/up-fetch:README.md'
12 - 'L-Blondy/up-fetch:src/up.ts'
13 - 'L-Blondy/up-fetch:src/types.ts'
14 - 'L-Blondy/up-fetch:src/fallback-options.ts'
15 - 'L-Blondy/up-fetch:src/utils.ts'
16 - 'L-Blondy/up-fetch:src/stream.ts'
17 - 'L-Blondy/up-fetch:src/tests/*.spec.ts'
18 - 'L-Blondy/up-fetch:CHANGELOG.md'
19metadata:
20 source: https://skillmd.com/skills/aibot88/upfetch
21---
22
23# upfetch
24
25Use `up-fetch` when you need a reusable fetch client with request-scoped defaults, automatic request/response shaping, runtime validation, retries, and lifecycle hooks.
26
27## Mental model
28
29- `up(fetchFn, getDefaultOptions?)` creates the reusable client.
30- `getDefaultOptions(input, options, ctx)` runs on every request.
31- `upfetch(input, options?, ctx?)` performs one request.
32- Keep `SKILL.md` high-level; load the relevant file under `references/` for details.
33
34## Minimum pattern
35
36```ts
37import { up } from 'up-fetch'
38import { z } from 'zod'
39
40export const upfetch = up(fetch, () => ({
41 baseUrl: 'https://api.example.com',
42 headers: {
43 Authorization: readToken() ? `Bearer ${readToken()}` : undefined,
44 },
45 timeout: 5000,
46}))
47
48const user = await upfetch('/users/1', {
49 schema: z.object({
50 id: z.number(),
51 name: z.string(),
52 }),
53})
54```
55
56## Workflow
57
581. Start with [client setup and dynamic defaults](references/client-setup-and-dynamic-defaults.md).
592. If the request needs auth, params, body shaping, or merge semantics, read [auth and request shaping](references/auth-and-request-shaping.md).
603. If the response contract matters, read [validation, parsing, and errors](references/validation-parsing-and-errors.md).
614. If retries, timeouts, or hook timing matter, read [retries, timeouts, and lifecycle](references/retries-timeouts-and-lifecycle.md).
625. If streaming or runtime quirks matter, read [streaming and runtime caveats](references/streaming-and-runtime-caveats.md).
63
64## High-value rules
65
66- Pass a function as the second argument to `up()`, not a plain object.
67- Read auth and other mutable defaults inside that function so values stay fresh.
68- Use `params` and `body` instead of hand-serializing query strings or JSON.
69- Use `schema` when you need runtime trust; TypeScript generics alone do not validate payloads.
70- If you want error-as-value behavior, set `reject: () => false` before relying on `parseResponse`.
71- Prefer `globalThis.fetch` over imported `undici.fetch`.
72
73## References
74
75- [Client setup and dynamic defaults](references/client-setup-and-dynamic-defaults.md)
76- [Auth and request shaping](references/auth-and-request-shaping.md)
77- [Validation, parsing, and errors](references/validation-parsing-and-errors.md)
78- [Retries, timeouts, and lifecycle](references/retries-timeouts-and-lifecycle.md)
79- [Streaming and runtime caveats](references/streaming-and-runtime-caveats.md)