@thorprovider/create-thor-app 4.0.1 → 4.1.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/README.md +25 -13
- package/dist/index.js +5 -4
- package/package.json +1 -1
- package/templates/default/.env.example +6 -5
- package/templates/default/README.md +13 -1
- package/templates/default/app/page.tsx +43 -2
- package/templates/default/components/providers/cart-provider.tsx +12 -0
- package/templates/default/components/providers/commerce-provider.tsx +2 -1
- package/templates/default/components/providers/region-provider.tsx +10 -0
- package/templates/default/docs/README.md +6 -0
- package/templates/default/docs/rules.md +1 -0
- package/templates/default/lib/commerce.ts +26 -10
- package/templates/default/lib/env.ts +55 -11
- package/templates/default/next-env.d.ts +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: create-thor-app
|
|
3
3
|
purpose: CLI scaffolding tool for generating new Thor Commerce applications from the core template
|
|
4
|
-
lastReviewed: "2026-
|
|
4
|
+
lastReviewed: "2026-05-01"
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# create-thor-app
|
|
8
8
|
|
|
9
9
|
CLI scaffolding tool to generate a new Thor Commerce application from the `@thorprovider/core` template.
|
|
10
10
|
|
|
11
|
+
The generated storefront is **mock-first by default**: if no live backend config is supplied, the app starts with the `mock` provider so layout and preview work can begin immediately.
|
|
12
|
+
|
|
11
13
|
Supports **dual-mode** operation:
|
|
12
14
|
- **Monorepo mode**: Copy live `packages/core`, run from within the monorepo
|
|
13
15
|
- **Standalone mode**: Use bundled template, run via `npx` from anywhere
|
|
@@ -102,6 +104,8 @@ When run outside the monorepo with `npx`, uses the pre-bundled template:
|
|
|
102
104
|
npx @thorprovider/create-thor-app my-store
|
|
103
105
|
```
|
|
104
106
|
|
|
107
|
+
If you do not pass backend flags, the generated app starts in mock mode by default.
|
|
108
|
+
|
|
105
109
|
**Differences from monorepo mode**:
|
|
106
110
|
- Template is frozen at build time (versions pre-baked into `templates/default/package.json`)
|
|
107
111
|
- No live dependency resolution — uses versions from the last build
|
|
@@ -114,9 +118,9 @@ npx @thorprovider/create-thor-app my-store
|
|
|
114
118
|
|------|-------------|---------|
|
|
115
119
|
| `--name <name>` | Project name (kebab-case) | Prompt if missing |
|
|
116
120
|
| `-o, --output <dir>` | Output directory | `./<name>` |
|
|
117
|
-
| `--commerce-provider <p>` | Commerce backend (`medusa`, `shopify`) → `.env` | `
|
|
118
|
-
| `--commerce-api-url <url>` | Backend API URL → `.env` | `http://localhost:9000` |
|
|
119
|
-
| `--commerce-api-key <key>` | Publishable API key → `.env` | placeholder |
|
|
121
|
+
| `--commerce-provider <p>` | Commerce backend (`mock`, `medusa`, `shopify`) → `.env` | `mock` |
|
|
122
|
+
| `--commerce-api-url <url>` | Backend API URL → `.env` (required only for real backends) | `http://localhost:9000` |
|
|
123
|
+
| `--commerce-api-key <key>` | Publishable API key → `.env` (required only for real backends) | placeholder |
|
|
120
124
|
| `--skip-git` | Skip git initialization | `false` |
|
|
121
125
|
| `--skip-install` | Skip yarn install | `false` |
|
|
122
126
|
| `--skip-remote` | Skip GitHub remote repo creation | `false` |
|
|
@@ -161,8 +165,13 @@ Next steps:
|
|
|
161
165
|
|
|
162
166
|
— or —
|
|
163
167
|
|
|
164
|
-
1.
|
|
165
|
-
|
|
168
|
+
1. Start in mock mode immediately:
|
|
169
|
+
`projects/my-store/.env` already defaults to `NEXT_PUBLIC_COMMERCE_PROVIDER=mock`
|
|
170
|
+
|
|
171
|
+
— or —
|
|
172
|
+
|
|
173
|
+
1. Configure your live backend:
|
|
174
|
+
Edit `projects/my-store/.env` with your commerce backend URL and API key
|
|
166
175
|
|
|
167
176
|
2. Add GitHub Actions secrets: ← (only when Vercel link succeeded)
|
|
168
177
|
https://github.com/<org>/my-store/settings/secrets/actions
|
|
@@ -183,7 +192,7 @@ Next steps:
|
|
|
183
192
|
**Generated structure**:
|
|
184
193
|
```
|
|
185
194
|
projects/my-store/
|
|
186
|
-
├── .env #
|
|
195
|
+
├── .env # Commerce config (mock by default, live backend optional)
|
|
187
196
|
├── .agents/
|
|
188
197
|
│ ├── AGENTS.md # Entry point for AI agents (V0, Claude Code, OpenCode)
|
|
189
198
|
│ ├── context/ # Architecture, types, API, patterns, rules
|
|
@@ -239,7 +248,10 @@ Use `--no-context` to skip generation.
|
|
|
239
248
|
|
|
240
249
|
### .env
|
|
241
250
|
|
|
242
|
-
Copied from `.env.example`.
|
|
251
|
+
Copied from `.env.example`.
|
|
252
|
+
|
|
253
|
+
- Default: starts in `mock` mode with no backend required
|
|
254
|
+
- Optional: add real provider + backend URL + publishable key to use live commerce data immediately
|
|
243
255
|
|
|
244
256
|
### .vercel/project.json (conditional)
|
|
245
257
|
|
|
@@ -299,7 +311,7 @@ yarn install
|
|
|
299
311
|
npx @thorprovider/create-thor-app my-store
|
|
300
312
|
|
|
301
313
|
cd my-store
|
|
302
|
-
#
|
|
314
|
+
# Optional: update .env with live backend settings, or keep mock mode
|
|
303
315
|
yarn dev
|
|
304
316
|
```
|
|
305
317
|
|
|
@@ -424,15 +436,15 @@ The package is structured for maintainability:
|
|
|
424
436
|
|
|
425
437
|
## Publishing
|
|
426
438
|
|
|
427
|
-
|
|
439
|
+
This package is published publicly to npm through the monorepo Changesets workflow:
|
|
428
440
|
|
|
429
441
|
```bash
|
|
430
442
|
yarn changeset
|
|
431
|
-
|
|
432
|
-
|
|
443
|
+
# open and merge your PR to main
|
|
444
|
+
# CI versions and publishes on push to main
|
|
433
445
|
```
|
|
434
446
|
|
|
435
|
-
|
|
447
|
+
Include `@thorprovider/create-thor-app` in a changeset whenever you change the CLI, its bundled template, or any publishable behavior consumers depend on.
|
|
436
448
|
|
|
437
449
|
---
|
|
438
450
|
|
package/dist/index.js
CHANGED
|
@@ -918,7 +918,7 @@ async function createApp(opts) {
|
|
|
918
918
|
});
|
|
919
919
|
log(" \u2713 package.json updated", c.green);
|
|
920
920
|
const envOverrides = {
|
|
921
|
-
|
|
921
|
+
NEXT_PUBLIC_COMMERCE_PROVIDER: commerceProvider ?? (commerceApiUrl && commerceApiKey ? "medusa" : "mock"),
|
|
922
922
|
...commerceApiUrl && { NEXT_PUBLIC_COMMERCE_API_URL: commerceApiUrl },
|
|
923
923
|
...commerceApiKey && { NEXT_PUBLIC_COMMERCE_API_KEY: commerceApiKey }
|
|
924
924
|
};
|
|
@@ -1016,8 +1016,9 @@ async function createApp(opts) {
|
|
|
1016
1016
|
${step++}. Backend already configured (via CLI flags) \u2713`, c.green);
|
|
1017
1017
|
} else {
|
|
1018
1018
|
log(`
|
|
1019
|
-
${step++}.
|
|
1020
|
-
log(
|
|
1019
|
+
${step++}. Optional: connect a real backend later`, c.cyan);
|
|
1020
|
+
log(" The scaffold starts in mock mode so you can design immediately.", c.yellow);
|
|
1021
|
+
log(` When ready, edit ${relPath}/.env and set NEXT_PUBLIC_COMMERCE_PROVIDER, NEXT_PUBLIC_COMMERCE_API_URL, and NEXT_PUBLIC_COMMERCE_API_KEY`, c.yellow);
|
|
1021
1022
|
}
|
|
1022
1023
|
if (!remoteCreated && !skipRemote) {
|
|
1023
1024
|
const vis = process.env.GITHUB_REPO_VISIBILITY === "public" ? "--public" : "--private";
|
|
@@ -1092,7 +1093,7 @@ if (nodeMajor < NODE_MIN_MAJOR || nodeMajor === NODE_MIN_MAJOR && nodeMinor < NO
|
|
|
1092
1093
|
);
|
|
1093
1094
|
process.exit(1);
|
|
1094
1095
|
}
|
|
1095
|
-
program.name("create-thor-app").description("Create a new Thor Commerce application").argument("[name]", "Project name (kebab-case)").option("--name <name>", "Project name (alternative to positional arg)").option("-o, --output <dir>", "Output directory (default: ./<name>)").option("--skip-git", "Skip git init").option("--skip-install", "Skip yarn install").option("--skip-remote", "Skip GitHub remote repo creation").option("--skip-vercel", "Skip Vercel project linking").option("--no-context", "Skip .agents/ directory generation").option("--no-validator", "Skip ai-validator.json injection (V0 Stop Hooks)").option("--commerce-provider <provider>", "Commerce backend provider (e.g., medusa, shopify)").option("--commerce-api-url <url>", "Commerce backend API URL").option("--commerce-api-key <key>", "Commerce publishable API key").action(async (positionalName, opts) => {
|
|
1096
|
+
program.name("create-thor-app").description("Create a new Thor Commerce application").argument("[name]", "Project name (kebab-case)").option("--name <name>", "Project name (alternative to positional arg)").option("-o, --output <dir>", "Output directory (default: ./<name>)").option("--skip-git", "Skip git init").option("--skip-install", "Skip yarn install").option("--skip-remote", "Skip GitHub remote repo creation").option("--skip-vercel", "Skip Vercel project linking").option("--no-context", "Skip .agents/ directory generation").option("--no-validator", "Skip ai-validator.json injection (V0 Stop Hooks)").option("--commerce-provider <provider>", "Commerce backend provider (e.g., mock, medusa, shopify)").option("--commerce-api-url <url>", "Commerce backend API URL").option("--commerce-api-key <key>", "Commerce publishable API key").action(async (positionalName, opts) => {
|
|
1096
1097
|
let rawName = positionalName || opts.name || "";
|
|
1097
1098
|
if (!rawName) {
|
|
1098
1099
|
rawName = await promptForName();
|
package/package.json
CHANGED
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|
# ============================================================================
|
|
6
6
|
|
|
7
7
|
# ---------------------------------------------------------------------------
|
|
8
|
-
# Commerce Backend (
|
|
8
|
+
# Commerce Backend (optional)
|
|
9
9
|
# ---------------------------------------------------------------------------
|
|
10
|
-
# Which commerce platform to use: medusa | shopify | bigcommerce | custom
|
|
11
|
-
|
|
10
|
+
# Which commerce platform to use: mock | medusa | shopify | bigcommerce | custom
|
|
11
|
+
# Use `mock` to design/layout the storefront before a real backend is connected.
|
|
12
|
+
NEXT_PUBLIC_COMMERCE_PROVIDER=mock
|
|
12
13
|
|
|
13
|
-
# Your commerce backend API URL
|
|
14
|
+
# Your commerce backend API URL (required for real backends, ignored in mock mode)
|
|
14
15
|
NEXT_PUBLIC_COMMERCE_API_URL=http://localhost:9000
|
|
15
16
|
|
|
16
|
-
# Publishable API key for your commerce backend
|
|
17
|
+
# Publishable API key for your commerce backend (required for real backends, ignored in mock mode)
|
|
17
18
|
NEXT_PUBLIC_COMMERCE_API_KEY=your-publishable-api-key
|
|
18
19
|
|
|
19
20
|
# ---------------------------------------------------------------------------
|
|
@@ -30,12 +30,23 @@ yarn dev
|
|
|
30
30
|
# → http://localhost:3000
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
Default runtime:
|
|
34
|
+
```
|
|
35
|
+
NEXT_PUBLIC_COMMERCE_PROVIDER=mock
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Live backend environment (only required when using a real provider):
|
|
34
39
|
```
|
|
35
40
|
NEXT_PUBLIC_COMMERCE_API_URL=https://your-backend.com
|
|
36
41
|
NEXT_PUBLIC_COMMERCE_API_KEY=pk_live_xxx
|
|
37
42
|
```
|
|
38
43
|
|
|
44
|
+
### Runtime modes
|
|
45
|
+
|
|
46
|
+
- `mock` or missing live-backend env vars → storefront uses fixture-backed mock commerce data
|
|
47
|
+
- real provider + API URL + API key → storefront uses the live backend immediately
|
|
48
|
+
- switching config later should not require rewriting app code that calls `commerce.*`, `useCart()`, or `useRegion()`
|
|
49
|
+
|
|
39
50
|
## Development with V0
|
|
40
51
|
|
|
41
52
|
1. Push this repo to GitHub
|
|
@@ -55,6 +66,7 @@ NEXT_PUBLIC_COMMERCE_API_KEY=pk_live_xxx
|
|
|
55
66
|
## What's Pre-Configured
|
|
56
67
|
|
|
57
68
|
- Commerce singleton with adapter-based backend access (retry built-in)
|
|
69
|
+
- Mock-first runtime that works without a live backend
|
|
58
70
|
- Shopping cart (persistent, backend-synced)
|
|
59
71
|
- Authentication (JWT)
|
|
60
72
|
- Dark mode + CSS token theming + backend-driven accent color
|
|
@@ -1,14 +1,55 @@
|
|
|
1
1
|
// V0 replaces this with the actual storefront homepage
|
|
2
2
|
import { commerce } from '@/lib/commerce'
|
|
3
|
+
import { formatPrice } from '@/lib/utils'
|
|
3
4
|
import type { Product } from '@thorprovider/types'
|
|
4
5
|
|
|
5
6
|
export default async function HomePage() {
|
|
6
7
|
const products: Product[] = await commerce.getProducts({ first: 8 })
|
|
8
|
+
const isMockMode = commerce.isMock === true
|
|
7
9
|
|
|
8
10
|
return (
|
|
9
11
|
<main className="container mx-auto px-4 py-8">
|
|
10
|
-
<
|
|
11
|
-
|
|
12
|
+
<div className="flex flex-col gap-3 md:flex-row md:items-end md:justify-between">
|
|
13
|
+
<div>
|
|
14
|
+
<h1 className="text-3xl font-bold">Store</h1>
|
|
15
|
+
<p className="text-muted-foreground">{products.length} products available</p>
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div className="rounded-full border px-4 py-2 text-sm text-muted-foreground">
|
|
19
|
+
{isMockMode
|
|
20
|
+
? 'Using mock catalog — connect a backend to switch to live commerce data.'
|
|
21
|
+
: 'Connected to live commerce data.'}
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
<div className="mt-8 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
|
26
|
+
{products.slice(0, 4).map((product) => {
|
|
27
|
+
const previewPrice = product.variants[0]?.price
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<article key={product.id} className="rounded-xl border bg-card p-4 text-card-foreground">
|
|
31
|
+
<div className="aspect-square overflow-hidden rounded-lg bg-muted">
|
|
32
|
+
{product.featuredImage ? (
|
|
33
|
+
<img
|
|
34
|
+
alt={product.featuredImage.altText || product.title}
|
|
35
|
+
className="h-full w-full object-cover"
|
|
36
|
+
src={product.featuredImage.url}
|
|
37
|
+
/>
|
|
38
|
+
) : null}
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
<div className="mt-4 space-y-1">
|
|
42
|
+
<h2 className="font-semibold">{product.title}</h2>
|
|
43
|
+
<p className="line-clamp-2 text-sm text-muted-foreground">{product.description}</p>
|
|
44
|
+
{previewPrice ? (
|
|
45
|
+
<p className="text-sm font-medium">{formatPrice(previewPrice)}</p>
|
|
46
|
+
) : null}
|
|
47
|
+
</div>
|
|
48
|
+
</article>
|
|
49
|
+
)
|
|
50
|
+
})}
|
|
51
|
+
</div>
|
|
52
|
+
|
|
12
53
|
{/* V0: Generate the actual storefront homepage here */}
|
|
13
54
|
</main>
|
|
14
55
|
)
|
|
@@ -37,6 +37,7 @@ export interface CartContextValue {
|
|
|
37
37
|
const CartContext = createContext<CartContextValue | null>(null)
|
|
38
38
|
|
|
39
39
|
const CART_ID_KEY = 'thor-cart-id'
|
|
40
|
+
const CART_PROVIDER_KEY = 'thor-cart-provider'
|
|
40
41
|
|
|
41
42
|
export function CartProvider({ children }: { children: ReactNode }) {
|
|
42
43
|
const [cart, setCart] = useState<Cart | null>(null)
|
|
@@ -52,6 +53,15 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
52
53
|
;(async () => {
|
|
53
54
|
try {
|
|
54
55
|
setIsLoading(true)
|
|
56
|
+
const currentProvider = commerce.isMock ? 'mock' : 'live'
|
|
57
|
+
const storedProvider = localStorage.getItem(CART_PROVIDER_KEY)
|
|
58
|
+
|
|
59
|
+
if (storedProvider && storedProvider !== currentProvider) {
|
|
60
|
+
localStorage.removeItem(CART_ID_KEY)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
localStorage.setItem(CART_PROVIDER_KEY, currentProvider)
|
|
64
|
+
|
|
55
65
|
const cartId = localStorage.getItem(CART_ID_KEY)
|
|
56
66
|
if (cartId) {
|
|
57
67
|
const loaded = await commerce.getCart(cartId)
|
|
@@ -74,6 +84,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
74
84
|
const newCart = await commerce.createCart()
|
|
75
85
|
setCart(newCart)
|
|
76
86
|
if (newCart.id) localStorage.setItem(CART_ID_KEY, newCart.id)
|
|
87
|
+
localStorage.setItem(CART_PROVIDER_KEY, commerce.isMock ? 'mock' : 'live')
|
|
77
88
|
return newCart
|
|
78
89
|
}, [cart])
|
|
79
90
|
|
|
@@ -127,6 +138,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|
|
127
138
|
const clearCart = useCallback(() => {
|
|
128
139
|
setCart(null)
|
|
129
140
|
localStorage.removeItem(CART_ID_KEY)
|
|
141
|
+
localStorage.removeItem(CART_PROVIDER_KEY)
|
|
130
142
|
}, [])
|
|
131
143
|
|
|
132
144
|
return (
|
|
@@ -14,13 +14,14 @@ import type { ReactNode } from 'react'
|
|
|
14
14
|
export interface CommerceContextValue {
|
|
15
15
|
/** The commerce adapter instance (same as `@/lib/commerce` export) */
|
|
16
16
|
commerce: typeof commerce
|
|
17
|
+
isMock: boolean
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
const CommerceContext = createContext<CommerceContextValue | null>(null)
|
|
20
21
|
|
|
21
22
|
export function CommerceProvider({ children }: { children: ReactNode }) {
|
|
22
23
|
return (
|
|
23
|
-
<CommerceContext.Provider value={{ commerce }}>
|
|
24
|
+
<CommerceContext.Provider value={{ commerce, isMock: commerce.isMock === true }}>
|
|
24
25
|
{children}
|
|
25
26
|
</CommerceContext.Provider>
|
|
26
27
|
)
|
|
@@ -30,6 +30,7 @@ export interface RegionContextValue {
|
|
|
30
30
|
const RegionContext = createContext<RegionContextValue | null>(null)
|
|
31
31
|
|
|
32
32
|
const REGION_KEY = 'thor-region-id'
|
|
33
|
+
const REGION_PROVIDER_KEY = 'thor-region-provider'
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Normalize adapter response to Region type.
|
|
@@ -64,6 +65,15 @@ export function RegionProvider({ children }: { children: ReactNode }) {
|
|
|
64
65
|
setIsLoading(true)
|
|
65
66
|
if (!commerce.getRegions) return
|
|
66
67
|
|
|
68
|
+
const currentProvider = commerce.isMock ? 'mock' : 'live'
|
|
69
|
+
const storedProvider = localStorage.getItem(REGION_PROVIDER_KEY)
|
|
70
|
+
|
|
71
|
+
if (storedProvider && storedProvider !== currentProvider) {
|
|
72
|
+
localStorage.removeItem(REGION_KEY)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
localStorage.setItem(REGION_PROVIDER_KEY, currentProvider)
|
|
76
|
+
|
|
67
77
|
const raw = await commerce.getRegions()
|
|
68
78
|
if (cancelled || !raw) return
|
|
69
79
|
|
|
@@ -8,6 +8,12 @@ lastReviewed: "2026-05-01"
|
|
|
8
8
|
|
|
9
9
|
Next.js 16 storefront scaffold for V0 AI code generation. Two npm deps: `@thorprovider/types` + `@thorprovider/adapters`.
|
|
10
10
|
|
|
11
|
+
## Runtime modes
|
|
12
|
+
|
|
13
|
+
- **Mock-first**: the template defaults to `mock` so previews work even before a backend exists.
|
|
14
|
+
- **Live immediately**: if real backend env vars are present, the same template runtime uses the live provider immediately.
|
|
15
|
+
- **Same app code**: pages/components should keep using `commerce`, `useCart()`, `useRegion()`, and `useCommerce()` without provider-specific branching.
|
|
16
|
+
|
|
11
17
|
## How It Works
|
|
12
18
|
|
|
13
19
|
V0 generates feature code (pages, components) on top of this frozen infrastructure. The template provides:
|
|
@@ -24,6 +24,7 @@ Non-negotiable constraints. Breaking these causes build failures, runtime crashe
|
|
|
24
24
|
| No hardcoded `#hex` / `rgb()` colors | CSS tokens enable dark mode + theming | Dark mode breaks |
|
|
25
25
|
| Use `next/link` for navigation | V0 is trained on Next.js primitives | Unnecessary abstraction |
|
|
26
26
|
| Use `next/image` for images | Optimized by default | Missing optimization |
|
|
27
|
+
| Do not fork UI into mock-vs-live implementations | Runtime already switches providers underneath the same interface | Drift, duplicated logic |
|
|
27
28
|
|
|
28
29
|
## Provider Chain (Frozen Order)
|
|
29
30
|
|
|
@@ -12,30 +12,47 @@
|
|
|
12
12
|
import { createProvider } from '@thorprovider/adapters'
|
|
13
13
|
import type { ProviderTypeString } from '@thorprovider/types'
|
|
14
14
|
import { isSupportedProviderType } from '@thorprovider/types'
|
|
15
|
-
import {
|
|
15
|
+
import { getConfiguredProvider, isRealBackendProvider, shouldUseMockProvider } from './env'
|
|
16
16
|
|
|
17
17
|
let _commerce: ReturnType<typeof createProvider> | undefined
|
|
18
18
|
|
|
19
19
|
function createCommerceInstance(): ReturnType<typeof createProvider> {
|
|
20
20
|
if (_commerce) return _commerce
|
|
21
21
|
|
|
22
|
-
const rawProviderType =
|
|
23
|
-
|
|
22
|
+
const rawProviderType = getConfiguredProvider()
|
|
23
|
+
|
|
24
|
+
if (rawProviderType === 'custom') {
|
|
25
|
+
throw new Error(
|
|
26
|
+
'[commerce] The "custom" provider is not supported by the V0-native template runtime. Configure a supported real backend or use mock mode.'
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const resolvedProviderType: ProviderTypeString = shouldUseMockProvider()
|
|
31
|
+
? 'mock'
|
|
32
|
+
: rawProviderType
|
|
33
|
+
|
|
34
|
+
if (!isSupportedProviderType(resolvedProviderType)) {
|
|
24
35
|
throw new Error(
|
|
25
|
-
`[commerce] Unsupported provider: "${
|
|
36
|
+
`[commerce] Unsupported provider: "${resolvedProviderType}". Set NEXT_PUBLIC_COMMERCE_PROVIDER to a supported value.`
|
|
26
37
|
)
|
|
27
38
|
}
|
|
28
39
|
|
|
29
|
-
const providerType: ProviderTypeString = rawProviderType
|
|
30
40
|
const baseUrl = (process.env.NEXT_PUBLIC_COMMERCE_API_URL ?? '').replace(/\/+$/, '')
|
|
31
41
|
const apiKey = process.env.NEXT_PUBLIC_COMMERCE_API_KEY ?? ''
|
|
42
|
+
const currencyCode = process.env.NEXT_PUBLIC_DEFAULT_CURRENCY || 'USD'
|
|
43
|
+
const isRealProvider = isRealBackendProvider(resolvedProviderType)
|
|
32
44
|
|
|
33
45
|
_commerce = createProvider({
|
|
34
|
-
provider:
|
|
46
|
+
provider: resolvedProviderType,
|
|
35
47
|
config: {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
...(isRealProvider
|
|
49
|
+
? {
|
|
50
|
+
baseUrl,
|
|
51
|
+
publishableKey: apiKey,
|
|
52
|
+
}
|
|
53
|
+
: {}),
|
|
54
|
+
currencyCode,
|
|
55
|
+
siteUrl: process.env.NEXT_PUBLIC_SITE_URL,
|
|
39
56
|
debug: process.env.NODE_ENV === 'development',
|
|
40
57
|
retry: {
|
|
41
58
|
maxRetries: 3,
|
|
@@ -60,7 +77,6 @@ function createCommerceInstance(): ReturnType<typeof createProvider> {
|
|
|
60
77
|
*/
|
|
61
78
|
export const commerce = new Proxy({} as ReturnType<typeof createProvider>, {
|
|
62
79
|
get(_target, prop: string | symbol) {
|
|
63
|
-
if (!isEnvConfigured()) return undefined
|
|
64
80
|
return createCommerceInstance()[prop as keyof ReturnType<typeof createProvider>]
|
|
65
81
|
},
|
|
66
82
|
})
|
|
@@ -7,11 +7,27 @@
|
|
|
7
7
|
|
|
8
8
|
import { z } from 'zod'
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const realProviderSchema = z.enum(['medusa', 'shopify', 'bigcommerce'])
|
|
11
|
+
|
|
12
|
+
const BaseEnvSchema = z.object({
|
|
11
13
|
NEXT_PUBLIC_COMMERCE_PROVIDER: z
|
|
12
|
-
.enum(['medusa', 'shopify', 'bigcommerce', 'custom'])
|
|
13
|
-
.default('
|
|
14
|
+
.enum(['mock', 'medusa', 'shopify', 'bigcommerce', 'custom'])
|
|
15
|
+
.default('mock'),
|
|
16
|
+
|
|
17
|
+
NEXT_PUBLIC_DEFAULT_CURRENCY: z
|
|
18
|
+
.string()
|
|
19
|
+
.default('USD'),
|
|
14
20
|
|
|
21
|
+
NEXT_PUBLIC_SALES_CHANNEL_ID: z
|
|
22
|
+
.string()
|
|
23
|
+
.optional(),
|
|
24
|
+
|
|
25
|
+
FRONTEND_WEBHOOK_SECRET: z
|
|
26
|
+
.string()
|
|
27
|
+
.optional(),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const RealBackendEnvSchema = BaseEnvSchema.extend({
|
|
15
31
|
NEXT_PUBLIC_COMMERCE_API_URL: z
|
|
16
32
|
.string()
|
|
17
33
|
.url(),
|
|
@@ -19,22 +35,44 @@ export const EnvSchema = z.object({
|
|
|
19
35
|
NEXT_PUBLIC_COMMERCE_API_KEY: z
|
|
20
36
|
.string()
|
|
21
37
|
.min(1),
|
|
38
|
+
})
|
|
22
39
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
.default('USD'),
|
|
26
|
-
|
|
27
|
-
NEXT_PUBLIC_SALES_CHANNEL_ID: z
|
|
40
|
+
const MockEnvSchema = BaseEnvSchema.extend({
|
|
41
|
+
NEXT_PUBLIC_COMMERCE_API_URL: z
|
|
28
42
|
.string()
|
|
29
43
|
.optional(),
|
|
30
44
|
|
|
31
|
-
|
|
45
|
+
NEXT_PUBLIC_COMMERCE_API_KEY: z
|
|
32
46
|
.string()
|
|
33
47
|
.optional(),
|
|
34
48
|
})
|
|
35
49
|
|
|
50
|
+
export const EnvSchema = MockEnvSchema
|
|
51
|
+
|
|
36
52
|
export type Env = z.infer<typeof EnvSchema>
|
|
37
53
|
|
|
54
|
+
export function getConfiguredProvider(): Env['NEXT_PUBLIC_COMMERCE_PROVIDER'] {
|
|
55
|
+
return BaseEnvSchema.shape.NEXT_PUBLIC_COMMERCE_PROVIDER.parse(
|
|
56
|
+
process.env.NEXT_PUBLIC_COMMERCE_PROVIDER
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function isRealBackendProvider(
|
|
61
|
+
provider: string | undefined
|
|
62
|
+
): provider is z.infer<typeof realProviderSchema> {
|
|
63
|
+
return realProviderSchema.safeParse(provider).success
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function shouldUseMockProvider(): boolean {
|
|
67
|
+
const provider = getConfiguredProvider()
|
|
68
|
+
|
|
69
|
+
if (provider === 'mock') {
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return !isRealBackendProvider(provider) || !isEnvConfigured()
|
|
74
|
+
}
|
|
75
|
+
|
|
38
76
|
/**
|
|
39
77
|
* Check whether required commerce env vars are present.
|
|
40
78
|
* Used to bail out gracefully during build/SSG.
|
|
@@ -55,14 +93,20 @@ export function isEnvConfigured(): boolean {
|
|
|
55
93
|
*/
|
|
56
94
|
export function getEnv(): Env {
|
|
57
95
|
try {
|
|
58
|
-
|
|
96
|
+
const rawEnv = {
|
|
59
97
|
NEXT_PUBLIC_COMMERCE_PROVIDER: process.env.NEXT_PUBLIC_COMMERCE_PROVIDER,
|
|
60
98
|
NEXT_PUBLIC_COMMERCE_API_URL: process.env.NEXT_PUBLIC_COMMERCE_API_URL,
|
|
61
99
|
NEXT_PUBLIC_COMMERCE_API_KEY: process.env.NEXT_PUBLIC_COMMERCE_API_KEY,
|
|
62
100
|
NEXT_PUBLIC_DEFAULT_CURRENCY: process.env.NEXT_PUBLIC_DEFAULT_CURRENCY,
|
|
63
101
|
NEXT_PUBLIC_SALES_CHANNEL_ID: process.env.NEXT_PUBLIC_SALES_CHANNEL_ID,
|
|
64
102
|
FRONTEND_WEBHOOK_SECRET: process.env.FRONTEND_WEBHOOK_SECRET,
|
|
65
|
-
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (shouldUseMockProvider()) {
|
|
106
|
+
return MockEnvSchema.parse(rawEnv)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return RealBackendEnvSchema.parse(rawEnv)
|
|
66
110
|
} catch (error) {
|
|
67
111
|
if (error instanceof z.ZodError) {
|
|
68
112
|
const issues = error.issues
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="next" />
|
|
2
2
|
/// <reference types="next/image-types/global" />
|
|
3
|
-
import "./.next/
|
|
3
|
+
import "./.next/types/routes.d.ts";
|
|
4
4
|
|
|
5
5
|
// NOTE: This file should not be edited
|
|
6
6
|
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|