Create reusable Swell fetch functions

This commit is contained in:
2022-09-18 19:57:19 +02:00
parent 3ca8ed0d06
commit e01ba0d6c6
4 changed files with 31 additions and 27 deletions

View File

@@ -1,9 +1,7 @@
import { error } from '@sveltejs/kit' import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import base64 from 'base-64'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' import { fetchSwell } from '$utils/functions/shopServer'
import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
try { try {
@@ -57,12 +55,7 @@ export const load: PageServerLoad = async () => {
/** /**
* Get products data from Swell * Get products data from Swell
*/ */
const shopProductsRes: any = await fetch(`${SWELL_API_ENDPOINT}/products`, { const shopProducts: any = await fetchSwell('/products')
headers: {
Authorization: `Basic ${base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`)}`
},
})
const shopProducts = await shopProductsRes.json()
if (shopProducts) { if (shopProducts) {
return { return {

View File

@@ -1,10 +1,8 @@
import { error } from '@sveltejs/kit' import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import base64 from 'base-64'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { getRandomItem } from '$utils/functions' import { getRandomItem } from '$utils/functions'
import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' import { fetchSwell } from '$utils/functions/shopServer'
import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private'
export const load: PageServerLoad = async ({}) => { export const load: PageServerLoad = async ({}) => {
try { try {
@@ -41,12 +39,7 @@ 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
const shopProductRes: any = await fetch(`${SWELL_API_ENDPOINT}/products/${randomPoster.product_id}`, { const shopProduct: any = await fetchSwell(`/products/${randomPoster.product_id}`)
headers: {
Authorization: `Basic ${base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`)}`
},
})
const shopProduct = await shopProductRes.json()
if (shopProduct) { if (shopProduct) {
return { return {

View File

@@ -1,9 +1,7 @@
import { error } from '@sveltejs/kit' import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import base64 from 'base-64'
import { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
import { PUBLIC_SWELL_STORE_ID } from '$env/static/public' import { fetchSwell } from '$utils/functions/shopServer'
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 {
@@ -38,12 +36,7 @@ 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 shopProductRes: any = await fetch(`${SWELL_API_ENDPOINT}/products/${poster.product_id}`, { const shopProduct: any = await fetchSwell(`/products/${poster.product_id}`)
headers: {
Authorization: `Basic ${base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`)}`
},
})
const shopProduct = await shopProductRes.json()
if (shopProduct) { if (shopProduct) {
return { return {

View File

@@ -0,0 +1,25 @@
import base64 from 'base-64'
import { PUBLIC_SWELL_STORE_ID } from '$env/static/public'
import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private'
/**
* Query data from Swell REST API
*/
export const fetchSwell = async (path: string, options?: any) => {
const basicAuth: string = base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`)
const req = await fetch(`${SWELL_API_ENDPOINT}/${path}`, {
headers: {
Authorization: `Basic ${basicAuth}`
},
})
if (req.ok) {
const res = await req.json()
if (res) {
return res
}
}
}