UNPKG

hydrogen-sanity

Version:
2 lines (1 loc) 1.35 kB
import{validatePreviewUrl as e}from"@sanity/preview-url-secret";import{json as o,redirect as n}from"@shopify/remix-oxygen";import{a as t}from"../_chunks-es/utils.js";const r=async({context:e,request:r})=>{if("POST"!==r.method)return o({message:"Method not allowed"},405);try{const{session:o}=e;if(!t(o))throw new Error("Session is not an instance of HydrogenSession");return await o.unset("projectId"),n("/")}catch(e){throw console.error(e),new Response("Unable to disable preview mode. Please check your preview configuration",{status:500})}},s=async({context:o,request:r})=>{try{const{sanity:s,session:i}=o,a=s.client.config().projectId;if(!s.preview)return new Response("Preview mode is not enabled in this environment.",{status:403});if(!s.preview.token)throw new Error("Enabling preview mode requires a token.");if(!a)throw new Error("No `projectId` found in the client config.");if(!t(i))throw new Error("Session is not an instance of HydrogenSession");const c=s.client.withConfig({useCdn:!1,token:s.preview.token}),{isValid:w,redirectTo:p="/"}=await e(c,r.url);return w?(await i.set("projectId",a),n(p)):new Response("Invalid secret",{status:401})}catch(e){throw console.error(e),new Response("Unable to enable preview mode. Please check your preview configuration",{status:500})}};export{r as action,s as loader};//# sourceMappingURL=route.js.map