[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:
60
src/routes/api/swell.ts
Normal file
60
src/routes/api/swell.ts
Normal 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`
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user