Create reusable Swell fetch functions
This commit is contained in:
25
src/utils/functions/shopServer.ts
Normal file
25
src/utils/functions/shopServer.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user