From c7b67b909c460ab15213e51af7884d9171648b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Sun, 18 Sep 2022 17:10:06 +0200 Subject: [PATCH] Use Swell REST API for fetching products --- src/routes/shop/+layout.server.ts | 11 +++++-- src/routes/shop/+page.server.ts | 11 +++++-- src/routes/shop/[type]-[name]/+page.server.ts | 10 ++++-- src/utils/functions/shop.ts | 31 ------------------- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/routes/shop/+layout.server.ts b/src/routes/shop/+layout.server.ts index ccfbd58..0d55974 100644 --- a/src/routes/shop/+layout.server.ts +++ b/src/routes/shop/+layout.server.ts @@ -1,7 +1,8 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { fetchAPI } from '$utils/api' -import { initSwell, getProducts } from '$utils/functions/shop' +import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' +import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private' export const load: PageServerLoad = async () => { try { @@ -55,8 +56,12 @@ export const load: PageServerLoad = async () => { /** * Get products data from Swell */ - initSwell() - const shopProducts = await getProducts('posters') + const shopProductsRes: any = await fetch(`${SWELL_API_ENDPOINT}/products`, { + headers: { + Authorization: `Basic ${Buffer.from(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`).toString('base64')}` + }, + }) + const shopProducts = await shopProductsRes.json() if (shopProducts) { return { diff --git a/src/routes/shop/+page.server.ts b/src/routes/shop/+page.server.ts index 4e91cb5..f9486b6 100644 --- a/src/routes/shop/+page.server.ts +++ b/src/routes/shop/+page.server.ts @@ -1,8 +1,9 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { fetchAPI } from '$utils/api' -import { initSwell, getProduct } from '$utils/functions/shop' import { getRandomItem } from '$utils/functions' +import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' +import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private' export const load: PageServerLoad = async ({}) => { try { @@ -39,8 +40,12 @@ export const load: PageServerLoad = async ({}) => { const randomPoster: any = getRandomItem(data.data.posters) // Fetch Swell API for product - initSwell() - const shopProduct = await getProduct(randomPoster.product_id) + const shopProductRes: any = await fetch(`${SWELL_API_ENDPOINT}/products/${randomPoster.product_id}`, { + headers: { + Authorization: `Basic ${Buffer.from(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`).toString('base64')}` + }, + }) + const shopProduct = await shopProductRes.json() if (shopProduct) { return { diff --git a/src/routes/shop/[type]-[name]/+page.server.ts b/src/routes/shop/[type]-[name]/+page.server.ts index b636a7e..6b51f0c 100644 --- a/src/routes/shop/[type]-[name]/+page.server.ts +++ b/src/routes/shop/[type]-[name]/+page.server.ts @@ -1,7 +1,8 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { fetchAPI } from '$utils/api' -import { getProduct } from '$utils/functions/shop' +import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' +import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private' export const load: PageServerLoad = async ({ params }) => { try { @@ -36,7 +37,12 @@ export const load: PageServerLoad = async ({ params }) => { const poster = data.data.poster[0] // Fetch Swell API for product - const shopProduct = await getProduct(poster.product_id) + const shopProductRes: any = await fetch(`${SWELL_API_ENDPOINT}/products/${poster.product_id}`, { + headers: { + Authorization: `Basic ${Buffer.from(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`).toString('base64')}` + }, + }) + const shopProduct = await shopProductRes.json() if (shopProduct) { return { diff --git a/src/utils/functions/shop.ts b/src/utils/functions/shop.ts index 7757f5c..86c63fb 100644 --- a/src/utils/functions/shop.ts +++ b/src/utils/functions/shop.ts @@ -65,35 +65,4 @@ export const removeCartItem = async (productId: string) => { if (updatedCart) { return updatedCart } -} - - - -/** - * Fetch products - */ -export const getProducts = async (category?: string, limit: number = 25, page: number = 1) => { - const products = await swell.products.list({ - where: { - active: true, - }, - category, - limit, - page - }) - - if (products) { - return products - } -} - -/** - * Retrieve a product - */ -export const getProduct = async (id: string) => { - const product = await swell.products.get(id) - - if (product) { - return product - } } \ No newline at end of file