Skip to content

Code examples in docs that crash at runtime (imports, async, destructuring) #5385

@AliMahmoudDev

Description

@AliMahmoudDev

Found several code examples in the API docs that will throw errors if copied and run:

1. DNS interceptor examples use new Agent() without importing it (Dispatcher.md)

Both DNS interceptor examples import Client but use new Agent(). Running these gives ReferenceError: Agent is not defined.

// Line 1116 (wrong import)
const { Client, interceptors } = require("undici");

// Line 1119 (uses Agent without importing)
const client = new Agent().compose([dns({ ...opts })])

Same issue in the DNS + LRU cache example around line 1132.

Fix: Change import to const { Agent, interceptors } = require("undici")

2. H2CClient example uses await in non-async callback (H2CClient.md)

once(server, "listening").then(() => {       // not async
  const response = await client.request(...)  // SyntaxError!

Fix: Add async to the callback: .then(async () => {

3. EnvHttpProxyAgent examples destructure json from Response (EnvHttpProxyAgent.md)

const { status, json } = await fetch(...)
const data = await json()  // TypeError: json is not a function

json is a prototype method on Response — destructuring yields undefined. This appears in two examples.

Fix: Use const response = await fetch(...) then await response.json()

4. Cookies.md: sameSite type says "String" instead of "Strict"

* **sameSite** `"String"|"Lax"|"None"` (optional)

Fix: Change "String" to "Strict"

5. Dispatcher.md: Retry interceptor example heading says "Redirect" (copy-paste error)

The heading reads "Example - Basic Redirect Interceptor" but the code uses retry.

6. RetryHandler.md: retry callback return type says number | null but should be void

The RetryAgent.md correctly documents this as => void, but RetryHandler.md says => number | null.

7. Fetch.md: Section heading says Header (singular) instead of Headers

8. README.md claims undici does not support the Expect header, but expectContinue option exists for H2

Happy to open a PR fixing these.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions