@veroxm/react-sdk 1.2.0 → 1.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.0
4
+
5
+ - The providers and hooks are `'use client'` modules: `VeroxmProvider`, `VeroxmPreviewProvider`, `VeroxmAuthProvider`,
6
+ `VeroxmTrackerProvider`, `VeroxmHydrationBoundary`, `useSlot` and the content hooks. A Server Component can import the
7
+ package root and render the providers directly with plain props; before, it had to wrap them in its own client
8
+ component. `useSlot` moved to its own module; it's still exported from the same places.
9
+ - A `VeroxmClient` created in a browser with an API key logs a warning once, outside production builds: the key is in
10
+ the page, and a key that reads content can read drafts too. See "Keys in the browser" in the README.
11
+
12
+ ## 1.3.0
13
+
14
+ - `<VeroxmForm lang="fr" />`: show the form in one of its languages (otherwise the visitor's browser language decides).
15
+
3
16
  ## 1.2.0
4
17
 
5
18
  - Forms: `<VeroxmForm baseUrl form mode />` shows a VeroXM form inline, as a popup (its children open it), full page
package/README.md CHANGED
@@ -62,6 +62,32 @@ the dashboard) -- it's the request's only source of project identity, so
62
62
  unlike the REST v2 API there's no separate project uuid to pass on every
63
63
  call.
64
64
 
65
+ ### Keys in the browser
66
+
67
+ The hooks read in the browser, so the key you give `<VeroxmProvider>` is in
68
+ the page: anyone can copy it and use it from anywhere, including to read
69
+ unpublished drafts. Outside production builds, a `VeroxmClient` created in a
70
+ browser with a key logs a warning once (from 1.4).
71
+
72
+ - In the App Router, read on the server where you can: a Server Component
73
+ with its own `VeroxmClient`, or the SSR helpers with
74
+ `<VeroxmHydrationBoundary>` (below). Keep the key in a server-only
75
+ environment variable (not `NEXT_PUBLIC_`).
76
+ - If a page does need the hooks in the browser, give it a key with only the
77
+ `read` ability, never `*`, so a copied key can't create, change or
78
+ delete content.
79
+
80
+ ### Server and client components
81
+
82
+ From 1.4 the providers and hooks are `'use client'` modules, and the package
83
+ root can be imported from a Server Component. A Server Component can render
84
+ `<VeroxmProvider>`, `<VeroxmPreviewProvider>`, `<VeroxmAuthProvider>`,
85
+ `<VeroxmTrackerProvider>` and `<VeroxmHydrationBoundary>` directly when their
86
+ props are plain values (strings, booleans, data). Objects such as a
87
+ `VeroxmClient` or a tracker, and render props such as `<Experience>`'s
88
+ children, can't cross from server to client: create and use those inside a
89
+ client component.
90
+
65
91
  ## Core hooks
66
92
 
67
93
  - **`useCollection(slug)`** -- a collection's field list. Build dynamic
package/dist/auth.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, useState } from 'react';
3
4
  const AuthContext = createContext(null);
package/dist/client.d.ts CHANGED
@@ -31,6 +31,8 @@ export interface VeroxmClientOptions {
31
31
  */
32
32
  getAccessToken?: () => Promise<string | null> | string | null;
33
33
  }
34
+ /** @internal Lets tests see the warning again. */
35
+ export declare function resetBrowserKeyWarning(): void;
34
36
  /**
35
37
  * Thin wrapper over graphql-request's GraphQLClient -- one method per
36
38
  * query this API exposes (docs/REACT-SDK-GRAPHQL-IMPLEMENTATION-PLAN.md
package/dist/client.js CHANGED
@@ -4,6 +4,41 @@ import { VeroxmGraphQLError } from './errors.js';
4
4
  const COLLECTION_FIELDS = `slug name description fields { name label type }`;
5
5
  const ENTRY_FIELDS = `id locale createdAt updatedAt publishedAt data`;
6
6
  const MEDIA_FIELDS = `id fileName fullUrl thumbUrl thumbWebpUrl fullWebpUrl caption tags size width height focalPoint { x y } hotspots { id x y label url }`;
7
+ let warnedBrowserKey = false;
8
+ /**
9
+ * Written as the exact `process.env.NODE_ENV` so bundlers (Next.js, Vite, webpack) replace it with a literal; a
10
+ * browser with no `process` at all lands in the catch.
11
+ */
12
+ function isProductionBuild() {
13
+ try {
14
+ return process.env.NODE_ENV === 'production';
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
20
+ /**
21
+ * Development-only: a VeroxmClient made in a browser carries its API key in the page. Reading content needs a key with
22
+ * `read`, which VeroXM treats as a server key (apps/api/src/public-api/key-kind.ts): anyone can copy it from the
23
+ * page, use it from anywhere, and read unpublished drafts with it. Silent in production builds and on servers; once
24
+ * per page load.
25
+ */
26
+ function warnIfBrowserKey() {
27
+ if (warnedBrowserKey || typeof window === 'undefined' || typeof document === 'undefined')
28
+ return;
29
+ if (isProductionBuild())
30
+ return;
31
+ warnedBrowserKey = true;
32
+ console.warn('[veroxm] A VeroxmClient was created in the browser with an API key (for example <VeroxmProvider apiKey=...>). ' +
33
+ 'Anyone who opens the page can copy that key and use it from anywhere, including to read unpublished drafts. ' +
34
+ 'Read content on the server instead (Server Components, or the fetch helpers with <VeroxmHydrationBoundary>), ' +
35
+ 'and keep the key in a server-only environment variable, not NEXT_PUBLIC_. This warning only appears outside ' +
36
+ 'production builds. https://docs.veroxm.com/platform-tools/react-sdk#keys');
37
+ }
38
+ /** @internal Lets tests see the warning again. */
39
+ export function resetBrowserKeyWarning() {
40
+ warnedBrowserKey = false;
41
+ }
7
42
  /**
8
43
  * Thin wrapper over graphql-request's GraphQLClient -- one method per
9
44
  * query this API exposes (docs/REACT-SDK-GRAPHQL-IMPLEMENTATION-PLAN.md
@@ -23,6 +58,8 @@ export class VeroxmClient {
23
58
  });
24
59
  this.projectUuid = options.projectUuid;
25
60
  this.cache = new VeroxmCache(options.cache);
61
+ if (options.apiKey)
62
+ warnIfBrowserKey();
26
63
  }
27
64
  /** Escape hatch for a custom GraphQL document against this same authenticated endpoint -- e.g. a query that only needs a subset of an object's fields, or combines more than one of this class's own queries into a single round trip. Errors are normalized the same way as every typed method below (see VeroxmGraphQLError). */
28
65
  async request(document, variables) {
@@ -29,3 +29,8 @@ export interface EditorOverlayProps {
29
29
  * first mounted actually draws, so a page gets exactly one.
30
30
  */
31
31
  export declare function VeroxmEditorOverlay({ onChange, tools }: EditorOverlayProps): ReactNode;
32
+ /**
33
+ * Opens a slot's editing panel, as clicking it would (the script tag's heatmap uses it for "Edit this slot").
34
+ * False when no editor overlay is on the page.
35
+ */
36
+ export declare function openEditorSlot(name: string, locale?: string): boolean;
@@ -208,6 +208,18 @@ export function VeroxmEditorOverlay({ onChange, tools }) {
208
208
  return null;
209
209
  return createPortal(_jsx(Overlay, { onChange: stableChange, tools: tools }), document.body);
210
210
  }
211
+ /** Set while an overlay is mounted: opens a slot's panel from outside it. */
212
+ let openFromOutside = null;
213
+ /**
214
+ * Opens a slot's editing panel, as clicking it would (the script tag's heatmap uses it for "Edit this slot").
215
+ * False when no editor overlay is on the page.
216
+ */
217
+ export function openEditorSlot(name, locale = '') {
218
+ if (!openFromOutside)
219
+ return false;
220
+ openFromOutside(name, locale);
221
+ return true;
222
+ }
211
223
  function Overlay({ onChange, tools }) {
212
224
  const rootRef = useRef(null);
213
225
  const [session, setSession] = useState(null);
@@ -220,6 +232,15 @@ function Overlay({ onChange, tools }) {
220
232
  const [highlightField, setHighlightField] = useState(null);
221
233
  const [inlineHint, setInlineHint] = useState(null);
222
234
  useLayoutTick(Boolean(hover || open || inlineHint), rootRef);
235
+ useEffect(() => {
236
+ openFromOutside = (name, locale) => {
237
+ setListOpen(false);
238
+ setOpen({ name, locale, nonce: Date.now() });
239
+ };
240
+ return () => {
241
+ openFromOutside = null;
242
+ };
243
+ }, []);
223
244
  useEffect(() => {
224
245
  call('session')
225
246
  .then(setSession)
package/dist/form.d.ts CHANGED
@@ -14,6 +14,8 @@ export interface VeroxmFormOptions {
14
14
  form: string;
15
15
  mode?: VeroxmFormMode;
16
16
  hidden?: Record<string, string>;
17
+ /** The language to show, when the form has it ("fr", "ar"); otherwise the visitor's browser language decides. */
18
+ lang?: string;
17
19
  /** inline: height in pixels (default 520). */
18
20
  height?: number;
19
21
  /** inline: grow and shrink with the form. */
package/dist/form.js CHANGED
@@ -47,7 +47,7 @@ export function useVeroxmForm(options, refs = {}) {
47
47
  latest.current = options;
48
48
  const { baseUrl, form, mode = 'inline' } = options;
49
49
  // What the embed is made from: a change re-creates it.
50
- const key = JSON.stringify([baseUrl, form, mode, options.hidden, options.height, options.autoHeight, options.size, options.position, options.openOn, options.rememberDismissDays, options.autoCloseSeconds, options.title]);
50
+ const key = JSON.stringify([baseUrl, form, mode, options.hidden, options.lang, options.height, options.autoHeight, options.size, options.position, options.openOn, options.rememberDismissDays, options.autoCloseSeconds, options.title]);
51
51
  const { container, trigger } = refs;
52
52
  const needsContainer = mode === 'inline';
53
53
  useEffect(() => {
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useCallback, useEffect, useRef, useState } from 'react';
2
3
  /**
3
4
  * Internal cache-aware fetch helper shared by every hook in this
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useVeroxmClient } from '../provider.js';
2
3
  import { useCachedQuery } from './use-cached-query.js';
3
4
  import { collectionKey, collectionTag } from '../query-keys.js';
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useVeroxmClient } from '../provider.js';
2
3
  import { useDraftMode } from '../preview.js';
3
4
  import { useCachedQuery } from './use-cached-query.js';
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useVeroxmClient } from '../provider.js';
2
3
  import { useCachedQuery } from './use-cached-query.js';
3
4
  import { entryKey, entryTags } from '../query-keys.js';
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useVeroxmClient } from '../provider.js';
2
3
  import { useCachedQuery } from './use-cached-query.js';
3
4
  import { mediaKey, mediaTag } from '../query-keys.js';
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
3
  import { useVeroxmClient } from './provider.js';
3
4
  /**
package/dist/preview.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, useState } from 'react';
3
4
  const DraftModeContext = createContext(null);
@@ -19,10 +19,14 @@ export interface VeroxmProviderProps extends Partial<VeroxmClientOptions> {
19
19
  * your app's root:
20
20
  *
21
21
  * ```tsx
22
- * <VeroxmProvider apiBase="https://cms.example.com" apiKey={process.env.VEROXM_API_KEY!}>
22
+ * <VeroxmProvider apiBase="https://cms.example.com" apiKey={apiKey}>
23
23
  * <App />
24
24
  * </VeroxmProvider>
25
25
  * ```
26
+ *
27
+ * The hooks read in the browser, so `apiKey` ends up in the page: anyone can copy it and read what it can read,
28
+ * drafts included. In the App Router, prefer reading on the server (Server Components, or ./ssr.ts with
29
+ * <VeroxmHydrationBoundary>). Outside production builds a browser client with a key logs a warning once.
26
30
  */
27
31
  export declare function VeroxmProvider(props: VeroxmProviderProps): ReactElement;
28
32
  /** 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. */
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,10 +10,14 @@ 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
23
  const { children, client, apiBase, apiKey, projectUuid, cache } = props;
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.2.0",
3
+ "version": "1.4.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",