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