import { SHOPIFY_STOREFRONT_API_URL } from '@/services/shopify/client'; import { GET_SHOP_POLICIES_QUERY } from '@/graphql/policies'; export interface ShopPolicy { id: string; title: string; handle: string; body: string; url: string; } interface ShopPoliciesResponse { data?: { shop?: Record; }; } // Handles Shopify uses for each policy — also the routes under /policies/[handle]. export const POLICY_HANDLES = [ 'terms-of-service', 'privacy-policy', 'refund-policy', 'shipping-policy', 'subscription-policy', ] as const; // Policies change rarely, so this uses its own fetch (revalidated hourly) // rather than the no-store `shopifyFetch` used for carts and products. export async function getShopPolicies(): Promise { const token = process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN; const response = await fetch(SHOPIFY_STOREFRONT_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(token ? { 'X-Shopify-Storefront-Access-Token': token } : {}), }, body: JSON.stringify({ query: GET_SHOP_POLICIES_QUERY }), next: { revalidate: 3600 }, }); if (!response.ok) { console.error('Failed to load shop policies:', response.status); return []; } const json: ShopPoliciesResponse = await response.json(); const shop = json.data?.shop ?? {}; return Object.values(shop).filter((policy): policy is ShopPolicy => Boolean(policy?.handle) ); } export async function getShopPolicy( handle: string ): Promise { const policies = await getShopPolicies(); return policies.find((policy) => policy.handle === handle) ?? null; }