From 449d331e419f6627d40544dadadeaaa196f2875b Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Mon, 25 Jan 2021 20:57:53 +0100 Subject: [PATCH] Use env var for Shop API url --- framework/vendure/api/index.ts | 4 ++-- framework/vendure/index.tsx | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/framework/vendure/api/index.ts b/framework/vendure/api/index.ts index 4bd6aaace..f6b06a10c 100644 --- a/framework/vendure/api/index.ts +++ b/framework/vendure/api/index.ts @@ -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` ) } diff --git a/framework/vendure/index.tsx b/framework/vendure/index.tsx index eba7be772..d80c69f6a 100644 --- a/framework/vendure/index.tsx +++ b/framework/vendure/index.tsx @@ -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()