@veroxm/react-sdk 1.3.0 → 1.5.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +99 -5
  3. package/dist/auth.js +1 -0
  4. package/dist/client.d.ts +27 -9
  5. package/dist/client.js +62 -14
  6. package/dist/component-boundary.d.ts +25 -0
  7. package/dist/component-boundary.js +21 -0
  8. package/dist/components.d.ts +52 -0
  9. package/dist/components.js +73 -0
  10. package/dist/editor/handler.d.ts +4 -0
  11. package/dist/editor/handler.js +52 -1
  12. package/dist/editor/overlay.d.ts +5 -0
  13. package/dist/editor/overlay.js +59 -0
  14. package/dist/errors.d.ts +14 -15
  15. package/dist/errors.js +27 -17
  16. package/dist/hooks/use-cached-query.js +1 -0
  17. package/dist/hooks/use-collection.js +1 -0
  18. package/dist/hooks/use-entries.js +1 -0
  19. package/dist/hooks/use-entry.js +1 -0
  20. package/dist/hooks/use-media.js +1 -0
  21. package/dist/hydration-boundary.js +1 -0
  22. package/dist/index.d.ts +5 -0
  23. package/dist/index.js +4 -0
  24. package/dist/next/cache-components.d.ts +12 -1
  25. package/dist/next/cache-components.js +11 -0
  26. package/dist/next/content-cached.d.ts +12 -0
  27. package/dist/next/content-cached.js +61 -0
  28. package/dist/next/content-core.d.ts +27 -0
  29. package/dist/next/content-core.js +39 -0
  30. package/dist/next/content.d.ts +19 -0
  31. package/dist/next/content.js +57 -0
  32. package/dist/next/index.d.ts +9 -0
  33. package/dist/next/index.js +8 -0
  34. package/dist/next/region.d.ts +8 -0
  35. package/dist/next/region.js +9 -0
  36. package/dist/next/route.js +3 -0
  37. package/dist/preview.js +1 -0
  38. package/dist/provider.d.ts +5 -1
  39. package/dist/provider.js +9 -4
  40. package/dist/slots-core.d.ts +75 -1
  41. package/dist/slots-core.js +59 -1
  42. package/dist/slots.d.ts +2 -12
  43. package/dist/slots.js +4 -28
  44. package/dist/tracking.js +1 -0
  45. package/dist/use-slot.d.ts +12 -0
  46. package/dist/use-slot.js +28 -0
  47. package/package.json +2 -2
package/dist/provider.js CHANGED
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { jsx as _jsx } from "react/jsx-runtime";
2
3
  import { createContext, useContext, useMemo } from 'react';
3
4
  import { VeroxmClient } from './client.js';
@@ -9,22 +10,26 @@ const VeroxmContext = createContext(null);
9
10
  * your app's root:
10
11
  *
11
12
  * ```tsx
12
- * <VeroxmProvider apiBase="https://cms.example.com" apiKey={process.env.VEROXM_API_KEY!}>
13
+ * <VeroxmProvider apiBase="https://cms.example.com" apiKey={apiKey}>
13
14
  * <App />
14
15
  * </VeroxmProvider>
15
16
  * ```
17
+ *
18
+ * The hooks read in the browser, so `apiKey` ends up in the page: anyone can copy it and read what it can read,
19
+ * drafts included. In the App Router, prefer reading on the server (Server Components, or ./ssr.ts with
20
+ * <VeroxmHydrationBoundary>). Outside production builds a browser client with a key logs a warning once.
16
21
  */
17
22
  export function VeroxmProvider(props) {
18
- const { children, client, apiBase, apiKey, projectUuid, cache } = props;
23
+ const { children, client, apiBase, apiKey, projectUuid, cache, timeoutMs, retries } = props;
19
24
  const value = useMemo(() => {
20
25
  if (client)
21
26
  return client;
22
27
  if (!apiBase || !apiKey) {
23
28
  throw new Error('<VeroxmProvider> needs either a `client` prop or both `apiBase` and `apiKey`.');
24
29
  }
25
- return new VeroxmClient({ apiBase, apiKey, projectUuid, cache });
30
+ return new VeroxmClient({ apiBase, apiKey, projectUuid, cache, timeoutMs, retries });
26
31
  // eslint-disable-next-line react-hooks/exhaustive-deps
27
- }, [client, apiBase, apiKey, projectUuid, cache]);
32
+ }, [client, apiBase, apiKey, projectUuid, cache, timeoutMs, retries]);
28
33
  return _jsx(VeroxmContext.Provider, { value: value, children: children });
29
34
  }
30
35
  /** Reads the VeroxmClient supplied by the nearest <VeroxmProvider> -- throws if none is mounted, same fail-loud philosophy as @veroxm/sdk's own methods (see packages/sdk-js/src/errors.ts's VeroXMNotAuthenticatedError). Every hook in ./hooks calls this rather than reading context directly. */
@@ -34,8 +34,59 @@ export type SlotFieldDef = (BaseField & {
34
34
  max?: number;
35
35
  }) | (BaseField & {
36
36
  type: 'chart';
37
+ }) | (BaseField & {
38
+ type: 'component';
39
+ components: Record<string, ComponentSchema>;
37
40
  });
38
41
  export type SlotFieldDefs = Record<string, SlotFieldDef>;
42
+ /** What VeroXM is told about a registered component: its fields, and the version of that field list. */
43
+ export interface ComponentSchema<F extends SlotFieldDefs = SlotFieldDefs> {
44
+ label?: string;
45
+ /** Raise it when the fields change in a way values saved for the old fields don't fit. */
46
+ version: number;
47
+ fields: F;
48
+ }
49
+ /**
50
+ * A component registered with defineComponent: its schema, plus the React component that renders it. The render
51
+ * component gets the values a marketer entered, typed from `fields`.
52
+ */
53
+ export interface RegisteredComponent<N extends string = string, F extends SlotFieldDefs = SlotFieldDefs> extends ComponentSchema<F> {
54
+ name: N;
55
+ render(props: {
56
+ values: SlotValues<F>;
57
+ }): ReactNode;
58
+ }
59
+ /** A component field's value: the registered component chosen, the version its values were saved for, and its values. */
60
+ export interface SlotComponentValue<N extends string = string, F extends SlotFieldDefs = SlotFieldDefs> {
61
+ component: N;
62
+ /** The component's version when the values were saved; null for a fallback from code. */
63
+ version: number | null;
64
+ values: SlotValues<F>;
65
+ }
66
+ type ComponentsOf<L extends readonly RegisteredComponent[]> = {
67
+ [C in L[number] as C['name']]: ComponentSchema<C['fields']>;
68
+ };
69
+ /**
70
+ * Registers a component marketers can choose for a place on the page (a component field, or <VeroxmRegion>). Only
71
+ * `fields` are ever editable: VeroXM stores values for them, checked like any slot field's, and never code.
72
+ *
73
+ * ```tsx
74
+ * export const Hero = defineComponent('hero', {
75
+ * label: 'Hero',
76
+ * version: 1,
77
+ * fields: { title: field.text({ max: 80, required: true }), image: field.media() },
78
+ * render: ({ values }) => <section><h1>{values.title}</h1></section>,
79
+ * });
80
+ * ```
81
+ */
82
+ export declare function defineComponent<const N extends string, F extends SlotFieldDefs>(name: N, def: {
83
+ label?: string;
84
+ version?: number;
85
+ fields: F;
86
+ render: (props: {
87
+ values: SlotValues<F>;
88
+ }) => ReactNode;
89
+ }): RegisteredComponent<N, F>;
39
90
  /** Field builders for a slot's `fields` prop. */
40
91
  export declare const field: {
41
92
  text: (o?: BaseField & {
@@ -119,7 +170,19 @@ export declare const field: {
119
170
  required?: boolean;
120
171
  type: "chart";
121
172
  };
173
+ /**
174
+ * One of the components you registered (defineComponent), chosen by a marketer, with that component's own fields.
175
+ * A component's fields can't be components or charts. Render the value with renderComponent.
176
+ */
177
+ component: <const L extends readonly RegisteredComponent[]>(components: L, o?: BaseField) => {
178
+ label?: string;
179
+ required?: boolean;
180
+ type: "component";
181
+ components: ComponentsOf<L>;
182
+ };
122
183
  };
184
+ /** The schemas of registered components, by name (what is reported to VeroXM; the render functions stay in code). */
185
+ export declare function componentSchemas(components: readonly RegisteredComponent[]): Record<string, ComponentSchema>;
123
186
  /** A media field's value: the image as stored in the VeroXM media library. */
124
187
  export interface SlotMedia {
125
188
  id: number;
@@ -202,7 +265,12 @@ type SlotValue<F> = F extends {
202
265
  type: 'map';
203
266
  } ? SlotMap | null : F extends {
204
267
  type: 'chart';
205
- } ? SlotChart | null : never;
268
+ } ? SlotChart | null : F extends {
269
+ type: 'component';
270
+ components: infer C extends Record<string, ComponentSchema>;
271
+ } ? {
272
+ [K in keyof C & string]: SlotComponentValue<K, C[K]['fields']>;
273
+ }[keyof C & string] | null : never;
206
274
  /** The values a slot renders with, typed from its field definitions. */
207
275
  export type SlotValues<F extends SlotFieldDefs> = {
208
276
  [K in keyof F]: SlotValue<F[K]>;
@@ -251,6 +319,11 @@ export interface SlotsConfig {
251
319
  * page reaches the browser. Default: VEROXM_EDGE=1.
252
320
  */
253
321
  edge?: boolean;
322
+ /**
323
+ * The secret of a project webhook (Settings > Webhooks) sent to your /api/veroxm/revalidate route: content changes
324
+ * then refresh cached content reads at once. Default: VEROXM_WEBHOOK_SECRET. Server only.
325
+ */
326
+ webhookSecret?: string;
254
327
  }
255
328
  /** Overrides the environment-variable defaults for every slot in this process. */
256
329
  export declare function configureSlots(next: SlotsConfig): void;
@@ -263,6 +336,7 @@ export declare function resolveSlotsConfig(): {
263
336
  report: boolean;
264
337
  revalidate: number;
265
338
  edge: boolean;
339
+ webhookSecret: string;
266
340
  fetch: typeof fetch | undefined;
267
341
  ready: boolean;
268
342
  };
@@ -1,3 +1,19 @@
1
+ /**
2
+ * Registers a component marketers can choose for a place on the page (a component field, or <VeroxmRegion>). Only
3
+ * `fields` are ever editable: VeroXM stores values for them, checked like any slot field's, and never code.
4
+ *
5
+ * ```tsx
6
+ * export const Hero = defineComponent('hero', {
7
+ * label: 'Hero',
8
+ * version: 1,
9
+ * fields: { title: field.text({ max: 80, required: true }), image: field.media() },
10
+ * render: ({ values }) => <section><h1>{values.title}</h1></section>,
11
+ * });
12
+ * ```
13
+ */
14
+ export function defineComponent(name, def) {
15
+ return { name, label: def.label, version: def.version ?? 1, fields: def.fields, render: def.render };
16
+ }
1
17
  /** Field builders for a slot's `fields` prop. */
2
18
  export const field = {
3
19
  text: (o = {}) => ({ type: 'text', ...o }),
@@ -17,7 +33,23 @@ export const field = {
17
33
  map: (o = {}) => ({ type: 'map', ...o }),
18
34
  /** A chart: numbers typed in, read from your own systems (Connected APIs), or from VeroXM's analytics. */
19
35
  chart: (o = {}) => ({ type: 'chart', ...o }),
36
+ /**
37
+ * One of the components you registered (defineComponent), chosen by a marketer, with that component's own fields.
38
+ * A component's fields can't be components or charts. Render the value with renderComponent.
39
+ */
40
+ component: (components, o = {}) => ({
41
+ type: 'component',
42
+ components: componentSchemas(components),
43
+ ...o,
44
+ }),
20
45
  };
46
+ /** The schemas of registered components, by name (what is reported to VeroXM; the render functions stay in code). */
47
+ export function componentSchemas(components) {
48
+ const out = {};
49
+ for (const c of components)
50
+ out[c.name] = { ...(c.label ? { label: c.label } : {}), version: c.version, fields: c.fields };
51
+ return out;
52
+ }
21
53
  let config = {};
22
54
  let warned = false;
23
55
  /** Overrides the environment-variable defaults for every slot in this process. */
@@ -41,6 +73,7 @@ export function resolveSlotsConfig() {
41
73
  report: config.report ?? isServer,
42
74
  revalidate: config.revalidate ?? 30,
43
75
  edge: config.edge ?? ['1', 'true'].includes((env('VEROXM_EDGE') ?? '').toLowerCase()),
76
+ webhookSecret: config.webhookSecret ?? env('VEROXM_WEBHOOK_SECRET') ?? '',
44
77
  fetch: config.fetch ?? (typeof fetch === 'function' ? fetch.bind(globalThis) : undefined),
45
78
  ready: Boolean(baseUrl && projectId && apiKey),
46
79
  };
@@ -242,13 +275,38 @@ function emptyValue(def) {
242
275
  export function mergeSlotValues(fields, fallback, published) {
243
276
  const out = {};
244
277
  for (const [name, def] of Object.entries(fields)) {
278
+ const fb = fallback?.[name];
279
+ if (def.type === 'component') {
280
+ out[name] = mergeComponentValue(def.components, published?.[name], fb);
281
+ continue;
282
+ }
245
283
  const live = published?.[name];
246
284
  const hasLive = live !== undefined && live !== null && live !== '';
247
- const fb = fallback?.[name];
248
285
  out[name] = hasLive ? live : fb !== undefined && fb !== null ? fb : emptyValue(def);
249
286
  }
250
287
  return out;
251
288
  }
289
+ function asComponentValue(v) {
290
+ return v && typeof v === 'object' && !Array.isArray(v) && typeof v.component === 'string'
291
+ ? v
292
+ : null;
293
+ }
294
+ /**
295
+ * A component field's value: the published choice when it names a component the code still registers, else the
296
+ * fallback's; its values merged field by field like a slot's (the fallback's values only when it chose the same
297
+ * component). Null when there is neither.
298
+ */
299
+ function mergeComponentValue(components, published, fallback) {
300
+ const live = asComponentValue(published);
301
+ const fb = asComponentValue(fallback);
302
+ const chosen = live && components[live.component] ? live : fb && components[fb.component] ? fb : null;
303
+ if (!chosen)
304
+ return null;
305
+ const schema = components[chosen.component];
306
+ const sameAsFallback = fb && fb.component === chosen.component ? fb.values : undefined;
307
+ const values = mergeSlotValues(schema.fields, sameAsFallback, chosen === live ? (live.values ?? {}) : null);
308
+ return { component: chosen.component, version: chosen === live ? (live.version ?? null) : null, values };
309
+ }
252
310
  /** SlotInfo for published content (or the fallback when null). */
253
311
  export function slotInfo(name, published) {
254
312
  return {
package/dist/slots.d.ts CHANGED
@@ -1,13 +1,3 @@
1
- import { type LoadSlotOptions, type SlotFieldDefs, type SlotInfo, type SlotValues } from './slots-core.js';
2
1
  export * from './slots-core.js';
3
- export interface SlotState<F extends SlotFieldDefs> {
4
- values: SlotValues<F>;
5
- slot: SlotInfo;
6
- loading: boolean;
7
- }
8
- /**
9
- * Client hook for React apps without server components (Vite, CRA, React
10
- * Native). Shows the fallback immediately and swaps in published content
11
- * when it arrives.
12
- */
13
- export declare function useSlot<F extends SlotFieldDefs>(name: string, options: LoadSlotOptions<F>): SlotState<F>;
2
+ export { useSlot } from './use-slot.js';
3
+ export type { SlotState } from './use-slot.js';
package/dist/slots.js CHANGED
@@ -1,30 +1,6 @@
1
- import { useEffect, useMemo, useRef, useState } from 'react';
2
- import { loadSlot, mergeSlotValues, slotInfo } from './slots-core.js';
3
1
  // Visual slots: everything from the server-safe core (slots-core.tsx),
4
- // plus useSlot, the client hook (which Server Components can't import).
2
+ // plus useSlot, the client hook (in its own 'use client' module, so a
3
+ // Server Component can import this module -- and the package root -- for
4
+ // <VeroxmSlot>, field and configureSlots).
5
5
  export * from './slots-core.js';
6
- /**
7
- * Client hook for React apps without server components (Vite, CRA, React
8
- * Native). Shows the fallback immediately and swaps in published content
9
- * when it arrives.
10
- */
11
- export function useSlot(name, options) {
12
- const optsRef = useRef(options);
13
- optsRef.current = options;
14
- const initial = useMemo(() => ({ values: mergeSlotValues(options.fields, options.fallback, null), slot: slotInfo(name, null), loading: true }),
15
- // eslint-disable-next-line react-hooks/exhaustive-deps
16
- [name]);
17
- const [state, setState] = useState(initial);
18
- useEffect(() => {
19
- let live = true;
20
- setState(initial);
21
- loadSlot(name, optsRef.current).then(({ values, slot }) => {
22
- if (live)
23
- setState({ values, slot, loading: false });
24
- });
25
- return () => {
26
- live = false;
27
- };
28
- }, [name, options.locale, initial]);
29
- return state;
30
- }
6
+ export { useSlot } from './use-slot.js';
package/dist/tracking.js CHANGED
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
3
  import { createContext, useContext, useEffect, useRef, useState } from 'react';
3
4
  const TrackerContext = createContext(null);
@@ -0,0 +1,12 @@
1
+ import { type LoadSlotOptions, type SlotFieldDefs, type SlotInfo, type SlotValues } from './slots-core.js';
2
+ export interface SlotState<F extends SlotFieldDefs> {
3
+ values: SlotValues<F>;
4
+ slot: SlotInfo;
5
+ loading: boolean;
6
+ }
7
+ /**
8
+ * Client hook for React apps without server components (Vite, CRA, React
9
+ * Native). Shows the fallback immediately and swaps in published content
10
+ * when it arrives.
11
+ */
12
+ export declare function useSlot<F extends SlotFieldDefs>(name: string, options: LoadSlotOptions<F>): SlotState<F>;
@@ -0,0 +1,28 @@
1
+ 'use client';
2
+ import { useEffect, useMemo, useRef, useState } from 'react';
3
+ import { loadSlot, mergeSlotValues, slotInfo } from './slots-core.js';
4
+ /**
5
+ * Client hook for React apps without server components (Vite, CRA, React
6
+ * Native). Shows the fallback immediately and swaps in published content
7
+ * when it arrives.
8
+ */
9
+ export function useSlot(name, options) {
10
+ const optsRef = useRef(options);
11
+ optsRef.current = options;
12
+ const initial = useMemo(() => ({ values: mergeSlotValues(options.fields, options.fallback, null), slot: slotInfo(name, null), loading: true }),
13
+ // eslint-disable-next-line react-hooks/exhaustive-deps
14
+ [name]);
15
+ const [state, setState] = useState(initial);
16
+ useEffect(() => {
17
+ let live = true;
18
+ setState(initial);
19
+ loadSlot(name, optsRef.current).then(({ values, slot }) => {
20
+ if (live)
21
+ setState({ values, slot, loading: false });
22
+ });
23
+ return () => {
24
+ live = false;
25
+ };
26
+ }, [name, options.locale, initial]);
27
+ return state;
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veroxm/react-sdk",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "React and Next.js SDK for VeroXM: content hooks, visual slots with A/B tests, the on-page editor, llms.txt and structured data.",
5
5
  "keywords": [
6
6
  "veroxm",
@@ -71,7 +71,7 @@
71
71
  "api:report": "node ../../scripts/api-report.mjs . --write"
72
72
  },
73
73
  "dependencies": {
74
- "graphql-request": "^7.0.0"
74
+ "@veroxm/sdk": "^1.10.0"
75
75
  },
76
76
  "peerDependencies": {
77
77
  "react": "^18.0.0 || ^19.0.0",