4
0
forked from crowetic/commerce

Updated the way the provider config is set

This commit is contained in:
Luis Alvarez 2021-02-24 21:55:45 -05:00
parent bcd1d46e30
commit 0cc443db06
6 changed files with 100 additions and 40 deletions

View File

@ -2,4 +2,5 @@ BIGCOMMERCE_STOREFRONT_API_URL=
BIGCOMMERCE_STOREFRONT_API_TOKEN=
BIGCOMMERCE_STORE_API_URL=
BIGCOMMERCE_STORE_API_TOKEN=
BIGCOMMERCE_STORE_API_CLIENT_ID=
BIGCOMMERCE_STORE_API_CLIENT_ID=
BIGCOMMERCE_CHANNEL_ID=

View File

@ -77,12 +77,12 @@ Main folder and its exposed functions
- `config.json`
- README.md
#### Example of correct usage of Commece Framework
#### Example of correct usage of Commerce Framework
```js
import { useUI } from '@components/ui'
import { useCustomer } from '@framework/customer'
import { useAddItem, useWishlist, useRemoveItem } from '@framework/wishlist'
import { useWishlist, useAddItem, useRemoveItem } from '@framework/wishlist'
```
## Config
@ -131,6 +131,7 @@ BIGCOMMERCE_STOREFRONT_API_TOKEN=<>
BIGCOMMERCE_STORE_API_URL=<>
BIGCOMMERCE_STORE_API_TOKEN=<>
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.

View File

@ -47,6 +47,7 @@ BIGCOMMERCE_STOREFRONT_API_TOKEN=<>
BIGCOMMERCE_STORE_API_URL=<>
BIGCOMMERCE_STORE_API_TOKEN=<>
BIGCOMMERCE_STORE_API_CLIENT_ID=<>
BIGCOMMERCE_CHANNEL_ID=<>
```
## General Usage

View File

@ -1,37 +1,11 @@
const providerConfig = require('./config.json')
module.exports = {
commerce: {
provider: 'bigcommerce',
...providerConfig,
},
images: {
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',
},
]
},
}

View 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
}

View File

@ -1,6 +1,49 @@
const providerConfig = require('./framework/bigcommerce/config.json')
const providerNextConfig = require('./framework/bigcommerce/next.config')
const bootstrap = require('./framework/commerce/utils/bootstrap')
const d = require('deepmerge')
// const providerConfig = require('./framework/bigcommerce/config.json')
// const providerNextConfig = require('./framework/bigcommerce/next.config')
// const bootstrap = require('./framework/commerce/utils/bootstrap')
// 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)