@preprio/prepr-nextjs
Version:
Next.js package for Prepr CMS preview functionality with advanced debugging and visual editing capabilities
7 lines • 2.58 kB
JavaScript
import {a}from'../chunk-NWRQFDMF.js';export{b as createPreprMiddleware}from'../chunk-NWRQFDMF.js';import {c}from'../chunk-E7ATRJ2F.js';import {headers}from'next/headers';var s=class extends Error{constructor(r,t,a,h){super(r);this.code=t;this.context=a;this.originalError=h;this.name="PreprError";}};function i(e){return c(this,null,function*(){return (yield headers()).get(e)})}function S(){return c(this,null,function*(){return i("prepr-customer-id")})}function g(){return c(this,null,function*(){return i("Prepr-Segments")})}function u(){return c(this,null,function*(){return i("Prepr-ABtesting")})}function T(){return c(this,null,function*(){let e={};return (yield headers()).forEach((r,t)=>{(t.startsWith("prepr")||t.startsWith("Prepr"))&&(e[t]=r);}),e})}function P(e){return e?e.startsWith("https://")?{valid:true}:{valid:false,error:"Token must be a valid HTTPS URL"}:{valid:false,error:"Token is required"}}function m(){return process.env.PREPR_ENV==="preview"}function x(e){if(!e)return null;try{let n=new URL(e);if(n.hostname!=="graphql.prepr.io")return null;let r=n.pathname.split("/"),t=r[r.length-1];return t&&t.length>0?t:null}catch(n){return null}}function d(e){return c(this,null,function*(){let n=P(e);if(!n.valid)throw new s(n.error,"INVALID_TOKEN","getPreprEnvironmentSegments");try{let r=yield fetch(e,{headers:{"User-Agent":`Prepr-Preview-Bar/${f()}`,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({query:`{
_Segments {
_id
name
}
}`})});if(!r.ok)throw new s(`HTTP ${r.status}: ${r.statusText}`,"HTTP_ERROR","getPreprEnvironmentSegments");let t=yield r.json();if(!t||!t.data||!t.data._Segments)throw new s("Invalid response format from Prepr API","INVALID_RESPONSE","getPreprEnvironmentSegments");return t.data._Segments}catch(r){throw r instanceof s?r:new s("Failed to fetch segments from Prepr API","FETCH_ERROR","getPreprEnvironmentSegments",r instanceof Error?r:new Error(String(r)))}})}function y(e){return c(this,null,function*(){let n=[],r=null,t=null;if(m())try{n=yield d(e),r=yield g(),t=yield u();}catch(a){console.error("Failed to fetch toolbar props:",a),n=[];}return {activeSegment:r,activeVariant:t,data:n}})}function f(){return a.version}export{s as PreprError,x as extractAccessToken,g as getActiveSegment,u as getActiveVariant,d as getPreprEnvironmentSegments,T as getPreprHeaders,S as getPreprUUID,y as getToolbarProps,m as isPreviewMode,P as validatePreprToken};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map