Pass parameters to Swell fetch function

This commit is contained in:
2022-09-18 21:48:31 +02:00
parent f31f25aac9
commit 719c7b6f87
2 changed files with 15 additions and 3 deletions

View File

@@ -55,7 +55,9 @@ export const load: PageServerLoad = async () => {
/** /**
* Get products data from Swell * Get products data from Swell
*/ */
const shopProducts: any = await fetchSwell('/products') const shopProducts: any = await fetchSwell('/products', {
category: 'posters',
})
if (shopProducts) { if (shopProducts) {
return { return {

View File

@@ -7,9 +7,19 @@ import { SWELL_API_TOKEN, SWELL_API_ENDPOINT } from '$env/static/private'
* Query data from Swell REST API * Query data from Swell REST API
*/ */
export const fetchSwell = async (path: string, options?: any) => { export const fetchSwell = async (path: string, options?: any) => {
const basicAuth: string = base64.encode(`${PUBLIC_SWELL_STORE_ID}:${SWELL_API_TOKEN}`) // Define options
const defaultOptions = {
'where[active]': true,
limit: 25,
page: 1,
}
options = { ...defaultOptions, ...options }
const parameters = new URLSearchParams(options).toString()
const req = await fetch(`${SWELL_API_ENDPOINT}/${path}`, { // 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: { headers: {
Authorization: `Basic ${basicAuth}` Authorization: `Basic ${basicAuth}`
}, },