@ecodrix/erix-api 3.2.0 → 3.3.1

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
- "use strict";var P=Object.create;var E=Object.defineProperty,v=Object.defineProperties,D=Object.getOwnPropertyDescriptor,L=Object.getOwnPropertyDescriptors,W=Object.getOwnPropertyNames,d=Object.getOwnPropertySymbols,C=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var h=(s,e,t)=>e in s?E(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,m=(s,e)=>{for(var t in e||(e={}))T.call(e,t)&&h(s,t,e[t]);if(d)for(var t of d(e))y.call(e,t)&&h(s,t,e[t]);return s},w=(s,e)=>v(s,L(e));var N=(s,e)=>{var t={};for(var o in s)T.call(s,o)&&e.indexOf(o)<0&&(t[o]=s[o]);if(s!=null&&d)for(var o of d(s))e.indexOf(o)<0&&y.call(s,o)&&(t[o]=s[o]);return t};var U=(s,e)=>{for(var t in e)E(s,t,{get:e[t],enumerable:!0})},V=(s,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of W(e))!T.call(s,n)&&n!==t&&E(s,n,{get:()=>e[n],enumerable:!(o=D(e,n))||o.enumerable});return s};var M=(s,e,t)=>(t=s!=null?P(C(s)):{},V(e||!s||!s.__esModule?E(t,"default",{value:s,enumerable:!0}):t,s)),k=s=>V(E({},"__esModule",{value:!0}),s);var a=(s,e,t)=>h(s,typeof e!="symbol"?e+"":e,t);var q={};U(q,{WebchatVisitor:()=>O,WebchatVisitorError:()=>l});module.exports=k(q);var f="https://api.ecodrix.com";function S(s="v1",e=f){return`${e.replace(/\/+$/,"")}/${s}/api`}var I=class extends Error{constructor(e){super(e),this.name="EcodrixError"}},R=class extends I{constructor(t,o,n){super(t);a(this,"status");a(this,"code");a(this,"issues");a(this,"field");this.name="APIError",this.status=o,this.code=n}};var l=class extends R{constructor(t,o,n,r){super(t,n,o);a(this,"field");this.name="WebchatVisitorError",this.field=r}},F=3e4,B=6e4,H=new Set(["RATE_LIMITED","INTERNAL_ERROR"]),K={400:"VALIDATION_ERROR",403:"ORIGIN_NOT_ALLOWED",404:"NOT_FOUND",429:"RATE_LIMITED"},O=class{constructor(e){a(this,"options",e);a(this,"host");a(this,"base");a(this,"socket",null);a(this,"opening",null);a(this,"retry");var t;this.host=((t=e.apiUrl)!=null?t:f).replace(/\/+$/,""),this.base=`${S("v1",this.host)}/infra/connect/webhooks/webchat/${encodeURIComponent(e.clientCode)}`}send(e,t={}){let{sessionId:o,parent:n}=this.options,i=t,{page:r}=i,g=N(i,["page"]);return this.call("/message",m(m({sessionId:o,text:e,parent:n},g),r&&{page:w(m({},r),{path:r.path.split(/[?#]/)[0]})}))}messages(){let{sessionId:e,parent:t}=this.options;return this.call(`/messages?${new URLSearchParams({sessionId:e,parent:t})}`)}contact(e){let{sessionId:t,parent:o}=this.options;return this.call("/contact",m({sessionId:t,parent:o},e))}connect(e){if(this.opening)return this.opening;let{clientCode:t,sessionId:o,parent:n}=this.options,r=import("socket.io-client").then(({io:g})=>{if(this.opening!==r)return;let i=g(`${this.host}/visitor`,{auth:{clientCode:t,sessionId:o,parent:n}});this.socket=i;let u=!1;i.on("connect",()=>{var p;u&&((p=e.onReconnect)==null||p.call(e)),u=!1}),i.on("disconnect",()=>{u=!0}),i.on("connect_error",p=>{var A,_,b;u=!0;let c=(_=(A=p.data)==null?void 0:A.code)!=null?_:"NETWORK_ERROR";(b=e.onError)==null||b.call(e,new l(p.message,c)),!i.active&&H.has(c)&&(clearTimeout(this.retry),this.retry=setTimeout(()=>{this.socket===i&&i.connect()},B))}),i.on("webchat.message",p=>{var c;return(c=e.onMessage)==null?void 0:c.call(e,p)}),i.on("webchat.typing",p=>{var c;return(c=e.onTyping)==null?void 0:c.call(e,p)})},g=>{var i;this.opening===r&&(this.opening=null,(i=e.onError)==null||i.call(e,new l((g==null?void 0:g.message)||"Network error","NETWORK_ERROR")))});return this.opening=r,r}disconnect(){var e;clearTimeout(this.retry),(e=this.socket)==null||e.disconnect(),this.socket=null,this.opening=null}async call(e,t){var u,p;let o=new AbortController,n=setTimeout(()=>o.abort(),F),r,g;try{r=await fetch(`${this.base}${e}`,{method:t?"POST":"GET",headers:t?{"content-type":"application/json"}:void 0,body:t?JSON.stringify(t):void 0,signal:o.signal}),g=await r.text()}catch(c){throw new l((c==null?void 0:c.message)||"Network error","NETWORK_ERROR")}finally{clearTimeout(n)}let i=null;try{i=JSON.parse(g)}catch(c){}if(r.ok&&(i!=null&&i.success))return i;throw new l((i==null?void 0:i.message)||(i==null?void 0:i.error)||`Request failed (${r.status})`,(p=(u=i==null?void 0:i.code)!=null?u:K[r.status])!=null?p:"INTERNAL_ERROR",r.status,i==null?void 0:i.field)}};0&&(module.exports={WebchatVisitor,WebchatVisitorError});
1
+ "use strict";var v=Object.create;var E=Object.defineProperty,P=Object.defineProperties,D=Object.getOwnPropertyDescriptor,L=Object.getOwnPropertyDescriptors,W=Object.getOwnPropertyNames,R=Object.getOwnPropertySymbols,C=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var h=(s,t,e)=>t in s?E(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,m=(s,t)=>{for(var e in t||(t={}))T.call(t,e)&&h(s,e,t[e]);if(R)for(var e of R(t))y.call(t,e)&&h(s,e,t[e]);return s},V=(s,t)=>P(s,L(t));var S=(s,t)=>{var e={};for(var o in s)T.call(s,o)&&t.indexOf(o)<0&&(e[o]=s[o]);if(s!=null&&R)for(var o of R(s))t.indexOf(o)<0&&y.call(s,o)&&(e[o]=s[o]);return e};var U=(s,t)=>{for(var e in t)E(s,e,{get:t[e],enumerable:!0})},w=(s,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of W(t))!T.call(s,r)&&r!==e&&E(s,r,{get:()=>t[r],enumerable:!(o=D(t,r))||o.enumerable});return s};var M=(s,t,e)=>(e=s!=null?v(C(s)):{},w(t||!s||!s.__esModule?E(e,"default",{value:s,enumerable:!0}):e,s)),k=s=>w(E({},"__esModule",{value:!0}),s);var p=(s,t,e)=>h(s,typeof t!="symbol"?t+"":t,e);var q={};U(q,{WebchatVisitor:()=>O,WebchatVisitorError:()=>l});module.exports=k(q);var f="https://api.ecodrix.com";function N(s="v1",t=f){return`${t.replace(/\/+$/,"")}/${s}/api`}var I=class extends Error{constructor(t){super(t),this.name="EcodrixError"}},d=class extends I{constructor(e,o,r){super(e);p(this,"status");p(this,"code");p(this,"issues");p(this,"field");this.name="APIError",this.status=o,this.code=r}};var l=class extends d{constructor(e,o,r,n){super(e,r,o);p(this,"field");this.name="WebchatVisitorError",this.field=n}},F=3e4,B=6e4,H=new Set(["RATE_LIMITED","INTERNAL_ERROR"]),K={400:"VALIDATION_ERROR",403:"ORIGIN_NOT_ALLOWED",404:"NOT_FOUND",429:"RATE_LIMITED"},O=class{constructor(t){p(this,"options",t);p(this,"host");p(this,"base");p(this,"socket",null);p(this,"opening",null);p(this,"retry");var e;this.host=((e=t.apiUrl)!=null?e:f).replace(/\/+$/,""),this.base=`${N("v1",this.host)}/infra/connect/webhooks/webchat/${encodeURIComponent(t.clientCode)}`}send(t,e={}){let{sessionId:o,parent:r}=this.options,i=e,{page:n}=i,u=S(i,["page"]);return this.call("/message",m(m({sessionId:o,text:t,parent:r},u),n&&{page:V(m({},n),{path:n.path.split(/[?#]/)[0]})}))}messages(){let{sessionId:t,parent:e}=this.options;return this.call(`/messages?${new URLSearchParams({sessionId:t,parent:e})}`)}receipts(t,e){let{sessionId:o,parent:r}=this.options;return this.call("/receipts",{sessionId:o,parent:r,ids:t,state:e})}contact(t){let{sessionId:e,parent:o}=this.options;return this.call("/contact",m({sessionId:e,parent:o},t))}connect(t){if(this.opening)return this.opening;let{clientCode:e,sessionId:o,parent:r}=this.options,n=import("socket.io-client").then(({io:u})=>{if(this.opening!==n)return;let i=u(`${this.host}/visitor`,{auth:{clientCode:e,sessionId:o,parent:r}});this.socket=i;let g=!1;i.on("connect",()=>{var a;g&&((a=t.onReconnect)==null||a.call(t)),g=!1}),i.on("disconnect",()=>{g=!0}),i.on("connect_error",a=>{var A,b,_;g=!0;let c=(b=(A=a.data)==null?void 0:A.code)!=null?b:"NETWORK_ERROR";(_=t.onError)==null||_.call(t,new l(a.message,c)),!i.active&&H.has(c)&&(clearTimeout(this.retry),this.retry=setTimeout(()=>{this.socket===i&&i.connect()},B))}),i.on("webchat.message",a=>{var c;return(c=t.onMessage)==null?void 0:c.call(t,a)}),i.on("webchat.typing",a=>{var c;return(c=t.onTyping)==null?void 0:c.call(t,a)}),i.on("webchat.status",a=>{var c;return(c=t.onStatus)==null?void 0:c.call(t,a)})},u=>{var i;this.opening===n&&(this.opening=null,(i=t.onError)==null||i.call(t,new l((u==null?void 0:u.message)||"Network error","NETWORK_ERROR")))});return this.opening=n,n}disconnect(){var t;clearTimeout(this.retry),(t=this.socket)==null||t.disconnect(),this.socket=null,this.opening=null}async call(t,e){var g,a;let o=new AbortController,r=setTimeout(()=>o.abort(),F),n,u;try{n=await fetch(`${this.base}${t}`,{method:e?"POST":"GET",headers:e?{"content-type":"application/json"}:void 0,body:e?JSON.stringify(e):void 0,signal:o.signal}),u=await n.text()}catch(c){throw new l((c==null?void 0:c.message)||"Network error","NETWORK_ERROR")}finally{clearTimeout(r)}let i=null;try{i=JSON.parse(u)}catch(c){}if(n.ok&&(i!=null&&i.success))return i;throw new l((i==null?void 0:i.message)||(i==null?void 0:i.error)||`Request failed (${n.status})`,(a=(g=i==null?void 0:i.code)!=null?g:K[n.status])!=null?a:"INTERNAL_ERROR",n.status,i==null?void 0:i.field)}};0&&(module.exports={WebchatVisitor,WebchatVisitorError});
@@ -17,6 +17,19 @@ interface VisitorMessage {
17
17
  from: "visitor" | "ai" | "team" | "system";
18
18
  /** Team replies only, and only when the workspace shows staff names. */
19
19
  senderFirstName?: string;
20
+ /**
21
+ * Ticks, as on WhatsApp. The visitor's own message: `delivered` once stored,
22
+ * `read` once the team opened the chat. A reply: `sent` until `receipts()`
23
+ * reports it `delivered` / `read`. Absent from an older API.
24
+ */
25
+ status?: VisitorMessageStatus;
26
+ }
27
+ /** `sent` ✓ · `delivered` ✓✓ · `read` ✓✓ (seen). */
28
+ type VisitorMessageStatus = "sent" | "delivered" | "read";
29
+ /** The live `webchat.status` event: these messages of the visitor's moved on (e.g. the team read them). */
30
+ interface VisitorStatus {
31
+ ids: string[];
32
+ status: "delivered" | "read";
20
33
  }
21
34
  /**
22
35
  * The live `webchat.typing` event. `waiting: true` (with `on: false`) means
@@ -155,4 +168,4 @@ declare class RateLimitError extends APIError {
155
168
  */
156
169
  declare function toAPIError(error: any): APIError;
157
170
 
158
- export { APIError as A, EcodrixError as E, PermissionError as P, RateLimitError as R, type VisitorMessage as V, type WebchatSettingsPatch as W, type WebchatOwnerSettings as a, AuthenticationError as b, type APIErrorIssue as c, type VisitorTyping as d, type WebchatSettings as e, toAPIError as t };
171
+ export { APIError as A, EcodrixError as E, PermissionError as P, RateLimitError as R, type VisitorMessage as V, type WebchatSettingsPatch as W, type WebchatOwnerSettings as a, AuthenticationError as b, type APIErrorIssue as c, type VisitorTyping as d, type WebchatSettings as e, type VisitorStatus as f, type VisitorMessageStatus as g, toAPIError as t };