🚧 Switch to swell-js to handle Shop cart

swell-node was relying too much on Node packages like crypto or events to be used with Cloudflare Pages or Vercel Edge Functions
This commit is contained in:
2022-09-18 12:11:44 +02:00
parent b3fdc5cea5
commit b66db25942
12 changed files with 210 additions and 351 deletions

View File

@@ -1,52 +0,0 @@
import { error } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import * as swell from '$utils/functions/swell'
export const POST: RequestHandler = async ({ request }) => {
try {
const body = await request.json()
const { action, cartId, productId } = body
let result = {}
if (body) {
switch (action) {
case 'getProducts': {
result = await swell.getProducts(body.category)
break
}
case 'getProduct': {
result = await swell.getProduct(productId)
break
}
case 'createCart': {
result = await swell.createCart()
break
}
case 'fetchCart': {
result = await swell.fetchCart(cartId)
break
}
case 'addToCart': {
result = await swell.addToCart(cartId, productId, body.quantity)
break
}
case 'updateCartItem': {
result = await swell.updateCartItem(cartId, productId, body.quantity)
break
}
case 'removeCartItem': {
result = await swell.removeCartItem(cartId, productId)
break
}
default: break
}
return new Response(JSON.stringify({
...result
}))
}
} catch (err) {
throw error(500, err.message || `Can't fetch query`)
}
}

View File

@@ -1,7 +1,7 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import { getProducts } from '$utils/functions/swell'
import { getProducts } from '$utils/functions/shop'
export const load: PageServerLoad = async () => {
try {

View File

@@ -1,7 +1,7 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import { getProduct } from '$utils/functions/swell'
import { getProduct } from '$utils/functions/shop'
import { getRandomItem } from '$utils/functions'
export const load: PageServerLoad = async ({}) => {
@@ -36,7 +36,7 @@ export const load: PageServerLoad = async ({}) => {
}`)
if (data) {
const randomPoster = getRandomItem(data.data.posters)
const randomPoster: any = getRandomItem(data.data.posters)
// Fetch Swell API for product
const shopProduct = await getProduct(randomPoster.product_id)

View File

@@ -1,7 +1,7 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import { getProduct } from '$utils/functions/swell'
import { getProduct } from '$utils/functions/shop'
export const load: PageServerLoad = async ({ params }) => {
try {