@eldrajs/theme-nuxt 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 +233 -0
- package/LICENSE +21 -0
- package/README.md +324 -2
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +56 -0
- package/dist/module.d.ts +56 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +265 -0
- package/dist/runtime/catalog.d.ts +119 -0
- package/dist/runtime/catalog.js +226 -0
- package/dist/runtime/commerce.d.ts +68 -0
- package/dist/runtime/commerce.js +40 -0
- package/dist/runtime/composables/useEldraPage.d.ts +17 -0
- package/dist/runtime/composables/useEldraPage.js +229 -0
- package/dist/runtime/designTokens.d.ts +59 -0
- package/dist/runtime/designTokens.js +55 -0
- package/dist/runtime/drafts.d.ts +3 -0
- package/dist/runtime/drafts.js +83 -0
- package/dist/runtime/links.d.ts +32 -0
- package/dist/runtime/links.js +132 -0
- package/dist/runtime/locale.d.ts +21 -0
- package/dist/runtime/locale.js +9 -0
- package/dist/runtime/localeState.d.ts +52 -0
- package/dist/runtime/localeState.js +48 -0
- package/dist/runtime/locales.d.ts +186 -0
- package/dist/runtime/locales.js +109 -0
- package/dist/runtime/messages.d.ts +59 -0
- package/dist/runtime/messages.js +66 -0
- package/dist/runtime/origins.d.ts +18 -0
- package/dist/runtime/origins.js +28 -0
- package/dist/runtime/plugin.d.ts +3 -0
- package/dist/runtime/plugin.js +83 -0
- package/dist/runtime/resolveRoute.d.ts +81 -0
- package/dist/runtime/resolveRoute.js +166 -0
- package/dist/runtime/routePath.d.ts +31 -0
- package/dist/runtime/routePath.js +7 -0
- package/dist/runtime/staticRoutes.d.ts +14 -0
- package/dist/runtime/staticRoutes.js +26 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +7 -0
- package/package.json +66 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { EldraFeatureClient, EldraOrganizationCommerce } from '@eldrajs/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* What the store sells in, as it reaches a theme on `runtimeConfig.public.eldra.commerce`:
|
|
4
|
+
* `{ currency, taxInclusivePricing, defaultTaxRate }`, or `null`.
|
|
5
|
+
*
|
|
6
|
+
* Published as `@eldrajs/theme-nuxt/commerce`, so a theme reading that key types it from here
|
|
7
|
+
* instead of hand-writing the record — it is the public contract of a public runtime-config key,
|
|
8
|
+
* and the first field added to it should not have to be added again in every theme. (Its own
|
|
9
|
+
* subpath rather than the package root: `nuxt-module-build` generates the root's `.d.mts` itself,
|
|
10
|
+
* and that file carries the module and `ModuleOptions` only.)
|
|
11
|
+
*
|
|
12
|
+
* It is the gateway's own organisation `commerce` record unchanged, so the shape is declared **once**
|
|
13
|
+
* — in `@eldrajs/sdk`, which is where the read comes from — and aliased here under the name the
|
|
14
|
+
* runtime config uses.
|
|
15
|
+
*
|
|
16
|
+
* Read **once, at build**, from the organisation the site is deployed for
|
|
17
|
+
* (`@eldrajs/sdk`'s `features.getCommerce()`), because every price a prerendered page carries is
|
|
18
|
+
* formatted against it: a value fetched in the browser would mean every amount on a static page
|
|
19
|
+
* rendering without a currency first and reflowing a tick later.
|
|
20
|
+
*
|
|
21
|
+
* `null` is a real answer, not a failure: a store that has not configured commerce publishes
|
|
22
|
+
* nothing, and a theme that knows that can render a price as a plain number instead of guessing a
|
|
23
|
+
* symbol. The module itself adds **no override** — the platform is the one source for this, and a
|
|
24
|
+
* site whose own currency setting disagreed with the catalogue would relabel real amounts rather
|
|
25
|
+
* than correct them.
|
|
26
|
+
*/
|
|
27
|
+
export type StoreCommerce = EldraOrganizationCommerce;
|
|
28
|
+
/**
|
|
29
|
+
* The slice of an `@eldrajs/sdk` client this read needs — a whole `EldraClient` satisfies it.
|
|
30
|
+
* Narrow on purpose: it is what lets the module's own tests hand `readStoreCommerce` a client that
|
|
31
|
+
* answers, omits or throws with no gateway anywhere near them.
|
|
32
|
+
*/
|
|
33
|
+
export interface StoreCommerceReader {
|
|
34
|
+
features: Pick<EldraFeatureClient, 'getCommerce'>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The one line a theme's build prints when it could not learn the store's currency. It says *plain
|
|
38
|
+
* numbers* because that is what a theme with no currency has to render: there is no code to print
|
|
39
|
+
* either — the "number plus its code" shape belongs to a currency that was published but is
|
|
40
|
+
* unusable, which this warning is never about.
|
|
41
|
+
*/
|
|
42
|
+
export declare const NO_CURRENCY_WARNING = "[eldra] the store publishes no currency \u2014 prices render as plain numbers";
|
|
43
|
+
/**
|
|
44
|
+
* How long the build waits for the answer. A bound rather than none, because this read is
|
|
45
|
+
* optional: the page and route-template reads a prerender depends on may hang until the gateway
|
|
46
|
+
* answers or fails, but a currency nobody can fetch must not be the reason a deploy never
|
|
47
|
+
* finishes.
|
|
48
|
+
*
|
|
49
|
+
* Exported so the test that proves the bound can assert the value rather than restate it.
|
|
50
|
+
*/
|
|
51
|
+
export declare const READ_TIMEOUT_MS = 10000;
|
|
52
|
+
/**
|
|
53
|
+
* The store's commerce settings for `runtimeConfig.public.eldra`, or `null`.
|
|
54
|
+
*
|
|
55
|
+
* Fail-soft by construction: a gateway that cannot be reached, a read that times out, a response
|
|
56
|
+
* that is not the shape the gateway documents and a store with no commerce at all all end the same
|
|
57
|
+
* way — `null`, **exactly one** warning, and a build that finishes. A theme must stay deployable
|
|
58
|
+
* while the platform is having a bad afternoon, and the only thing lost is a currency symbol.
|
|
59
|
+
*
|
|
60
|
+
* When the read itself failed, that one warning carries the cause: "not configured" and
|
|
61
|
+
* "refused or unreachable" are different problems to whoever is reading the build log, and only
|
|
62
|
+
* the second is a mistake someone can fix. It is still one line, so a build log cannot be read as
|
|
63
|
+
* reporting two separate faults.
|
|
64
|
+
*
|
|
65
|
+
* `reader` is `null` for a site built without gateway credentials (a scaffolded theme nobody has
|
|
66
|
+
* connected yet, a CI build with no secrets). Same outcome, same single warning, no request.
|
|
67
|
+
*/
|
|
68
|
+
export declare function readStoreCommerce(reader: StoreCommerceReader | null, warn?: (message: string) => void): Promise<StoreCommerce | null>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const NO_CURRENCY_WARNING = "[eldra] the store publishes no currency \u2014 prices render as plain numbers";
|
|
2
|
+
export const READ_TIMEOUT_MS = 1e4;
|
|
3
|
+
export async function readStoreCommerce(reader, warn = console.warn) {
|
|
4
|
+
const read = reader === null ? { commerce: null, cause: null } : await readOrNull(reader);
|
|
5
|
+
if (read.commerce === null) {
|
|
6
|
+
warn(read.cause === null ? NO_CURRENCY_WARNING : `${NO_CURRENCY_WARNING} (${read.cause})`);
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
return read.commerce;
|
|
10
|
+
}
|
|
11
|
+
async function readOrNull(reader) {
|
|
12
|
+
const controller = new AbortController();
|
|
13
|
+
const timer = setTimeout(
|
|
14
|
+
() => controller.abort(new Error(`timed out after ${READ_TIMEOUT_MS}ms`)),
|
|
15
|
+
READ_TIMEOUT_MS
|
|
16
|
+
);
|
|
17
|
+
let answer;
|
|
18
|
+
try {
|
|
19
|
+
answer = await reader.features.getCommerce(void 0, { signal: controller.signal });
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return { commerce: null, cause: `the organisation read failed: ${describe(error)}` };
|
|
22
|
+
} finally {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
}
|
|
25
|
+
const commerce = toStoreCommerce(answer);
|
|
26
|
+
return {
|
|
27
|
+
commerce,
|
|
28
|
+
cause: commerce === null && answer !== null && answer !== void 0 ? "the organisation published an incomplete commerce record" : null
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function toStoreCommerce(value) {
|
|
32
|
+
if (value === null || typeof value !== "object") return null;
|
|
33
|
+
const { currency, taxInclusivePricing, defaultTaxRate } = value;
|
|
34
|
+
if (typeof currency !== "string" || currency === "") return null;
|
|
35
|
+
if (typeof taxInclusivePricing !== "boolean" || typeof defaultTaxRate !== "number") return null;
|
|
36
|
+
return { currency, taxInclusivePricing, defaultTaxRate };
|
|
37
|
+
}
|
|
38
|
+
function describe(error) {
|
|
39
|
+
return error instanceof Error ? error.message : String(error);
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type EntryDoc } from '@eldrajs/theme-core';
|
|
2
|
+
import { type ComputedRef, type Ref } from 'vue';
|
|
3
|
+
import type { CatalogRouteRef } from '../catalog.js';
|
|
4
|
+
import { type ResolvedEldraRoute } from '../resolveRoute.js';
|
|
5
|
+
export declare function useEldraPage(): {
|
|
6
|
+
page: Ref<EntryDoc | null>;
|
|
7
|
+
template: Ref<EntryDoc | null>;
|
|
8
|
+
entry: Ref<EntryDoc | null>;
|
|
9
|
+
catalog: ComputedRef<CatalogRouteRef | null>;
|
|
10
|
+
layout: ComputedRef<unknown | null>;
|
|
11
|
+
blocks: ComputedRef<EntryDoc[]>;
|
|
12
|
+
reusableComponentProjection: ComputedRef<unknown | undefined>;
|
|
13
|
+
links: ComputedRef<ResolvedEldraRoute['links']>;
|
|
14
|
+
pending: Ref<boolean>;
|
|
15
|
+
/** Readonly: it is derived from the resolution (`ResolvedEldraRoute.error`), not held here. */
|
|
16
|
+
error: ComputedRef<string | null>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { EldraClientError } from "@eldrajs/theme-core";
|
|
2
|
+
import { useEldra } from "@eldrajs/theme-vue";
|
|
3
|
+
import {
|
|
4
|
+
clearNuxtData,
|
|
5
|
+
loadPayload,
|
|
6
|
+
setResponseStatus,
|
|
7
|
+
useAsyncData,
|
|
8
|
+
useRequestEvent,
|
|
9
|
+
useRoute,
|
|
10
|
+
useRouter,
|
|
11
|
+
useRuntimeConfig
|
|
12
|
+
} from "nuxt/app";
|
|
13
|
+
import { computed, getCurrentInstance, ref, watch } from "vue";
|
|
14
|
+
import { overlayPreviewDrafts } from "../drafts.js";
|
|
15
|
+
import { normalizeLocale } from "../locale.js";
|
|
16
|
+
import {
|
|
17
|
+
localeForPath,
|
|
18
|
+
resolveLocaleRouting,
|
|
19
|
+
resolveStoreLocales,
|
|
20
|
+
stripLocalePrefix
|
|
21
|
+
} from "../locales.js";
|
|
22
|
+
import { isStudioPreviewFrame } from "../origins.js";
|
|
23
|
+
import { EMPTY_ELDRA_ROUTE, resolveEldraRoute } from "../resolveRoute.js";
|
|
24
|
+
import { canonicalRoutePath } from "../routePath.js";
|
|
25
|
+
import { knownPrerenderedRoutes, prerenderedRoutes } from "../staticRoutes.js";
|
|
26
|
+
const PAGE_KEY_PREFIX = "eldra-page:";
|
|
27
|
+
const routeDataKey = (path) => `${PAGE_KEY_PREFIX}${path}`;
|
|
28
|
+
export function useEldraPage() {
|
|
29
|
+
const route = useRoute();
|
|
30
|
+
const activePath = createActivePath(route, useRouter());
|
|
31
|
+
const requestEvent = useRequestEvent();
|
|
32
|
+
const ctx = useEldra();
|
|
33
|
+
const cfg = useRuntimeConfig().public.eldra;
|
|
34
|
+
const routing = resolveLocaleRouting(resolveStoreLocales(cfg.locales), cfg.locale);
|
|
35
|
+
const routeTarget = (path) => ({
|
|
36
|
+
path: stripLocalePrefix(path, routing),
|
|
37
|
+
locale: localeForPath(path, routing)
|
|
38
|
+
});
|
|
39
|
+
const runtimeLocale = (path) => normalizeLocale(ctx.preview.active ? ctx.preview.locale : null) ?? routeTarget(path).locale;
|
|
40
|
+
const buildAnswersRoutes = () => !ctx.preview.active && !isStudioPreviewFrame(cfg.studioOrigins);
|
|
41
|
+
const cachedRoute = (key, nuxtApp, context) => {
|
|
42
|
+
if (context.cause !== "refresh:manual" && context.cause !== "refresh:hook") {
|
|
43
|
+
const fromBuild = nuxtApp.isHydrating ? nuxtApp.payload.data[key] : nuxtApp.static.data[key];
|
|
44
|
+
if (fromBuild !== void 0) return fromBuild;
|
|
45
|
+
}
|
|
46
|
+
if (!buildAnswersRoutes()) return void 0;
|
|
47
|
+
const prerendered = knownPrerenderedRoutes();
|
|
48
|
+
if (prerendered === null) return void 0;
|
|
49
|
+
return prerendered.has(key.slice(PAGE_KEY_PREFIX.length)) ? void 0 : EMPTY_ELDRA_ROUTE;
|
|
50
|
+
};
|
|
51
|
+
const PREVIEW_AUTH_RECOVERY_GRACE_MS = 1e4;
|
|
52
|
+
let heldPreviewAuthFailure = null;
|
|
53
|
+
const holdPreviewAuthFailure = (cause) => {
|
|
54
|
+
if (!ctx.preview.active) return false;
|
|
55
|
+
if (!(cause instanceof EldraClientError) || cause.status !== 401) return false;
|
|
56
|
+
const tokenRevision = ctx.preview.tokenRevision;
|
|
57
|
+
const now = Date.now();
|
|
58
|
+
if (heldPreviewAuthFailure === null) {
|
|
59
|
+
heldPreviewAuthFailure = { tokenRevision, at: now };
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (tokenRevision > heldPreviewAuthFailure.tokenRevision) return false;
|
|
63
|
+
return now - heldPreviewAuthFailure.at <= PREVIEW_AUTH_RECOVERY_GRACE_MS;
|
|
64
|
+
};
|
|
65
|
+
const hasCredentials = cfg.gatewayUrl !== "" && cfg.orgId !== "";
|
|
66
|
+
const reportServerFailure = () => {
|
|
67
|
+
if (!import.meta.server || requestEvent === void 0) return;
|
|
68
|
+
setResponseStatus(requestEvent, 500, "Eldra route resolution failed");
|
|
69
|
+
};
|
|
70
|
+
const resolveRouteOutcome = async () => {
|
|
71
|
+
const path = activePath();
|
|
72
|
+
if (buildAnswersRoutes()) {
|
|
73
|
+
const prerendered = await prerenderedRoutes();
|
|
74
|
+
if (prerendered !== null) {
|
|
75
|
+
if (!prerendered.has(path)) return { route: EMPTY_ELDRA_ROUTE, holdPrevious: false };
|
|
76
|
+
const fromBuild = await prerenderedRouteData(path);
|
|
77
|
+
if (fromBuild !== void 0) return { route: fromBuild, holdPrevious: false };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const route2 = await resolveEldraRoute(
|
|
82
|
+
ctx.client,
|
|
83
|
+
cfg,
|
|
84
|
+
routeTarget(path).path,
|
|
85
|
+
runtimeLocale(path)
|
|
86
|
+
);
|
|
87
|
+
heldPreviewAuthFailure = null;
|
|
88
|
+
return { route: route2, holdPrevious: false };
|
|
89
|
+
} catch (cause) {
|
|
90
|
+
if (holdPreviewAuthFailure(cause)) return { route: EMPTY_ELDRA_ROUTE, holdPrevious: true };
|
|
91
|
+
if (!hasCredentials) return { route: EMPTY_ELDRA_ROUTE, holdPrevious: false };
|
|
92
|
+
reportServerFailure();
|
|
93
|
+
return {
|
|
94
|
+
route: {
|
|
95
|
+
...EMPTY_ELDRA_ROUTE,
|
|
96
|
+
error: cause instanceof Error ? cause.message : String(cause)
|
|
97
|
+
},
|
|
98
|
+
holdPrevious: false
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const resolveCurrentRoute = async () => (await resolveRouteOutcome()).route;
|
|
103
|
+
const { data: resolvedRoute, pending } = useAsyncData(
|
|
104
|
+
() => routeDataKey(activePath()),
|
|
105
|
+
resolveCurrentRoute,
|
|
106
|
+
{
|
|
107
|
+
// The same source the key reads, and the **canonical** path in both (`../routePath.ts`): a
|
|
108
|
+
// static host that answers `/products/x` with a 308 to `/products/x/` makes Nuxt re-navigate
|
|
109
|
+
// between the two during hydration, and watching the raw path would make that a route change
|
|
110
|
+
// — a second gateway resolve, `pending` back to true, and the whole block tree torn down and
|
|
111
|
+
// built again while the answer is in flight.
|
|
112
|
+
watch: [() => activePath()],
|
|
113
|
+
default: () => EMPTY_ELDRA_ROUTE,
|
|
114
|
+
getCachedData: cachedRoute
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
const previewResolvedRoute = ref(void 0);
|
|
118
|
+
let previewRefreshRequest = 0;
|
|
119
|
+
const resolvePreviewRoute = () => {
|
|
120
|
+
if (!ctx.preview.active) return;
|
|
121
|
+
const request = ++previewRefreshRequest;
|
|
122
|
+
const path = activePath();
|
|
123
|
+
const activeKey = routeDataKey(path);
|
|
124
|
+
clearNuxtData((key) => key.startsWith(PAGE_KEY_PREFIX) && key !== activeKey);
|
|
125
|
+
void resolveRouteOutcome().then(({ route: route2, holdPrevious }) => {
|
|
126
|
+
if (request !== previewRefreshRequest || path !== activePath()) return;
|
|
127
|
+
if (holdPrevious) return;
|
|
128
|
+
previewResolvedRoute.value = route2;
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
watch(() => ctx.preview.refreshRevision, resolvePreviewRoute);
|
|
132
|
+
watch(
|
|
133
|
+
() => ctx.preview.active,
|
|
134
|
+
(active2) => {
|
|
135
|
+
if (active2) resolvePreviewRoute();
|
|
136
|
+
},
|
|
137
|
+
{ immediate: true }
|
|
138
|
+
);
|
|
139
|
+
watch(
|
|
140
|
+
() => activePath(),
|
|
141
|
+
() => {
|
|
142
|
+
previewRefreshRequest += 1;
|
|
143
|
+
previewResolvedRoute.value = void 0;
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
const active = computed(
|
|
147
|
+
() => previewResolvedRoute.value ?? resolvedRoute.value ?? EMPTY_ELDRA_ROUTE
|
|
148
|
+
);
|
|
149
|
+
const error = computed(() => active.value.error ?? null);
|
|
150
|
+
const page = computed(() => {
|
|
151
|
+
void ctx.preview.revision;
|
|
152
|
+
return overlayPreviewDrafts(
|
|
153
|
+
active.value.page,
|
|
154
|
+
ctx.preview.drafts,
|
|
155
|
+
ctx.preview.draftSchemaApiIds
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
const template = computed(
|
|
159
|
+
() => overlayPreviewDrafts(active.value.template, ctx.preview.drafts, ctx.preview.draftSchemaApiIds)
|
|
160
|
+
);
|
|
161
|
+
const entry = computed(() => overlayEntryDraft(active.value.entry, ctx.preview.drafts));
|
|
162
|
+
const catalog = computed(() => active.value.catalog);
|
|
163
|
+
const linkState = computed(
|
|
164
|
+
() => active.value.links ?? { pages: [], templates: [], targets: /* @__PURE__ */ new Map() }
|
|
165
|
+
);
|
|
166
|
+
watch(
|
|
167
|
+
linkState,
|
|
168
|
+
(links) => {
|
|
169
|
+
ctx.links.pages = links.pages;
|
|
170
|
+
ctx.links.templates = links.templates;
|
|
171
|
+
ctx.links.targets = links.targets;
|
|
172
|
+
},
|
|
173
|
+
{ immediate: true }
|
|
174
|
+
);
|
|
175
|
+
const layout = computed(
|
|
176
|
+
() => template.value?.data.layout ?? page.value?.data.layout ?? null
|
|
177
|
+
);
|
|
178
|
+
const blocks = computed(() => {
|
|
179
|
+
const raw = template.value?.data.blocks ?? page.value?.data.blocks;
|
|
180
|
+
return Array.isArray(raw) ? raw.filter(isEntryDoc) : [];
|
|
181
|
+
});
|
|
182
|
+
const reusableComponentProjection = computed(
|
|
183
|
+
() => page.value?.reusableComponentProjection ?? template.value?.reusableComponentProjection
|
|
184
|
+
);
|
|
185
|
+
return {
|
|
186
|
+
page,
|
|
187
|
+
template,
|
|
188
|
+
entry,
|
|
189
|
+
catalog,
|
|
190
|
+
layout,
|
|
191
|
+
blocks,
|
|
192
|
+
reusableComponentProjection,
|
|
193
|
+
links: linkState,
|
|
194
|
+
pending,
|
|
195
|
+
error
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async function prerenderedRouteData(path) {
|
|
199
|
+
try {
|
|
200
|
+
const payload = await loadPayload(path);
|
|
201
|
+
const data = payload?.data;
|
|
202
|
+
const value = data?.[routeDataKey(path)];
|
|
203
|
+
return value === void 0 ? void 0 : value;
|
|
204
|
+
} catch {
|
|
205
|
+
return void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function createActivePath(route, router) {
|
|
209
|
+
const committed = () => canonicalRoutePath(router.currentRoute.value.path);
|
|
210
|
+
if (getCurrentInstance() === null) return committed;
|
|
211
|
+
let pinned = committed();
|
|
212
|
+
return () => {
|
|
213
|
+
const settled = canonicalRoutePath(route.path);
|
|
214
|
+
if (pinned === null) return settled;
|
|
215
|
+
if (settled === pinned) {
|
|
216
|
+
pinned = null;
|
|
217
|
+
return settled;
|
|
218
|
+
}
|
|
219
|
+
return pinned;
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function overlayEntryDraft(entry, drafts) {
|
|
223
|
+
if (entry === null) return null;
|
|
224
|
+
const draft = drafts[entry.id];
|
|
225
|
+
return draft === void 0 ? entry : { ...entry, data: { ...entry.data, ...draft } };
|
|
226
|
+
}
|
|
227
|
+
function isEntryDoc(value) {
|
|
228
|
+
return typeof value === "object" && value !== null && typeof value.id === "string" && typeof value.data === "object" && value.data !== null;
|
|
229
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { EldraClient } from '@eldrajs/sdk';
|
|
2
|
+
import { type ThemeDesignTokens } from '@eldrajs/theme-core';
|
|
3
|
+
/**
|
|
4
|
+
* Re-exported from `@eldrajs/theme-core` under the name this package's own reads use, the same way
|
|
5
|
+
* `./commerce.ts`/`./locales.ts` alias the SDK's organisation types and `./messages.ts` aliases
|
|
6
|
+
* `ThemeMessages`.
|
|
7
|
+
*/
|
|
8
|
+
export type { ThemeDesignTokens };
|
|
9
|
+
/**
|
|
10
|
+
* The slice of an `@eldrajs/sdk` client this read needs — a whole `EldraClient` satisfies it.
|
|
11
|
+
* Narrow on purpose, like `ThemeMessagesReader`: it is what lets this read's own tests hand it a
|
|
12
|
+
* client that answers, omits or throws with no gateway anywhere near them.
|
|
13
|
+
*
|
|
14
|
+
* `request` rather than a `features.*` method: the public route this reads
|
|
15
|
+
* (`GET /site/v1/design-tokens`) is a **site**-module read, not an organisation-feature one, so
|
|
16
|
+
* there is no typed `EldraFeatureClient` method for it to call through — the client's own generic
|
|
17
|
+
* escape hatch carries the same base URL, org header and retry policy every other read here uses.
|
|
18
|
+
*/
|
|
19
|
+
export interface DesignTokensReader {
|
|
20
|
+
request: EldraClient['request'];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The one line a build prints when it could not read the platform's design-token overrides. Said
|
|
24
|
+
* unconditionally whenever the read comes back `null` — absent gateway credentials included — the
|
|
25
|
+
* same discipline `THEME_MESSAGES_WARNING`/`NO_CURRENCY_WARNING` follow and for the same reason:
|
|
26
|
+
* "serving the theme's own tokens" is the real, useful thing to know happened, not a fault to hide.
|
|
27
|
+
*/
|
|
28
|
+
export declare const DESIGN_TOKENS_WARNING = "[eldra] could not read the platform\u2019s design-token overrides \u2014 serving the theme\u2019s own tokens";
|
|
29
|
+
/**
|
|
30
|
+
* How long the build waits for the answer. A bound rather than none, for the same reason
|
|
31
|
+
* `messages.ts`/`commerce.ts`/`locales.ts` have one: this read is optional, and a design-token
|
|
32
|
+
* override nobody can fetch must not be the reason a deploy never finishes.
|
|
33
|
+
*
|
|
34
|
+
* Exported so the test that proves the bound can assert the value rather than restate it.
|
|
35
|
+
*/
|
|
36
|
+
export declare const READ_TIMEOUT_MS = 10000;
|
|
37
|
+
/**
|
|
38
|
+
* The platform's resolved design-token catalog for the build-time merge, or `null`.
|
|
39
|
+
*
|
|
40
|
+
* Fail-soft by construction, exactly like `readThemeMessages`: a gateway that cannot be reached, a
|
|
41
|
+
* read that times out, a response that is not the shape the gateway documents and a site built with
|
|
42
|
+
* no gateway credentials at all end the same way — `null` and **one** warning — so a theme keeps
|
|
43
|
+
* deploying while serving its own tokens.
|
|
44
|
+
*/
|
|
45
|
+
export declare function readDesignTokens(reader: DesignTokensReader | null, warn?: (message: string) => void): Promise<ThemeDesignTokens | null>;
|
|
46
|
+
/**
|
|
47
|
+
* The platform's design-token catalog as it reaches the running site on
|
|
48
|
+
* `runtimeConfig.public.eldra.designTokens` — already `normalizeThemeDesignTokens`'d at build time
|
|
49
|
+
* (this is `readDesignTokens`'s own answer, round-tripped through the runtime config payload), or
|
|
50
|
+
* `null`.
|
|
51
|
+
*
|
|
52
|
+
* Re-validates rather than trusting the payload outright, for the same reason
|
|
53
|
+
* `toStoreLocales`/`resolveStoreLocales` do: this is also what reads the value back out in the
|
|
54
|
+
* browser, where it arrives as whatever Nitro serialised — `''` for an absent one, same as
|
|
55
|
+
* `commerce`/`locale`. A value that fails the theme's own validation is treated exactly like an
|
|
56
|
+
* absent one, so the runtime plugin's own fallback to the manifest's tokens is the only thing that
|
|
57
|
+
* ever reaches `context.designTokens`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveSiteDesignTokens(value: unknown): ThemeDesignTokens | null;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { normalizeThemeDesignTokens } from "@eldrajs/theme-core";
|
|
2
|
+
export const DESIGN_TOKENS_WARNING = "[eldra] could not read the platform\u2019s design-token overrides \u2014 serving the theme\u2019s own tokens";
|
|
3
|
+
export const READ_TIMEOUT_MS = 1e4;
|
|
4
|
+
export async function readDesignTokens(reader, warn = console.warn) {
|
|
5
|
+
const read = reader === null ? { tokens: null, cause: null } : await readOrNull(reader);
|
|
6
|
+
if (read.tokens === null) {
|
|
7
|
+
warn(read.cause === null ? DESIGN_TOKENS_WARNING : `${DESIGN_TOKENS_WARNING} (${read.cause})`);
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return read.tokens;
|
|
11
|
+
}
|
|
12
|
+
async function readOrNull(reader) {
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
const timer = setTimeout(
|
|
15
|
+
() => controller.abort(new Error(`timed out after ${READ_TIMEOUT_MS}ms`)),
|
|
16
|
+
READ_TIMEOUT_MS
|
|
17
|
+
);
|
|
18
|
+
try {
|
|
19
|
+
const response = await reader.request({
|
|
20
|
+
path: "/site/v1/design-tokens",
|
|
21
|
+
signal: controller.signal
|
|
22
|
+
});
|
|
23
|
+
const tokens = toResolvedDesignTokens(response);
|
|
24
|
+
return {
|
|
25
|
+
tokens,
|
|
26
|
+
cause: tokens === null ? "the platform published an unusable response" : null
|
|
27
|
+
};
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return { tokens: null, cause: describe(error) };
|
|
30
|
+
} finally {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function toResolvedDesignTokens(value) {
|
|
35
|
+
if (typeof value !== "object" || value === null) return null;
|
|
36
|
+
const record = value;
|
|
37
|
+
if (typeof record.revision !== "number" || !Number.isFinite(record.revision)) return null;
|
|
38
|
+
if (typeof record.resolved !== "object" || record.resolved === null) return null;
|
|
39
|
+
try {
|
|
40
|
+
return normalizeThemeDesignTokens(record.resolved);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function resolveSiteDesignTokens(value) {
|
|
46
|
+
if (value === null || value === void 0 || value === "") return null;
|
|
47
|
+
try {
|
|
48
|
+
return normalizeThemeDesignTokens(value);
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function describe(error) {
|
|
54
|
+
return error instanceof Error ? error.message : String(error);
|
|
55
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type EntryDoc } from '@eldrajs/theme-core';
|
|
2
|
+
/** Applies in-memory builder updates to resolved entries without putting draft data in URLs. */
|
|
3
|
+
export declare function overlayPreviewDrafts(page: EntryDoc | null | undefined, drafts: Record<string, Record<string, unknown>>, draftSchemaApiIds?: Readonly<Record<string, string>>): EntryDoc | null;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeLayoutDocument
|
|
3
|
+
} from "@eldrajs/theme-core";
|
|
4
|
+
import themeManifest from "virtual:eldra/manifest";
|
|
5
|
+
export function overlayPreviewDrafts(page, drafts, draftSchemaApiIds = {}) {
|
|
6
|
+
if (page == null) return null;
|
|
7
|
+
const pageDraft = drafts[page.id];
|
|
8
|
+
const data = pageDraft === void 0 ? { ...page.data } : { ...page.data, ...pageDraft };
|
|
9
|
+
const resolvedById = new Map(
|
|
10
|
+
(Array.isArray(page.data.blocks) ? page.data.blocks : []).filter((block) => isEntryDoc(block)).map((block) => [block.id, block])
|
|
11
|
+
);
|
|
12
|
+
const layoutEntryIds = layoutBlockEntryIds(data.layout, resolvedById, draftSchemaApiIds);
|
|
13
|
+
if (layoutEntryIds !== null) {
|
|
14
|
+
const reusableEntryIds = reusableProjectionEntryIds(
|
|
15
|
+
page.reusableComponentProjection,
|
|
16
|
+
resolvedById,
|
|
17
|
+
draftSchemaApiIds
|
|
18
|
+
);
|
|
19
|
+
const blocks2 = [.../* @__PURE__ */ new Set([...layoutEntryIds, ...reusableEntryIds])].flatMap((entryId) => {
|
|
20
|
+
const published = resolvedById.get(entryId);
|
|
21
|
+
const draft = drafts[entryId];
|
|
22
|
+
if (published !== void 0) {
|
|
23
|
+
return [{ ...published, ...draft === void 0 ? {} : { data: draft } }];
|
|
24
|
+
}
|
|
25
|
+
const schemaApiId = draftSchemaApiIds[entryId];
|
|
26
|
+
return draft !== void 0 && schemaApiId !== void 0 ? [{ id: entryId, schemaApiId, data: draft }] : [];
|
|
27
|
+
});
|
|
28
|
+
return { ...page, data: { ...data, blocks: blocks2 } };
|
|
29
|
+
}
|
|
30
|
+
const blocks = Array.isArray(data.blocks) ? data.blocks.map((block) => resolvedReference(block, resolvedById)) : data.blocks;
|
|
31
|
+
if (!Array.isArray(blocks)) return { ...page, data };
|
|
32
|
+
return {
|
|
33
|
+
...page,
|
|
34
|
+
data: {
|
|
35
|
+
...data,
|
|
36
|
+
blocks: blocks.map((block) => {
|
|
37
|
+
if (!isEntryDoc(block)) return block;
|
|
38
|
+
const draft = drafts[block.id];
|
|
39
|
+
return draft === void 0 ? block : { ...block, data: draft };
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function reusableProjectionEntryIds(value, byId, draftSchemaApiIds) {
|
|
45
|
+
if (typeof value !== "object" || value === null) return [];
|
|
46
|
+
const revisions = value.revisions;
|
|
47
|
+
if (!Array.isArray(revisions)) return [];
|
|
48
|
+
return revisions.flatMap((revision) => {
|
|
49
|
+
if (typeof revision !== "object" || revision === null) return [];
|
|
50
|
+
return layoutBlockEntryIds(revision.document, byId, draftSchemaApiIds) ?? [];
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function buildSlotCatalog() {
|
|
54
|
+
const blocks = themeManifest.blocks ?? [];
|
|
55
|
+
const out = {};
|
|
56
|
+
for (const block of blocks) {
|
|
57
|
+
if (block.apiId && Array.isArray(block.slots)) out[block.apiId] = block.slots;
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
const THEME_SLOT_CATALOG = buildSlotCatalog();
|
|
62
|
+
function layoutBlockEntryIds(value, byId, draftSchemaApiIds) {
|
|
63
|
+
if (value === void 0) return null;
|
|
64
|
+
const slotContext = {
|
|
65
|
+
slotCatalog: THEME_SLOT_CATALOG,
|
|
66
|
+
entryApiId: (entryId) => byId.get(entryId)?.schemaApiId ?? draftSchemaApiIds[entryId]
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
return normalizeLayoutDocument(value, void 0, void 0, slotContext).blockEntryIds;
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function resolvedReference(value, byId) {
|
|
75
|
+
if (isEntryDoc(value)) return value;
|
|
76
|
+
if (typeof value !== "object" || value === null) return value;
|
|
77
|
+
const reference = value;
|
|
78
|
+
const id = typeof reference.id === "string" ? reference.id : reference.type === "entry" && typeof reference.value === "string" ? reference.value : void 0;
|
|
79
|
+
return typeof id === "string" ? byId.get(id) ?? value : value;
|
|
80
|
+
}
|
|
81
|
+
function isEntryDoc(value) {
|
|
82
|
+
return typeof value === "object" && value !== null && typeof value.id === "string" && typeof value.data === "object" && value.data !== null;
|
|
83
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { EldraClient, EntryDoc } from '@eldrajs/theme-core';
|
|
2
|
+
import type { LinkTargetInfo } from '@eldrajs/theme-core/links';
|
|
3
|
+
export interface LinkTargetSources {
|
|
4
|
+
/**
|
|
5
|
+
* The CMS schemas an `entry` target may belong to. A link target carries
|
|
6
|
+
* `_type: "entry"` and not its schema, so the schemas worth reading are the
|
|
7
|
+
* ones the site has a route template for — an entry no template serves has no
|
|
8
|
+
* href to resolve to anyway.
|
|
9
|
+
*/
|
|
10
|
+
entrySchemaApiIds?: readonly string[];
|
|
11
|
+
/**
|
|
12
|
+
* The site's own pages, which route resolution has already listed. A page's
|
|
13
|
+
* path comes from that list rather than from a lookup, so this only supplies
|
|
14
|
+
* the title a blank label falls back to — no extra read.
|
|
15
|
+
*/
|
|
16
|
+
pages?: readonly EntryDoc[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What the site knows about every object its links point at, keyed
|
|
20
|
+
* `${_type}:${id}` — the key `linkTargetKeys` produces.
|
|
21
|
+
*
|
|
22
|
+
* One batched read per type, never one per link: products, collections and
|
|
23
|
+
* categories by an `id:in:` filter in chunks, entries one read per schema the
|
|
24
|
+
* site routes, each paged to the end rather than to the first hundred rows.
|
|
25
|
+
* The category read is skipped entirely by a reader that does not offer one.
|
|
26
|
+
* Only the ids that were asked for are kept, so a gateway that ignores the
|
|
27
|
+
* filter cannot put its whole catalogue into the page's payload. A read that
|
|
28
|
+
* fails leaves those targets unknown rather than throwing — a link whose target
|
|
29
|
+
* cannot be resolved renders unlinked, exactly as a deleted target does, and one
|
|
30
|
+
* failing catalog read must not take a whole page down.
|
|
31
|
+
*/
|
|
32
|
+
export declare function collectLinkTargets(client: EldraClient, keys: readonly string[], locale?: string, sources?: LinkTargetSources): Promise<Map<string, LinkTargetInfo>>;
|