feat: dual ESM+CJS builds + toJSONResponse/fetchJSON for non-streaming runtimes #478
ClosedAlemTuzlakwants to merge 12 commits into
main fromfix/cjs-output-and-json-responseopened View on GitHubtombeckenham requested changes
@@ -426,0 +476,21 @@476+ method: 'POST',477+ headers: requestHeaders,478+ body: JSON.stringify(requestBody),479+ credentials: resolvedOptions.credentials || 'same-origin',480+ signal: abortSignal || resolvedOptions.signal,481+ })482+483+ if (!response.ok) {484+ throw new Error(485+ `HTTP error! status: ${response.status} ${response.statusText}`,486+ )487+ }488+489+ const payload = (await response.json()) as unknown490+ if (!Array.isArray(payload)) {491+ throw new Error(492+ 'fetchJSON: expected response body to be a JSON array of StreamChunks. Did you forget to use `toJSONResponse(stream)` on the server?',493+ )494+ }495+ for (const chunk of payload) {496+ yield chunk as StreamChunk@@ -426,0 +469,21 @@469+ messages,470+ data,471+ ...resolvedOptions.body,472+ }473+474+ const fetchClient = resolvedOptions.fetchClient ?? fetch475+ const response = await fetchClient(resolvedUrl, {476+ method: 'POST',477+ headers: requestHeaders,478+ body: JSON.stringify(requestBody),479+ credentials: resolvedOptions.credentials || 'same-origin',480+ signal: abortSignal || resolvedOptions.signal,481+ })482+483+ if (!response.ok) {484+ throw new Error(485+ `HTTP error! status: ${response.status} ${response.statusText}`,486+ )487+ }488+489+ const payload = (await response.json()) as unknown@@ -426,0 +463,21 @@463+ const requestHeaders: Record<string, string> = {464+ 'Content-Type': 'application/json',465+ ...mergeHeaders(resolvedOptions.headers),466+ }467+468+ const requestBody = {469+ messages,470+ data,471+ ...resolvedOptions.body,472+ }473+474+ const fetchClient = resolvedOptions.fetchClient ?? fetch475+ const response = await fetchClient(resolvedUrl, {476+ method: 'POST',477+ headers: requestHeaders,478+ body: JSON.stringify(requestBody),479+ credentials: resolvedOptions.credentials || 'same-origin',480+ signal: abortSignal || resolvedOptions.signal,481+ })482+483+ if (!response.ok) {@@ -426,0 +431,21 @@431+ * server drains the chat stream fully, JSON-serialises the collected432+ * chunks into an array, and this adapter fetches the array and replays433+ * each chunk one-by-one into the normal client pipeline.434+ *435+ * Trade-off: you lose incremental rendering — the UI sees every chunk436+ * only after the request resolves. Use SSE/HTTP-stream adapters when the437+ * runtime supports them.438+ *439+ * @param url - The API endpoint URL (or a function that returns the URL)440+ * @param options - Fetch options (headers, credentials, body, etc.) or a function that returns options (can be async)441+ * @returns A connection adapter for JSON-array responses442+ *443+ * @example444+ * ```typescript445+ * // Expo / RN client that hits an Expo API route returning toJSONResponse(stream)446+ * const connection = fetchJSON('/api/chat')447+ *448+ * const client = new ChatClient({ connection })449+ * ```450+ */451+export function fetchJSON(tombeckenham assignedtombeckenham
AlemTuzlak closed this
Sign in to join this conversation.
🚀 Changeset Version Preview#
10 package(s) bumped directly, 23 bumped as dependents.
🟥 Major bumps#
@tanstack/ai-anthropic@tanstack/ai-event-client@tanstack/ai-preact@tanstack/ai-react@tanstack/ai-solid@tanstack/ai-svelte@tanstack/ai-vue@tanstack/ai-code-mode@tanstack/ai-code-mode-skills@tanstack/ai-elevenlabs@tanstack/ai-fal@tanstack/ai-gemini@tanstack/ai-grok@tanstack/ai-groq@tanstack/ai-isolate-node@tanstack/ai-isolate-quickjs@tanstack/ai-ollama@tanstack/ai-openai@tanstack/ai-openrouter@tanstack/ai-react-ui@tanstack/ai-solid-ui🟨 Minor bumps#
@tanstack/ai@tanstack/ai-client@tanstack/ai-isolate-cloudflare🟩 Patch bumps#
@tanstack/ai-code-mode-models-eval@tanstack/ai-devtools-core@tanstack/ai-vue-ui@tanstack/preact-ai-devtools@tanstack/react-ai-devtools@tanstack/solid-ai-devtoolsts-svelte-chatts-vue-chatvanilla-chat