Use env var for Shop API url

This commit is contained in:
Michael Bromley 2021-01-25 20:57:53 +01:00
parent eb7286f8e3
commit 449d331e41
2 changed files with 14 additions and 9 deletions

View File

@ -3,11 +3,11 @@ import fetchGraphqlApi from './utils/fetch-graphql-api'
export interface VendureConfig extends CommerceAPIConfig {}
const API_URL = process.env.VENDURE_SHOP_API_URL
const API_URL = process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL
if (!API_URL) {
throw new Error(
`The environment variable VENDURE_SHOP_API_URL is missing and it's required to access your store`
`The environment variable NEXT_PUBLIC_VENDURE_SHOP_API_URL is missing and it's required to access your store`
)
}

View File

@ -1,10 +1,6 @@
import { ReactNode } from 'react'
import * as React from 'react'
import {
CommerceConfig,
CommerceProvider as CoreCommerceProvider,
useCommerce as useCoreCommerce,
} from '@commerce'
import { ReactNode } from 'react'
import { CommerceConfig, CommerceProvider as CoreCommerceProvider, useCommerce as useCoreCommerce } from '@commerce'
import { FetcherError } from '@commerce/utils/errors'
async function getText(res: Response) {
@ -27,12 +23,21 @@ export const vendureConfig: CommerceConfig = {
locale: 'en-us',
cartCookie: 'bc_cartId',
async fetcher({ url, method = 'POST', variables, query, body: bodyObj }) {
const shopApiUrl = process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL
if (!shopApiUrl) {
throw new Error('The Vendure Shop API url has not been provided. Please define NEXT_PUBLIC_VENDURE_SHOP_API_URL in .env.local')
}
const hasBody = Boolean(variables || query)
const body = hasBody
? JSON.stringify({ query, variables })
: undefined
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
const res = await fetch('http://localhost:3001/shop-api'!, { method, body, headers, credentials: 'include' })
const res = await fetch(shopApiUrl, {
method,
body,
headers,
credentials: 'include'
})
if (res.ok) {
const { data } = await res.json()