Use Swell REST API for fetching products
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api'
|
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 () => {
|
export const load: PageServerLoad = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -55,8 +56,12 @@ export const load: PageServerLoad = async () => {
|
|||||||
/**
|
/**
|
||||||
* Get products data from Swell
|
* Get products data from Swell
|
||||||
*/
|
*/
|
||||||
initSwell()
|
const shopProductsRes: any = await fetch(`${SWELL_API_ENDPOINT}/products`, {
|
||||||
const shopProducts = await getProducts('posters')
|
headers: {
|
||||||
|
Authorization: `Basic ${Buffer.from(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`).toString('base64')}`
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const shopProducts = await shopProductsRes.json()
|
||||||
|
|
||||||
if (shopProducts) {
|
if (shopProducts) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { initSwell, getProduct } from '$utils/functions/shop'
|
|
||||||
import { getRandomItem } from '$utils/functions'
|
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 ({}) => {
|
export const load: PageServerLoad = async ({}) => {
|
||||||
try {
|
try {
|
||||||
@@ -39,8 +40,12 @@ export const load: PageServerLoad = async ({}) => {
|
|||||||
const randomPoster: any = getRandomItem(data.data.posters)
|
const randomPoster: any = getRandomItem(data.data.posters)
|
||||||
|
|
||||||
// Fetch Swell API for product
|
// Fetch Swell API for product
|
||||||
initSwell()
|
const shopProductRes: any = await fetch(`${SWELL_API_ENDPOINT}/products/${randomPoster.product_id}`, {
|
||||||
const shopProduct = await getProduct(randomPoster.product_id)
|
headers: {
|
||||||
|
Authorization: `Basic ${Buffer.from(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`).toString('base64')}`
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const shopProduct = await shopProductRes.json()
|
||||||
|
|
||||||
if (shopProduct) {
|
if (shopProduct) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api'
|
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 }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
try {
|
try {
|
||||||
@@ -36,7 +37,12 @@ export const load: PageServerLoad = async ({ params }) => {
|
|||||||
const poster = data.data.poster[0]
|
const poster = data.data.poster[0]
|
||||||
|
|
||||||
// Fetch Swell API for product
|
// 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) {
|
if (shopProduct) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -66,34 +66,3 @@ export const removeCartItem = async (productId: string) => {
|
|||||||
return 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user