Use Swell REST API for fetching products

This commit is contained in:
2022-09-18 17:10:06 +02:00
parent 2393d28597
commit c7b67b909c
4 changed files with 24 additions and 39 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -66,34 +66,3 @@ export const removeCartItem = async (productId: string) => {
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
}
}