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

@@ -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
}
}
}