@shopify/hydrogen 2023.7.1 → 2023.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ var react = require('react');
6
6
  var react$1 = require('@remix-run/react');
7
7
  var jsxRuntime = require('react/jsx-runtime');
8
8
 
9
- var ze=Object.defineProperty;var Ye=(e,t)=>()=>(e&&(t=e(e=0)),t);var Xe=(e,t)=>{for(var r in t)ze(e,r,{get:t[r],enumerable:!0});};var Ne={};Xe(Ne,{default:()=>Le,logSeoTags:()=>ve});function Le({headTags:e}){return ve(e),null}function ve(e){console.log(" "),console.log("%cSEO Meta Tags",`${xt}`),console.log(" "),e.forEach(t=>{if(t.tag==="script"){if(console.log("%c\u2022 JSON LD ",Z),t.children)try{console.table(JSON.parse(t.children),["name","content"]);}catch{console.log(t.children);}}else {if(console.log(`%c\u2022 ${t.tag} `,Z),t.children)if(typeof t.children=="string")console.log(`\u21B3 ${t.children}`);else try{Object.entries(JSON.parse(t.children)).map(([r,a])=>console.log(`\u21B3 ${a}`));}catch{console.log(t.children);}if(t.props.property==="og:image:url"){let r=t.props.content;At(r).then(a=>{let n=`font-size: 400px; padding: 10px; background: white url(${a}) no-repeat center; background-size: contain;`;console.log("%c\u2022 Share image preview",Z),console.log("%c ",n),console.log(`\u21B3 ${r}`);}).catch(a=>{console.error(a);});}Object.entries(t.props).map(([r,a])=>{console.log(`\u21B3 ${r} \u2192 ${a}`);});}console.log(" ");});}async function At(e){let a=await(await(await fetch(e)).blob()).arrayBuffer();return `data:image/png;base64,${Ot(a)}`}function Ot(e){let t="",r=new Uint8Array(e),a=r.byteLength;for(let n=0;n<a;n++)t+=String.fromCharCode(r[n]);return btoa(t)}var Z,xt,Fe=Ye(()=>{Z="text-transform: uppercase;",xt="text-transform: uppercase; font-weight: bold; text-transform: uppercase;font-weight: bold";});function me(e){let t=Array.isArray(e)?e:[e],r="";for(let a of t)a!=null&&(typeof a=="object"?!!a.body&&typeof a.body=="string"?r+=a.body:r+=JSON.stringify(a):r+=a);return encodeURIComponent(r)}var k="public",Ze="private",_="no-store",Ce={maxAge:"max-age",staleWhileRevalidate:"stale-while-revalidate",sMaxAge:"s-maxage",staleIfError:"stale-if-error"};function P(e){let t=[];return Object.keys(e).forEach(r=>{r==="mode"?t.push(e[r]):Ce[r]&&t.push(`${Ce[r]}=${e[r]}`);}),t.join(", ")}function Q(){return {mode:_}}function he(e){if(e?.mode&&e?.mode!==k&&e?.mode!==Ze)throw Error("'mode' must be either 'public' or 'private'")}function I(e){return he(e),{mode:k,maxAge:1,staleWhileRevalidate:9,...e}}function H(e){return he(e),{mode:k,maxAge:3600,staleWhileRevalidate:82800,...e}}function G(e){return e}function w(e){return String(e).includes("__proto__")?JSON.parse(e,et):JSON.parse(e)}function et(e,t){if(e!=="__proto__")return t}function U(e,t){return e&&t?{...e,...t}:e||I()}function V(e){return P(U(e))}async function tt(e,t){if(!e)return;let r=await e.match(t);if(!r){return}return r}async function rt(e,t,r,a){if(!e)return;let n=U(a),o=V(U(n,{maxAge:(n.maxAge||0)+(n.staleWhileRevalidate||0)})),i=V(U(n));r.headers.set("cache-control",o),r.headers.set("real-cache-control",i),r.headers.set("cache-put-date",new Date().toUTCString()),await e.put(t,r);}async function at(e,t){!e||await e.delete(t);}function nt(e,t){let r=e.headers.get("real-cache-control"),a=0;if(r){let o=r.match(/max-age=(\d*)/);o&&o.length>1&&(a=parseFloat(o[1]));}return [(new Date().valueOf()-new Date(t).valueOf())/1e3,a]}function ot(e,t){let r=t.headers.get("cache-put-date");if(!r)return !1;let[a,n]=nt(t,r),o=a>n;return o}var E={get:tt,set:rt,delete:at,generateDefaultCacheControlHeader:V,isStale:ot};function B(e){return `https://shopify.dev/?${e}`}function it(e){return e||I()}async function Se(e,t){if(!e)return;let r=B(t),a=new Request(r),n=await E.get(e,a);if(!n)return;let o=await n.text();try{return [w(o),n]}catch{return [o,n]}}async function j(e,t,r,a){if(!e)return;let n=B(t),o=new Request(n),i=new Response(JSON.stringify(r));await E.set(e,o,i,it(a));}function Ie(e,t){return E.isStale(new Request(B(e)),t)}function st(e,t){return [e,{status:t.status,statusText:t.statusText,headers:Array.from(t.headers.entries())}]}function Re([e,t]){return [e,new Response(e,t)]}var Te=e=>!e?.errors,W=new Set;async function J(e,t,{strategy:r=I(),cacheInstance:a,shouldCacheResult:n=()=>!0,waitUntil:o}){if(!a||!r||r.mode===_)return t();let i=me([...typeof e=="string"?[e]:e]),s=await Se(a,i);if(s){let[c,p]=s;if(!W.has(i)&&Ie(i,p)){W.add(i);let d=Promise.resolve().then(async()=>{try{let l=await t();n(l)&&await j(a,i,l,r);}catch(l){l.message&&(l.message="SWR in sub-request failed: "+l.message),console.error(l);}finally{W.delete(i);}});o?.(d);}return c}let u=await t();if(n(u)){let c=j(a,i,u,r);o?.(c);}return u}async function xe(e,t,{cacheInstance:r,cache:a,cacheKey:n=[e,t],shouldCacheResponse:o=()=>!0,waitUntil:i,returnType:s="json"}={}){return !a&&(!t.method||t.method==="GET")&&(a=I()),J(n,async()=>{let u=await fetch(e,t),c;try{c=await u[s]();}catch{c=await u.text();}return st(c,u)},{cacheInstance:r,waitUntil:i,strategy:a??null,shouldCacheResult:u=>o(...Re(u))}).then(Re)}var Ae="Custom-Storefront-Request-Group-ID";function Oe(){return typeof crypto<"u"&&!!crypto.randomUUID?crypto.randomUUID():`weak-${Math.random().toString(16).substring(2)}`}var K="2023.7.1";var we=class extends Error{},yt=e=>e instanceof we,ft=/(^|}\s)query[\s({]/im,gt=/(^|}\s)mutation[\s({]/im;function De(e){return e.replace(/\s*#.*$/gm,"").replace(/\s+/gm," ").trim()}var mt={language:"EN",country:"US"};function qr(e){let {storefrontHeaders:t,cache:r,waitUntil:a,buyerIp:n,i18n:o,requestGroupId:i,storefrontId:s,...u}=e,{getPublicTokenHeaders:p,getPrivateTokenHeaders:d,getStorefrontApiUrl:l,getShopifyDomain:h}=hydrogenReact.createStorefrontClient(u),m=(u.privateStorefrontToken?d:p)({contentType:"json",buyerIp:t?.buyerIp||n});if(m[Ae]=t?.requestGroupId||i||Oe(),s&&(m[hydrogenReact.SHOPIFY_STOREFRONT_ID_HEADER]=s),(m["user-agent"]=`Hydrogen ${K}`),t&&t.cookie){let y=hydrogenReact.getShopifyCookies(t.cookie??"");y[hydrogenReact.SHOPIFY_Y]&&(m[hydrogenReact.SHOPIFY_STOREFRONT_Y_HEADER]=y[hydrogenReact.SHOPIFY_Y]),y[hydrogenReact.SHOPIFY_S]&&(m[hydrogenReact.SHOPIFY_STOREFRONT_S_HEADER]=y[hydrogenReact.SHOPIFY_S]);}async function O({query:y,mutation:S,variables:A,cache:Ve,headers:b=[],storefrontApiVersion:Be}){let je=b instanceof Headers?Object.fromEntries(b.entries()):Array.isArray(b)?Object.fromEntries(b):b;y=y??S;let M={...A};o&&(!A?.country&&/\$country/.test(y)&&(M.country=o.country),!A?.language&&/\$language/.test(y)&&(M.language=o.language));let We=l({storefrontApiVersion:Be}),Je={method:"POST",headers:{...m,...je},body:JSON.stringify({query:y,variables:M})},[$,ye]=await xe(We,Je,{cacheInstance:S?void 0:r,cache:Ve||I(),shouldCacheResponse:Te,waitUntil:a}),fe={response:ye,type:S?"mutation":"query",query:y,queryVariables:M,errors:void 0};if(!ye.ok){let q;try{q=w($);}catch{q=[{message:$}];}Me({...fe,errors:q});}let{data:Ke,errors:ge}=$;return ge?.length&&Me({...fe,errors:ge,ErrorConstructor:we}),Ke}return {storefront:{query:(y,S)=>{if(y=De(y),gt.test(y))throw new Error("[h2:error:storefront.query] Cannot execute mutations");return O({...S,query:y})},mutate:(y,S)=>{if(y=De(y),ft.test(y))throw new Error("[h2:error:storefront.mutate] Cannot execute queries");return O({...S,mutation:y})},cache:r,CacheNone:Q,CacheLong:H,CacheShort:I,CacheCustom:G,generateCacheControlHeader:P,getPublicTokenHeaders:p,getPrivateTokenHeaders:d,getShopifyDomain:h,getApiUrl:l,isApiError:yt,i18n:o??mt}}}function Me({response:e,errors:t,type:r,query:a,queryVariables:n,ErrorConstructor:o=Error}){let i=e.headers.get("x-request-id"),s=(typeof t=="string"?t:t?.map?.(u=>u.message).join(`
9
+ var ze=Object.defineProperty;var Ye=(e,t)=>()=>(e&&(t=e(e=0)),t);var Xe=(e,t)=>{for(var r in t)ze(e,r,{get:t[r],enumerable:!0});};var Ne={};Xe(Ne,{default:()=>Le,logSeoTags:()=>ve});function Le({headTags:e}){return ve(e),null}function ve(e){console.log(" "),console.log("%cSEO Meta Tags",`${xt}`),console.log(" "),e.forEach(t=>{if(t.tag==="script"){if(console.log("%c\u2022 JSON LD ",Z),t.children)try{console.table(JSON.parse(t.children),["name","content"]);}catch{console.log(t.children);}}else {if(console.log(`%c\u2022 ${t.tag} `,Z),t.children)if(typeof t.children=="string")console.log(`\u21B3 ${t.children}`);else try{Object.entries(JSON.parse(t.children)).map(([r,a])=>console.log(`\u21B3 ${a}`));}catch{console.log(t.children);}if(t.props.property==="og:image:url"){let r=t.props.content;At(r).then(a=>{let n=`font-size: 400px; padding: 10px; background: white url(${a}) no-repeat center; background-size: contain;`;console.log("%c\u2022 Share image preview",Z),console.log("%c ",n),console.log(`\u21B3 ${r}`);}).catch(a=>{console.error(a);});}Object.entries(t.props).map(([r,a])=>{console.log(`\u21B3 ${r} \u2192 ${a}`);});}console.log(" ");});}async function At(e){let a=await(await(await fetch(e)).blob()).arrayBuffer();return `data:image/png;base64,${Ot(a)}`}function Ot(e){let t="",r=new Uint8Array(e),a=r.byteLength;for(let n=0;n<a;n++)t+=String.fromCharCode(r[n]);return btoa(t)}var Z,xt,Fe=Ye(()=>{Z="text-transform: uppercase;",xt="text-transform: uppercase; font-weight: bold; text-transform: uppercase;font-weight: bold";});function me(e){let t=Array.isArray(e)?e:[e],r="";for(let a of t)a!=null&&(typeof a=="object"?!!a.body&&typeof a.body=="string"?r+=a.body:r+=JSON.stringify(a):r+=a);return encodeURIComponent(r)}var k="public",Ze="private",_="no-store",Ce={maxAge:"max-age",staleWhileRevalidate:"stale-while-revalidate",sMaxAge:"s-maxage",staleIfError:"stale-if-error"};function P(e){let t=[];return Object.keys(e).forEach(r=>{r==="mode"?t.push(e[r]):Ce[r]&&t.push(`${Ce[r]}=${e[r]}`);}),t.join(", ")}function Q(){return {mode:_}}function he(e){if(e?.mode&&e?.mode!==k&&e?.mode!==Ze)throw Error("'mode' must be either 'public' or 'private'")}function I(e){return he(e),{mode:k,maxAge:1,staleWhileRevalidate:9,...e}}function H(e){return he(e),{mode:k,maxAge:3600,staleWhileRevalidate:82800,...e}}function G(e){return e}function M(e){return String(e).includes("__proto__")?JSON.parse(e,et):JSON.parse(e)}function et(e,t){if(e!=="__proto__")return t}function U(e,t){return e&&t?{...e,...t}:e||I()}function V(e){return P(U(e))}async function tt(e,t){if(!e)return;let r=await e.match(t);if(!r){return}return r}async function rt(e,t,r,a){if(!e)return;let n=U(a),o=V(U(n,{maxAge:(n.maxAge||0)+(n.staleWhileRevalidate||0)})),i=V(U(n));r.headers.set("cache-control",o),r.headers.set("real-cache-control",i),r.headers.set("cache-put-date",new Date().toUTCString()),await e.put(t,r);}async function at(e,t){!e||await e.delete(t);}function nt(e,t){let r=e.headers.get("real-cache-control"),a=0;if(r){let o=r.match(/max-age=(\d*)/);o&&o.length>1&&(a=parseFloat(o[1]));}return [(new Date().valueOf()-new Date(t).valueOf())/1e3,a]}function ot(e,t){let r=t.headers.get("cache-put-date");if(!r)return !1;let[a,n]=nt(t,r),o=a>n;return o}var E={get:tt,set:rt,delete:at,generateDefaultCacheControlHeader:V,isStale:ot};function B(e){return `https://shopify.dev/?${e}`}function it(e){return e||I()}async function Se(e,t){if(!e)return;let r=B(t),a=new Request(r),n=await E.get(e,a);if(!n)return;let o=await n.text();try{return [M(o),n]}catch{return [o,n]}}async function j(e,t,r,a){if(!e)return;let n=B(t),o=new Request(n),i=new Response(JSON.stringify(r));await E.set(e,o,i,it(a));}function Ie(e,t){return E.isStale(new Request(B(e)),t)}function st(e,t){return [e,{status:t.status,statusText:t.statusText,headers:Array.from(t.headers.entries())}]}function Re([e,t]){return [e,new Response(e,t)]}var Te=e=>!e?.errors,W=new Set;async function J(e,t,{strategy:r=I(),cacheInstance:a,shouldCacheResult:n=()=>!0,waitUntil:o}){if(!a||!r||r.mode===_)return t();let i=me([...typeof e=="string"?[e]:e]),s=await Se(a,i);if(s){let[c,p]=s;if(!W.has(i)&&Ie(i,p)){W.add(i);let d=Promise.resolve().then(async()=>{try{let l=await t();n(l)&&await j(a,i,l,r);}catch(l){l.message&&(l.message="SWR in sub-request failed: "+l.message),console.error(l);}finally{W.delete(i);}});o?.(d);}return c}let u=await t();if(n(u)){let c=j(a,i,u,r);o?.(c);}return u}async function xe(e,t,{cacheInstance:r,cache:a,cacheKey:n=[e,t],shouldCacheResponse:o=()=>!0,waitUntil:i,returnType:s="json"}={}){return !a&&(!t.method||t.method==="GET")&&(a=I()),J(n,async()=>{let u=await fetch(e,t),c;try{c=await u[s]();}catch{try{c=await u.text();}catch{throw new Error(`Storefront API response code: ${u.status} (Request Id: ${u.headers.get("x-request-id")})`)}}return st(c,u)},{cacheInstance:r,waitUntil:i,strategy:a??null,shouldCacheResult:u=>o(...Re(u))}).then(Re)}var Ae="Custom-Storefront-Request-Group-ID";function Oe(){return typeof crypto<"u"&&!!crypto.randomUUID?crypto.randomUUID():`weak-${Math.random().toString(16).substring(2)}`}var K="2023.7.2";var Me=class extends Error{},yt=e=>e instanceof Me,ft=/(^|}\s)query[\s({]/im,gt=/(^|}\s)mutation[\s({]/im;function De(e){return e.replace(/\s*#.*$/gm,"").replace(/\s+/gm," ").trim()}var mt={language:"EN",country:"US"};function qr(e){let {storefrontHeaders:t,cache:r,waitUntil:a,buyerIp:n,i18n:o,requestGroupId:i,storefrontId:s,...u}=e,{getPublicTokenHeaders:p,getPrivateTokenHeaders:d,getStorefrontApiUrl:l,getShopifyDomain:h}=hydrogenReact.createStorefrontClient(u),m=(u.privateStorefrontToken?d:p)({contentType:"json",buyerIp:t?.buyerIp||n});if(m[Ae]=t?.requestGroupId||i||Oe(),s&&(m[hydrogenReact.SHOPIFY_STOREFRONT_ID_HEADER]=s),(m["user-agent"]=`Hydrogen ${K}`),t&&t.cookie){let y=hydrogenReact.getShopifyCookies(t.cookie??"");y[hydrogenReact.SHOPIFY_Y]&&(m[hydrogenReact.SHOPIFY_STOREFRONT_Y_HEADER]=y[hydrogenReact.SHOPIFY_Y]),y[hydrogenReact.SHOPIFY_S]&&(m[hydrogenReact.SHOPIFY_STOREFRONT_S_HEADER]=y[hydrogenReact.SHOPIFY_S]);}async function O({query:y,mutation:S,variables:A,cache:Ve,headers:b=[],storefrontApiVersion:Be}){let je=b instanceof Headers?Object.fromEntries(b.entries()):Array.isArray(b)?Object.fromEntries(b):b;y=y??S;let w={...A};o&&(!A?.country&&/\$country/.test(y)&&(w.country=o.country),!A?.language&&/\$language/.test(y)&&(w.language=o.language));let We=l({storefrontApiVersion:Be}),Je={method:"POST",headers:{...m,...je},body:JSON.stringify({query:y,variables:w})},[$,ye]=await xe(We,Je,{cacheInstance:S?void 0:r,cache:Ve||I(),shouldCacheResponse:Te,waitUntil:a}),fe={response:ye,type:S?"mutation":"query",query:y,queryVariables:w,errors:void 0};if(!ye.ok){let q;try{q=M($);}catch{q=[{message:$}];}we({...fe,errors:q});}let{data:Ke,errors:ge}=$;return ge?.length&&we({...fe,errors:ge,ErrorConstructor:Me}),Ke}return {storefront:{query:(y,S)=>{if(y=De(y),gt.test(y))throw new Error("[h2:error:storefront.query] Cannot execute mutations");return O({...S,query:y})},mutate:(y,S)=>{if(y=De(y),ft.test(y))throw new Error("[h2:error:storefront.mutate] Cannot execute queries");return O({...S,mutation:y})},cache:r,CacheNone:Q,CacheLong:H,CacheShort:I,CacheCustom:G,generateCacheControlHeader:P,getPublicTokenHeaders:p,getPrivateTokenHeaders:d,getShopifyDomain:h,getApiUrl:l,isApiError:yt,i18n:o??mt}}}function we({response:e,errors:t,type:r,query:a,queryVariables:n,ErrorConstructor:o=Error}){let i=e.headers.get("x-request-id"),s=(typeof t=="string"?t:t?.map?.(u=>u.message).join(`
10
10
  `))||`API response error: ${e.status}`;throw new o(`[h2:error:storefront.${r}] `+s+(i?` - Request ID: ${i}`:""),{cause:{errors:t,requestId:i}})}function Hr(e){let{cache:t,waitUntil:r}=e;return function(n,o,i){return J(n,i,{strategy:o,cacheInstance:t,waitUntil:r})}}var z=class{#e;constructor(){this.#e=new Map;}add(t){throw new Error("Method not implemented. Use `put` instead.")}addAll(t){throw new Error("Method not implemented. Use `put` instead.")}matchAll(t,r){throw new Error("Method not implemented. Use `match` instead.")}async put(t,r){if(t.method!=="GET")throw new TypeError("Cannot cache response to non-GET request.");if(r.status===206)throw new TypeError("Cannot cache response to a range request (206 Partial Content).");if(r.headers.get("vary")?.includes("*"))throw new TypeError("Cannot cache response with 'Vary: *' header.");this.#e.set(t.url,{body:new Uint8Array(await r.arrayBuffer()),status:r.status,headers:[...r.headers],timestamp:Date.now()});}async match(t){if(t.method!=="GET")return;let r=this.#e.get(t.url);if(!r)return;let{body:a,timestamp:n,...o}=r,i=new Headers(o.headers),s=i.get("cache-control")||i.get("real-cache-control")||"",u=parseInt(s.match(/max-age=(\d+)/)?.[1]||"0",10),c=parseInt(s.match(/stale-while-revalidate=(\d+)/)?.[1]||"0",10),p=(Date.now()-n)/1e3;if(p>u+c){this.#e.delete(t.url);return}let l=p>u;return i.set("cache",l?"STALE":"HIT"),i.set("date",new Date(n).toUTCString()),new Response(a,{status:o.status??200,headers:i})}async delete(t){return this.#e.has(t.url)?(this.#e.delete(t.url),!0):!1}keys(t){let r=[];for(let a of this.#e.keys())(!t||t.url===a)&&r.push(new Request(a));return Promise.resolve(r)}};async function Ct(e){let{storefront:t,request:r,noAdminRedirect:a,response:n=new Response("Not Found",{status:404})}=e,{pathname:o,search:i}=new URL(r.url),s=o+i;if(o==="/admin"&&!a)return serverRuntime.redirect(`${t.getShopifyDomain()}/admin`);try{let{urlRedirects:u}=await t.query(St,{variables:{query:"path:"+s}}),c=u?.edges?.[0]?.node?.target;if(c)return new Response(null,{status:301,headers:{location:c}});let p=new URLSearchParams(i),d=p.get("return_to")||p.get("redirect");if(d){if(ht(d))return serverRuntime.redirect(d);console.warn(`Cross-domain redirects are not supported. Tried to redirect from ${s} to ${d}`);}}catch(u){console.error(`Failed to fetch redirects from Storefront API for route ${s}`,u);}return n}function ht(e){try{new URL(e);}catch{return !0}return !1}var St=`#graphql
11
11
  query redirects($query: String) {
12
12
  urlRedirects(first: 1, query: $query) {