@thorprovider/create-thor-app 4.1.4 → 4.1.7
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/package.json +1 -1
- package/templates/default/app/layout.tsx +25 -2
- package/templates/default/components/providers/app-providers.tsx +26 -11
- package/templates/default/components/providers/cart-provider.tsx +9 -8
- package/templates/default/components/providers/commerce-provider.tsx +28 -6
- package/templates/default/components/providers/region-provider.tsx +6 -4
- package/templates/default/components/providers/theme-provider.tsx +21 -0
- package/templates/default/lib/commerce-client.ts +87 -0
- package/templates/default/lib/commerce.ts +2 -0
- package/templates/default/package.json +4 -2
package/package.json
CHANGED
|
@@ -2,6 +2,8 @@ import React from 'react'
|
|
|
2
2
|
import type { Metadata } from 'next'
|
|
3
3
|
import { Inter } from 'next/font/google'
|
|
4
4
|
import { AppProviders } from '@/components/providers/app-providers'
|
|
5
|
+
import type { CommerceClientConfig } from '@/lib/commerce-client'
|
|
6
|
+
import { getConfiguredProvider, shouldUseMockProvider } from '@/lib/env'
|
|
5
7
|
import './globals.css'
|
|
6
8
|
|
|
7
9
|
const inter = Inter({ subsets: ['latin'], variable: '--font-inter' })
|
|
@@ -11,15 +13,36 @@ export const metadata: Metadata = {
|
|
|
11
13
|
description: 'An e-commerce store built with Thor Commerce.',
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Root Layout — async Server Component.
|
|
18
|
+
*
|
|
19
|
+
* Reads NEXT_PUBLIC_* env vars at request time (server-side), builds a
|
|
20
|
+
* serializable CommerceClientConfig, and injects it into the client
|
|
21
|
+
* provider tree. This prevents build-time inlining of undefined values
|
|
22
|
+
* in V0's preview sandbox.
|
|
23
|
+
*/
|
|
24
|
+
export default async function RootLayout({
|
|
15
25
|
children,
|
|
16
26
|
}: Readonly<{
|
|
17
27
|
children: React.ReactNode
|
|
18
28
|
}>) {
|
|
29
|
+
const rawProvider = getConfiguredProvider()
|
|
30
|
+
const isMock = shouldUseMockProvider()
|
|
31
|
+
|
|
32
|
+
const commerceConfig: CommerceClientConfig = {
|
|
33
|
+
provider: rawProvider === 'custom' ? 'mock' : rawProvider,
|
|
34
|
+
baseUrl: process.env.NEXT_PUBLIC_COMMERCE_API_URL ?? '',
|
|
35
|
+
apiKey: process.env.NEXT_PUBLIC_COMMERCE_API_KEY ?? '',
|
|
36
|
+
currencyCode: process.env.NEXT_PUBLIC_DEFAULT_CURRENCY ?? 'USD',
|
|
37
|
+
salesChannelId: process.env.NEXT_PUBLIC_SALES_CHANNEL_ID,
|
|
38
|
+
siteUrl: process.env.NEXT_PUBLIC_SITE_URL,
|
|
39
|
+
isMock,
|
|
40
|
+
}
|
|
41
|
+
|
|
19
42
|
return (
|
|
20
43
|
<html lang="en" suppressHydrationWarning>
|
|
21
44
|
<body className={`${inter.variable} font-sans antialiased`}>
|
|
22
|
-
<AppProviders>
|
|
45
|
+
<AppProviders commerceConfig={commerceConfig}>
|
|
23
46
|
{children}
|
|
24
47
|
</AppProviders>
|
|
25
48
|
</body>
|
|
@@ -4,25 +4,40 @@
|
|
|
4
4
|
* Application Providers — Composites all context providers.
|
|
5
5
|
*
|
|
6
6
|
* Provider Hierarchy (ordered by dependencies — FROZEN, do not reorder):
|
|
7
|
-
* 1.
|
|
8
|
-
* 2.
|
|
9
|
-
* 3.
|
|
7
|
+
* 1. ThemeProvider — Dark/light mode via .dark class (no dependencies)
|
|
8
|
+
* 2. CommerceProvider — Commerce backend access (no dependencies)
|
|
9
|
+
* 3. RegionProvider — Region/currency context (depends on commerce)
|
|
10
|
+
* 4. CartProvider — Shopping cart (depends on commerce)
|
|
11
|
+
*
|
|
12
|
+
* Receives `commerceConfig` from the async RSC layout (app/layout.tsx).
|
|
13
|
+
* The config is read from process.env server-side and forwarded here as
|
|
14
|
+
* a serializable prop, avoiding build-time inlining of NEXT_PUBLIC_* vars.
|
|
10
15
|
*/
|
|
11
16
|
|
|
12
17
|
import React from 'react'
|
|
18
|
+
import { ThemeProvider } from './theme-provider'
|
|
13
19
|
import { CommerceProvider } from './commerce-provider'
|
|
14
20
|
import { RegionProvider } from './region-provider'
|
|
15
21
|
import { CartProvider } from './cart-provider'
|
|
22
|
+
import type { CommerceClientConfig } from '@/lib/commerce-client'
|
|
16
23
|
import type { ReactNode } from 'react'
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
interface AppProvidersProps {
|
|
26
|
+
children: ReactNode
|
|
27
|
+
commerceConfig: CommerceClientConfig
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function AppProviders({ children, commerceConfig }: AppProvidersProps) {
|
|
19
31
|
return (
|
|
20
|
-
<
|
|
21
|
-
<
|
|
22
|
-
<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
|
|
33
|
+
<CommerceProvider config={commerceConfig}>
|
|
34
|
+
<RegionProvider>
|
|
35
|
+
<CartProvider>
|
|
36
|
+
{children}
|
|
37
|
+
</CartProvider>
|
|
38
|
+
</RegionProvider>
|
|
39
|
+
</CommerceProvider>
|
|
40
|
+
</ThemeProvider>
|
|
27
41
|
)
|
|
28
42
|
}
|
|
43
|
+
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react'
|
|
21
|
-
import {
|
|
21
|
+
import { useCommerce } from './commerce-provider'
|
|
22
22
|
import type { ReactNode } from 'react'
|
|
23
23
|
import type { Cart, CartItem, CartLineInput, CartLineUpdate } from '@thorprovider/types'
|
|
24
24
|
|
|
@@ -40,6 +40,7 @@ const CART_ID_KEY = 'thor-cart-id'
|
|
|
40
40
|
const CART_PROVIDER_KEY = 'thor-cart-provider'
|
|
41
41
|
|
|
42
42
|
export function CartProvider({ children }: { children: ReactNode }) {
|
|
43
|
+
const { commerce, isMock } = useCommerce()
|
|
43
44
|
const [cart, setCart] = useState<Cart | null>(null)
|
|
44
45
|
const [isLoading, setIsLoading] = useState(true)
|
|
45
46
|
const [error, setError] = useState<Error | null>(null)
|
|
@@ -53,7 +54,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
53
54
|
;(async () => {
|
|
54
55
|
try {
|
|
55
56
|
setIsLoading(true)
|
|
56
|
-
const currentProvider =
|
|
57
|
+
const currentProvider = isMock ? 'mock' : 'live'
|
|
57
58
|
const storedProvider = localStorage.getItem(CART_PROVIDER_KEY)
|
|
58
59
|
|
|
59
60
|
if (storedProvider && storedProvider !== currentProvider) {
|
|
@@ -77,16 +78,16 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
77
78
|
}
|
|
78
79
|
})()
|
|
79
80
|
return () => { cancelled = true }
|
|
80
|
-
}, [])
|
|
81
|
+
}, [commerce, isMock])
|
|
81
82
|
|
|
82
83
|
const ensureCart = useCallback(async (): Promise<Cart> => {
|
|
83
84
|
if (cart) return cart
|
|
84
85
|
const newCart = await commerce.createCart()
|
|
85
86
|
setCart(newCart)
|
|
86
87
|
if (newCart.id) localStorage.setItem(CART_ID_KEY, newCart.id)
|
|
87
|
-
localStorage.setItem(CART_PROVIDER_KEY,
|
|
88
|
+
localStorage.setItem(CART_PROVIDER_KEY, isMock ? 'mock' : 'live')
|
|
88
89
|
return newCart
|
|
89
|
-
}, [cart])
|
|
90
|
+
}, [cart, commerce, isMock])
|
|
90
91
|
|
|
91
92
|
const addItem = useCallback(async (variantId: string, quantity = 1) => {
|
|
92
93
|
setIsLoading(true)
|
|
@@ -102,7 +103,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
102
103
|
} finally {
|
|
103
104
|
setIsLoading(false)
|
|
104
105
|
}
|
|
105
|
-
}, [ensureCart])
|
|
106
|
+
}, [ensureCart, commerce])
|
|
106
107
|
|
|
107
108
|
const removeItem = useCallback(async (lineItemId: string) => {
|
|
108
109
|
if (!cart?.id) return
|
|
@@ -116,7 +117,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
116
117
|
} finally {
|
|
117
118
|
setIsLoading(false)
|
|
118
119
|
}
|
|
119
|
-
}, [cart])
|
|
120
|
+
}, [cart, commerce])
|
|
120
121
|
|
|
121
122
|
const updateQuantity = useCallback(async (lineItemId: string, quantity: number) => {
|
|
122
123
|
if (!cart?.id) return
|
|
@@ -133,7 +134,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
133
134
|
} finally {
|
|
134
135
|
setIsLoading(false)
|
|
135
136
|
}
|
|
136
|
-
}, [cart])
|
|
137
|
+
}, [cart, commerce])
|
|
137
138
|
|
|
138
139
|
const clearCart = useCallback(() => {
|
|
139
140
|
setCart(null)
|
|
@@ -5,23 +5,45 @@
|
|
|
5
5
|
*
|
|
6
6
|
* V0: Use `useCommerce()` for client-side access to commerce methods.
|
|
7
7
|
* Server components should import `commerce` directly from `@/lib/commerce`.
|
|
8
|
+
*
|
|
9
|
+
* Receives `config` (CommerceClientConfig) from AppProviders, which is read
|
|
10
|
+
* from process.env server-side in layout.tsx and forwarded as a serializable
|
|
11
|
+
* prop — avoiding build-time inlining of undefined NEXT_PUBLIC_* values.
|
|
8
12
|
*/
|
|
9
13
|
|
|
10
|
-
import React, { createContext, useContext } from 'react'
|
|
11
|
-
import {
|
|
14
|
+
import React, { createContext, useContext, useState } from 'react'
|
|
15
|
+
import { createCommerceClient } from '@/lib/commerce-client'
|
|
16
|
+
import type { CommerceClientConfig, CommerceInstance } from '@/lib/commerce-client'
|
|
12
17
|
import type { ReactNode } from 'react'
|
|
13
18
|
|
|
14
19
|
export interface CommerceContextValue {
|
|
15
|
-
/** The commerce adapter instance
|
|
16
|
-
commerce:
|
|
20
|
+
/** The commerce adapter instance */
|
|
21
|
+
commerce: CommerceInstance
|
|
22
|
+
/** True when the server determined env vars are missing or provider is mock */
|
|
17
23
|
isMock: boolean
|
|
24
|
+
/** Default currency code from server config (e.g. "USD") */
|
|
25
|
+
defaultCurrency: string
|
|
18
26
|
}
|
|
19
27
|
|
|
20
28
|
const CommerceContext = createContext<CommerceContextValue | null>(null)
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
interface CommerceProviderProps {
|
|
31
|
+
children: ReactNode
|
|
32
|
+
config: CommerceClientConfig
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function CommerceProvider({ children, config }: CommerceProviderProps) {
|
|
36
|
+
// useState initializer runs once — creates the instance from server-injected config
|
|
37
|
+
const [commerceInstance] = useState<CommerceInstance>(() => createCommerceClient(config))
|
|
38
|
+
|
|
23
39
|
return (
|
|
24
|
-
<CommerceContext.Provider
|
|
40
|
+
<CommerceContext.Provider
|
|
41
|
+
value={{
|
|
42
|
+
commerce: commerceInstance,
|
|
43
|
+
isMock: config.isMock,
|
|
44
|
+
defaultCurrency: config.currencyCode || 'USD',
|
|
45
|
+
}}
|
|
46
|
+
>
|
|
25
47
|
{children}
|
|
26
48
|
</CommerceContext.Provider>
|
|
27
49
|
)
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react'
|
|
18
|
-
import {
|
|
18
|
+
import { useCommerce } from './commerce-provider'
|
|
19
19
|
import type { ReactNode } from 'react'
|
|
20
20
|
import type { Region } from '@thorprovider/types'
|
|
21
21
|
|
|
@@ -52,11 +52,13 @@ function normalizeRegion(raw: Record<string, unknown>): Region {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export function RegionProvider({ children }: { children: ReactNode }) {
|
|
55
|
+
const { commerce, isMock, defaultCurrency } = useCommerce()
|
|
55
56
|
const [regions, setRegions] = useState<Region[]>([])
|
|
56
57
|
const [region, setCurrentRegion] = useState<Region | null>(null)
|
|
57
58
|
const [isLoading, setIsLoading] = useState(true)
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
// Use region's currency when available; fall back to server-configured default
|
|
61
|
+
const currencyCode = region?.currency ?? defaultCurrency
|
|
60
62
|
|
|
61
63
|
useEffect(() => {
|
|
62
64
|
let cancelled = false
|
|
@@ -65,7 +67,7 @@ export function RegionProvider({ children }: { children: ReactNode }) {
|
|
|
65
67
|
setIsLoading(true)
|
|
66
68
|
if (!commerce.getRegions) return
|
|
67
69
|
|
|
68
|
-
const currentProvider =
|
|
70
|
+
const currentProvider = isMock ? 'mock' : 'live'
|
|
69
71
|
const storedProvider = localStorage.getItem(REGION_PROVIDER_KEY)
|
|
70
72
|
|
|
71
73
|
if (storedProvider && storedProvider !== currentProvider) {
|
|
@@ -91,7 +93,7 @@ export function RegionProvider({ children }: { children: ReactNode }) {
|
|
|
91
93
|
}
|
|
92
94
|
})()
|
|
93
95
|
return () => { cancelled = true }
|
|
94
|
-
}, [])
|
|
96
|
+
}, [commerce, isMock])
|
|
95
97
|
|
|
96
98
|
const setRegion = useCallback((regionId: string) => {
|
|
97
99
|
const found = regions.find((r) => r.id === regionId)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { ThemeProvider as NextThemesProvider } from 'next-themes'
|
|
5
|
+
import type { ThemeProviderProps } from 'next-themes'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ThemeProvider — thin wrapper over next-themes.
|
|
9
|
+
*
|
|
10
|
+
* FROZEN — do not modify.
|
|
11
|
+
* Already wired in app-providers.tsx with attribute="class" defaultTheme="system".
|
|
12
|
+
* Dark mode tokens are in styles/tokens.css under .dark { ... }.
|
|
13
|
+
*
|
|
14
|
+
* To toggle theme in a component:
|
|
15
|
+
* import { useTheme } from 'next-themes'
|
|
16
|
+
* const { theme, setTheme } = useTheme()
|
|
17
|
+
* setTheme('dark') | setTheme('light') | setTheme('system')
|
|
18
|
+
*/
|
|
19
|
+
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
|
|
20
|
+
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
|
|
21
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commerce Client Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates a commerce adapter instance from an explicit config object.
|
|
5
|
+
* Unlike lib/commerce.ts (server-only), this module is safe to import
|
|
6
|
+
* in Client Components — it never reads process.env directly.
|
|
7
|
+
*
|
|
8
|
+
* Usage (client components / providers):
|
|
9
|
+
* import { createCommerceClient } from '@/lib/commerce-client'
|
|
10
|
+
* const instance = createCommerceClient(config) // config injected by layout.tsx
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createProvider } from '@thorprovider/adapters'
|
|
14
|
+
import type { ProviderTypeString } from '@thorprovider/types'
|
|
15
|
+
import { isSupportedProviderType } from '@thorprovider/types'
|
|
16
|
+
|
|
17
|
+
/** Real-backend provider identifiers (must stay in sync with env.ts realProviderSchema) */
|
|
18
|
+
const REAL_BACKEND_PROVIDERS = new Set(['medusa', 'shopify', 'bigcommerce'])
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Serializable commerce config — safe to pass across the RSC boundary.
|
|
22
|
+
* All values are read server-side by layout.tsx and forwarded as props.
|
|
23
|
+
*/
|
|
24
|
+
export interface CommerceClientConfig {
|
|
25
|
+
/** Provider type resolved by the server (e.g. "medusa", "mock") */
|
|
26
|
+
provider: string
|
|
27
|
+
/** API base URL (empty string when isMock) */
|
|
28
|
+
baseUrl: string
|
|
29
|
+
/** Publishable API key (empty string when isMock) */
|
|
30
|
+
apiKey: string
|
|
31
|
+
/** ISO 4217 currency code, e.g. "USD" */
|
|
32
|
+
currencyCode: string
|
|
33
|
+
/** Optional Medusa sales channel ID */
|
|
34
|
+
salesChannelId?: string
|
|
35
|
+
/** Optional canonical site URL */
|
|
36
|
+
siteUrl?: string
|
|
37
|
+
/** True when the server determined env vars are missing or provider is mock */
|
|
38
|
+
isMock: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Commerce adapter instance type */
|
|
42
|
+
export type CommerceInstance = ReturnType<typeof createProvider>
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a commerce adapter instance from a serializable config.
|
|
46
|
+
* Call once per app lifecycle via useState initializer in CommerceProvider.
|
|
47
|
+
*/
|
|
48
|
+
export function createCommerceClient(config: CommerceClientConfig): CommerceInstance {
|
|
49
|
+
const resolvedProvider: ProviderTypeString = config.isMock
|
|
50
|
+
? 'mock'
|
|
51
|
+
: (config.provider as ProviderTypeString)
|
|
52
|
+
|
|
53
|
+
if (!isSupportedProviderType(resolvedProvider)) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`[commerce-client] Unsupported provider: "${resolvedProvider}". ` +
|
|
56
|
+
`Ensure NEXT_PUBLIC_COMMERCE_PROVIDER is set to a supported value.`
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const isReal = REAL_BACKEND_PROVIDERS.has(resolvedProvider)
|
|
61
|
+
|
|
62
|
+
return createProvider({
|
|
63
|
+
provider: resolvedProvider,
|
|
64
|
+
config: {
|
|
65
|
+
...(isReal
|
|
66
|
+
? {
|
|
67
|
+
baseUrl: config.baseUrl.replace(/\/+$/, ''),
|
|
68
|
+
publishableKey: config.apiKey,
|
|
69
|
+
}
|
|
70
|
+
: {}),
|
|
71
|
+
currencyCode: config.currencyCode || 'USD',
|
|
72
|
+
siteUrl: config.siteUrl,
|
|
73
|
+
debug: process.env.NODE_ENV === 'development',
|
|
74
|
+
retry: {
|
|
75
|
+
maxRetries: 3,
|
|
76
|
+
timeoutMs: 10000,
|
|
77
|
+
initialDelayMs: 300,
|
|
78
|
+
backoffMultiplier: 2,
|
|
79
|
+
maxDelayMs: 5000,
|
|
80
|
+
},
|
|
81
|
+
auth: {
|
|
82
|
+
method: 'jwt',
|
|
83
|
+
storage: 'localStorage',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
}
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* const products = await commerce.getProducts({ first: 20 })
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import 'server-only'
|
|
13
|
+
|
|
12
14
|
import { createProvider } from '@thorprovider/adapters'
|
|
13
15
|
import type { ProviderTypeString } from '@thorprovider/types'
|
|
14
16
|
import { isSupportedProviderType } from '@thorprovider/types'
|
|
@@ -11,12 +11,14 @@
|
|
|
11
11
|
"generate:agent-index": "node ../../scripts/generate-core-agents-index.cjs"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@thorprovider/adapters": "^4.2.
|
|
15
|
-
"@thorprovider/types": "^3.2.
|
|
14
|
+
"@thorprovider/adapters": "^4.2.1",
|
|
15
|
+
"@thorprovider/types": "^3.2.1",
|
|
16
16
|
"clsx": "^2.1.1",
|
|
17
17
|
"next": "16.0.10",
|
|
18
|
+
"next-themes": "^0.4.6",
|
|
18
19
|
"react": "19.2.0",
|
|
19
20
|
"react-dom": "19.2.0",
|
|
21
|
+
"server-only": "^0.0.1",
|
|
20
22
|
"tailwind-merge": "^3.3.1",
|
|
21
23
|
"zod": "^3.25.0"
|
|
22
24
|
},
|