@weaverse/next 0.1.0-alpha.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +155 -73
  2. package/dist/{chunk-6PMZ77RA.mjs → chunk-LH6AAEIK.mjs} +115 -8
  3. package/dist/chunk-LH6AAEIK.mjs.map +1 -0
  4. package/dist/index.js +180 -32
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +86 -47
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/server.js +321 -91
  9. package/dist/server.js.map +1 -1
  10. package/dist/server.mjs +205 -28
  11. package/dist/server.mjs.map +1 -1
  12. package/dist/types/client.d.ts +16 -0
  13. package/dist/types/client.d.ts.map +1 -0
  14. package/dist/types/hooks.d.ts +20 -0
  15. package/dist/types/hooks.d.ts.map +1 -0
  16. package/dist/types/index.d.ts +34 -0
  17. package/dist/types/index.d.ts.map +1 -0
  18. package/dist/types/item.d.ts +60 -0
  19. package/dist/types/item.d.ts.map +1 -0
  20. package/dist/types/loader.d.ts +25 -0
  21. package/dist/types/loader.d.ts.map +1 -0
  22. package/dist/types/main.d.ts +11 -0
  23. package/dist/types/main.d.ts.map +1 -0
  24. package/dist/types/provider.d.ts +82 -0
  25. package/dist/types/provider.d.ts.map +1 -0
  26. package/dist/types/registry.d.ts +15 -0
  27. package/dist/types/registry.d.ts.map +1 -0
  28. package/dist/types/renderer.d.ts +21 -0
  29. package/dist/types/renderer.d.ts.map +1 -0
  30. package/dist/types/request-info.d.ts +19 -0
  31. package/dist/types/request-info.d.ts.map +1 -0
  32. package/dist/types/revalidate-item.d.ts +82 -0
  33. package/dist/types/revalidate-item.d.ts.map +1 -0
  34. package/dist/types/root-provider.d.ts +62 -0
  35. package/dist/types/root-provider.d.ts.map +1 -0
  36. package/dist/types/runtime.d.ts +113 -0
  37. package/dist/types/runtime.d.ts.map +1 -0
  38. package/dist/types/seo.d.ts +55 -0
  39. package/dist/types/seo.d.ts.map +1 -0
  40. package/dist/types/server/configs.d.ts +27 -0
  41. package/dist/types/server/configs.d.ts.map +1 -0
  42. package/dist/types/server/normalize-page-url.d.ts +19 -0
  43. package/dist/types/server/normalize-page-url.d.ts.map +1 -0
  44. package/dist/types/server/revalidate-handler.d.ts +55 -0
  45. package/dist/types/server/revalidate-handler.d.ts.map +1 -0
  46. package/dist/types/server/server-client.d.ts +21 -0
  47. package/dist/types/server/server-client.d.ts.map +1 -0
  48. package/dist/types/server.d.ts +19 -0
  49. package/dist/types/server.d.ts.map +1 -0
  50. package/dist/types/studio-bridge.d.ts +16 -0
  51. package/dist/types/studio-bridge.d.ts.map +1 -0
  52. package/dist/types/studio-connect.d.ts +27 -0
  53. package/dist/types/studio-connect.d.ts.map +1 -0
  54. package/dist/types/studio-router.d.ts +50 -0
  55. package/dist/types/studio-router.d.ts.map +1 -0
  56. package/dist/types/studio-script-src.d.ts +12 -0
  57. package/dist/types/studio-script-src.d.ts.map +1 -0
  58. package/dist/types/theme-settings-store.d.ts +18 -0
  59. package/dist/types/theme-settings-store.d.ts.map +1 -0
  60. package/dist/types/translation-context.d.ts +156 -0
  61. package/dist/types/translation-context.d.ts.map +1 -0
  62. package/dist/types/translation-store.d.ts +44 -0
  63. package/dist/types/translation-store.d.ts.map +1 -0
  64. package/dist/types/types.d.ts +665 -0
  65. package/dist/types/types.d.ts.map +1 -0
  66. package/dist/types/use-weaverse-next-studio.d.ts +34 -0
  67. package/dist/types/use-weaverse-next-studio.d.ts.map +1 -0
  68. package/dist/types/utils.d.ts +12 -0
  69. package/dist/types/utils.d.ts.map +1 -0
  70. package/package.json +12 -8
  71. package/src/client.ts +102 -0
  72. package/src/hooks.ts +50 -0
  73. package/src/index.ts +153 -0
  74. package/src/item.ts +167 -0
  75. package/src/loader.ts +135 -0
  76. package/src/main.tsx +45 -0
  77. package/src/provider.tsx +225 -0
  78. package/src/registry.ts +63 -0
  79. package/src/renderer.tsx +100 -0
  80. package/src/request-info.ts +124 -0
  81. package/src/revalidate-item.ts +211 -0
  82. package/src/root-provider.tsx +182 -0
  83. package/src/runtime.ts +555 -0
  84. package/src/seo.ts +157 -0
  85. package/src/server/configs.ts +178 -0
  86. package/src/server/normalize-page-url.ts +73 -0
  87. package/src/server/revalidate-handler.ts +357 -0
  88. package/src/server/server-client.ts +728 -0
  89. package/src/server.ts +67 -0
  90. package/src/studio-bridge.tsx +82 -0
  91. package/src/studio-connect.tsx +88 -0
  92. package/src/studio-router.ts +85 -0
  93. package/src/studio-script-src.ts +128 -0
  94. package/src/theme-settings-store.ts +47 -0
  95. package/src/translation-context.tsx +299 -0
  96. package/src/translation-store.ts +69 -0
  97. package/src/types.ts +753 -0
  98. package/src/use-weaverse-next-studio.tsx +106 -0
  99. package/src/utils.ts +37 -0
  100. package/dist/chunk-6PMZ77RA.mjs.map +0 -1
  101. package/dist/index.d.mts +0 -580
  102. package/dist/index.d.ts +0 -580
  103. package/dist/seo-Dos9t22_.d.mts +0 -477
  104. package/dist/seo-Dos9t22_.d.ts +0 -477
  105. package/dist/server.d.mts +0 -99
  106. package/dist/server.d.ts +0 -99
@@ -0,0 +1,299 @@
1
+ 'use client'
2
+
3
+ import { useSafeExternalStore } from '@weaverse/react'
4
+ import { createContext, type ReactNode, useContext, useMemo } from 'react'
5
+ import type { TranslationStore } from './translation-store'
6
+
7
+ /**
8
+ * Traverse a nested object by dot-path.
9
+ *
10
+ * @example
11
+ * getNestedKey({ cart: { title: 'Cart' } }, 'cart.title') // => 'Cart'
12
+ * getNestedKey({}, 'cart.title', 'fallback') // => 'fallback'
13
+ */
14
+ export function getNestedKey(
15
+ obj: Record<string, unknown>,
16
+ path: string,
17
+ fallback?: string
18
+ ): string | undefined {
19
+ let keys = path.split('.')
20
+ let current: unknown = obj
21
+
22
+ for (let key of keys) {
23
+ if (current == null || typeof current !== 'object') {
24
+ return fallback
25
+ }
26
+ current = (current as Record<string, unknown>)[key]
27
+ }
28
+
29
+ if (typeof current === 'string') {
30
+ return current
31
+ }
32
+
33
+ return fallback
34
+ }
35
+
36
+ /**
37
+ * Replace `{{variable}}` patterns in a string with provided values.
38
+ *
39
+ * @example
40
+ * interpolate('Hello {{name}}!', { name: 'World' }) // => 'Hello World!'
41
+ * interpolate('-{{percentage}}% Off', { percentage: 15 }) // => '-15% Off'
42
+ */
43
+ export function interpolate(
44
+ template: string,
45
+ variables?: Record<string, string | number>
46
+ ): string {
47
+ if (!variables) {
48
+ return template
49
+ }
50
+
51
+ return template.replace(/\{\{(\w+)\}\}/g, (match, key: string) => {
52
+ let value = variables[key]
53
+ return value === undefined ? match : String(value)
54
+ })
55
+ }
56
+
57
+ // ---------------------------------------------------------------------------
58
+ // Context & Provider
59
+ // ---------------------------------------------------------------------------
60
+
61
+ /**
62
+ * Signature for a translation function.
63
+ */
64
+ export type TranslateFunction = (
65
+ key: string,
66
+ variables?: Record<string, string | number>
67
+ ) => string
68
+
69
+ /** Translation function and live override stores exposed by `useTranslation`. */
70
+ export interface TranslationValue {
71
+ /** Active-locale translated keys loaded from the storefront API. */
72
+ merchantOverrides: Record<string, unknown> | null
73
+ /**
74
+ * Translate a key with optional variable interpolation.
75
+ *
76
+ * Priority chain (highest wins):
77
+ * 1. External `t` (passed into `WeaverseNextRootProvider` / `TranslationProvider`)
78
+ * 2. Design overrides (live Builder edits from `TranslationStore`)
79
+ * 3. `merchantOverrides` (DB/API overrides for the current locale)
80
+ * 4. `staticContent` (theme default JSON)
81
+ * 5. The key itself (fallback)
82
+ *
83
+ * @example
84
+ * t('cart.title') // => "Cart"
85
+ * t('badge.sale', { percentage: 15 }) // => "-15% Off"
86
+ */
87
+ t: TranslateFunction
88
+ /**
89
+ * Legacy name for the live design-mode translation store.
90
+ *
91
+ * @deprecated Migrate to {@link TranslationValue.translationStore}. This
92
+ * alias points at the same store instance.
93
+ */
94
+ themeTextStore: TranslationStore | null
95
+ /** Exposed so the runtime/Studio bridge can attach it to `internal`. */
96
+ translationStore: TranslationStore | null
97
+ }
98
+
99
+ const TranslationContext = createContext<TranslationValue | null>(null)
100
+ TranslationContext.displayName = 'WeaverseNextTranslationContext'
101
+
102
+ /** Sources and content rendered by {@link TranslationProvider}. */
103
+ export interface TranslationProviderProps {
104
+ /** React subtree that can call `useTranslation`. */
105
+ children: ReactNode
106
+ /** Active-locale static-text overrides loaded from Weaverse. */
107
+ merchantOverrides?: Record<string, unknown>
108
+ /** Default-locale static content from the theme schema. */
109
+ staticContent: Record<string, unknown>
110
+ /**
111
+ * Optional external translation function.
112
+ * When provided it becomes the **highest-priority** source:
113
+ * if it returns a non-empty string that differs from the raw key,
114
+ * that value is used; otherwise the internal resolution chain continues.
115
+ */
116
+ t?: TranslateFunction
117
+ /**
118
+ * Legacy prop name for the live translation store.
119
+ *
120
+ * @deprecated Migrate to {@link TranslationProviderProps.translationStore}.
121
+ * Still accepted for backward compatibility; ignored when `translationStore`
122
+ * is also provided.
123
+ */
124
+ themeTextStore?: TranslationStore | null
125
+ /** Optional store for live design-mode text overrides from the builder. */
126
+ translationStore?: TranslationStore | null
127
+ }
128
+
129
+ /** Translation sources accepted by {@link createTranslate}. */
130
+ export interface CreateTranslateSources {
131
+ /** Unsaved live overrides received from Builder design mode. */
132
+ designOverrides?: Record<string, string>
133
+ /** Host application's highest-priority translation function. */
134
+ externalT?: TranslateFunction
135
+ /** Active-locale static-text overrides loaded from Weaverse. */
136
+ merchantOverrides?: Record<string, unknown>
137
+ /** Default-locale static content from the theme schema. */
138
+ staticContent: Record<string, unknown>
139
+ }
140
+
141
+ /**
142
+ * Build the `t()` translation function over a fixed set of sources.
143
+ *
144
+ * Resolution order, highest priority first:
145
+ * 1. `externalT` – host i18n (e.g. Shopify), unless it echoes the key
146
+ * 2. `designOverrides` – live design-mode edits, flat dot-path keys
147
+ * 3. `merchantOverrides` – locale overrides from the API, nested
148
+ * 4. `staticContent` – the theme's default locale JSON
149
+ * 5. the key itself
150
+ *
151
+ * Design overrides are looked up per-key (own properties only) rather than
152
+ * merged into `merchantOverrides`: a flat key like `cart.title` must override
153
+ * exactly that leaf without wiping sibling keys (`cart.subtitle`), and a
154
+ * per-key own-property lookup never builds nested objects from
155
+ * merchant-controlled keys, so it cannot pollute `Object.prototype`.
156
+ */
157
+ export function createTranslate(
158
+ sources: CreateTranslateSources
159
+ ): TranslateFunction {
160
+ let { staticContent, merchantOverrides, designOverrides, externalT } = sources
161
+ return (key, variables) => {
162
+ // Priority 1: external t (e.g. Shopify i18n, third-party library).
163
+ if (externalT) {
164
+ let external = externalT(key, variables)
165
+ // Accept the external value unless it echoed the key unchanged.
166
+ if (external && external !== key) {
167
+ return external
168
+ }
169
+ }
170
+
171
+ // Priority 2: live design-mode override (flat, keyed by exact dot-path,
172
+ // own properties only so inherited keys never resolve to functions).
173
+ let design =
174
+ designOverrides && Object.hasOwn(designOverrides, key)
175
+ ? designOverrides[key]
176
+ : undefined
177
+ // Priority 3: merchant overrides (nested). Priority 4: static content.
178
+ let override = merchantOverrides
179
+ ? getNestedKey(merchantOverrides, key)
180
+ : undefined
181
+ let staticValue = getNestedKey(staticContent, key)
182
+
183
+ // Priority 5: the key itself as the final fallback.
184
+ let raw = design ?? override ?? staticValue ?? key
185
+ return interpolate(raw, variables)
186
+ }
187
+ }
188
+
189
+ // No-op fallback for useSafeExternalStore when store is null (production).
190
+ // Module-level constants — stable references, zero allocation per render.
191
+ const EMPTY: Record<string, string> = {}
192
+ const NOOP_SUBSCRIBE = () => () => {
193
+ // No-op unsubscribe for production/static renders without a translation store.
194
+ }
195
+ const NOOP_SNAPSHOT = () => EMPTY
196
+
197
+ /**
198
+ * Provider that wraps the app to expose the `t()` function.
199
+ *
200
+ * - `staticContent` – the theme's default locale JSON (from themeSchema.i18n)
201
+ * - `merchantOverrides` – locale-specific overrides fetched from the API
202
+ * - `translationStore` – optional store for live design-mode text overrides
203
+ * (the deprecated `themeTextStore` prop is accepted as a fallback)
204
+ */
205
+ export function TranslationProvider({
206
+ staticContent,
207
+ merchantOverrides,
208
+ t: externalT,
209
+ translationStore,
210
+ themeTextStore,
211
+ children,
212
+ }: TranslationProviderProps) {
213
+ // Canonical store, falling back to the deprecated `themeTextStore` prop.
214
+ let store = translationStore ?? themeTextStore ?? null
215
+
216
+ // Subscribe to design-mode overrides. No-op when store is null (production).
217
+ let designOverrides = useSafeExternalStore(
218
+ store?.subscribe ?? NOOP_SUBSCRIBE,
219
+ store?.getSnapshot ?? NOOP_SNAPSHOT,
220
+ store?.getServerSnapshot ?? NOOP_SNAPSHOT
221
+ )
222
+
223
+ let value = useMemo<TranslationValue>(
224
+ () => ({
225
+ t: createTranslate({
226
+ staticContent,
227
+ merchantOverrides,
228
+ designOverrides,
229
+ externalT,
230
+ }),
231
+ translationStore: store,
232
+ // Deprecated alias — same instance, kept for backward compatibility.
233
+ themeTextStore: store,
234
+ merchantOverrides: merchantOverrides ?? null,
235
+ }),
236
+ [staticContent, merchantOverrides, designOverrides, externalT, store]
237
+ )
238
+
239
+ return (
240
+ <TranslationContext.Provider value={value}>
241
+ {children}
242
+ </TranslationContext.Provider>
243
+ )
244
+ }
245
+
246
+ /**
247
+ * Hook to access the translation `t()` function.
248
+ *
249
+ * Must be used inside a component wrapped by `WeaverseNextRootProvider` or
250
+ * `TranslationProvider`.
251
+ *
252
+ * @example
253
+ * ```tsx
254
+ * function CartTitle() {
255
+ * let { t } = useTranslation()
256
+ * return <h2>{t('cart.title')}</h2>
257
+ * }
258
+ * ```
259
+ */
260
+ export function useTranslation(): TranslationValue {
261
+ let ctx = useContext(TranslationContext)
262
+ if (!ctx) {
263
+ throw new Error(
264
+ 'useTranslation must be used within <TranslationProvider>. Wrap your app with <WeaverseNextRootProvider>.'
265
+ )
266
+ }
267
+ return ctx
268
+ }
269
+
270
+ // ---------------------------------------------------------------------------
271
+ // Deprecated aliases (backward compatibility — kept exported indefinitely)
272
+ // ---------------------------------------------------------------------------
273
+
274
+ /**
275
+ * Legacy name for {@link TranslationProvider}.
276
+ *
277
+ * @deprecated Replace `ThemeTextProvider` with {@link TranslationProvider};
278
+ * the props and runtime behavior are unchanged.
279
+ */
280
+ export const ThemeTextProvider = TranslationProvider
281
+ /**
282
+ * Legacy name for {@link useTranslation}.
283
+ *
284
+ * @deprecated Replace `useThemeText` with {@link useTranslation}; the return
285
+ * value is unchanged.
286
+ */
287
+ export const useThemeText = useTranslation
288
+ /**
289
+ * Legacy name for {@link TranslationValue}.
290
+ *
291
+ * @deprecated Import {@link TranslationValue} instead.
292
+ */
293
+ export type ThemeTextValue = TranslationValue
294
+ /**
295
+ * Legacy name for {@link TranslationProviderProps}.
296
+ *
297
+ * @deprecated Import {@link TranslationProviderProps} instead.
298
+ */
299
+ export type ThemeTextProviderProps = TranslationProviderProps
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Minimal mutable store for design-mode static text overrides.
3
+ *
4
+ * `useSyncExternalStore`-compatible. Passed into {@link TranslationProvider}
5
+ * (from `./translation-context`) which subscribes internally and layers
6
+ * overrides on top of `merchantOverrides`/`staticContent`. The Studio bridge
7
+ * calls `updateOverrides()` via `runtime.internal.translationStore` (also
8
+ * exposed as the deprecated `runtime.internal.themeTextStore` alias) so live
9
+ * static-text edits reach the runtime — identical surface to Hydrogen's store,
10
+ * so Builder's existing static-text RPC works with `@weaverse/next` unchanged.
11
+ *
12
+ * Shape: flat `Record<string, string>` — dot-notation keys map to values
13
+ * (e.g. "cart.title" → "Shopping Cart").
14
+ */
15
+ export class TranslationStore {
16
+ /** Current flat translation overrides keyed by dot path. */
17
+ overrides: Record<string, string> = {}
18
+ private readonly listeners: Set<() => void> = new Set()
19
+
20
+ /** Replace every override and notify subscribers. */
21
+ setOverrides = (overrides: Record<string, string>) => {
22
+ this.overrides = { ...overrides }
23
+ this.emit()
24
+ }
25
+
26
+ /** Merge changed overrides into the current values and notify subscribers. */
27
+ updateOverrides = (newOverrides: Record<string, string>) => {
28
+ this.overrides = { ...this.overrides, ...newOverrides }
29
+ this.emit()
30
+ }
31
+
32
+ /** Return the current client-side override snapshot. */
33
+ getSnapshot = () => this.overrides
34
+
35
+ /** Return the override snapshot used during server rendering. */
36
+ getServerSnapshot = () => this.overrides
37
+
38
+ /** Subscribe to override changes and return an unsubscribe function. */
39
+ subscribe = (callback: () => void) => {
40
+ this.listeners.add(callback)
41
+ return () => {
42
+ this.listeners.delete(callback)
43
+ }
44
+ }
45
+
46
+ private readonly emit = () => {
47
+ for (const listener of Array.from(this.listeners)) {
48
+ try {
49
+ listener()
50
+ } catch (error) {
51
+ console.error('TranslationStore: Listener error', error)
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Legacy constructor name for {@link TranslationStore}.
59
+ *
60
+ * @deprecated Replace `ThemeTextStore` with {@link TranslationStore}; the
61
+ * store contract and runtime behavior are unchanged.
62
+ */
63
+ export const ThemeTextStore = TranslationStore
64
+ /**
65
+ * Legacy type name for {@link TranslationStore}.
66
+ *
67
+ * @deprecated Import {@link TranslationStore} instead.
68
+ */
69
+ export type ThemeTextStore = TranslationStore