35 lines
917 B
TypeScript
35 lines
917 B
TypeScript
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) => {
|
|
// Define options
|
|
const defaultOptions = {
|
|
'where[active]': true,
|
|
limit: 25,
|
|
page: 1,
|
|
}
|
|
options = { ...defaultOptions, ...options }
|
|
const parameters = new URLSearchParams(options).toString()
|
|
|
|
// Make request
|
|
const url = `${SWELL_API_ENDPOINT}${path}?${parameters}`
|
|
const basicAuth: string = base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`)
|
|
const req = await fetch(url, {
|
|
headers: {
|
|
Authorization: `Basic ${basicAuth}`
|
|
},
|
|
})
|
|
|
|
if (req.ok) {
|
|
const res = await req.json()
|
|
|
|
if (res) {
|
|
return res
|
|
}
|
|
}
|
|
} |