use proper env

This commit is contained in:
Jieren Chen 2024-09-20 08:55:37 -04:00
parent 30087c63c8
commit 27b11abe1e
No known key found for this signature in database
GPG Key ID: 2FF322D21B5DB10B
3 changed files with 14 additions and 33 deletions

View File

@ -5,7 +5,7 @@ import { GridTileImage } from './grid/tile';
export async function Carousel({currency}: {currency: string}) { export async function Carousel({currency}: {currency: string}) {
// Collections that start with `hidden-*` are hidden from the search page. // Collections that start with `hidden-*` are hidden from the search page.
const products = await getCollectionProducts({ const products = await getCollectionProducts({
collection: process.env.FW_COLLECTION || '', collection: process.env.NEXT_PUBLIC_FW_COLLECTION || '',
currency, currency,
}); });

View File

@ -45,7 +45,7 @@ function ThreeItemGridItem({
export async function ThreeItemGrid({currency}: { currency: string}) { export async function ThreeItemGrid({currency}: { currency: string}) {
const homepageItems = await getCollectionProducts({ const homepageItems = await getCollectionProducts({
collection: process.env.FW_COLLECTION || '', collection: process.env.NEXT_PUBLIC_FW_COLLECTION || '',
currency, currency,
}); });

View File

@ -2,8 +2,8 @@ import { Cart, Menu, Product } from "lib/shopify/types";
import { reshapeCart, reshapeProduct, reshapeProducts } from "./reshape"; import { reshapeCart, reshapeProduct, reshapeProducts } from "./reshape";
import { FourthwallCart, FourthwallCheckout, FourthwallProduct } from "./types"; import { FourthwallCart, FourthwallCheckout, FourthwallProduct } from "./types";
const API_URL = process.env.FW_API_URL const API_URL = process.env.NEXT_PUBLIC_FW_API_URL;
const API_SECRET = process.env.FW_SECRET const FW_PUBLIC_TOKEN = process.env.NEXT_PUBLIC_FW_PUBLIC_TOKEN;
/** /**
* Helpers * Helpers
@ -17,6 +17,7 @@ async function fourthwallGet<T>(url: string, options: RequestInit = {}): Promise
...options, ...options,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-FW-Public-Token': FW_PUBLIC_TOKEN || '',
...options.headers ...options.headers
} }
} }
@ -43,6 +44,7 @@ async function fourthwallPost<T>(url: string, data: any, options: RequestInit =
...options, ...options,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-FW-Public-Token': FW_PUBLIC_TOKEN || '',
...options.headers ...options.headers
}, },
body: JSON.stringify(data) body: JSON.stringify(data)
@ -77,7 +79,7 @@ export async function getCollectionProducts({
reverse?: boolean; reverse?: boolean;
sortKey?: string; sortKey?: string;
}): Promise<Product[]> { }): Promise<Product[]> {
const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${API_URL}/api/public/v1.0/collections/${collection}/products?secret=${API_SECRET}&currency=${currency}`, { const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${API_URL}/api/public/v1.0/collections/${collection}/products?&currency=${currency}`, {
headers: { headers: {
'X-ShopId': process.env.FW_SHOPID || '' '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<Product | undefined> { export async function getProduct({ handle, currency } : { handle: string, currency: string }): Promise<Product | undefined> {
// TODO: replace with real URL // TODO: replace with real URL
const res = await fourthwallGet<FourthwallProduct>(`${API_URL}/api/public/v1.0/products/${handle}?secret=${API_SECRET}&currency=${currency}`, { const res = await fourthwallGet<FourthwallProduct>(`${API_URL}/api/public/v1.0/products/${handle}?&currency=${currency}`);
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
}
});
return reshapeProduct(res.body); return reshapeProduct(res.body);
} }
@ -119,7 +117,7 @@ export async function getCart(cartId: string | undefined, currency: string): Pro
return undefined; return undefined;
} }
const res = await fourthwallGet<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}?secret=${API_SECRET}&currency=${currency}`, { const res = await fourthwallGet<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}?currency=${currency}`, {
cache: 'no-store' cache: 'no-store'
}); });
@ -127,12 +125,8 @@ export async function getCart(cartId: string | undefined, currency: string): Pro
} }
export async function createCart(): Promise<Cart> { export async function createCart(): Promise<Cart> {
const res = await fourthwallPost<FourthwallCart>(`https://api.staging.fourthwall.com/api/public/v1.0/carts?secret=${API_SECRET}`, { const res = await fourthwallPost<FourthwallCart>(`https://api.staging.fourthwall.com/api/public/v1.0/carts`, {
items: [] items: []
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
}
}); });
return reshapeCart(res.body); return reshapeCart(res.body);
@ -148,12 +142,9 @@ export async function addToCart(
quantity: line.quantity quantity: line.quantity
})); }));
const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/add?secret=${API_SECRET}`, { const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/add`, {
items, items,
}, { }, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store' cache: 'no-store'
}); });
@ -165,12 +156,9 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise
variantId: id variantId: id
})); }));
const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/remove?secret=${API_SECRET}`, { const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/remove`, {
items, items,
}, { }, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store' cache: 'no-store'
}); });
@ -186,12 +174,9 @@ export async function updateCart(
quantity: line.quantity quantity: line.quantity
})); }));
const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/change?secret=${API_SECRET}`, { const res = await fourthwallPost<FourthwallCart>(`${API_URL}/api/public/v1.0/carts/${cartId}/change`, {
items, items,
}, { }, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store' cache: 'no-store'
}); });
@ -202,13 +187,9 @@ export async function createCheckout(
cartId: string, cartId: string,
cartCurrency: string cartCurrency: string
): Promise<FourthwallCheckout> { ): Promise<FourthwallCheckout> {
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, cartId,
cartCurrency cartCurrency
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
}
}); });
return res.body; return res.body;