|
8 | 8 | * 1. Create a Shopify store at https://shopify.com
|
9 | 9 | * 2. Go to Settings > Apps and sales channels > Develop apps
|
10 | 10 | * 3. Create a new app and get your Storefront API access token
|
11 |
| - * 4. Add your credentials to docusaurus.config.ts customFields |
12 |
| - * |
13 |
| - * For development, add these to your docusaurus.config.ts: |
14 |
| - * customFields: { |
15 |
| - * SHOPIFY_STORE_DOMAIN: 'your-store.myshopify.com', |
16 |
| - * SHOPIFY_STOREFRONT_ACCESS_TOKEN: 'your-token-here', |
17 |
| - * } |
| 11 | + * 4. Credentials are configured in docusaurus.config.ts customFields |
18 | 12 | */
|
19 | 13 |
|
20 |
| -// Get credentials from Docusaurus customFields (configured in docusaurus.config.ts) |
21 |
| -// These are set at build time and won't expose credentials in the code |
22 |
| -const SHOPIFY_STORE_DOMAIN = |
23 |
| - (typeof window !== 'undefined' && (window as any).docusaurus?.siteConfig?.customFields?.SHOPIFY_STORE_DOMAIN as string) || ''; |
24 |
| -const SHOPIFY_STOREFRONT_ACCESS_TOKEN = |
25 |
| - (typeof window !== 'undefined' && (window as any).docusaurus?.siteConfig?.customFields?.SHOPIFY_STOREFRONT_ACCESS_TOKEN as string) || ''; |
| 14 | +import siteConfig from '@generated/docusaurus.config'; |
26 | 15 |
|
27 |
| -const SHOPIFY_GRAPHQL_URL = `https://${SHOPIFY_STORE_DOMAIN}/api/2024-01/graphql.json`; |
| 16 | +// Get credentials from Docusaurus customFields |
| 17 | +function getShopifyConfig() { |
| 18 | + const domain = (siteConfig.customFields?.SHOPIFY_STORE_DOMAIN as string) || ''; |
| 19 | + const token = (siteConfig.customFields?.SHOPIFY_STOREFRONT_ACCESS_TOKEN as string) || ''; |
| 20 | + |
| 21 | + return { domain, token }; |
| 22 | +} |
| 23 | + |
| 24 | +const SHOPIFY_GRAPHQL_URL = (domain: string) => `https://${domain}/api/2024-01/graphql.json`; |
28 | 25 |
|
29 | 26 | interface ShopifyProduct {
|
30 | 27 | id: string;
|
@@ -97,16 +94,18 @@ interface ShopifyCheckout {
|
97 | 94 | * Make a request to Shopify's Storefront API
|
98 | 95 | */
|
99 | 96 | async function shopifyFetch<T>(query: string, variables = {}): Promise<T> {
|
100 |
| - if (!SHOPIFY_STORE_DOMAIN || !SHOPIFY_STOREFRONT_ACCESS_TOKEN) { |
| 97 | + const config = getShopifyConfig(); |
| 98 | + |
| 99 | + if (!config.domain || !config.token) { |
101 | 100 | console.warn('Shopify credentials not configured. Using mock data.');
|
102 | 101 | throw new Error('Shopify not configured');
|
103 | 102 | }
|
104 | 103 |
|
105 |
| - const response = await fetch(SHOPIFY_GRAPHQL_URL, { |
| 104 | + const response = await fetch(SHOPIFY_GRAPHQL_URL(config.domain), { |
106 | 105 | method: 'POST',
|
107 | 106 | headers: {
|
108 | 107 | 'Content-Type': 'application/json',
|
109 |
| - 'X-Shopify-Storefront-Access-Token': SHOPIFY_STOREFRONT_ACCESS_TOKEN, |
| 108 | + 'X-Shopify-Storefront-Access-Token': config.token, |
110 | 109 | },
|
111 | 110 | body: JSON.stringify({ query, variables }),
|
112 | 111 | });
|
@@ -473,7 +472,8 @@ export async function removeFromCheckout(
|
473 | 472 | * Check if Shopify is configured
|
474 | 473 | */
|
475 | 474 | export function isShopifyConfigured(): boolean {
|
476 |
| - return Boolean(SHOPIFY_STORE_DOMAIN && SHOPIFY_STOREFRONT_ACCESS_TOKEN); |
| 475 | + const config = getShopifyConfig(); |
| 476 | + return Boolean(config.domain && config.token); |
477 | 477 | }
|
478 | 478 |
|
479 | 479 | export type { ShopifyProduct, ShopifyCheckout };
|
0 commit comments