@veroxm/react-sdk 1.4.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.
- package/CHANGELOG.md +24 -0
- package/README.md +73 -5
- package/dist/client.d.ts +25 -9
- package/dist/client.js +25 -14
- package/dist/component-boundary.d.ts +25 -0
- package/dist/component-boundary.js +21 -0
- package/dist/components.d.ts +52 -0
- package/dist/components.js +73 -0
- package/dist/editor/handler.d.ts +4 -0
- package/dist/editor/handler.js +52 -1
- package/dist/editor/overlay.js +38 -0
- package/dist/errors.d.ts +14 -15
- package/dist/errors.js +27 -17
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/next/cache-components.d.ts +12 -1
- package/dist/next/cache-components.js +11 -0
- package/dist/next/content-cached.d.ts +12 -0
- package/dist/next/content-cached.js +61 -0
- package/dist/next/content-core.d.ts +27 -0
- package/dist/next/content-core.js +39 -0
- package/dist/next/content.d.ts +19 -0
- package/dist/next/content.js +57 -0
- package/dist/next/index.d.ts +9 -0
- package/dist/next/index.js +8 -0
- package/dist/next/region.d.ts +8 -0
- package/dist/next/region.js +9 -0
- package/dist/next/route.js +3 -0
- package/dist/provider.js +3 -3
- package/dist/slots-core.d.ts +75 -1
- package/dist/slots-core.js +59 -1
- package/package.json +2 -2
package/dist/errors.js
CHANGED
|
@@ -1,25 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { VeroXMApiError, VeroXMGraphQLError } from '@veroxm/sdk';
|
|
2
2
|
/**
|
|
3
|
-
* Thrown for any GraphQL
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* Thrown for any failed GraphQL response: a 2xx response whose `errors` array is non-empty, or a non-2xx status. The
|
|
4
|
+
* message is the first GraphQL error's (or the API's own error text), the same "surface the server's own message"
|
|
5
|
+
* convention as @veroxm/sdk.
|
|
6
|
+
*
|
|
7
|
+
* From 1.5 this is a subclass of @veroxm/sdk's `VeroXMGraphQLError` (and so of `VeroXMApiError`): it also has `body`
|
|
8
|
+
* and `code` (the first error's `extensions.code`, or the API's reason), and one `instanceof VeroXMApiError` check covers
|
|
9
|
+
* errors from both SDKs. Network errors, timeouts (`VeroXMTimeoutError`) and cancels are not wrapped.
|
|
9
10
|
*/
|
|
10
|
-
export class VeroxmGraphQLError extends
|
|
11
|
-
constructor(
|
|
12
|
-
super(
|
|
11
|
+
export class VeroxmGraphQLError extends VeroXMGraphQLError {
|
|
12
|
+
constructor(status, errors) {
|
|
13
|
+
super(status, { errors });
|
|
13
14
|
this.name = 'VeroxmGraphQLError';
|
|
14
|
-
this.errors = errors;
|
|
15
|
-
this.status = status;
|
|
16
15
|
}
|
|
17
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Wraps what a GraphQL request rejected with: an API answer (`VeroXMApiError`, including `VeroXMGraphQLError`) becomes
|
|
18
|
+
* a VeroxmGraphQLError; anything else (a network error, a timeout, a cancel) is returned unchanged so it isn't
|
|
19
|
+
* misreported as a GraphQL error, and a thrown non-Error becomes a plain Error.
|
|
20
|
+
*/
|
|
18
21
|
static from(cause) {
|
|
19
|
-
if (cause instanceof
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
if (cause instanceof VeroxmGraphQLError)
|
|
23
|
+
return cause;
|
|
24
|
+
if (cause instanceof VeroXMApiError) {
|
|
25
|
+
const errors = cause instanceof VeroXMGraphQLError ? cause.errors : [];
|
|
26
|
+
const wrapped = new VeroxmGraphQLError(cause.status, errors);
|
|
27
|
+
// Keep what the API said: its message, body and reason code.
|
|
28
|
+
const target = wrapped;
|
|
29
|
+
target.message = cause.message;
|
|
30
|
+
target.body = cause.body;
|
|
31
|
+
target.code = cause.code;
|
|
32
|
+
return wrapped;
|
|
23
33
|
}
|
|
24
34
|
return cause instanceof Error ? cause : new Error(String(cause));
|
|
25
35
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -45,6 +45,11 @@ export type { VeroxmFormProps, VeroxmFormOptions, VeroxmFormMode, VeroxmFormHand
|
|
|
45
45
|
export { VeroxmTrackerProvider, useTracker, usePageViews, useExperience, Experience, useContentView, useRecommendations, } from './tracking.js';
|
|
46
46
|
export type { VeroxmTrackerProviderProps, VeroxmTrackerLike, ExperienceDecision, ExperienceState, ExperienceProps, RecommendationsState, } from './tracking.js';
|
|
47
47
|
export { VeroxmSlot, useSlot, field, configureSlots, loadSlot, mergeSlotValues } from './slots.js';
|
|
48
|
+
export { defineComponent } from './slots.js';
|
|
49
|
+
export { VeroxmRegion, renderComponent, regionSlotProps } from './components.js';
|
|
50
|
+
export { VeroxmComponentBoundary } from './component-boundary.js';
|
|
51
|
+
export type { RegisteredComponent, ComponentSchema, SlotComponentValue } from './slots.js';
|
|
52
|
+
export type { VeroxmRegionProps, RegionFallback, RenderComponentOptions } from './components.js';
|
|
48
53
|
export type { VeroxmSlotProps, SlotFieldDef, SlotFieldDefs, SlotValues, SlotInfo, SlotTest, SlotMedia, SlotLink, SlotMenuItem, SlotVideo, SlotSlide, SlotMap, SlotPlace, SlotChart, SlotState, SlotsConfig, LoadSlotOptions, SlotPerson, } from './slots.js';
|
|
49
54
|
export { loadStructuredData, jsonLdScript } from './structured-data.js';
|
|
50
55
|
export type { StructuredDataOptions } from './structured-data.js';
|
package/dist/index.js
CHANGED
|
@@ -32,5 +32,9 @@ export { VeroxmForm, useVeroxmForm } from './form.js';
|
|
|
32
32
|
export { VeroxmTrackerProvider, useTracker, usePageViews, useExperience, Experience, useContentView, useRecommendations, } from './tracking.js';
|
|
33
33
|
// Visual slots (Experience Layer): named regions marketers edit from VeroXM.
|
|
34
34
|
export { VeroxmSlot, useSlot, field, configureSlots, loadSlot, mergeSlotValues } from './slots.js';
|
|
35
|
+
// Registered components: marketers choose which one shows in a region, and fill in its fields.
|
|
36
|
+
export { defineComponent } from './slots.js';
|
|
37
|
+
export { VeroxmRegion, renderComponent, regionSlotProps } from './components.js';
|
|
38
|
+
export { VeroxmComponentBoundary } from './component-boundary.js';
|
|
35
39
|
// Structured data (schema.org JSON-LD) for a page: render jsonLdScript(data) in a <script type="application/ld+json">.
|
|
36
40
|
export { loadStructuredData, jsonLdScript } from './structured-data.js';
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { ReactNode } from 'react';
|
|
2
|
-
import type { SlotFieldDefs, VeroxmSlotProps } from '../slots-core.js';
|
|
2
|
+
import type { RegisteredComponent, SlotFieldDefs, VeroxmSlotProps } from '../slots-core.js';
|
|
3
|
+
import { type VeroxmRegionProps } from '../components.js';
|
|
3
4
|
export declare function VeroxmSlot<F extends SlotFieldDefs>(props: VeroxmSlotProps<F>): Promise<ReactNode>;
|
|
5
|
+
/** <VeroxmRegion> with this entry's cached <VeroxmSlot>: a slot with one component field (see '@veroxm/react-sdk/next'). */
|
|
6
|
+
export declare function VeroxmRegion<const L extends readonly RegisteredComponent[]>(props: VeroxmRegionProps<L>): Promise<ReactNode>;
|
|
7
|
+
export { defineComponent } from '../slots-core.js';
|
|
8
|
+
export { renderComponent, regionSlotProps } from '../components.js';
|
|
9
|
+
export { VeroxmComponentBoundary } from '../component-boundary.js';
|
|
10
|
+
export type { RegisteredComponent, ComponentSchema, SlotComponentValue } from '../slots-core.js';
|
|
11
|
+
export type { VeroxmRegionProps, RegionFallback, RenderComponentOptions } from '../components.js';
|
|
12
|
+
export { getEntries, getEntry, getCollection, getMedia } from './content-cached.js';
|
|
13
|
+
export { CONTENT_TAG, contentTag, mediaTag } from './content-core.js';
|
|
14
|
+
export type { ContentReadOptions } from './content-core.js';
|
|
4
15
|
export { VeroxmEditor } from './editor.js';
|
|
5
16
|
export { field, configureSlots, mergeSlotValues } from '../slots-core.js';
|
|
6
17
|
export type { VeroxmSlotProps, SlotFieldDef, SlotFieldDefs, SlotValues, SlotInfo, SlotTest, SlotMedia, SlotLink, SlotMenuItem, SlotVideo, SlotSlide, SlotMap, SlotPlace, SlotChart, SlotsConfig } from '../slots-core.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { regionSlotProps } from '../components.js';
|
|
1
2
|
import { slotWith } from './slot.js';
|
|
2
3
|
import { cachedPublishedSlot } from './cached.js';
|
|
3
4
|
// @veroxm/react-sdk/next/cache-components -- <VeroxmSlot> for Next.js apps
|
|
@@ -11,6 +12,16 @@ import { cachedPublishedSlot } from './cached.js';
|
|
|
11
12
|
export function VeroxmSlot(props) {
|
|
12
13
|
return slotWith(props, cachedPublishedSlot, true);
|
|
13
14
|
}
|
|
15
|
+
/** <VeroxmRegion> with this entry's cached <VeroxmSlot>: a slot with one component field (see '@veroxm/react-sdk/next'). */
|
|
16
|
+
export function VeroxmRegion(props) {
|
|
17
|
+
return VeroxmSlot(regionSlotProps(props));
|
|
18
|
+
}
|
|
19
|
+
export { defineComponent } from '../slots-core.js';
|
|
20
|
+
export { renderComponent, regionSlotProps } from '../components.js';
|
|
21
|
+
export { VeroxmComponentBoundary } from '../component-boundary.js';
|
|
22
|
+
// Content for Server Components, cached with 'use cache' (tags veroxm-content, veroxm-content:<collection>).
|
|
23
|
+
export { getEntries, getEntry, getCollection, getMedia } from './content-cached.js';
|
|
24
|
+
export { CONTENT_TAG, contentTag, mediaTag } from './content-core.js';
|
|
14
25
|
export { VeroxmEditor } from './editor.js';
|
|
15
26
|
export { field, configureSlots, mergeSlotValues } from '../slots-core.js';
|
|
16
27
|
// Site parts: header, footer and menus from slot fields.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CollectionMeta, Entry, EntriesQueryOptions, EntryConnection, MediaView } from '../types.js';
|
|
2
|
+
import { type ContentReadOptions } from './content-core.js';
|
|
3
|
+
/** A page of published entries (the same options as useEntries), cached with 'use cache'. */
|
|
4
|
+
export declare function getEntries<T = Record<string, unknown>>(collection: string, options?: EntriesQueryOptions & ContentReadOptions): Promise<EntryConnection<T>>;
|
|
5
|
+
/** One published entry, or null when there's none with that id. */
|
|
6
|
+
export declare function getEntry<T = Record<string, unknown>>(collection: string, id: number, options?: {
|
|
7
|
+
timestamps?: boolean;
|
|
8
|
+
} & ContentReadOptions): Promise<Entry<T> | null>;
|
|
9
|
+
/** A collection's field list, or null. */
|
|
10
|
+
export declare function getCollection(slug: string, options?: ContentReadOptions): Promise<CollectionMeta | null>;
|
|
11
|
+
/** One media item, or null. */
|
|
12
|
+
export declare function getMedia(id: number, options?: ContentReadOptions): Promise<MediaView | null>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { cacheLife, cacheTag } from 'next/cache';
|
|
2
|
+
import { DEFAULT_REVALIDATE, assertServer, serverClientWith, tagsFor } from './content-core.js';
|
|
3
|
+
// The content helpers for apps with Next.js Cache Components (`cacheComponents: true`), from
|
|
4
|
+
// '@veroxm/react-sdk/next/cache-components'. Same names and results as the '@veroxm/react-sdk/next' ones; each read is
|
|
5
|
+
// a 'use cache' function, so it's part of the static shell, tagged `veroxm-content` and `veroxm-content:<collection>`
|
|
6
|
+
// (refreshed by revalidateTag or VeroXM's webhook through your /api/veroxm route). Kept apart because 'use cache' only
|
|
7
|
+
// compiles when Cache Components is on.
|
|
8
|
+
function life(read) {
|
|
9
|
+
const seconds = read.revalidate === false ? 60 * 60 * 24 * 365 : (read.revalidate ?? DEFAULT_REVALIDATE);
|
|
10
|
+
cacheLife({ stale: Math.min(seconds, 300), revalidate: seconds, expire: Math.max(seconds, 86400) });
|
|
11
|
+
}
|
|
12
|
+
async function entriesCached(collection, query, read) {
|
|
13
|
+
'use cache';
|
|
14
|
+
cacheTag(...tagsFor({ collection }, read.tags));
|
|
15
|
+
life(read);
|
|
16
|
+
return serverClientWith(undefined).listEntries(collection, query);
|
|
17
|
+
}
|
|
18
|
+
async function entryCached(collection, id, timestamps, read) {
|
|
19
|
+
'use cache';
|
|
20
|
+
cacheTag(...tagsFor({ collection }, read.tags));
|
|
21
|
+
life(read);
|
|
22
|
+
return serverClientWith(undefined).getEntry(collection, id, { timestamps });
|
|
23
|
+
}
|
|
24
|
+
async function collectionCached(slug, read) {
|
|
25
|
+
'use cache';
|
|
26
|
+
cacheTag(...tagsFor({ collection: slug }, read.tags));
|
|
27
|
+
life(read);
|
|
28
|
+
return serverClientWith(undefined).getCollection(slug);
|
|
29
|
+
}
|
|
30
|
+
async function mediaCached(id, read) {
|
|
31
|
+
'use cache';
|
|
32
|
+
cacheTag(...tagsFor({ media: id }, read.tags));
|
|
33
|
+
life(read);
|
|
34
|
+
return serverClientWith(undefined).getMedia(id);
|
|
35
|
+
}
|
|
36
|
+
function split(options) {
|
|
37
|
+
const { revalidate, tags, ...rest } = options;
|
|
38
|
+
return { read: { ...(revalidate !== undefined ? { revalidate } : {}), ...(tags ? { tags } : {}) }, rest };
|
|
39
|
+
}
|
|
40
|
+
/** A page of published entries (the same options as useEntries), cached with 'use cache'. */
|
|
41
|
+
export async function getEntries(collection, options = {}) {
|
|
42
|
+
assertServer('getEntries');
|
|
43
|
+
const { read, rest } = split(options);
|
|
44
|
+
return (await entriesCached(collection, rest, read));
|
|
45
|
+
}
|
|
46
|
+
/** One published entry, or null when there's none with that id. */
|
|
47
|
+
export async function getEntry(collection, id, options = {}) {
|
|
48
|
+
assertServer('getEntry');
|
|
49
|
+
const { read, rest } = split(options);
|
|
50
|
+
return (await entryCached(collection, id, rest.timestamps ?? false, read));
|
|
51
|
+
}
|
|
52
|
+
/** A collection's field list, or null. */
|
|
53
|
+
export async function getCollection(slug, options = {}) {
|
|
54
|
+
assertServer('getCollection');
|
|
55
|
+
return (await collectionCached(slug, split(options).read));
|
|
56
|
+
}
|
|
57
|
+
/** One media item, or null. */
|
|
58
|
+
export async function getMedia(id, options = {}) {
|
|
59
|
+
assertServer('getMedia');
|
|
60
|
+
return (await mediaCached(id, split(options).read));
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { VeroxmClient } from '../client.js';
|
|
2
|
+
/** Every content read is tagged with this: `revalidateTag('veroxm-content')` refreshes them all. */
|
|
3
|
+
export declare const CONTENT_TAG = "veroxm-content";
|
|
4
|
+
/** The tag of one collection's reads (entries, single entries, its field list): `revalidateTag(contentTag('posts'))`. */
|
|
5
|
+
export declare function contentTag(collection: string): string;
|
|
6
|
+
/** The tag of one media item's read. */
|
|
7
|
+
export declare function mediaTag(id: number): string;
|
|
8
|
+
/** Seconds a content read stays fresh in Next.js's cache unless a tag is revalidated first. */
|
|
9
|
+
export declare const DEFAULT_REVALIDATE = 60;
|
|
10
|
+
export interface ContentReadOptions {
|
|
11
|
+
/** Seconds Next.js may serve this read from its cache (default 60), or false to keep it until a tag is revalidated. */
|
|
12
|
+
revalidate?: number | false;
|
|
13
|
+
/** Extra cache tags for this read, besides `veroxm-content` and the collection's. */
|
|
14
|
+
tags?: string[];
|
|
15
|
+
}
|
|
16
|
+
/** Throws in a browser: these helpers read with the server's key. */
|
|
17
|
+
export declare function assertServer(name: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* A VeroxmClient for the server, from VEROXM_API_BASE and VEROXM_API_KEY (or configureSlots), sending with `fetchImpl`.
|
|
20
|
+
* Throws an error that says which setting is missing, rather than reading anonymously.
|
|
21
|
+
*/
|
|
22
|
+
export declare function serverClientWith(fetchImpl: typeof fetch | undefined): VeroxmClient;
|
|
23
|
+
/** The tags of a read: everything, then the collection's (or the media item's), then the caller's own. */
|
|
24
|
+
export declare function tagsFor(scope: {
|
|
25
|
+
collection?: string;
|
|
26
|
+
media?: number;
|
|
27
|
+
}, extra?: readonly string[]): string[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { VeroxmClient } from '../client.js';
|
|
2
|
+
import { resolveSlotsConfig } from '../slots-core.js';
|
|
3
|
+
// Server-side content reads for Server Components (increment 5 of the product roadmap): what both Next.js entries
|
|
4
|
+
// share. The key comes from the server's environment (VEROXM_API_BASE, VEROXM_API_KEY, as slots use) and never
|
|
5
|
+
// reaches the browser; reads go through VeroxmClient, so they get @veroxm/sdk's errors, timeouts and retries.
|
|
6
|
+
/** Every content read is tagged with this: `revalidateTag('veroxm-content')` refreshes them all. */
|
|
7
|
+
export const CONTENT_TAG = 'veroxm-content';
|
|
8
|
+
/** The tag of one collection's reads (entries, single entries, its field list): `revalidateTag(contentTag('posts'))`. */
|
|
9
|
+
export function contentTag(collection) {
|
|
10
|
+
return `veroxm-content:${collection}`;
|
|
11
|
+
}
|
|
12
|
+
/** The tag of one media item's read. */
|
|
13
|
+
export function mediaTag(id) {
|
|
14
|
+
return `veroxm-media:${id}`;
|
|
15
|
+
}
|
|
16
|
+
/** Seconds a content read stays fresh in Next.js's cache unless a tag is revalidated first. */
|
|
17
|
+
export const DEFAULT_REVALIDATE = 60;
|
|
18
|
+
/** Throws in a browser: these helpers read with the server's key. */
|
|
19
|
+
export function assertServer(name) {
|
|
20
|
+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
21
|
+
throw new Error(`[veroxm] ${name}() reads with your server key and only runs on the server (a Server Component, route handler or server action).`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A VeroxmClient for the server, from VEROXM_API_BASE and VEROXM_API_KEY (or configureSlots), sending with `fetchImpl`.
|
|
26
|
+
* Throws an error that says which setting is missing, rather than reading anonymously.
|
|
27
|
+
*/
|
|
28
|
+
export function serverClientWith(fetchImpl) {
|
|
29
|
+
const cfg = resolveSlotsConfig();
|
|
30
|
+
const missing = [!cfg.baseUrl && 'VEROXM_API_BASE', !cfg.apiKey && 'VEROXM_API_KEY'].filter(Boolean);
|
|
31
|
+
if (missing.length) {
|
|
32
|
+
throw new Error(`[veroxm] set ${missing.join(' and ')} on the server (or call configureSlots) to read content.`);
|
|
33
|
+
}
|
|
34
|
+
return new VeroxmClient({ apiBase: cfg.baseUrl, apiKey: cfg.apiKey, projectUuid: cfg.projectId || undefined, fetch: fetchImpl });
|
|
35
|
+
}
|
|
36
|
+
/** The tags of a read: everything, then the collection's (or the media item's), then the caller's own. */
|
|
37
|
+
export function tagsFor(scope, extra = []) {
|
|
38
|
+
return [CONTENT_TAG, ...(scope.collection ? [contentTag(scope.collection)] : []), ...(scope.media !== undefined ? [mediaTag(scope.media)] : []), ...extra].slice(0, 64);
|
|
39
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { VeroxmClient } from '../client.js';
|
|
2
|
+
import type { CollectionMeta, Entry, EntriesQueryOptions, EntryConnection, MediaView } from '../types.js';
|
|
3
|
+
import { type ContentReadOptions } from './content-core.js';
|
|
4
|
+
/** A page of published entries (the same options as useEntries). */
|
|
5
|
+
export declare function getEntries<T = Record<string, unknown>>(collection: string, options?: EntriesQueryOptions & ContentReadOptions): Promise<EntryConnection<T>>;
|
|
6
|
+
/** One published entry, or null when there's none with that id (call notFound() yourself). */
|
|
7
|
+
export declare function getEntry<T = Record<string, unknown>>(collection: string, id: number, options?: {
|
|
8
|
+
timestamps?: boolean;
|
|
9
|
+
} & ContentReadOptions): Promise<Entry<T> | null>;
|
|
10
|
+
/** A collection's field list, or null when the project has no such collection. */
|
|
11
|
+
export declare function getCollection(slug: string, options?: ContentReadOptions): Promise<CollectionMeta | null>;
|
|
12
|
+
/** One media item, or null. */
|
|
13
|
+
export declare function getMedia(id: number, options?: ContentReadOptions): Promise<MediaView | null>;
|
|
14
|
+
/**
|
|
15
|
+
* The server's VeroxmClient (reads cached like the helpers above, tagged `veroxm-content`), for the SSR helpers
|
|
16
|
+
* (fetchEntries and the rest) when client components also need the data: pass their `hydration` to
|
|
17
|
+
* <VeroxmHydrationBoundary>.
|
|
18
|
+
*/
|
|
19
|
+
export declare function getServerClient(options?: ContentReadOptions): VeroxmClient;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { cache } from 'react';
|
|
2
|
+
import { DEFAULT_REVALIDATE, assertServer, serverClientWith, tagsFor } from './content-core.js';
|
|
3
|
+
/** A fetch that adds Next.js's cache options to every request. */
|
|
4
|
+
function nextFetch(options, scope) {
|
|
5
|
+
const revalidate = options.revalidate ?? DEFAULT_REVALIDATE;
|
|
6
|
+
return (input, init) => fetch(input, { ...init, cache: 'force-cache', next: { revalidate, tags: tagsFor(scope, options.tags) } });
|
|
7
|
+
}
|
|
8
|
+
const client = (name, options, scope) => {
|
|
9
|
+
assertServer(name);
|
|
10
|
+
return serverClientWith(nextFetch(options, scope));
|
|
11
|
+
};
|
|
12
|
+
// One read per distinct call in a render (React's cache), keyed by its arguments as JSON.
|
|
13
|
+
const once = cache(async (kind, json) => {
|
|
14
|
+
const args = JSON.parse(json);
|
|
15
|
+
switch (kind) {
|
|
16
|
+
case 'entries':
|
|
17
|
+
return client('getEntries', args.read, { collection: args.collection }).listEntries(args.collection, args.query);
|
|
18
|
+
case 'entry':
|
|
19
|
+
return client('getEntry', args.read, { collection: args.collection }).getEntry(args.collection, args.id, { timestamps: args.timestamps });
|
|
20
|
+
case 'collection':
|
|
21
|
+
return client('getCollection', args.read, { collection: args.slug }).getCollection(args.slug);
|
|
22
|
+
case 'media':
|
|
23
|
+
return client('getMedia', args.read, { media: args.id }).getMedia(args.id);
|
|
24
|
+
default:
|
|
25
|
+
throw new Error(`[veroxm] unknown read ${kind}`);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
function split(options) {
|
|
29
|
+
const { revalidate, tags, ...rest } = options;
|
|
30
|
+
return { read: { revalidate, tags }, rest };
|
|
31
|
+
}
|
|
32
|
+
/** A page of published entries (the same options as useEntries). */
|
|
33
|
+
export async function getEntries(collection, options = {}) {
|
|
34
|
+
const { read, rest } = split(options);
|
|
35
|
+
return (await once('entries', JSON.stringify({ collection, query: rest, read })));
|
|
36
|
+
}
|
|
37
|
+
/** One published entry, or null when there's none with that id (call notFound() yourself). */
|
|
38
|
+
export async function getEntry(collection, id, options = {}) {
|
|
39
|
+
const { read, rest } = split(options);
|
|
40
|
+
return (await once('entry', JSON.stringify({ collection, id, timestamps: rest.timestamps ?? false, read })));
|
|
41
|
+
}
|
|
42
|
+
/** A collection's field list, or null when the project has no such collection. */
|
|
43
|
+
export async function getCollection(slug, options = {}) {
|
|
44
|
+
return (await once('collection', JSON.stringify({ slug, read: split(options).read })));
|
|
45
|
+
}
|
|
46
|
+
/** One media item, or null. */
|
|
47
|
+
export async function getMedia(id, options = {}) {
|
|
48
|
+
return (await once('media', JSON.stringify({ id, read: split(options).read })));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The server's VeroxmClient (reads cached like the helpers above, tagged `veroxm-content`), for the SSR helpers
|
|
52
|
+
* (fetchEntries and the rest) when client components also need the data: pass their `hydration` to
|
|
53
|
+
* <VeroxmHydrationBoundary>.
|
|
54
|
+
*/
|
|
55
|
+
export function getServerClient(options = {}) {
|
|
56
|
+
return client('getServerClient', options, {});
|
|
57
|
+
}
|
package/dist/next/index.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export { VeroxmSlot } from './slot.js';
|
|
2
|
+
export { getEntries, getEntry, getCollection, getMedia, getServerClient } from './content.js';
|
|
3
|
+
export { CONTENT_TAG, contentTag, mediaTag } from './content-core.js';
|
|
4
|
+
export type { ContentReadOptions } from './content-core.js';
|
|
5
|
+
export { VeroxmRegion } from './region.js';
|
|
6
|
+
export { defineComponent } from '../slots-core.js';
|
|
7
|
+
export { renderComponent, regionSlotProps } from '../components.js';
|
|
8
|
+
export { VeroxmComponentBoundary } from '../component-boundary.js';
|
|
9
|
+
export type { RegisteredComponent, ComponentSchema, SlotComponentValue } from '../slots-core.js';
|
|
10
|
+
export type { VeroxmRegionProps, RegionFallback, RenderComponentOptions } from '../components.js';
|
|
2
11
|
export { VeroxmEditor } from './editor.js';
|
|
3
12
|
export { field, configureSlots, loadSlot, mergeSlotValues } from '../slots-core.js';
|
|
4
13
|
export type { VeroxmSlotProps, SlotFieldDef, SlotFieldDefs, SlotValues, SlotInfo, SlotTest, SlotMedia, SlotLink, SlotMenuItem, SlotVideo, SlotSlide, SlotMap, SlotPlace, SlotChart, SlotsConfig, LoadSlotOptions, SlotPerson } from '../slots-core.js';
|
package/dist/next/index.js
CHANGED
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
// // app/api/veroxm/[...veroxm]/route.ts
|
|
6
6
|
// export { GET, PUT, POST } from '@veroxm/react-sdk/next/route';
|
|
7
7
|
export { VeroxmSlot } from './slot.js';
|
|
8
|
+
// Content for Server Components, read on the server with VEROXM_API_KEY and cached by Next.js.
|
|
9
|
+
export { getEntries, getEntry, getCollection, getMedia, getServerClient } from './content.js';
|
|
10
|
+
export { CONTENT_TAG, contentTag, mediaTag } from './content-core.js';
|
|
11
|
+
// Registered components: marketers choose which one shows in a region, and fill in its fields.
|
|
12
|
+
export { VeroxmRegion } from './region.js';
|
|
13
|
+
export { defineComponent } from '../slots-core.js';
|
|
14
|
+
export { renderComponent, regionSlotProps } from '../components.js';
|
|
15
|
+
export { VeroxmComponentBoundary } from '../component-boundary.js';
|
|
8
16
|
export { VeroxmEditor } from './editor.js';
|
|
9
17
|
export { field, configureSlots, loadSlot, mergeSlotValues } from '../slots-core.js';
|
|
10
18
|
// Site parts (also in '@veroxm/react-sdk'), here so a Next.js site imports from one place.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { type VeroxmRegionProps } from '../components.js';
|
|
3
|
+
import type { RegisteredComponent } from '../slots-core.js';
|
|
4
|
+
/**
|
|
5
|
+
* <VeroxmRegion> for Next.js App Router: a slot with one component field, rendered by this entry's <VeroxmSlot>, so it
|
|
6
|
+
* gets draft-mode editing on the page, A/B tests, personalised versions and the same caching as any slot.
|
|
7
|
+
*/
|
|
8
|
+
export declare function VeroxmRegion<const L extends readonly RegisteredComponent[]>(props: VeroxmRegionProps<L>): Promise<ReactNode>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { regionSlotProps } from '../components.js';
|
|
2
|
+
import { VeroxmSlot } from './slot.js';
|
|
3
|
+
/**
|
|
4
|
+
* <VeroxmRegion> for Next.js App Router: a slot with one component field, rendered by this entry's <VeroxmSlot>, so it
|
|
5
|
+
* gets draft-mode editing on the page, A/B tests, personalised versions and the same caching as any slot.
|
|
6
|
+
*/
|
|
7
|
+
export function VeroxmRegion(props) {
|
|
8
|
+
return VeroxmSlot(regionSlotProps(props));
|
|
9
|
+
}
|
package/dist/next/route.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { draftMode } from 'next/headers';
|
|
2
2
|
import { revalidateTag } from 'next/cache';
|
|
3
3
|
import { createEditorHandler } from '../editor/handler.js';
|
|
4
|
+
import { CONTENT_TAG } from './content-core.js';
|
|
4
5
|
const handle = createEditorHandler({
|
|
5
6
|
isEditing: async () => (await draftMode()).isEnabled,
|
|
6
7
|
enter: async () => (await draftMode()).enable(),
|
|
7
8
|
exit: async () => (await draftMode()).disable(),
|
|
8
9
|
published: () => revalidateTag('veroxm-slots', { expire: 0 }),
|
|
10
|
+
// A signed content webhook: every content read (getEntries, getEntry...) is tagged veroxm-content.
|
|
11
|
+
contentChanged: () => revalidateTag(CONTENT_TAG, { expire: 0 }),
|
|
9
12
|
});
|
|
10
13
|
async function route(request, ctx) {
|
|
11
14
|
const params = ctx?.params ? await ctx.params : undefined;
|
package/dist/provider.js
CHANGED
|
@@ -20,16 +20,16 @@ const VeroxmContext = createContext(null);
|
|
|
20
20
|
* <VeroxmHydrationBoundary>). Outside production builds a browser client with a key logs a warning once.
|
|
21
21
|
*/
|
|
22
22
|
export function VeroxmProvider(props) {
|
|
23
|
-
const { children, client, apiBase, apiKey, projectUuid, cache } = props;
|
|
23
|
+
const { children, client, apiBase, apiKey, projectUuid, cache, timeoutMs, retries } = props;
|
|
24
24
|
const value = useMemo(() => {
|
|
25
25
|
if (client)
|
|
26
26
|
return client;
|
|
27
27
|
if (!apiBase || !apiKey) {
|
|
28
28
|
throw new Error('<VeroxmProvider> needs either a `client` prop or both `apiBase` and `apiKey`.');
|
|
29
29
|
}
|
|
30
|
-
return new VeroxmClient({ apiBase, apiKey, projectUuid, cache });
|
|
30
|
+
return new VeroxmClient({ apiBase, apiKey, projectUuid, cache, timeoutMs, retries });
|
|
31
31
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
32
|
-
}, [client, apiBase, apiKey, projectUuid, cache]);
|
|
32
|
+
}, [client, apiBase, apiKey, projectUuid, cache, timeoutMs, retries]);
|
|
33
33
|
return _jsx(VeroxmContext.Provider, { value: value, children: children });
|
|
34
34
|
}
|
|
35
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. */
|
package/dist/slots-core.d.ts
CHANGED
|
@@ -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 :
|
|
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
|
};
|
package/dist/slots-core.js
CHANGED
|
@@ -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 {
|