Pass parameters to Swell fetch function
This commit is contained in:
@@ -7,9 +7,19 @@ 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}`)
|
||||
// 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: {
|
||||
Authorization: `Basic ${basicAuth}`
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user