@veroxm/react-sdk 0.0.0-stage → 0.1.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +295 -2
- package/dist/cache.d.ts +69 -0
- package/dist/cache.js +89 -0
- package/dist/client-slots.d.ts +35 -0
- package/dist/client-slots.js +169 -0
- package/dist/client.d.ts +65 -0
- package/dist/client.js +87 -0
- package/dist/editor/constants.d.ts +12 -0
- package/dist/editor/constants.js +12 -0
- package/dist/editor/handler.d.ts +16 -0
- package/dist/editor/handler.js +175 -0
- package/dist/editor/inline.d.ts +25 -0
- package/dist/editor/inline.js +149 -0
- package/dist/editor/menu-input.d.ts +26 -0
- package/dist/editor/menu-input.js +110 -0
- package/dist/editor/overlay.d.ts +31 -0
- package/dist/editor/overlay.js +856 -0
- package/dist/editor/rich-text.d.ts +21 -0
- package/dist/editor/rich-text.js +232 -0
- package/dist/editor/server.d.ts +9 -0
- package/dist/editor/server.js +43 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +26 -0
- package/dist/hooks/use-cached-query.d.ts +21 -0
- package/dist/hooks/use-cached-query.js +59 -0
- package/dist/hooks/use-collection.d.ts +10 -0
- package/dist/hooks/use-collection.js +14 -0
- package/dist/hooks/use-entries.d.ts +18 -0
- package/dist/hooks/use-entries.js +25 -0
- package/dist/hooks/use-entry.d.ts +21 -0
- package/dist/hooks/use-entry.js +22 -0
- package/dist/hooks/use-media.d.ts +9 -0
- package/dist/hooks/use-media.js +13 -0
- package/dist/hydration-boundary.d.ts +35 -0
- package/dist/hydration-boundary.js +38 -0
- package/dist/image.d.ts +34 -0
- package/dist/image.js +39 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +32 -0
- package/dist/next/cache-components.d.ts +15 -0
- package/dist/next/cache-components.js +25 -0
- package/dist/next/cached.d.ts +9 -0
- package/dist/next/cached.js +20 -0
- package/dist/next/edge-slot.d.ts +21 -0
- package/dist/next/edge-slot.js +29 -0
- package/dist/next/editor.d.ts +7 -0
- package/dist/next/editor.js +13 -0
- package/dist/next/index.d.ts +17 -0
- package/dist/next/index.js +21 -0
- package/dist/next/llms.d.ts +2 -0
- package/dist/next/llms.js +44 -0
- package/dist/next/route.d.ts +12 -0
- package/dist/next/route.js +16 -0
- package/dist/next/slot.d.ts +21 -0
- package/dist/next/slot.js +160 -0
- package/dist/node.d.ts +43 -0
- package/dist/node.js +67 -0
- package/dist/preview.d.ts +35 -0
- package/dist/preview.js +46 -0
- package/dist/provider.d.ts +29 -0
- package/dist/provider.js +37 -0
- package/dist/query-keys.d.ts +19 -0
- package/dist/query-keys.js +49 -0
- package/dist/site/chart-view.d.ts +14 -0
- package/dist/site/chart-view.js +44 -0
- package/dist/site/chart.d.ts +127 -0
- package/dist/site/chart.js +639 -0
- package/dist/site/map-embed.d.ts +32 -0
- package/dist/site/map-embed.js +139 -0
- package/dist/site/map-math.d.ts +27 -0
- package/dist/site/map-math.js +89 -0
- package/dist/site/nav-menu.d.ts +19 -0
- package/dist/site/nav-menu.js +76 -0
- package/dist/site/site-parts.d.ts +87 -0
- package/dist/site/site-parts.js +46 -0
- package/dist/site/slider.d.ts +17 -0
- package/dist/site/slider.js +78 -0
- package/dist/site/styles.d.ts +3 -0
- package/dist/site/styles.js +155 -0
- package/dist/site/video-embed.d.ts +15 -0
- package/dist/site/video-embed.js +27 -0
- package/dist/site/video.d.ts +18 -0
- package/dist/site/video.js +73 -0
- package/dist/slots-core.d.ts +397 -0
- package/dist/slots-core.js +367 -0
- package/dist/slots.d.ts +13 -0
- package/dist/slots.js +30 -0
- package/dist/ssr.d.ts +20 -0
- package/dist/ssr.js +35 -0
- package/dist/structured-data.d.ts +13 -0
- package/dist/structured-data.js +36 -0
- package/dist/tracking.d.ts +142 -0
- package/dist/tracking.js +152 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.js +6 -0
- package/package.json +95 -4
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 VeroXM
|
|
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
CHANGED
|
@@ -1,3 +1,296 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @veroxm/react-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React hooks for VeroXM's public GraphQL API -- read content, media, and
|
|
4
|
+
collection metadata from any React 18/19 app (Next.js App Router or Pages
|
|
5
|
+
Router, Vite, React Native). GraphQL-first: hooks call the GraphQL API
|
|
6
|
+
directly (`/public/v2/graphql`), not a wrapper over `@veroxm/sdk`'s REST
|
|
7
|
+
client. One runtime dependency, `graphql-request`, kept deliberately thin
|
|
8
|
+
rather than a full Apollo/urql client -- this API is queries-only, so a
|
|
9
|
+
normalized entity cache and mutation machinery would be more than this
|
|
10
|
+
surface needs.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @veroxm/react-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Peer dependencies: `react` 18 or 19 (and `react-dom`, `next` 15+ when you use them).
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { VeroxmProvider, useCollection, useEntry, useEntries, useMedia } from '@veroxm/react-sdk';
|
|
24
|
+
|
|
25
|
+
function App() {
|
|
26
|
+
return (
|
|
27
|
+
<VeroxmProvider apiBase="https://your-veroxm-host" apiKey={process.env.VEROXM_API_KEY!}>
|
|
28
|
+
<PostsList />
|
|
29
|
+
</VeroxmProvider>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface Post {
|
|
34
|
+
title: string;
|
|
35
|
+
body: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function PostsList() {
|
|
39
|
+
// Typed access to one content collection's entries -- `T` is whatever
|
|
40
|
+
// shape you expect `data` to be, same opt-in cast convention as
|
|
41
|
+
// @veroxm/sdk's ContentResource.
|
|
42
|
+
const { data, loading, error } = useEntries<Post>('posts', {
|
|
43
|
+
sort: 'created_at:desc',
|
|
44
|
+
limit: 10,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (loading) return <p>Loading...</p>;
|
|
48
|
+
if (error) return <p>{error.message}</p>;
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<ul>
|
|
52
|
+
{data!.items.map((entry) => (
|
|
53
|
+
<li key={entry.id}>{entry.data.title}</li>
|
|
54
|
+
))}
|
|
55
|
+
</ul>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The `apiKey` is a project-scoped API token (Developer tab > API Keys in
|
|
61
|
+
the dashboard) -- it's the request's only source of project identity, so
|
|
62
|
+
unlike the REST v2 API there's no separate project uuid to pass on every
|
|
63
|
+
call.
|
|
64
|
+
|
|
65
|
+
## Core hooks
|
|
66
|
+
|
|
67
|
+
- **`useCollection(slug)`** -- a collection's field list. Build dynamic
|
|
68
|
+
forms/renderers against a collection without hardcoding its shape.
|
|
69
|
+
- **`useEntry(collection, id, { timestamps? })`** -- a single entry by id.
|
|
70
|
+
`data` is `null` (once `loading` is false) when no entry with that id
|
|
71
|
+
exists. Only ever returns a **published** entry -- same restriction
|
|
72
|
+
`@veroxm/sdk`'s `ContentResource.get()` documents for REST, since the
|
|
73
|
+
underlying `entry` query has no `state` argument at all.
|
|
74
|
+
- **`useEntries(collection, options?)`** -- a page of entries. `options`
|
|
75
|
+
mirrors `ListOptions` from `@veroxm/sdk`/the REST v2 API (`where`,
|
|
76
|
+
`sort`, `locale`, `state`, `offset`, `limit`, `timestamps`), so porting
|
|
77
|
+
REST-based fetching code is mostly a hook-name-and-shape swap. Compared
|
|
78
|
+
by *value* across renders (a stable JSON key), so a fresh object literal
|
|
79
|
+
every render is fine -- you don't need to `useMemo()` it yourself. Pass
|
|
80
|
+
`withTotalCount: true` to also get `data.totalCount`; it costs one extra
|
|
81
|
+
query server-side, so it's opt-in.
|
|
82
|
+
- **`useMedia(id)`** -- a single media item by id.
|
|
83
|
+
|
|
84
|
+
Every hook returns the same shape:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
interface QueryState<T> {
|
|
88
|
+
data: T | undefined;
|
|
89
|
+
error: Error | undefined;
|
|
90
|
+
loading: boolean; // true only while there is no data at all yet
|
|
91
|
+
isValidating: boolean; // true during ANY fetch, including a background revalidation
|
|
92
|
+
refetch: () => void;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`where` filters use the same operator vocabulary as the REST API, just
|
|
97
|
+
typed as an array instead of an untyped DSL object:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
useEntries('posts', {
|
|
101
|
+
where: [{ field: 'title', op: 'like', value: 'Hello' }],
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Caching + revalidation
|
|
106
|
+
|
|
107
|
+
Every hook reads through a tag-based request cache owned by the
|
|
108
|
+
`VeroxmClient` your `<VeroxmProvider>` constructs (or that you construct
|
|
109
|
+
yourself and pass in via the `client` prop). Each cached query is tagged
|
|
110
|
+
with the collection slug it read from (plus locale, when given) --
|
|
111
|
+
`invalidateCollection()`/`invalidateLocale()`/`invalidateMedia()` drop
|
|
112
|
+
everything sharing that tag, matching the API's own server-side
|
|
113
|
+
cache-invalidation model:
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
import { useVeroxmClient } from '@veroxm/react-sdk';
|
|
117
|
+
|
|
118
|
+
function RefreshButton() {
|
|
119
|
+
const client = useVeroxmClient();
|
|
120
|
+
return <button onClick={() => client.cache.invalidateCollection('posts')}>Refresh</button>;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
A fresh cache hit returns immediately with `loading: false` and no network
|
|
125
|
+
call; a stale one (older than `staleTimeMs`, 30s by default) returns the
|
|
126
|
+
cached value immediately too, then revalidates in the background
|
|
127
|
+
(`isValidating` flips to `true`, `data` stays the last-known-good value
|
|
128
|
+
throughout). This GraphQL surface is queries-only, so nothing invalidates
|
|
129
|
+
the cache automatically on a write -- call `client.cache.invalidateX()`
|
|
130
|
+
yourself once you know content changed (a webhook-driven revalidation
|
|
131
|
+
route, a manual "Refresh" action). Tune the cache via
|
|
132
|
+
`<VeroxmProvider cache={{ staleTimeMs: 60_000 }}>` or
|
|
133
|
+
`new VeroxmClient({ ..., cache: { staleTimeMs: 60_000 } })`.
|
|
134
|
+
|
|
135
|
+
## Live preview / draft mode
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
import { VeroxmPreviewProvider, useDraftMode } from '@veroxm/react-sdk';
|
|
139
|
+
|
|
140
|
+
<VeroxmPreviewProvider initialDraftMode={isPreviewingInNextjs}>
|
|
141
|
+
<App />
|
|
142
|
+
</VeroxmPreviewProvider>;
|
|
143
|
+
|
|
144
|
+
function EditToggle() {
|
|
145
|
+
const { isDraftMode, enableDraftMode, disableDraftMode } = useDraftMode();
|
|
146
|
+
// ...
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
While active, `useEntries()` requests `state: 'only_draft'` instead of the
|
|
151
|
+
default published-only, unless you already passed an explicit `state`
|
|
152
|
+
(which always wins). `useEntry()` deliberately does **not** respect draft
|
|
153
|
+
mode -- its underlying query has no `state` argument to override. To
|
|
154
|
+
preview one specific unpublished entry, use `useEntries()` with
|
|
155
|
+
`state: 'only_draft'` plus a `where` filter identifying it (e.g. by
|
|
156
|
+
`slug`). `useDraftMode()` is safe to call with no `<VeroxmPreviewProvider>`
|
|
157
|
+
mounted (`isDraftMode: false`, so an app that never uses preview mode
|
|
158
|
+
doesn't need to wrap anything).
|
|
159
|
+
|
|
160
|
+
## SSR / Next.js helpers
|
|
161
|
+
|
|
162
|
+
Framework-agnostic fetch functions -- no hooks, safe to call from a Server
|
|
163
|
+
Component, `getServerSideProps`, or a route handler -- paired with
|
|
164
|
+
`<VeroxmHydrationBoundary>` so the client-side hook for the identical query
|
|
165
|
+
skips its immediate refetch after hydration:
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
import { fetchEntries, VeroxmClient, VeroxmHydrationBoundary } from '@veroxm/react-sdk';
|
|
169
|
+
|
|
170
|
+
// Server Component
|
|
171
|
+
async function PostsPage() {
|
|
172
|
+
const client = new VeroxmClient({ apiBase: process.env.VEROXM_HOST!, apiKey: process.env.VEROXM_API_KEY! });
|
|
173
|
+
const { data, hydration } = await fetchEntries(client, 'posts', { limit: 10 });
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<VeroxmHydrationBoundary state={hydration}>
|
|
177
|
+
<PostsList initialPosts={data.items} />
|
|
178
|
+
</VeroxmHydrationBoundary>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
`fetchCollection`/`fetchCollections`/`fetchEntry`/`fetchEntries`/
|
|
184
|
+
`fetchMedia` each return `{ data, hydration }` -- pass `hydration` straight
|
|
185
|
+
through to `<VeroxmHydrationBoundary state={...}>` (an array when a page
|
|
186
|
+
made more than one server-side fetch). The client instance you construct
|
|
187
|
+
server-side is a separate object from the browser's; seeding only takes
|
|
188
|
+
effect once its data crosses into the hydration boundary client-side.
|
|
189
|
+
|
|
190
|
+
## `<VeroxmImage>`
|
|
191
|
+
|
|
192
|
+
Renders one of `MediaView`'s already-generated variants as a `<picture>` --
|
|
193
|
+
the WebP source preferred, the original format as the `<img>` fallback,
|
|
194
|
+
lazy-loaded by default:
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
import { useMedia, VeroxmImage } from '@veroxm/react-sdk';
|
|
198
|
+
|
|
199
|
+
function Avatar({ mediaId }: { mediaId: number }) {
|
|
200
|
+
const { data } = useMedia(mediaId);
|
|
201
|
+
if (!data) return null;
|
|
202
|
+
return <VeroxmImage media={data} thumb width={64} height={64} />;
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`alt` defaults to the media item's own `caption`, then an empty string --
|
|
207
|
+
pass an explicit `alt` to override either.
|
|
208
|
+
|
|
209
|
+
## Visitor tracking
|
|
210
|
+
|
|
211
|
+
Pair with `@veroxm/sdk`'s `createTracker()` (see that package's README for
|
|
212
|
+
consent and delivery details):
|
|
213
|
+
|
|
214
|
+
```tsx
|
|
215
|
+
'use client';
|
|
216
|
+
import { createTracker } from '@veroxm/sdk';
|
|
217
|
+
import { VeroxmTrackerProvider, usePageViews, useTracker } from '@veroxm/react-sdk';
|
|
218
|
+
import { usePathname } from 'next/navigation';
|
|
219
|
+
|
|
220
|
+
// Create once, in the browser only (null during server rendering).
|
|
221
|
+
const tracker = typeof window === 'undefined' ? null
|
|
222
|
+
: createTracker({ baseUrl, projectId, eventsKey });
|
|
223
|
+
|
|
224
|
+
export function Tracking({ children }: { children: React.ReactNode }) {
|
|
225
|
+
return <VeroxmTrackerProvider tracker={tracker}><PageViews />{children}</VeroxmTrackerProvider>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function PageViews() {
|
|
229
|
+
usePageViews(usePathname()); // one page view per route change
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function BookDemoButton() {
|
|
234
|
+
const tracker = useTracker(); // a harmless no-op outside a provider
|
|
235
|
+
return <button onClick={() => tracker.track('demo_requested')}>Book a demo</button>;
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Experiences
|
|
240
|
+
|
|
241
|
+
Render this visitor's variant of a placement (set up in the dashboard under
|
|
242
|
+
**Experiences**). Both forms ask again when consent or identity changes,
|
|
243
|
+
and fall back to your default for "none", errors, missing consent and a
|
|
244
|
+
control variant with no entry or config:
|
|
245
|
+
|
|
246
|
+
```tsx
|
|
247
|
+
import { Experience, useExperience } from '@veroxm/react-sdk';
|
|
248
|
+
|
|
249
|
+
<Experience<HeroEntry> placement="home.hero" fallback={<DefaultHero />}>
|
|
250
|
+
{(d) => <Hero entry={d.content ?? defaultHero} cta={d.config?.cta as string | undefined} />}
|
|
251
|
+
</Experience>
|
|
252
|
+
|
|
253
|
+
function Banner() {
|
|
254
|
+
const { decision, loading } = useExperience<BannerEntry>('home.banner');
|
|
255
|
+
if (loading) return <BannerSkeleton />;
|
|
256
|
+
return decision?.content ? <PromoBanner entry={decision.content} /> : null;
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Recommendations
|
|
261
|
+
|
|
262
|
+
```tsx
|
|
263
|
+
import { useContentView, useRecommendations } from '@veroxm/react-sdk';
|
|
264
|
+
|
|
265
|
+
function ArticlePage({ article }: { article: Article }) {
|
|
266
|
+
useContentView(article.id); // the signal recommendations learn from
|
|
267
|
+
const { items } = useRecommendations<Article>('articles', { limit: 3, exclude: [article.id] });
|
|
268
|
+
return <ReadNext articles={items} />;
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Errors
|
|
273
|
+
|
|
274
|
+
Every failed request throws `VeroxmGraphQLError` (`.status`, `.errors` --
|
|
275
|
+
the raw GraphQL errors array, useful for checking a specific
|
|
276
|
+
`extensions.code` like `QUERY_TOO_COMPLEX` -- and a `.message` pulled from
|
|
277
|
+
the first GraphQL error):
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
import { VeroxmGraphQLError } from '@veroxm/react-sdk';
|
|
281
|
+
|
|
282
|
+
const { error } = useEntries('posts');
|
|
283
|
+
if (error instanceof VeroxmGraphQLError && error.errors[0]?.extensions?.code === 'QUERY_TOO_COMPLEX') {
|
|
284
|
+
// this query's filters/pagination need narrowing
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Documentation and support
|
|
289
|
+
|
|
290
|
+
- Guide: https://docs.veroxm.com/platform-tools/react-sdk
|
|
291
|
+
- All VeroXM SDKs: https://docs.veroxm.com/sdks
|
|
292
|
+
- Changelog: https://docs.veroxm.com/changelog
|
|
293
|
+
|
|
294
|
+
## License
|
|
295
|
+
|
|
296
|
+
MIT
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface CacheOptions {
|
|
2
|
+
/**
|
|
3
|
+
* How long a cached entry is considered fresh before a background
|
|
4
|
+
* revalidation is triggered the next time a hook reads it. Defaults to
|
|
5
|
+
* 30_000ms (30s). Set to 0 to always revalidate in the background on
|
|
6
|
+
* every access (still returning the cached value immediately); set to
|
|
7
|
+
* `Infinity` to never auto-revalidate an entry -- only
|
|
8
|
+
* invalidateCollection()/invalidateTag()/invalidateAll() (or a fresh
|
|
9
|
+
* page load) will refresh it.
|
|
10
|
+
*/
|
|
11
|
+
staleTimeMs?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Tag-based request cache + in-flight dedupe, owned by one VeroxmClient
|
|
15
|
+
* instance and shared by every hook that client supplies
|
|
16
|
+
* (docs/REACT-SDK-GRAPHQL-IMPLEMENTATION-PLAN.md §5.3). Deliberately NOT
|
|
17
|
+
* a normalized entity cache (the kind Apollo/urql build): an Entry's
|
|
18
|
+
* `data` is an opaque per-project JSON blob (see the plan's §2
|
|
19
|
+
* architecture note), so there's no stable, collection-agnostic way to
|
|
20
|
+
* decompose it into normalized fields and invalidate them individually.
|
|
21
|
+
* Instead, invalidation is tag-based -- every cached query is tagged
|
|
22
|
+
* with the collection slug (+ locale) it read from, matching
|
|
23
|
+
* ReadCacheService's own invalidation model server-side
|
|
24
|
+
* (apps/api's cache-aside layer), just applied client-side.
|
|
25
|
+
*
|
|
26
|
+
* This GraphQL surface is queries-only (see plan §0), so there is no
|
|
27
|
+
* mutation to auto-invalidate on write. Call invalidateCollection() (or
|
|
28
|
+
* invalidateMedia()/invalidateAll()) yourself once you know a collection
|
|
29
|
+
* changed -- e.g. from a webhook-driven Next.js revalidation route, or a
|
|
30
|
+
* manual "Refresh" action in your own UI.
|
|
31
|
+
*/
|
|
32
|
+
export declare class VeroxmCache {
|
|
33
|
+
private readonly entries;
|
|
34
|
+
private readonly inFlight;
|
|
35
|
+
private readonly staleTimeMs;
|
|
36
|
+
constructor(options?: CacheOptions);
|
|
37
|
+
/** The cached value for `key`, if one exists -- `undefined` means "never fetched, or invalidated since", regardless of whether a fresher one is currently in flight. Doesn't distinguish fresh from stale; call isStale() separately if that matters to you. */
|
|
38
|
+
get<T>(key: string): T | undefined;
|
|
39
|
+
/** True when `key` has no cached entry at all, or its entry is older than this cache's staleTimeMs. */
|
|
40
|
+
isStale(key: string): boolean;
|
|
41
|
+
set<T>(key: string, data: T, tags: readonly string[]): void;
|
|
42
|
+
/**
|
|
43
|
+
* Seeds the cache with already-known data -- identical to set() today;
|
|
44
|
+
* kept as its own named method because callers reach for it for a
|
|
45
|
+
* different reason (supplying data this cache didn't fetch itself,
|
|
46
|
+
* e.g. <VeroxmHydrationBoundary>'s SSR-fetched props, see §5.5) and
|
|
47
|
+
* that intent is worth keeping visible at call sites.
|
|
48
|
+
*/
|
|
49
|
+
seed<T>(key: string, data: T, tags: readonly string[]): void;
|
|
50
|
+
/**
|
|
51
|
+
* Runs `fetcher()` for `key`, deduping concurrent callers: if a
|
|
52
|
+
* request for the same key is already in flight, every caller awaits
|
|
53
|
+
* that SAME promise instead of issuing a second network request (e.g.
|
|
54
|
+
* two components rendering useEntries() for the same collection+
|
|
55
|
+
* filters at once). Does NOT consult the cache itself -- callers
|
|
56
|
+
* decide when to fetch (see hooks/use-cached-query.ts); this only
|
|
57
|
+
* dedupes concurrent in-flight fetches for the same key.
|
|
58
|
+
*/
|
|
59
|
+
dedupe<T>(key: string, fetcher: () => Promise<T>): Promise<T>;
|
|
60
|
+
/** Drops every cached entry tagged with this collection slug -- call after you know a collection's content changed. Affects both useEntries() and useEntry() results, and useCollection()'s own field-list cache. */
|
|
61
|
+
invalidateCollection(slug: string): void;
|
|
62
|
+
/** Drops every cached entry tagged with this locale. */
|
|
63
|
+
invalidateLocale(locale: string): void;
|
|
64
|
+
/** Drops the cached entry for this media id. */
|
|
65
|
+
invalidateMedia(id: number): void;
|
|
66
|
+
invalidateTag(tag: string): void;
|
|
67
|
+
/** Drops every cached entry, regardless of tag. */
|
|
68
|
+
invalidateAll(): void;
|
|
69
|
+
}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tag-based request cache + in-flight dedupe, owned by one VeroxmClient
|
|
3
|
+
* instance and shared by every hook that client supplies
|
|
4
|
+
* (docs/REACT-SDK-GRAPHQL-IMPLEMENTATION-PLAN.md §5.3). Deliberately NOT
|
|
5
|
+
* a normalized entity cache (the kind Apollo/urql build): an Entry's
|
|
6
|
+
* `data` is an opaque per-project JSON blob (see the plan's §2
|
|
7
|
+
* architecture note), so there's no stable, collection-agnostic way to
|
|
8
|
+
* decompose it into normalized fields and invalidate them individually.
|
|
9
|
+
* Instead, invalidation is tag-based -- every cached query is tagged
|
|
10
|
+
* with the collection slug (+ locale) it read from, matching
|
|
11
|
+
* ReadCacheService's own invalidation model server-side
|
|
12
|
+
* (apps/api's cache-aside layer), just applied client-side.
|
|
13
|
+
*
|
|
14
|
+
* This GraphQL surface is queries-only (see plan §0), so there is no
|
|
15
|
+
* mutation to auto-invalidate on write. Call invalidateCollection() (or
|
|
16
|
+
* invalidateMedia()/invalidateAll()) yourself once you know a collection
|
|
17
|
+
* changed -- e.g. from a webhook-driven Next.js revalidation route, or a
|
|
18
|
+
* manual "Refresh" action in your own UI.
|
|
19
|
+
*/
|
|
20
|
+
export class VeroxmCache {
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
this.entries = new Map();
|
|
23
|
+
this.inFlight = new Map();
|
|
24
|
+
this.staleTimeMs = options.staleTimeMs ?? 30000;
|
|
25
|
+
}
|
|
26
|
+
/** The cached value for `key`, if one exists -- `undefined` means "never fetched, or invalidated since", regardless of whether a fresher one is currently in flight. Doesn't distinguish fresh from stale; call isStale() separately if that matters to you. */
|
|
27
|
+
get(key) {
|
|
28
|
+
return this.entries.get(key)?.data;
|
|
29
|
+
}
|
|
30
|
+
/** True when `key` has no cached entry at all, or its entry is older than this cache's staleTimeMs. */
|
|
31
|
+
isStale(key) {
|
|
32
|
+
const entry = this.entries.get(key);
|
|
33
|
+
if (!entry)
|
|
34
|
+
return true;
|
|
35
|
+
return Date.now() - entry.cachedAt > this.staleTimeMs;
|
|
36
|
+
}
|
|
37
|
+
set(key, data, tags) {
|
|
38
|
+
this.entries.set(key, { data, tags, cachedAt: Date.now() });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Seeds the cache with already-known data -- identical to set() today;
|
|
42
|
+
* kept as its own named method because callers reach for it for a
|
|
43
|
+
* different reason (supplying data this cache didn't fetch itself,
|
|
44
|
+
* e.g. <VeroxmHydrationBoundary>'s SSR-fetched props, see §5.5) and
|
|
45
|
+
* that intent is worth keeping visible at call sites.
|
|
46
|
+
*/
|
|
47
|
+
seed(key, data, tags) {
|
|
48
|
+
this.set(key, data, tags);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Runs `fetcher()` for `key`, deduping concurrent callers: if a
|
|
52
|
+
* request for the same key is already in flight, every caller awaits
|
|
53
|
+
* that SAME promise instead of issuing a second network request (e.g.
|
|
54
|
+
* two components rendering useEntries() for the same collection+
|
|
55
|
+
* filters at once). Does NOT consult the cache itself -- callers
|
|
56
|
+
* decide when to fetch (see hooks/use-cached-query.ts); this only
|
|
57
|
+
* dedupes concurrent in-flight fetches for the same key.
|
|
58
|
+
*/
|
|
59
|
+
async dedupe(key, fetcher) {
|
|
60
|
+
const existing = this.inFlight.get(key);
|
|
61
|
+
if (existing)
|
|
62
|
+
return existing;
|
|
63
|
+
const promise = fetcher().finally(() => this.inFlight.delete(key));
|
|
64
|
+
this.inFlight.set(key, promise);
|
|
65
|
+
return promise;
|
|
66
|
+
}
|
|
67
|
+
/** Drops every cached entry tagged with this collection slug -- call after you know a collection's content changed. Affects both useEntries() and useEntry() results, and useCollection()'s own field-list cache. */
|
|
68
|
+
invalidateCollection(slug) {
|
|
69
|
+
this.invalidateTag(`collection:${slug}`);
|
|
70
|
+
}
|
|
71
|
+
/** Drops every cached entry tagged with this locale. */
|
|
72
|
+
invalidateLocale(locale) {
|
|
73
|
+
this.invalidateTag(`locale:${locale}`);
|
|
74
|
+
}
|
|
75
|
+
/** Drops the cached entry for this media id. */
|
|
76
|
+
invalidateMedia(id) {
|
|
77
|
+
this.invalidateTag(`media:${id}`);
|
|
78
|
+
}
|
|
79
|
+
invalidateTag(tag) {
|
|
80
|
+
for (const [key, entry] of this.entries) {
|
|
81
|
+
if (entry.tags.includes(tag))
|
|
82
|
+
this.entries.delete(key);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Drops every cached entry, regardless of tag. */
|
|
86
|
+
invalidateAll() {
|
|
87
|
+
this.entries.clear();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { field, type SlotFieldDefs, type SlotInfo, type SlotValues } from './slots-core.js';
|
|
3
|
+
export { field };
|
|
4
|
+
export type { SlotFieldDefs, SlotValues, SlotInfo };
|
|
5
|
+
/** True while this browser is in an "Edit on page" session. */
|
|
6
|
+
export declare function isEditingSlots(): boolean;
|
|
7
|
+
/** Where the server mounted veroxmMiddleware. Default '/api/veroxm'. */
|
|
8
|
+
export declare function configureClientSlots(options: {
|
|
9
|
+
routeBase?: string;
|
|
10
|
+
}): void;
|
|
11
|
+
export interface ClientSlotOptions<F extends SlotFieldDefs> {
|
|
12
|
+
fields: F;
|
|
13
|
+
fallback?: Partial<SlotValues<F>>;
|
|
14
|
+
label?: string;
|
|
15
|
+
locale?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ClientSlotState<F extends SlotFieldDefs> {
|
|
18
|
+
values: SlotValues<F>;
|
|
19
|
+
slot: SlotInfo;
|
|
20
|
+
loading: boolean;
|
|
21
|
+
editing: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** A slot's values for client-rendered apps. Prefer <VeroxmSlot>, which also marks the slot for the editor. */
|
|
24
|
+
export declare function useVeroxmSlot<F extends SlotFieldDefs>(name: string, options: ClientSlotOptions<F>): ClientSlotState<F>;
|
|
25
|
+
export interface ClientVeroxmSlotProps<F extends SlotFieldDefs> extends ClientSlotOptions<F> {
|
|
26
|
+
name: string;
|
|
27
|
+
children: (values: SlotValues<F>, slot: SlotInfo) => ReactNode;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A slot in a client-rendered React app: published content (fallback until
|
|
31
|
+
* it loads), or the draft plus the on-page editor during "Edit on page".
|
|
32
|
+
*/
|
|
33
|
+
export declare function VeroxmSlot<F extends SlotFieldDefs>({ name, children, ...options }: ClientVeroxmSlotProps<F>): ReactNode;
|
|
34
|
+
/** Test helper. */
|
|
35
|
+
export declare function resetClientSlotsForTests(): void;
|