mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Merge branch 'agnostic' of https://github.com/vercel/commerce into agnostic
This commit is contained in:
@@ -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
|
||||
|
@@ -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',
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
@@ -2,12 +2,6 @@ import type { Wishlist as BCWishlist } from '@framework/api/wishlist'
|
||||
import type { Customer as BCCustomer } from '@framework/api/customers'
|
||||
import type { SearchProductsData as BCSearchProductsData } from '@framework/api/catalog/products'
|
||||
|
||||
export type Features = 'wishlist' | 'checkout' | string
|
||||
|
||||
export type CommerceProviderConfig = {
|
||||
features: Record<Features, boolean>
|
||||
}
|
||||
|
||||
export type Discount = {
|
||||
// The value of the discount, can be an amount or percentage
|
||||
value: number
|
||||
|
11
framework/commerce/utils/bootstrap.js
vendored
11
framework/commerce/utils/bootstrap.js
vendored
@@ -1,11 +0,0 @@
|
||||
module.exports = ({ features }) => {
|
||||
let output = {
|
||||
env: {},
|
||||
}
|
||||
if (!!Object.keys(features).length) {
|
||||
Object.keys(features).map(
|
||||
(r) => (output.env[`COMMERCE_${r.toUpperCase()}_ENABLED`] = features[r])
|
||||
)
|
||||
}
|
||||
return output
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
import commerceProviderConfig from '../config.json'
|
||||
import type { CommerceProviderConfig } from '../types'
|
||||
import memo from 'lodash.memoize'
|
||||
|
||||
type FeaturesAPI = {
|
||||
isEnabled: (desideredFeature: string) => boolean
|
||||
}
|
||||
|
||||
function isFeatureEnabled(config: CommerceProviderConfig) {
|
||||
const features = config.features
|
||||
return (desideredFeature: string) =>
|
||||
Object.keys(features)
|
||||
.filter((k) => features[k])
|
||||
.includes(desideredFeature)
|
||||
}
|
||||
|
||||
export function toEnvConfig(
|
||||
configMap: CommerceProviderConfig['features']
|
||||
): Map<string, boolean> {
|
||||
let toEnvConfigMap = new Map<string, boolean>()
|
||||
Object.keys(configMap).map((r) =>
|
||||
toEnvConfigMap.set(`${r.toUpperCase()}_ENABLED`, configMap[r])
|
||||
)
|
||||
return toEnvConfigMap
|
||||
}
|
||||
|
||||
function boostrap(): FeaturesAPI {
|
||||
const basis = {
|
||||
isEnabled: () => false,
|
||||
}
|
||||
|
||||
if (!commerceProviderConfig) {
|
||||
console.log('No config.json found - Please add a config.json')
|
||||
return basis
|
||||
}
|
||||
|
||||
if (commerceProviderConfig.features) {
|
||||
return {
|
||||
...basis,
|
||||
isEnabled: memo(isFeatureEnabled(commerceProviderConfig)),
|
||||
}
|
||||
}
|
||||
|
||||
return basis
|
||||
}
|
||||
|
||||
export default boostrap()
|
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
|
||||
}
|
Reference in New Issue
Block a user