[wip] 🔥 Integrate Swell into the shop

Create a custom and internal API for fetching and updating content to Swell Admin API (using swell-node)
This commit is contained in:
2021-11-07 11:51:01 +01:00
parent 2f043af38e
commit bd74f5612c
14 changed files with 543 additions and 148 deletions

60
src/routes/api/swell.ts Normal file
View File

@@ -0,0 +1,60 @@
import { addToCart, createCart, fetchCart, getProduct, updateCartItem } from '$utils/swellFunctions'
// Block GET requests
export async function get ({ body, query }) {
return {
status: 403,
body: 'nope!'
}
}
/**
* POST request
*/
export async function post ({ headers, query, body, params, ...rest }) {
try {
const bodyParsed = JSON.parse(Buffer.from(body).toString())
const { action, cartId, productId } = bodyParsed
let result = {}
if (bodyParsed) {
switch (action) {
case 'getProduct': {
result = await getProduct(productId)
break
}
case 'createCart': {
result = await createCart()
break
}
case 'fetchCart': {
result = await fetchCart(cartId)
break
}
case 'addToCart': {
result = await addToCart(cartId, productId, bodyParsed.quantity)
break
}
case 'updateCartItem': {
result = await updateCartItem(cartId, productId, bodyParsed.quantity)
break
}
default: break
}
}
return {
status: 200,
body: result,
}
}
catch (error) {
return {
status: error.status || 500,
body: error.message || error.text || `Can't fetch query`
}
}
}