Pass parameters to Swell fetch function
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
@@ -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}`
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user