Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

request-options.ts

24 lines1.2 KB

1/**
2 * Extract `headers` and `signal` from a `Request | RequestInit` for the OpenAI
3 * SDK's per-call `RequestOptions`. `Request` exposes `headers` as a `Headers`
4 * instance (HeadersInit-compatible) while `RequestInit` exposes `HeadersInit`
5 * directly — this helper accepts either shape so callers don't need to cast.
6 *
7 * Always returns an object (possibly empty) rather than `undefined` so test
8 * assertions that match the second argument shape via `expect.anything()` /
9 * `expect.objectContaining()` keep working when no request override was set.
10 */
11export function extractRequestOptions(
12 request: Request | RequestInit | undefined,
13): { headers?: HeadersInit; signal?: AbortSignal | null } {
14 if (!request) return {}
15 // Conditional spread: under exactOptionalPropertyTypes the target's
16 // `headers?: HeadersInit` and `signal?: AbortSignal | null` forbid an
17 // explicit `undefined`. Omit the keys entirely when the source values
18 // are absent so the OpenAI SDK sees `headers: undefined` as "not set"
19 // rather than a present-but-undefined value.
20 return {
21 ...(request.headers !== undefined && { headers: request.headers }),
22 ...(request.signal != null && { signal: request.signal }),
23 }