@structyl/api-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 your-lib contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # @structyl/api-client
2
+
3
+ > Framework-agnostic React data fetching — an Axios client with a built-in query cache.
4
+
5
+ ![npm](https://img.shields.io/npm/v/@structyl/api-client)
6
+ ![license](https://img.shields.io/npm/l/@structyl/api-client)
7
+
8
+ `@structyl/api-client` is a lightweight data-fetching layer for React. It pairs a configured Axios client with its own query cache and a set of hooks for queries, mutations, infinite lists, and Suspense — no separate query library required. It works identically in Vite/CRA, Next.js (App and Pages Router), Remix, Astro, and React Native, and is part of the [structyl](https://github.com/imirfanul/structyl) ecosystem.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ # pnpm
14
+ pnpm add @structyl/api-client axios
15
+
16
+ # npm
17
+ npm install @structyl/api-client axios
18
+
19
+ # yarn
20
+ yarn add @structyl/api-client axios
21
+ ```
22
+
23
+ `axios` and `react` (18 or 19) are peer dependencies and are not bundled.
24
+
25
+ ## Usage
26
+
27
+ ```tsx
28
+ import {
29
+ createApiClient,
30
+ ApiProvider,
31
+ useApiQuery,
32
+ useApiMutation,
33
+ } from '@structyl/api-client';
34
+
35
+ // 1. Create a client once, at the app root.
36
+ const api = createApiClient({
37
+ baseURL: 'https://api.example.com',
38
+ getAuthToken: () => localStorage.getItem('token'),
39
+ });
40
+
41
+ // 2. Provide it to your tree.
42
+ export default function App() {
43
+ return (
44
+ <ApiProvider client={api}>
45
+ <UserList />
46
+ <CreateUser />
47
+ </ApiProvider>
48
+ );
49
+ }
50
+
51
+ // 3. Query data.
52
+ type User = { id: string; name: string };
53
+
54
+ function UserList() {
55
+ const { data, isLoading, error } = useApiQuery<User[]>('/users');
56
+
57
+ if (isLoading) return <p>Loading…</p>;
58
+ if (error) return <p>Error: {error.message}</p>;
59
+
60
+ return (
61
+ <ul>
62
+ {data?.map((u) => (
63
+ <li key={u.id}>{u.name}</li>
64
+ ))}
65
+ </ul>
66
+ );
67
+ }
68
+
69
+ // 4. Mutate data and invalidate the cache.
70
+ function CreateUser() {
71
+ const { mutate, isPending } = useApiMutation<User, { name: string }>('/users', {
72
+ method: 'POST',
73
+ invalidates: [['/users']],
74
+ });
75
+
76
+ return (
77
+ <button disabled={isPending} onClick={() => mutate({ name: 'Alice' })}>
78
+ Create user
79
+ </button>
80
+ );
81
+ }
82
+ ```
83
+
84
+ ### Server-side prefetching (SSR)
85
+
86
+ The `@structyl/api-client/server` entry point lets you prefetch on the server and hydrate the client to avoid a loading flicker.
87
+
88
+ ```tsx
89
+ import { QueryClient, prefetchApiQuery, dehydrate } from '@structyl/api-client/server';
90
+
91
+ const queryClient = new QueryClient();
92
+ await prefetchApiQuery(queryClient, api, '/users');
93
+
94
+ // Pass dehydrate(queryClient) to <ApiProvider hydratedState={...}> on the client.
95
+ const hydratedState = dehydrate(queryClient);
96
+ ```
97
+
98
+ ## Features
99
+
100
+ - **Configured Axios client** — `createApiClient` with `baseURL`, custom headers, timeout, bearer-token injection (`getAuthToken`), and automatic 401 token refresh (`refreshToken`).
101
+ - **Built-in query cache** — request deduplication, stale-while-revalidate, garbage collection, and external invalidation. No separate query library to install.
102
+ - **Complete hook set** — queries, mutations, infinite/cursor pagination, parallel queries, Suspense, and prefetching.
103
+ - **Smart fetching** — `staleTime`/`gcTime`, retries, polling, refetch-on-focus, debouncing, `keepPreviousData`, `placeholderData`, and `select` transforms.
104
+ - **Optimistic mutations** — apply optimistic updates with automatic rollback on error, plus upload-progress reporting.
105
+ - **SSR-ready** — prefetch, dehydrate, and hydrate via the `/server` entry; safe in the Next.js App Router.
106
+ - **Devtools** — an optional cache inspector via the `/devtools` entry.
107
+ - **Typed and tree-shakeable** — first-class TypeScript types, ESM + CJS builds, no side effects.
108
+
109
+ ## API
110
+
111
+ ### Core (`@structyl/api-client`)
112
+
113
+ | Export | Description |
114
+ | --- | --- |
115
+ | `createApiClient(config)` | Creates an Axios-backed `ApiClient` with auth, refresh, and timeout handling. |
116
+ | `ApiClient` | The client class wrapping the configured Axios instance. |
117
+ | `ApiProvider` | Context provider that supplies the client and query cache to hooks. |
118
+ | `useApiClient()` / `useApiContext()` | Access the `ApiClient` / full context from within the provider. |
119
+ | `useApiQuery(key, urlOrFn?, options?)` | Fetch and cache data, with retries, polling, focus refetch, and `select`. |
120
+ | `useApiMutation(urlOrFn, options?)` | POST/PUT/PATCH/DELETE with cache invalidation and optimistic updates. |
121
+ | `useInfiniteApiQuery(...)` | Cursor/page-based infinite queries. |
122
+ | `useApiQueries(...)` | Run multiple queries in parallel. |
123
+ | `useSuspenseApiQuery(...)` | Suspense-enabled query with non-nullable data. |
124
+ | `usePrefetch()` | Imperatively warm the cache for a query. |
125
+ | `persistCache(queryClient, config)` | Persist and restore the cache to any storage backend. |
126
+ | `QueryClient` | The query cache instance used by the provider. |
127
+
128
+ ### Server (`@structyl/api-client/server`)
129
+
130
+ | Export | Description |
131
+ | --- | --- |
132
+ | `prefetchApiQuery(queryClient, apiClient, keyOrUrl, urlOrFn?, options?)` | Prefetch a query on the server. |
133
+ | `dehydrate(queryClient)` / `hydrate(queryClient, state)` | Serialize/restore cache state for SSR. |
134
+ | `QueryClient` | Re-exported for server usage. |
135
+
136
+ ### Devtools (`@structyl/api-client/devtools`)
137
+
138
+ | Export | Description |
139
+ | --- | --- |
140
+ | `ApiDevTools` | Floating cache inspector component for development. |
141
+
142
+ Exported types include `ApiError`, `ApiClientConfig`, `UseApiQueryOptions`, `UseApiMutationOptions`, `OptimisticConfig`, `ApiQueryResult`, `ApiMutationResult`, `QueryStatus`, `QueryClientConfig`, `UseInfiniteApiQueryOptions`, `InfiniteData`, `InfiniteApiQueryResult`, `ApiQueryConfig`, `SuspenseApiQueryResult`, `PersistenceConfig`, and `PersistenceStorage`.
143
+
144
+ ## Part of structyl
145
+
146
+ Part of the [structyl](https://github.com/imirfanul/structyl) component library. Read the full docs at [structyl.dev](https://structyl.dev).
147
+
148
+ ## License
149
+
150
+ MIT
@@ -0,0 +1,181 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+
6
+ var ApiContext = react.createContext(null);
7
+ function useApiContext() {
8
+ const ctx = react.useContext(ApiContext);
9
+ if (!ctx) {
10
+ throw new Error(
11
+ "[api-client] Hooks must be used within <ApiProvider>. Wrap your app root with <ApiProvider client={api}>."
12
+ );
13
+ }
14
+ return ctx;
15
+ }
16
+ function ApiDevTools() {
17
+ const [open, setOpen] = react.useState(false);
18
+ const [entries, setEntries] = react.useState([]);
19
+ const { queryClient } = useApiContext();
20
+ react.useEffect(() => {
21
+ const update = () => {
22
+ const snapshot = queryClient.cache.snapshot();
23
+ setEntries(
24
+ Object.entries(snapshot).map(([key, entry]) => ({ key, entry }))
25
+ );
26
+ };
27
+ update();
28
+ return queryClient.cache.subscribeGlobal(update);
29
+ }, [queryClient]);
30
+ return /* @__PURE__ */ jsxRuntime.jsxs(
31
+ "div",
32
+ {
33
+ style: {
34
+ position: "fixed",
35
+ bottom: 16,
36
+ right: 16,
37
+ zIndex: 9999,
38
+ fontFamily: "monospace",
39
+ fontSize: 12
40
+ },
41
+ children: [
42
+ /* @__PURE__ */ jsxRuntime.jsx(
43
+ "button",
44
+ {
45
+ onClick: () => setOpen((o) => !o),
46
+ style: {
47
+ background: "#1a1a2e",
48
+ color: "#e94560",
49
+ border: "1px solid #e94560",
50
+ borderRadius: 6,
51
+ padding: "6px 12px",
52
+ cursor: "pointer",
53
+ fontFamily: "monospace"
54
+ },
55
+ children: open ? "\u2715 DevTools" : "\u{1F50D} API Cache"
56
+ }
57
+ ),
58
+ open && /* @__PURE__ */ jsxRuntime.jsxs(
59
+ "div",
60
+ {
61
+ style: {
62
+ marginTop: 8,
63
+ background: "#16213e",
64
+ border: "1px solid #0f3460",
65
+ borderRadius: 8,
66
+ padding: 12,
67
+ width: 420,
68
+ maxHeight: 500,
69
+ overflowY: "auto",
70
+ color: "#e0e0e0"
71
+ },
72
+ children: [
73
+ /* @__PURE__ */ jsxRuntime.jsxs(
74
+ "div",
75
+ {
76
+ style: {
77
+ display: "flex",
78
+ justifyContent: "space-between",
79
+ marginBottom: 8
80
+ },
81
+ children: [
82
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { style: { color: "#e94560" }, children: [
83
+ "API Cache (",
84
+ entries.length,
85
+ " keys)"
86
+ ] }),
87
+ /* @__PURE__ */ jsxRuntime.jsx(
88
+ "button",
89
+ {
90
+ onClick: () => queryClient.cache.clear(),
91
+ style: {
92
+ background: "transparent",
93
+ border: "1px solid #e94560",
94
+ color: "#e94560",
95
+ borderRadius: 4,
96
+ padding: "2px 8px",
97
+ cursor: "pointer",
98
+ fontSize: 11
99
+ },
100
+ children: "Clear"
101
+ }
102
+ )
103
+ ]
104
+ }
105
+ ),
106
+ entries.length === 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { style: { color: "#888", textAlign: "center", padding: 16 }, children: "Cache is empty" }),
107
+ entries.map(({ key, entry }) => {
108
+ const age = entry.updatedAt ? Math.round((Date.now() - entry.updatedAt) / 1e3) : null;
109
+ const statusColor = {
110
+ success: "#4caf50",
111
+ error: "#f44336",
112
+ loading: "#ff9800",
113
+ idle: "#888"
114
+ }[entry.status] ?? "#888";
115
+ return /* @__PURE__ */ jsxRuntime.jsxs(
116
+ "div",
117
+ {
118
+ style: {
119
+ marginBottom: 8,
120
+ background: "#0f3460",
121
+ borderRadius: 6,
122
+ padding: 8
123
+ },
124
+ children: [
125
+ /* @__PURE__ */ jsxRuntime.jsxs(
126
+ "div",
127
+ {
128
+ style: {
129
+ display: "flex",
130
+ justifyContent: "space-between",
131
+ marginBottom: 4
132
+ },
133
+ children: [
134
+ /* @__PURE__ */ jsxRuntime.jsx(
135
+ "code",
136
+ {
137
+ style: {
138
+ color: "#e0e0e0",
139
+ wordBreak: "break-all",
140
+ fontSize: 11
141
+ },
142
+ children: key.length > 60 ? key.slice(0, 57) + "\u2026" : key
143
+ }
144
+ ),
145
+ /* @__PURE__ */ jsxRuntime.jsxs(
146
+ "span",
147
+ {
148
+ style: { color: statusColor, marginLeft: 8, flexShrink: 0 },
149
+ children: [
150
+ "\u25CF ",
151
+ entry.status
152
+ ]
153
+ }
154
+ )
155
+ ]
156
+ }
157
+ ),
158
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { color: "#888", fontSize: 10 }, children: [
159
+ age !== null ? `age: ${age}s` : "no data",
160
+ " \xB7 ",
161
+ "stale: ",
162
+ entry.staleTime > 0 ? `${entry.staleTime / 1e3}s` : "always",
163
+ entry.updatedAt === 0 && " \xB7 \u26A0 invalidated"
164
+ ] })
165
+ ]
166
+ },
167
+ key
168
+ );
169
+ })
170
+ ]
171
+ }
172
+ )
173
+ ]
174
+ }
175
+ );
176
+ }
177
+ ApiDevTools.displayName = "ApiDevTools";
178
+
179
+ exports.ApiDevTools = ApiDevTools;
180
+ //# sourceMappingURL=devtools.cjs.map
181
+ //# sourceMappingURL=devtools.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/provider.tsx","../src/devtools/ApiDevTools.tsx"],"names":["createContext","useContext","useState","useEffect","jsxs","jsx"],"mappings":";;;;;AAYA,IAAM,UAAA,GAAaA,oBAAsC,IAAI,CAAA;AAEtD,SAAS,aAAA,GAAiC;AAC/C,EAAA,MAAM,GAAA,GAAMC,iBAAW,UAAU,CAAA;AACjC,EAAA,IAAI,CAAC,GAAA,EAAK;AACR,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,GAAA;AACT;ACZO,SAAS,WAAA,GAAiC;AAC/C,EAAA,MAAM,CAAC,IAAA,EAAM,OAAO,CAAA,GAAIC,eAAS,KAAK,CAAA;AACtC,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAIA,cAAA,CAA0B,EAAE,CAAA;AAC1D,EAAA,MAAM,EAAE,WAAA,EAAY,GAAI,aAAA,EAAc;AAEtC,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,MAAM,SAAS,MAAM;AACnB,MAAA,MAAM,QAAA,GAAW,WAAA,CAAY,KAAA,CAAM,QAAA,EAAS;AAC5C,MAAA,UAAA;AAAA,QACE,MAAA,CAAO,OAAA,CAAQ,QAAQ,CAAA,CAAE,GAAA,CAAI,CAAC,CAAC,GAAA,EAAK,KAAK,CAAA,MAAO,EAAE,GAAA,EAAK,OAAM,CAAE;AAAA,OACjE;AAAA,IACF,CAAA;AACA,IAAA,MAAA,EAAO;AACP,IAAA,OAAO,WAAA,CAAY,KAAA,CAAM,eAAA,CAAgB,MAAM,CAAA;AAAA,EACjD,CAAA,EAAG,CAAC,WAAW,CAAC,CAAA;AAEhB,EAAA,uBACEC,eAAA;AAAA,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BAAM,SAAA;AAAA,wBACC,MAAM,SAAA,GAAY,CAAA,GAAI,GAAG,KAAA,CAAM,SAAA,GAAY,GAAI,CAAA,CAAA,CAAA,GAAM,QAAA;AAAA,wBAC5D,KAAA,CAAM,cAAc,CAAA,IAAK;AAAA,uBAAA,EAC5B;AAAA;AAAA,mBAAA;AAAA,kBAnCK;AAAA,iBAoCP;AAAA,cAEJ,CAAC;AAAA;AAAA;AAAA;AACH;AAAA;AAAA,GAEJ;AAEJ;AAEA,WAAA,CAAY,WAAA,GAAc,aAAA","file":"devtools.cjs","sourcesContent":["import React, { createContext, useContext, useRef } from 'react';\nimport type { ReactNode } from 'react';\nimport { QueryClient } from './cache';\nimport type { ApiClient } from './client';\nimport type { DehydratedState } from './server';\nimport type { QueryClientConfig } from './types';\n\ninterface ApiContextValue {\n apiClient: ApiClient;\n queryClient: QueryClient;\n}\n\nconst ApiContext = createContext<ApiContextValue | null>(null);\n\nexport function useApiContext(): ApiContextValue {\n const ctx = useContext(ApiContext);\n if (!ctx) {\n throw new Error(\n '[api-client] Hooks must be used within <ApiProvider>. ' +\n 'Wrap your app root with <ApiProvider client={api}>.',\n );\n }\n return ctx;\n}\n\nexport function useApiClient(): ApiClient {\n return useApiContext().apiClient;\n}\n\nfunction makeQueryClient(config?: QueryClientConfig): QueryClient {\n return new QueryClient(config);\n}\n\nlet browserQueryClient: QueryClient | undefined;\n\nfunction getQueryClient(config?: QueryClientConfig): QueryClient {\n if (typeof window === 'undefined') return makeQueryClient(config);\n if (!browserQueryClient) browserQueryClient = makeQueryClient(config);\n return browserQueryClient;\n}\n\nexport interface ApiProviderProps {\n client: ApiClient;\n children: ReactNode;\n /** Inject a pre-built QueryClient (advanced / testing) */\n queryClient?: QueryClient;\n /** Override the GC time for entries with no active subscriber (ms, default 5 min) */\n gcTime?: number;\n /** Hydrate from server-prefetched state (SSR) */\n hydratedState?: DehydratedState;\n /** Global QueryClient configuration (callbacks etc.) */\n queryClientConfig?: QueryClientConfig;\n}\n\nexport function ApiProvider({\n client,\n children,\n queryClient,\n gcTime,\n hydratedState,\n queryClientConfig,\n}: ApiProviderProps): React.JSX.Element {\n const qcRef = useRef<QueryClient | null>(null);\n if (!qcRef.current) {\n const config: QueryClientConfig = {\n gcTime,\n ...queryClientConfig,\n };\n qcRef.current = queryClient ?? getQueryClient(config);\n if (hydratedState) {\n qcRef.current.cache.restore(hydratedState.entries);\n }\n }\n\n const ctxRef = useRef<ApiContextValue | null>(null);\n if (!ctxRef.current || ctxRef.current.apiClient !== client) {\n ctxRef.current = { apiClient: client, queryClient: qcRef.current };\n }\n\n return (\n <ApiContext.Provider value={ctxRef.current}>{children}</ApiContext.Provider>\n );\n}\n\nApiProvider.displayName = 'ApiProvider';\n","'use client'; // safe to include here — this is a UI component\n\nimport React, { useState, useEffect } from 'react';\nimport { useApiContext } from '../provider';\nimport type { CacheEntry } from '../cache';\n\ninterface CacheSnapshot {\n key: string;\n entry: CacheEntry;\n}\n\nexport function ApiDevTools(): React.JSX.Element {\n const [open, setOpen] = useState(false);\n const [entries, setEntries] = useState<CacheSnapshot[]>([]);\n const { queryClient } = useApiContext();\n\n useEffect(() => {\n const update = () => {\n const snapshot = queryClient.cache.snapshot();\n setEntries(\n Object.entries(snapshot).map(([key, entry]) => ({ key, entry })),\n );\n };\n update();\n return queryClient.cache.subscribeGlobal(update);\n }, [queryClient]);\n\n return (\n <div\n style={{\n position: 'fixed',\n bottom: 16,\n right: 16,\n zIndex: 9999,\n fontFamily: 'monospace',\n fontSize: 12,\n }}\n >\n <button\n onClick={() => setOpen((o) => !o)}\n style={{\n background: '#1a1a2e',\n color: '#e94560',\n border: '1px solid #e94560',\n borderRadius: 6,\n padding: '6px 12px',\n cursor: 'pointer',\n fontFamily: 'monospace',\n }}\n >\n {open ? '✕ DevTools' : '🔍 API Cache'}\n </button>\n\n {open && (\n <div\n style={{\n marginTop: 8,\n background: '#16213e',\n border: '1px solid #0f3460',\n borderRadius: 8,\n padding: 12,\n width: 420,\n maxHeight: 500,\n overflowY: 'auto',\n color: '#e0e0e0',\n }}\n >\n <div\n style={{\n display: 'flex',\n justifyContent: 'space-between',\n marginBottom: 8,\n }}\n >\n <strong style={{ color: '#e94560' }}>\n API Cache ({entries.length} keys)\n </strong>\n <button\n onClick={() => queryClient.cache.clear()}\n style={{\n background: 'transparent',\n border: '1px solid #e94560',\n color: '#e94560',\n borderRadius: 4,\n padding: '2px 8px',\n cursor: 'pointer',\n fontSize: 11,\n }}\n >\n Clear\n </button>\n </div>\n\n {entries.length === 0 && (\n <div style={{ color: '#888', textAlign: 'center', padding: 16 }}>\n Cache is empty\n </div>\n )}\n\n {entries.map(({ key, entry }) => {\n const age =\n entry.updatedAt\n ? Math.round((Date.now() - entry.updatedAt) / 1000)\n : null;\n const statusColor =\n {\n success: '#4caf50',\n error: '#f44336',\n loading: '#ff9800',\n idle: '#888',\n }[entry.status] ?? '#888';\n return (\n <div\n key={key}\n style={{\n marginBottom: 8,\n background: '#0f3460',\n borderRadius: 6,\n padding: 8,\n }}\n >\n <div\n style={{\n display: 'flex',\n justifyContent: 'space-between',\n marginBottom: 4,\n }}\n >\n <code\n style={{\n color: '#e0e0e0',\n wordBreak: 'break-all',\n fontSize: 11,\n }}\n >\n {key.length > 60 ? key.slice(0, 57) + '…' : key}\n </code>\n <span\n style={{ color: statusColor, marginLeft: 8, flexShrink: 0 }}\n >\n ● {entry.status}\n </span>\n </div>\n <div style={{ color: '#888', fontSize: 10 }}>\n {age !== null ? `age: ${age}s` : 'no data'}\n {' · '}\n stale: {entry.staleTime > 0 ? `${entry.staleTime / 1000}s` : 'always'}\n {entry.updatedAt === 0 && ' · ⚠ invalidated'}\n </div>\n </div>\n );\n })}\n </div>\n )}\n </div>\n );\n}\n\nApiDevTools.displayName = 'ApiDevTools';\n"]}
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+
3
+ declare function ApiDevTools(): React.JSX.Element;
4
+ declare namespace ApiDevTools {
5
+ var displayName: string;
6
+ }
7
+
8
+ export { ApiDevTools };
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+
3
+ declare function ApiDevTools(): React.JSX.Element;
4
+ declare namespace ApiDevTools {
5
+ var displayName: string;
6
+ }
7
+
8
+ export { ApiDevTools };
@@ -0,0 +1,179 @@
1
+ import { createContext, useState, useEffect, useContext } from 'react';
2
+ import { jsxs, jsx } from 'react/jsx-runtime';
3
+
4
+ var ApiContext = createContext(null);
5
+ function useApiContext() {
6
+ const ctx = useContext(ApiContext);
7
+ if (!ctx) {
8
+ throw new Error(
9
+ "[api-client] Hooks must be used within <ApiProvider>. Wrap your app root with <ApiProvider client={api}>."
10
+ );
11
+ }
12
+ return ctx;
13
+ }
14
+ function ApiDevTools() {
15
+ const [open, setOpen] = useState(false);
16
+ const [entries, setEntries] = useState([]);
17
+ const { queryClient } = useApiContext();
18
+ useEffect(() => {
19
+ const update = () => {
20
+ const snapshot = queryClient.cache.snapshot();
21
+ setEntries(
22
+ Object.entries(snapshot).map(([key, entry]) => ({ key, entry }))
23
+ );
24
+ };
25
+ update();
26
+ return queryClient.cache.subscribeGlobal(update);
27
+ }, [queryClient]);
28
+ return /* @__PURE__ */ jsxs(
29
+ "div",
30
+ {
31
+ style: {
32
+ position: "fixed",
33
+ bottom: 16,
34
+ right: 16,
35
+ zIndex: 9999,
36
+ fontFamily: "monospace",
37
+ fontSize: 12
38
+ },
39
+ children: [
40
+ /* @__PURE__ */ jsx(
41
+ "button",
42
+ {
43
+ onClick: () => setOpen((o) => !o),
44
+ style: {
45
+ background: "#1a1a2e",
46
+ color: "#e94560",
47
+ border: "1px solid #e94560",
48
+ borderRadius: 6,
49
+ padding: "6px 12px",
50
+ cursor: "pointer",
51
+ fontFamily: "monospace"
52
+ },
53
+ children: open ? "\u2715 DevTools" : "\u{1F50D} API Cache"
54
+ }
55
+ ),
56
+ open && /* @__PURE__ */ jsxs(
57
+ "div",
58
+ {
59
+ style: {
60
+ marginTop: 8,
61
+ background: "#16213e",
62
+ border: "1px solid #0f3460",
63
+ borderRadius: 8,
64
+ padding: 12,
65
+ width: 420,
66
+ maxHeight: 500,
67
+ overflowY: "auto",
68
+ color: "#e0e0e0"
69
+ },
70
+ children: [
71
+ /* @__PURE__ */ jsxs(
72
+ "div",
73
+ {
74
+ style: {
75
+ display: "flex",
76
+ justifyContent: "space-between",
77
+ marginBottom: 8
78
+ },
79
+ children: [
80
+ /* @__PURE__ */ jsxs("strong", { style: { color: "#e94560" }, children: [
81
+ "API Cache (",
82
+ entries.length,
83
+ " keys)"
84
+ ] }),
85
+ /* @__PURE__ */ jsx(
86
+ "button",
87
+ {
88
+ onClick: () => queryClient.cache.clear(),
89
+ style: {
90
+ background: "transparent",
91
+ border: "1px solid #e94560",
92
+ color: "#e94560",
93
+ borderRadius: 4,
94
+ padding: "2px 8px",
95
+ cursor: "pointer",
96
+ fontSize: 11
97
+ },
98
+ children: "Clear"
99
+ }
100
+ )
101
+ ]
102
+ }
103
+ ),
104
+ entries.length === 0 && /* @__PURE__ */ jsx("div", { style: { color: "#888", textAlign: "center", padding: 16 }, children: "Cache is empty" }),
105
+ entries.map(({ key, entry }) => {
106
+ const age = entry.updatedAt ? Math.round((Date.now() - entry.updatedAt) / 1e3) : null;
107
+ const statusColor = {
108
+ success: "#4caf50",
109
+ error: "#f44336",
110
+ loading: "#ff9800",
111
+ idle: "#888"
112
+ }[entry.status] ?? "#888";
113
+ return /* @__PURE__ */ jsxs(
114
+ "div",
115
+ {
116
+ style: {
117
+ marginBottom: 8,
118
+ background: "#0f3460",
119
+ borderRadius: 6,
120
+ padding: 8
121
+ },
122
+ children: [
123
+ /* @__PURE__ */ jsxs(
124
+ "div",
125
+ {
126
+ style: {
127
+ display: "flex",
128
+ justifyContent: "space-between",
129
+ marginBottom: 4
130
+ },
131
+ children: [
132
+ /* @__PURE__ */ jsx(
133
+ "code",
134
+ {
135
+ style: {
136
+ color: "#e0e0e0",
137
+ wordBreak: "break-all",
138
+ fontSize: 11
139
+ },
140
+ children: key.length > 60 ? key.slice(0, 57) + "\u2026" : key
141
+ }
142
+ ),
143
+ /* @__PURE__ */ jsxs(
144
+ "span",
145
+ {
146
+ style: { color: statusColor, marginLeft: 8, flexShrink: 0 },
147
+ children: [
148
+ "\u25CF ",
149
+ entry.status
150
+ ]
151
+ }
152
+ )
153
+ ]
154
+ }
155
+ ),
156
+ /* @__PURE__ */ jsxs("div", { style: { color: "#888", fontSize: 10 }, children: [
157
+ age !== null ? `age: ${age}s` : "no data",
158
+ " \xB7 ",
159
+ "stale: ",
160
+ entry.staleTime > 0 ? `${entry.staleTime / 1e3}s` : "always",
161
+ entry.updatedAt === 0 && " \xB7 \u26A0 invalidated"
162
+ ] })
163
+ ]
164
+ },
165
+ key
166
+ );
167
+ })
168
+ ]
169
+ }
170
+ )
171
+ ]
172
+ }
173
+ );
174
+ }
175
+ ApiDevTools.displayName = "ApiDevTools";
176
+
177
+ export { ApiDevTools };
178
+ //# sourceMappingURL=devtools.mjs.map
179
+ //# sourceMappingURL=devtools.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/provider.tsx","../src/devtools/ApiDevTools.tsx"],"names":["jsx"],"mappings":";;;AAYA,IAAM,UAAA,GAAa,cAAsC,IAAI,CAAA;AAEtD,SAAS,aAAA,GAAiC;AAC/C,EAAA,MAAM,GAAA,GAAM,WAAW,UAAU,CAAA;AACjC,EAAA,IAAI,CAAC,GAAA,EAAK;AACR,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,GAAA;AACT;ACZO,SAAS,WAAA,GAAiC;AAC/C,EAAA,MAAM,CAAC,IAAA,EAAM,OAAO,CAAA,GAAI,SAAS,KAAK,CAAA;AACtC,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAI,QAAA,CAA0B,EAAE,CAAA;AAC1D,EAAA,MAAM,EAAE,WAAA,EAAY,GAAI,aAAA,EAAc;AAEtC,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,M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AAAA,8BAAA;AAAA,gCACC,OAAO,EAAE,KAAA,EAAO,aAAa,UAAA,EAAY,CAAA,EAAG,YAAY,CAAA,EAAE;AAAA,gCAC3D,QAAA,EAAA;AAAA,kCAAA,SAAA;AAAA,kCACI,KAAA,CAAM;AAAA;AAAA;AAAA;AACX;AAAA;AAAA,uBACF;AAAA,sCACA,IAAA,CAAC,SAAI,KAAA,EAAO,EAAE,OAAO,MAAA,EAAQ,QAAA,EAAU,IAAG,EACvC,QAAA,EAAA;AAAA,wBAAA,GAAA,KAAQ,IAAA,GAAO,CAAA,KAAA,EAAQ,GAAG,CAAA,CAAA,CAAA,GAAM,SAAA;AAAA,wBAChC,QAAA;AAAA,wBAAM,SAAA;AAAA,wBACC,MAAM,SAAA,GAAY,CAAA,GAAI,GAAG,KAAA,CAAM,SAAA,GAAY,GAAI,CAAA,CAAA,CAAA,GAAM,QAAA;AAAA,wBAC5D,KAAA,CAAM,cAAc,CAAA,IAAK;AAAA,uBAAA,EAC5B;AAAA;AAAA,mBAAA;AAAA,kBAnCK;AAAA,iBAoCP;AAAA,cAEJ,CAAC;AAAA;AAAA;AAAA;AACH;AAAA;AAAA,GAEJ;AAEJ;AAEA,WAAA,CAAY,WAAA,GAAc,aAAA","file":"devtools.mjs","sourcesContent":["import React, { createContext, useContext, useRef } from 'react';\nimport type { ReactNode } from 'react';\nimport { QueryClient } from './cache';\nimport type { ApiClient } from './client';\nimport type { DehydratedState } from './server';\nimport type { QueryClientConfig } from './types';\n\ninterface ApiContextValue {\n apiClient: ApiClient;\n queryClient: QueryClient;\n}\n\nconst ApiContext = createContext<ApiContextValue | null>(null);\n\nexport function useApiContext(): ApiContextValue {\n const ctx = useContext(ApiContext);\n if (!ctx) {\n throw new Error(\n '[api-client] Hooks must be used within <ApiProvider>. ' +\n 'Wrap your app root with <ApiProvider client={api}>.',\n );\n }\n return ctx;\n}\n\nexport function useApiClient(): ApiClient {\n return useApiContext().apiClient;\n}\n\nfunction makeQueryClient(config?: QueryClientConfig): QueryClient {\n return new QueryClient(config);\n}\n\nlet browserQueryClient: QueryClient | undefined;\n\nfunction getQueryClient(config?: QueryClientConfig): QueryClient {\n if (typeof window === 'undefined') return makeQueryClient(config);\n if (!browserQueryClient) browserQueryClient = makeQueryClient(config);\n return browserQueryClient;\n}\n\nexport interface ApiProviderProps {\n client: ApiClient;\n children: ReactNode;\n /** Inject a pre-built QueryClient (advanced / testing) */\n queryClient?: QueryClient;\n /** Override the GC time for entries with no active subscriber (ms, default 5 min) */\n gcTime?: number;\n /** Hydrate from server-prefetched state (SSR) */\n hydratedState?: DehydratedState;\n /** Global QueryClient configuration (callbacks etc.) */\n queryClientConfig?: QueryClientConfig;\n}\n\nexport function ApiProvider({\n client,\n children,\n queryClient,\n gcTime,\n hydratedState,\n queryClientConfig,\n}: ApiProviderProps): React.JSX.Element {\n const qcRef = useRef<QueryClient | null>(null);\n if (!qcRef.current) {\n const config: QueryClientConfig = {\n gcTime,\n ...queryClientConfig,\n };\n qcRef.current = queryClient ?? getQueryClient(config);\n if (hydratedState) {\n qcRef.current.cache.restore(hydratedState.entries);\n }\n }\n\n const ctxRef = useRef<ApiContextValue | null>(null);\n if (!ctxRef.current || ctxRef.current.apiClient !== client) {\n ctxRef.current = { apiClient: client, queryClient: qcRef.current };\n }\n\n return (\n <ApiContext.Provider value={ctxRef.current}>{children}</ApiContext.Provider>\n );\n}\n\nApiProvider.displayName = 'ApiProvider';\n","'use client'; // safe to include here — this is a UI component\n\nimport React, { useState, useEffect } from 'react';\nimport { useApiContext } from '../provider';\nimport type { CacheEntry } from '../cache';\n\ninterface CacheSnapshot {\n key: string;\n entry: CacheEntry;\n}\n\nexport function ApiDevTools(): React.JSX.Element {\n const [open, setOpen] = useState(false);\n const [entries, setEntries] = useState<CacheSnapshot[]>([]);\n const { queryClient } = useApiContext();\n\n useEffect(() => {\n const update = () => {\n const snapshot = queryClient.cache.snapshot();\n setEntries(\n Object.entries(snapshot).map(([key, entry]) => ({ key, entry })),\n );\n };\n update();\n return queryClient.cache.subscribeGlobal(update);\n }, [queryClient]);\n\n return (\n <div\n style={{\n position: 'fixed',\n bottom: 16,\n right: 16,\n zIndex: 9999,\n fontFamily: 'monospace',\n fontSize: 12,\n }}\n >\n <button\n onClick={() => setOpen((o) => !o)}\n style={{\n background: '#1a1a2e',\n color: '#e94560',\n border: '1px solid #e94560',\n borderRadius: 6,\n padding: '6px 12px',\n cursor: 'pointer',\n fontFamily: 'monospace',\n }}\n >\n {open ? '✕ DevTools' : '🔍 API Cache'}\n </button>\n\n {open && (\n <div\n style={{\n marginTop: 8,\n background: '#16213e',\n border: '1px solid #0f3460',\n borderRadius: 8,\n padding: 12,\n width: 420,\n maxHeight: 500,\n overflowY: 'auto',\n color: '#e0e0e0',\n }}\n >\n <div\n style={{\n display: 'flex',\n justifyContent: 'space-between',\n marginBottom: 8,\n }}\n >\n <strong style={{ color: '#e94560' }}>\n API Cache ({entries.length} keys)\n </strong>\n <button\n onClick={() => queryClient.cache.clear()}\n style={{\n background: 'transparent',\n border: '1px solid #e94560',\n color: '#e94560',\n borderRadius: 4,\n padding: '2px 8px',\n cursor: 'pointer',\n fontSize: 11,\n }}\n >\n Clear\n </button>\n </div>\n\n {entries.length === 0 && (\n <div style={{ color: '#888', textAlign: 'center', padding: 16 }}>\n Cache is empty\n </div>\n )}\n\n {entries.map(({ key, entry }) => {\n const age =\n entry.updatedAt\n ? Math.round((Date.now() - entry.updatedAt) / 1000)\n : null;\n const statusColor =\n {\n success: '#4caf50',\n error: '#f44336',\n loading: '#ff9800',\n idle: '#888',\n }[entry.status] ?? '#888';\n return (\n <div\n key={key}\n style={{\n marginBottom: 8,\n background: '#0f3460',\n borderRadius: 6,\n padding: 8,\n }}\n >\n <div\n style={{\n display: 'flex',\n justifyContent: 'space-between',\n marginBottom: 4,\n }}\n >\n <code\n style={{\n color: '#e0e0e0',\n wordBreak: 'break-all',\n fontSize: 11,\n }}\n >\n {key.length > 60 ? key.slice(0, 57) + '…' : key}\n </code>\n <span\n style={{ color: statusColor, marginLeft: 8, flexShrink: 0 }}\n >\n ● {entry.status}\n </span>\n </div>\n <div style={{ color: '#888', fontSize: 10 }}>\n {age !== null ? `age: ${age}s` : 'no data'}\n {' · '}\n stale: {entry.staleTime > 0 ? `${entry.staleTime / 1000}s` : 'always'}\n {entry.updatedAt === 0 && ' · ⚠ invalidated'}\n </div>\n </div>\n );\n })}\n </div>\n )}\n </div>\n );\n}\n\nApiDevTools.displayName = 'ApiDevTools';\n"]}