UNPKG

hydrogen-sanity

Version:
2 lines (1 loc) 1.41 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sanity/preview-url-secret"),r=require("@shopify/remix-oxygen"),t=require("../_chunks-cjs/utils.cjs");exports.action=async({context:e,request:n})=>{if("POST"!==n.method)return r.json({message:"Method not allowed"},405);try{const{session:n}=e;if(!t.a(n))throw new Error("Session is not an instance of HydrogenSession");return await n.unset("projectId"),r.redirect("/")}catch(e){throw console.error(e),new Response("Unable to disable preview mode. Please check your preview configuration",{status:500})}},exports.loader=async({context:n,request:o})=>{try{const{sanity:s,session:i}=n,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.a(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:u="/"}=await e.validatePreviewUrl(c,o.url);return w?(await i.set("projectId",a),r.redirect(u)):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})}};//# sourceMappingURL=route.cjs.map