@houzhen888/groove-grow 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{b as T,o as y}from"./chunk-D5TMAVB3.js";var e=class extends Error{constructor(c,t,I,d){super(t);this.code=c;this.status=I;this.requestId=d;this.name="GrooveError"}code;status;requestId},A=Symbol("GrooveNoContent");function P(a){let h=a.webView??"hosted";if(!["hosted","embedded"].includes(h))throw new e("INVALID_REQUEST","Invalid Web View mode");let c=a.scope?T.parse(a.scope):void 0,t=new URL(a.baseUrl);if(t.username||t.password||t.search||t.hash)throw new e("INVALID_URL","baseUrl must not contain credentials, query, or fragment");if(t.protocol!=="https:"&&!(t.protocol==="http:"&&["localhost","127.0.0.1","[::1]"].includes(t.hostname)))throw new e("INVALID_URL","Use HTTPS, or HTTP on localhost during development");let I=a.fetch??globalThis.fetch.bind(globalThis),d=a.timeoutMs??1e4;if(!Number.isFinite(d)||d<=0||d>12e4)throw new e("INVALID_TIMEOUT","timeoutMs must be between 1 and 120000");let u=null,f,l=0,w=new Set;function v(){l+=1,u=null;for(let n of w)n.abort();w.clear()}async function N(n,O={},E="POST"){if(!u)throw new e("NOT_IDENTIFIED","Call identify with a server-issued customer token first");let m=l,r=new AbortController;w.add(r);let b=()=>m!==l?new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight"):new e("TIMEOUT","Groove request timed out"),p,S=new Promise((o,s)=>{p=()=>s(b()),r.signal.addEventListener("abort",p,{once:!0})}),_=setTimeout(()=>r.abort(),d),R=async()=>{let o=await I(`${t.href.replace(/\/$/,"")}/api/v1/${n}`,{method:E,headers:{"Content-Type":"application/json",Authorization:`Bearer ${u}`,...c?{"X-Groove-Project":c.projectId,"X-Groove-Environment":c.environment}:{}},body:E==="POST"?JSON.stringify(O):void 0,signal:r.signal,credentials:"omit",cache:"no-store",redirect:"error"});if(r.signal.aborted)throw b();let s=o.status===204?A:await o.json();if(r.signal.aborted)throw b();if(m!==l)throw new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight");if(!o.ok){let i=y.safeParse(s);throw new e(i.success?i.data.error.code:"HTTP_ERROR",i.success?i.data.error.message:"Groove request failed",o.status,o.headers.get("X-Request-Id")??void 0)}if(c&&s&&typeof s=="object"&&"scope"in s){let i=T.safeParse(s.scope);if(!i.success||i.data.projectId!==c.projectId||i.data.environment!==c.environment)throw new e("SCOPE_MISMATCH","Response scope does not match this SDK instance")}let g=o.headers.get("X-Grow-Web-View");if(g){if(!/^\/web-view\/renderer\/[a-f0-9]{64}$/.test(g))throw new e("INVALID_RESPONSE","Invalid Web View renderer metadata");f=g}return s};try{return await Promise.race([R(),S])}catch(o){throw m!==l?new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight"):o instanceof e?o:new e(r.signal.aborted?"TIMEOUT":"NETWORK_ERROR",r.signal.aborted?"Groove request timed out":"Unable to read the Groove response")}finally{clearTimeout(_),r.signal.removeEventListener("abort",p),w.delete(r)}}return{webViewUrl(){if(h!=="embedded"){if(!f)throw new e("INVALID_RESPONSE","The gateway did not provide a hosted Web View renderer");return new URL(f,t).href}},identify(n){if(!n.trim()||/\s/.test(n)||n.length>4096)throw new e("INVALID_TOKEN","A nonempty server-issued token is required");v(),u=n},reset:v,request:N}}export{e as a,P as b};
1
+ import{b as T,o as y}from"./chunk-B5O7FDKE.js";var e=class extends Error{constructor(c,t,I,d){super(t);this.code=c;this.status=I;this.requestId=d;this.name="GrooveError"}code;status;requestId},A=Symbol("GrooveNoContent");function P(a){let h=a.webView??"hosted";if(!["hosted","embedded"].includes(h))throw new e("INVALID_REQUEST","Invalid Web View mode");let c=a.scope?T.parse(a.scope):void 0,t=new URL(a.baseUrl);if(t.username||t.password||t.search||t.hash)throw new e("INVALID_URL","baseUrl must not contain credentials, query, or fragment");if(t.protocol!=="https:"&&!(t.protocol==="http:"&&["localhost","127.0.0.1","[::1]"].includes(t.hostname)))throw new e("INVALID_URL","Use HTTPS, or HTTP on localhost during development");let I=a.fetch??globalThis.fetch.bind(globalThis),d=a.timeoutMs??1e4;if(!Number.isFinite(d)||d<=0||d>12e4)throw new e("INVALID_TIMEOUT","timeoutMs must be between 1 and 120000");let u=null,f,l=0,w=new Set;function v(){l+=1,u=null;for(let n of w)n.abort();w.clear()}async function N(n,O={},E="POST"){if(!u)throw new e("NOT_IDENTIFIED","Call identify with a server-issued customer token first");let m=l,r=new AbortController;w.add(r);let b=()=>m!==l?new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight"):new e("TIMEOUT","Groove request timed out"),p,S=new Promise((o,s)=>{p=()=>s(b()),r.signal.addEventListener("abort",p,{once:!0})}),_=setTimeout(()=>r.abort(),d),R=async()=>{let o=await I(`${t.href.replace(/\/$/,"")}/api/v1/${n}`,{method:E,headers:{"Content-Type":"application/json",Authorization:`Bearer ${u}`,...c?{"X-Groove-Project":c.projectId,"X-Groove-Environment":c.environment}:{}},body:E==="POST"?JSON.stringify(O):void 0,signal:r.signal,credentials:"omit",cache:"no-store",redirect:"error"});if(r.signal.aborted)throw b();let s=o.status===204?A:await o.json();if(r.signal.aborted)throw b();if(m!==l)throw new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight");if(!o.ok){let i=y.safeParse(s);throw new e(i.success?i.data.error.code:"HTTP_ERROR",i.success?i.data.error.message:"Groove request failed",o.status,o.headers.get("X-Request-Id")??void 0)}if(c&&s&&typeof s=="object"&&"scope"in s){let i=T.safeParse(s.scope);if(!i.success||i.data.projectId!==c.projectId||i.data.environment!==c.environment)throw new e("SCOPE_MISMATCH","Response scope does not match this SDK instance")}let g=o.headers.get("X-Grow-Web-View");if(g){if(!/^\/web-view\/renderer\/[a-f0-9]{64}$/.test(g))throw new e("INVALID_RESPONSE","Invalid Web View renderer metadata");f=g}return s};try{return await Promise.race([R(),S])}catch(o){throw m!==l?new e("IDENTITY_CHANGED","Customer identity changed while the request was in flight"):o instanceof e?o:new e(r.signal.aborted?"TIMEOUT":"NETWORK_ERROR",r.signal.aborted?"Groove request timed out":"Unable to read the Groove response")}finally{clearTimeout(_),r.signal.removeEventListener("abort",p),w.delete(r)}}return{webViewUrl(){if(h!=="embedded"){if(!f)throw new e("INVALID_RESPONSE","The gateway did not provide a hosted Web View renderer");return new URL(f,t).href}},identify(n){if(!n.trim()||/\s/.test(n)||n.length>4096)throw new e("INVALID_TOKEN","A nonempty server-issued token is required");v(),u=n},reset:v,request:N}}export{e as a,P as b};