UNPKG

hydrogen-sanity

Version:
2 lines (1 loc) 2.88 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sanity/client"),t=require("@sanity/react-loader"),n=require("@shopify/hydrogen"),i=require("./_chunks-cjs/utils.cjs");const r=n.CacheLong();const a=n.CacheLong();Object.defineProperty(exports,"SanityClient",{enumerable:!0,get:function(){return e.SanityClient}}),Object.defineProperty(exports,"createClient",{enumerable:!0,get:function(){return e.createClient}}),exports.createSanityContext=function(a){const{cache:o,waitUntil:s=()=>Promise.resolve(),request:c,preview:l,defaultStrategy:u}=a,d=o?n.createWithCache({cache:o,waitUntil:s,request:c}):null;let h=a.client instanceof e.SanityClient?a.client:e.createClient(a.client);if("1"===h.config().apiVersion&&(console.warn("No API version specified, defaulting to `v2022-03-07` which supports perspectives.\nYou can find the latest version in the Sanity changelog: https://www.sanity.io/changelog"),h=h.withConfig({apiVersion:"v2022-03-07"})),l&&l.enabled){if(!l.token)throw new Error("Enabling preview mode requires a token.");const e=h.withConfig({useCdn:!1,token:l.token,perspective:"previewDrafts",stega:{...h.config().stega,enabled:!0,studioUrl:l.studioUrl}});t.setServerClient(e)}else t.setServerClient(h);return{async loadQuery(e,a,o){if(!d)return await t.loadQuery(e,a,o);const s=l&&l.enabled?n.CacheNone():o?.hydrogen?.cache||u||r,c=await i.h(e,a),h=o?.hydrogen?.shouldCacheResult??(()=>!0),y=async function({addDebugData:n}){if("development"===process.env.NODE_ENV){n({displayName:o?.hydrogen?.debug?.displayName||"query Sanity"})}return await t.loadQuery(e,a,o)};return await("run"in d?d.run({cacheKey:c,cacheStrategy:s,shouldCacheResult:h},y):d(c,s,y))},client:h,preview:l}},exports.createSanityLoader=function(r){const{withCache:o,preview:s,strategy:c}=r;let l=r.client instanceof e.SanityClient?r.client:e.createClient(r.client);if("1"===l.config().apiVersion&&(console.warn("No API version specified, defaulting to `v2022-03-07` which supports perspectives.\nYou can find the latest version in the Sanity changelog: https://www.sanity.io/changelog"),l=l.withConfig({apiVersion:"v2022-03-07"})),s&&s.enabled){if(!s.token)throw new Error("Enabling preview mode requires a token.");const e=l.withConfig({useCdn:!1,token:s.token,perspective:"previewDrafts",stega:{...l.config().stega,enabled:!0,studioUrl:s.studioUrl}});t.setServerClient(e)}else t.setServerClient(l);return{async loadQuery(e,r,l){const u=s&&s.enabled?n.CacheNone():l?.hydrogen?.cache||c||a,d=await i.h(e,r),h=l?.hydrogen?.shouldCacheResult??(()=>!0),y=async function({addDebugData:n}){if("development"===process.env.NODE_ENV){n({displayName:l?.hydrogen?.debug?.displayName||"query Sanity"})}return await t.loadQuery(e,r,l)};return await("run"in o?o.run({cacheKey:d,cacheStrategy:u,shouldCacheResult:h},y):o(d,u,y))},client:l,preview:s}};//# sourceMappingURL=index.cjs.map