Updated the way the provider config is set
This commit is contained in:
		| @@ -2,4 +2,5 @@ BIGCOMMERCE_STOREFRONT_API_URL= | |||||||
| BIGCOMMERCE_STOREFRONT_API_TOKEN= | BIGCOMMERCE_STOREFRONT_API_TOKEN= | ||||||
| BIGCOMMERCE_STORE_API_URL= | BIGCOMMERCE_STORE_API_URL= | ||||||
| BIGCOMMERCE_STORE_API_TOKEN= | BIGCOMMERCE_STORE_API_TOKEN= | ||||||
| BIGCOMMERCE_STORE_API_CLIENT_ID= | BIGCOMMERCE_STORE_API_CLIENT_ID= | ||||||
|  | BIGCOMMERCE_CHANNEL_ID= | ||||||
| @@ -77,12 +77,12 @@ Main folder and its exposed functions | |||||||
| - `config.json` | - `config.json` | ||||||
| - README.md | - README.md | ||||||
|  |  | ||||||
| #### Example of correct usage of Commece Framework | #### Example of correct usage of Commerce Framework | ||||||
|  |  | ||||||
| ```js | ```js | ||||||
| import { useUI } from '@components/ui' | import { useUI } from '@components/ui' | ||||||
| import { useCustomer } from '@framework/customer' | import { useCustomer } from '@framework/customer' | ||||||
| import { useAddItem, useWishlist, useRemoveItem } from '@framework/wishlist' | import { useWishlist, useAddItem, useRemoveItem } from '@framework/wishlist' | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| ## Config | ## Config | ||||||
| @@ -131,6 +131,7 @@ BIGCOMMERCE_STOREFRONT_API_TOKEN=<> | |||||||
| BIGCOMMERCE_STORE_API_URL=<> | BIGCOMMERCE_STORE_API_URL=<> | ||||||
| BIGCOMMERCE_STORE_API_TOKEN=<> | BIGCOMMERCE_STORE_API_TOKEN=<> | ||||||
| BIGCOMMERCE_STORE_API_CLIENT_ID=<> | BIGCOMMERCE_STORE_API_CLIENT_ID=<> | ||||||
|  | BIGCOMMERCE_CHANNEL_ID=<> | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| If your project was started with a "Deploy with Vercel" button, you can use Vercel's CLI to retrieve these credentials. | If your project was started with a "Deploy with Vercel" button, you can use Vercel's CLI to retrieve these credentials. | ||||||
|   | |||||||
| @@ -47,6 +47,7 @@ BIGCOMMERCE_STOREFRONT_API_TOKEN=<> | |||||||
| BIGCOMMERCE_STORE_API_URL=<> | BIGCOMMERCE_STORE_API_URL=<> | ||||||
| BIGCOMMERCE_STORE_API_TOKEN=<> | BIGCOMMERCE_STORE_API_TOKEN=<> | ||||||
| BIGCOMMERCE_STORE_API_CLIENT_ID=<> | BIGCOMMERCE_STORE_API_CLIENT_ID=<> | ||||||
|  | BIGCOMMERCE_CHANNEL_ID=<> | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| ## General Usage | ## General Usage | ||||||
|   | |||||||
| @@ -1,37 +1,11 @@ | |||||||
|  | const providerConfig = require('./config.json') | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|  |   commerce: { | ||||||
|  |     provider: 'bigcommerce', | ||||||
|  |     ...providerConfig, | ||||||
|  |   }, | ||||||
|   images: { |   images: { | ||||||
|     domains: ['cdn11.bigcommerce.com'], |     domains: ['cdn11.bigcommerce.com'], | ||||||
|   }, |   }, | ||||||
|   i18n: { |  | ||||||
|     locales: ['en-US', 'es'], |  | ||||||
|     defaultLocale: 'en-US', |  | ||||||
|   }, |  | ||||||
|   rewrites() { |  | ||||||
|     return [ |  | ||||||
|       { |  | ||||||
|         source: '/checkout', |  | ||||||
|         destination: '/api/bigcommerce/checkout', |  | ||||||
|       }, |  | ||||||
|       // The logout is also an action so this route is not required, but it's also another way |  | ||||||
|       // you can allow a logout! |  | ||||||
|       { |  | ||||||
|         source: '/logout', |  | ||||||
|         destination: '/api/bigcommerce/customers/logout?redirect_to=/', |  | ||||||
|       }, |  | ||||||
|       // Rewrites for /search |  | ||||||
|       { |  | ||||||
|         source: '/search/designers/:name', |  | ||||||
|         destination: '/search', |  | ||||||
|       }, |  | ||||||
|       { |  | ||||||
|         source: '/search/designers/:name/:category', |  | ||||||
|         destination: '/search', |  | ||||||
|       }, |  | ||||||
|       { |  | ||||||
|         // This rewrite will also handle `/search/designers` |  | ||||||
|         source: '/search/:category', |  | ||||||
|         destination: '/search', |  | ||||||
|       }, |  | ||||||
|     ] |  | ||||||
|   }, |  | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								framework/commerce/with-config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								framework/commerce/with-config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | |||||||
|  | /** | ||||||
|  |  * This file is expected to be used in next.config.js only | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | const merge = require('deepmerge') | ||||||
|  |  | ||||||
|  | const PROVIDERS = ['bigcommerce'] | ||||||
|  |  | ||||||
|  | function getProviderName() { | ||||||
|  |   return process.env.BIGCOMMERCE_STOREFRONT_API_URL ? 'bigcommerce' : null | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = (nextConfig = {}) => { | ||||||
|  |   const commerce = nextConfig.commerce || {} | ||||||
|  |   const name = commerce.provider || getProviderName() | ||||||
|  |  | ||||||
|  |   if (!name) { | ||||||
|  |     throw new Error( | ||||||
|  |       `The commerce provider is missing, please add a valid provider name or its environment variables` | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  |   if (!PROVIDERS.includes(name)) { | ||||||
|  |     throw new Error( | ||||||
|  |       `The commerce provider "${name}" can't be found, please use one of "${PROVIDERS.join( | ||||||
|  |         ', ' | ||||||
|  |       )}"` | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   const commerceNextConfig = require(`../${name}/next.config`) | ||||||
|  |   const config = merge(commerceNextConfig, nextConfig) | ||||||
|  |  | ||||||
|  |   config.env = config.env || {} | ||||||
|  |  | ||||||
|  |   Object.entries(config.commerce.features).forEach(([k, v]) => { | ||||||
|  |     if (v) config.env[`COMMERCE_${k.toUpperCase()}_ENABLED`] = true | ||||||
|  |   }) | ||||||
|  |  | ||||||
|  |   return config | ||||||
|  | } | ||||||
| @@ -1,6 +1,49 @@ | |||||||
| const providerConfig = require('./framework/bigcommerce/config.json') | // const providerConfig = require('./framework/bigcommerce/config.json') | ||||||
| const providerNextConfig = require('./framework/bigcommerce/next.config') | // const providerNextConfig = require('./framework/bigcommerce/next.config') | ||||||
| const bootstrap = require('./framework/commerce/utils/bootstrap') | // const bootstrap = require('./framework/commerce/utils/bootstrap') | ||||||
| const d = require('deepmerge') | // const d = require('deepmerge') | ||||||
|  |  | ||||||
| module.exports = d(providerNextConfig, bootstrap(providerConfig)) | // module.exports = d(providerNextConfig, bootstrap(providerConfig)) | ||||||
|  |  | ||||||
|  | const withCommerceConfig = require('./framework/commerce/with-config') | ||||||
|  |  | ||||||
|  | const commerce = { provider: 'bigcommerce' } | ||||||
|  | const isBC = commerce.provider === 'bigcommerce' | ||||||
|  |  | ||||||
|  | module.exports = withCommerceConfig({ | ||||||
|  |   commerce, | ||||||
|  |   i18n: { | ||||||
|  |     locales: ['en-US', 'es'], | ||||||
|  |     defaultLocale: 'en-US', | ||||||
|  |   }, | ||||||
|  |   rewrites() { | ||||||
|  |     return [ | ||||||
|  |       isBC && { | ||||||
|  |         source: '/checkout', | ||||||
|  |         destination: '/api/bigcommerce/checkout', | ||||||
|  |       }, | ||||||
|  |       // The logout is also an action so this route is not required, but it's also another way | ||||||
|  |       // you can allow a logout! | ||||||
|  |       isBC && { | ||||||
|  |         source: '/logout', | ||||||
|  |         destination: '/api/bigcommerce/customers/logout?redirect_to=/', | ||||||
|  |       }, | ||||||
|  |       // Rewrites for /search | ||||||
|  |       { | ||||||
|  |         source: '/search/designers/:name', | ||||||
|  |         destination: '/search', | ||||||
|  |       }, | ||||||
|  |       { | ||||||
|  |         source: '/search/designers/:name/:category', | ||||||
|  |         destination: '/search', | ||||||
|  |       }, | ||||||
|  |       { | ||||||
|  |         // This rewrite will also handle `/search/designers` | ||||||
|  |         source: '/search/:category', | ||||||
|  |         destination: '/search', | ||||||
|  |       }, | ||||||
|  |     ].filter((x) => x) | ||||||
|  |   }, | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | console.log('RESULT', module.exports) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user