Skip to content

Commit

Permalink
Work in progress for shopify ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Sfayson1 committed Dec 18, 2024
1 parent 89b13e9 commit 895fd8e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/utils/shopify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export async function shopifyFetch({ query, variables }) {
const endpoint = `https://${process.env.SHOPIFY_STORE_DOMAIN}/api/2023-10/graphql.json`;
const key = process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN;

if (!endpoint == !key) {
throw new Error("Shopify Store domain or access token is missing.");
}

const body = JSON.stringify({ query, variables });

try {
const result = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': key,
},
body,
});

if (!result.ok) {
const errorDetails = await result.text();
throw new Error(`Shopify API error: ${result.status} - ${errorDetails}`);
}

const responseBody = await result.json();

return {
status: result.status,
body: responseBody,
}
} catch (error) {
console.error('Error:', error);
return {
status: 500,
error: error.message || 'Error receiving data',
};
}

}

0 comments on commit 895fd8e

Please sign in to comment.