🚧 Migrate to new SvelteKit routing system

A bit annoying but for the best I guess?
This commit is contained in:
2022-08-16 15:54:15 +02:00
parent cf2becc931
commit 5e5c08ddd1
40 changed files with 775 additions and 774 deletions

View File

@@ -1,44 +0,0 @@
import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit'
// Block GET requests
export async function GET ({}: RequestEvent): Promise<RequestHandlerOutput> {
return {
status: 403,
body: 'nope!'
}
}
/**
* POST request
*/
export async function POST ({ request }: RequestEvent): Promise<RequestHandlerOutput> {
const body = await request.text()
if (body) {
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${import.meta.env.VITE_NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: import.meta.env.VITE_NEWSLETTER_API_TOKEN,
email_address: body,
})
})
const res = await req.json()
if (res && res.email_address && res.status === 'PENDING') {
return {
status: 200,
body: {
code: 'PENDING'
},
}
}
return {
status: 403,
body: res.error,
}
}
}

View File

@@ -0,0 +1,29 @@
import { error } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
export const POST: RequestHandler = async ({ request }) => {
try {
const body = await request.text()
if (body) {
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${import.meta.env.VITE_NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: import.meta.env.VITE_NEWSLETTER_API_TOKEN,
email_address: body,
})
})
const res = await req.json()
if (res && res.email_address && res.status === 'PENDING') {
return new Response(JSON.stringify({
code: 'PENDING'
}))
}
}
} catch (err) {
throw error(403, err)
}
}

View File

@@ -1,75 +0,0 @@
import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit'
import {
getProducts,
getProduct,
createCart,
fetchCart,
addToCart,
updateCartItem,
removeCartItem,
} from '$utils/functions/swell'
// Block GET requests
export async function GET ({}: RequestEvent): Promise<RequestHandlerOutput> {
return {
status: 403,
body: 'nope!'
}
}
/**
* POST request
*/
export async function POST ({ request }: RequestEvent): Promise<RequestHandlerOutput> {
try {
const body = await request.json()
const { action, cartId, productId } = body
let result = {}
if (body) {
switch (action) {
case 'getProducts': {
result = await getProducts(body.category)
break
}
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, body.quantity)
break
}
case 'updateCartItem': {
result = await updateCartItem(cartId, productId, body.quantity)
break
}
case 'removeCartItem': {
result = await removeCartItem(cartId, productId)
break
}
default: break
}
}
return {
status: 200,
body: result,
}
}
catch (error) {
return {
status: error.status || 500,
body: error.message || error.text || `Can't fetch query`
}
}
}

View File

@@ -0,0 +1,52 @@
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`)
}
}