Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2

Phase 2: TanStack DB over a fake API #3

Mergedfukomerged 12 commits intomain fromphase-2-datamerged by fuko

Showing 64 changed files with 87 additions and 0 deletions (loading 61).

x
0 of 64 viewed, 64 left
40 additions and 0 deletionssrc/api/client.tsAdded
View file
@@ -0,0 +1,40 @@
+import { fakeFetch } from "./fake-fetch";
+import type { ApiErrorBody } from "./fake-server";
+​
+/** A non-2xx response. Mutation handlers let it throw, which rolls the optimistic change back. */
+export class ApiError extends Error {
+ constructor(
+ readonly status: number,
+ readonly code: string,
+ message: string,
+ ) {
+ super(message);
+ this.name = "ApiError";
+ }
+}
+​
+async function request<T>(
+ method: string,
+ url: string,
+ { body, signal }: { body?: unknown; signal?: AbortSignal } = {},
+): Promise<T> {
+ const response = await fakeFetch(url, {
+ method,
+ signal,
+ headers: body === undefined ? undefined : { "Content-Type": "application/json" },
+ body: body === undefined ? undefined : JSON.stringify(body),
+ });
+ if (!response.ok) {