From 27b11abe1ee2d50a9b7a846d92617fbf70860e91 Mon Sep 17 00:00:00 2001 From: Jieren Chen Date: Fri, 20 Sep 2024 08:55:37 -0400 Subject: [PATCH] use proper env --- components/carousel.tsx | 2 +- components/grid/three-items.tsx | 2 +- lib/fourthwall/index.ts | 43 +++++++++------------------------ 3 files changed, 14 insertions(+), 33 deletions(-) diff --git a/components/carousel.tsx b/components/carousel.tsx index 907b9496b..a073cf90a 100644 --- a/components/carousel.tsx +++ b/components/carousel.tsx @@ -5,7 +5,7 @@ import { GridTileImage } from './grid/tile'; export async function Carousel({currency}: {currency: string}) { // Collections that start with `hidden-*` are hidden from the search page. const products = await getCollectionProducts({ - collection: process.env.FW_COLLECTION || '', + collection: process.env.NEXT_PUBLIC_FW_COLLECTION || '', currency, }); diff --git a/components/grid/three-items.tsx b/components/grid/three-items.tsx index 7583c7d51..5869405a2 100644 --- a/components/grid/three-items.tsx +++ b/components/grid/three-items.tsx @@ -45,7 +45,7 @@ function ThreeItemGridItem({ export async function ThreeItemGrid({currency}: { currency: string}) { const homepageItems = await getCollectionProducts({ - collection: process.env.FW_COLLECTION || '', + collection: process.env.NEXT_PUBLIC_FW_COLLECTION || '', currency, }); diff --git a/lib/fourthwall/index.ts b/lib/fourthwall/index.ts index a3fb7c6d2..a933d3e78 100644 --- a/lib/fourthwall/index.ts +++ b/lib/fourthwall/index.ts @@ -2,8 +2,8 @@ import { Cart, Menu, Product } from "lib/shopify/types"; import { reshapeCart, reshapeProduct, reshapeProducts } from "./reshape"; import { FourthwallCart, FourthwallCheckout, FourthwallProduct } from "./types"; -const API_URL = process.env.FW_API_URL -const API_SECRET = process.env.FW_SECRET +const API_URL = process.env.NEXT_PUBLIC_FW_API_URL; +const FW_PUBLIC_TOKEN = process.env.NEXT_PUBLIC_FW_PUBLIC_TOKEN; /** * Helpers @@ -17,6 +17,7 @@ async function fourthwallGet(url: string, options: RequestInit = {}): Promise ...options, headers: { 'Content-Type': 'application/json', + 'X-FW-Public-Token': FW_PUBLIC_TOKEN || '', ...options.headers } } @@ -43,6 +44,7 @@ async function fourthwallPost(url: string, data: any, options: RequestInit = ...options, headers: { 'Content-Type': 'application/json', + 'X-FW-Public-Token': FW_PUBLIC_TOKEN || '', ...options.headers }, body: JSON.stringify(data) @@ -77,7 +79,7 @@ export async function getCollectionProducts({ reverse?: boolean; sortKey?: string; }): Promise { - const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${API_URL}/api/public/v1.0/collections/${collection}/products?secret=${API_SECRET}¤cy=${currency}`, { + const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${API_URL}/api/public/v1.0/collections/${collection}/products?¤cy=${currency}`, { headers: { 'X-ShopId': process.env.FW_SHOPID || '' } @@ -97,11 +99,7 @@ export async function getCollectionProducts({ */ export async function getProduct({ handle, currency } : { handle: string, currency: string }): Promise { // TODO: replace with real URL - const res = await fourthwallGet(`${API_URL}/api/public/v1.0/products/${handle}?secret=${API_SECRET}¤cy=${currency}`, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - } - }); + const res = await fourthwallGet(`${API_URL}/api/public/v1.0/products/${handle}?¤cy=${currency}`); return reshapeProduct(res.body); } @@ -119,7 +117,7 @@ export async function getCart(cartId: string | undefined, currency: string): Pro return undefined; } - const res = await fourthwallGet(`${API_URL}/api/public/v1.0/carts/${cartId}?secret=${API_SECRET}¤cy=${currency}`, { + const res = await fourthwallGet(`${API_URL}/api/public/v1.0/carts/${cartId}?currency=${currency}`, { cache: 'no-store' }); @@ -127,12 +125,8 @@ export async function getCart(cartId: string | undefined, currency: string): Pro } export async function createCart(): Promise { - const res = await fourthwallPost(`https://api.staging.fourthwall.com/api/public/v1.0/carts?secret=${API_SECRET}`, { + const res = await fourthwallPost(`https://api.staging.fourthwall.com/api/public/v1.0/carts`, { items: [] - }, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - } }); return reshapeCart(res.body); @@ -148,12 +142,9 @@ export async function addToCart( quantity: line.quantity })); - const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/add?secret=${API_SECRET}`, { + const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/add`, { items, }, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - }, cache: 'no-store' }); @@ -165,12 +156,9 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise variantId: id })); - const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/remove?secret=${API_SECRET}`, { + const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/remove`, { items, }, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - }, cache: 'no-store' }); @@ -186,12 +174,9 @@ export async function updateCart( quantity: line.quantity })); - const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/change?secret=${API_SECRET}`, { + const res = await fourthwallPost(`${API_URL}/api/public/v1.0/carts/${cartId}/change`, { items, }, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - }, cache: 'no-store' }); @@ -202,13 +187,9 @@ export async function createCheckout( cartId: string, cartCurrency: string ): Promise { - const res = await fourthwallPost<{ id: string }>(`${API_URL}/api/public/v1.0/checkouts?secret=${API_SECRET}`, { + const res = await fourthwallPost<{ id: string }>(`${API_URL}/api/public/v1.0/checkouts`, { cartId, cartCurrency - }, { - headers: { - 'X-ShopId': process.env.FW_SHOPID || '' - } }); return res.body;