eventsourcingdb 1.0.1 → 1.1.0

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.
package/README.md CHANGED
@@ -94,6 +94,22 @@ const writtenEvents = await client.writeEvents([
94
94
 
95
95
  *Note that according to the CloudEvents standard, event IDs must be of type string.*
96
96
 
97
+ #### Using the `isEventQlTrue` precondition
98
+
99
+ If you want to write events depending on an EventQL query, import the `isEventQlTrue` function and pass it as an array of preconditions in the second argument:
100
+
101
+ ```typescript
102
+ import { isEventQlTrue } from 'eventsourcingdb';
103
+
104
+ const writtenEvents = await client.writeEvents([
105
+ // events
106
+ ], [
107
+ isEventQlTrue(`FROM e IN events WHERE e.type == 'io.eventsourcingdb.library.book-borrowed' PROJECT INTO COUNT() < 10`)
108
+ ]);
109
+ ```
110
+
111
+ *Note that the query must return a single row with a single value, which is interpreted as a boolean.*
112
+
97
113
  ### Reading Events
98
114
 
99
115
  To read all events of a subject, call the `readEvents` function with the subject as the first argument and an options object as the second argument. Set the `recursive` option to `false`. This ensures that only events of the given subject are returned, not events of nested subjects.
package/dist/index.d.mts CHANGED
@@ -53,12 +53,18 @@ interface IsSubjectOnEventIdPrecondition {
53
53
  subject: string;
54
54
  eventId: string;
55
55
  }
56
+ interface IsEventQlTruePrecondition {
57
+ query: string;
58
+ }
56
59
  type Precondition = {
57
60
  type: 'isSubjectPristine';
58
61
  payload: IsSubjectPristinePrecondition;
59
62
  } | {
60
63
  type: 'isSubjectOnEventId';
61
64
  payload: IsSubjectOnEventIdPrecondition;
65
+ } | {
66
+ type: 'isEventQlTrue';
67
+ payload: IsEventQlTruePrecondition;
62
68
  };
63
69
 
64
70
  type Order = 'chronological' | 'antichronological';
package/dist/index.d.ts CHANGED
@@ -53,12 +53,18 @@ interface IsSubjectOnEventIdPrecondition {
53
53
  subject: string;
54
54
  eventId: string;
55
55
  }
56
+ interface IsEventQlTruePrecondition {
57
+ query: string;
58
+ }
56
59
  type Precondition = {
57
60
  type: 'isSubjectPristine';
58
61
  payload: IsSubjectPristinePrecondition;
59
62
  } | {
60
63
  type: 'isSubjectOnEventId';
61
64
  payload: IsSubjectOnEventIdPrecondition;
65
+ } | {
66
+ type: 'isEventQlTrue';
67
+ payload: IsEventQlTruePrecondition;
62
68
  };
63
69
 
64
70
  type Order = 'chronological' | 'antichronological';
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var w=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var F=(r,t)=>{for(var e in t)w(r,e,{get:t[e],enumerable:!0})},L=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of x(t))!$.call(r,i)&&i!==e&&w(r,i,{get:()=>t[i],enumerable:!(n=k(t,i))||n.enumerable});return r};var R=r=>L(w({},"__esModule",{value:!0}),r);var M={};F(M,{Client:()=>v,Container:()=>h,isSubjectOnEventId:()=>O,isSubjectPristine:()=>C});module.exports=R(M);var y=r=>({...r,time:new Date(r.time)});var u=(r,t)=>{if(typeof t!=typeof r)return!1;if(t===null||r===null)return t===null&&r===null;if(typeof t=="object"){if(Array.isArray(t)||Array.isArray(r))return!1;let e=t,n=r;for(let i of Object.keys(e))if(!(i in n)||!u(n[i],e[i]))return!1;return!0}return!0};var H={specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"},S=r=>u(r,H);var l=async function*(r,t){let e=r.getReader(),n=new TextDecoder("utf-8"),i="",p=()=>{e.cancel().catch(()=>{})};if(t.aborted){await e.cancel().catch(()=>{});return}t.addEventListener("abort",p);try{for(;!t.aborted;){let{done:c,value:d}=await e.read();if(c)break;i+=n.decode(d,{stream:!0});let a;for(;a=i.indexOf(`
2
- `),a!==-1;){let o=i.slice(0,a).trim();i=i.slice(a+1),o&&(yield JSON.parse(o))}}}finally{t.removeEventListener("abort",p),await e.cancel().catch(()=>{})}};var B={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},E=r=>!(!u(r,B)||r.type!=="event");var I={type:"error",payload:{error:"string"}},m=r=>!(!u(r,I)||r.type!=="error");var N={type:"eventType",payload:{eventType:"string",isPhantom:!0}},T=r=>!(!u(r,N)||r.type!=="eventType");var J={type:"heartbeat"},g=r=>!(!u(r,J)||r.type!=="heartbeat");var U={type:"row"},j=r=>!(!u(r,U)||r.type!=="row"||!("payload"in r));var z={type:"subject",payload:{subject:"string"}},A=r=>!(!u(r,z)||r.type!=="subject");var v=class{#o;#e;#t(t){return new URL(t,this.#o).toString()}constructor(t,e){this.#o=t,this.#e=e}async ping(){let t=this.#t("/api/v1/ping"),e=await fetch(t,{method:"get"});if(e.status!==200)throw new Error(`Failed to ping, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#t("/api/v1/verify-api-token"),e=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#e}`}});if(e.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let n=this.#t("/api/v1/write-events"),i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({events:t,preconditions:e})});if(i.status!==200)throw new Error(`Failed to write events, got HTTP status code '${i.status}', expected '200'.`);let p=await i.json();if(!Array.isArray(p))throw new Error("Failed to parse response.");return p.map(d=>{if(!S(d))throw new Error("Failed to parse response item.");return y(d)})}readEvents(t,e,n){let i=this.#t("/api/v1/read-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,o;if(n&&!n.aborted){let s=()=>c.abort();n.addEventListener("abort",s,{once:!0}),o=()=>n.removeEventListener("abort",s)}try{let s=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(s.status!==200)throw new Error(`Failed to read events, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to read events.");for await(let f of l(s.body,d)){if(m(f))throw new Error(`${f.payload.error}.`);if(E(f)){yield y(f.payload);continue}throw new Error("Failed to read events.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{o&&o(),a&&c.abort()}}()}runEventQlQuery(t,e){let n=this.#t("/api/v1/run-eventql-query"),i=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let o=()=>p.abort();e.addEventListener("abort",o,{once:!0}),a=()=>e.removeEventListener("abort",o)}try{let o=await fetch(n,{method:"post",headers:{authorization:`Bearer ${i}`,"content-type":"application/json"},body:JSON.stringify({query:t}),signal:c});if(o.status!==200)throw new Error(`Failed to run EventQL query, got HTTP status code '${o.status}', expected '200'.`);if(!o.body)throw new Error("Failed to run EventQL query.");for await(let s of l(o.body,c)){if(m(s))throw new Error(`${s.payload.error}.`);if(j(s)){yield s.payload;continue}throw new Error("Failed to run EventQL query.")}}catch(o){if(o instanceof DOMException&&o.name==="AbortError")return;throw o}finally{a&&a(),d&&p.abort()}}()}observeEvents(t,e,n){let i=this.#t("/api/v1/observe-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,o;if(n&&!n.aborted){let s=()=>c.abort();n.addEventListener("abort",s,{once:!0}),o=()=>n.removeEventListener("abort",s)}try{let s=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(s.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to observe events.");for await(let f of l(s.body,d))if(!g(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(E(f)){yield y(f.payload);continue}throw new Error("Failed to observe events.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{o&&o(),a&&c.abort()}}()}async registerEventSchema(t,e){let n=this.#t("/api/v1/register-event-schema"),i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({eventType:t,schema:e})});if(i.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${i.status}', expected '200'.`)}readSubjects(t,e){let n=this.#t("/api/v1/read-subjects"),i=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let o=()=>p.abort();e.addEventListener("abort",o,{once:!0}),a=()=>e.removeEventListener("abort",o)}try{let o=await fetch(n,{method:"post",headers:{authorization:`Bearer ${i}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:c});if(o.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${o.status}', expected '200'.`);if(!o.body)throw new Error("Failed to read subjects.");for await(let s of l(o.body,c)){if(m(s))throw new Error(`${s.payload.error}.`);if(A(s)){yield s.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(o){if(o instanceof DOMException&&o.name==="AbortError")return;throw o}finally{a&&a(),d&&p.abort()}}()}readEventTypes(t){let e=this.#t("/api/v1/read-event-types"),n=this.#e;return async function*(){let i=new AbortController,p=t??i.signal,c=!t,d;if(t&&!t.aborted){let a=()=>i.abort();t.addEventListener("abort",a,{once:!0}),d=()=>t.removeEventListener("abort",a)}try{let a=await fetch(e,{method:"post",headers:{authorization:`Bearer ${n}`},signal:p});if(a.status!==200)throw new Error(`Failed to read event types, got HTTP status code '${a.status}', expected '200'.`);if(!a.body)throw new Error("Failed to read event types.");for await(let o of l(a.body,p))if(!g(o)){if(m(o))throw new Error(`${o.payload.error}.`);if(T(o)){yield o.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{d&&d(),c&&i.abort()}}()}};var b=require("testcontainers");var h=class{#o="thenativeweb/eventsourcingdb";#e="latest";#t=3e3;#n="secret";#r;withImageTag(t){return this.#e=t,this}withApiToken(t){return this.#n=t,this}withPort(t){return this.#t=t,this}async start(){this.#r=await new b.GenericContainer(`${this.#o}:${this.#e}`).withExposedPorts(this.#t).withCommand(["run","--api-token",this.#n,"--data-directory-temporary","--http-enabled","--https-enabled=false"]).withWaitStrategy(b.Wait.forHttp("/api/v1/ping",this.#t).withStartupTimeout(1e4)).start()}getHost(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getHost()}getMappedPort(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getMappedPort(this.#t)}getBaseUrl(){if(this.#r===void 0)throw new Error("Container must be running.");let t=this.getHost(),e=this.getMappedPort();return new URL(`http://${t}:${e}`)}getApiToken(){return this.#n}isRunning(){return this.#r!==void 0}async stop(){this.#r!==void 0&&(await this.#r.stop(),this.#r=void 0)}getClient(){return new v(this.getBaseUrl(),this.getApiToken())}};var O=(r,t)=>({type:"isSubjectOnEventId",payload:{subject:r,eventId:t}});var C=r=>({type:"isSubjectPristine",payload:{subject:r}});0&&(module.exports={Client,Container,isSubjectOnEventId,isSubjectPristine});
1
+ "use strict";var E=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var F=(r,t)=>{for(var e in t)E(r,e,{get:t[e],enumerable:!0})},L=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of x(t))!$.call(r,i)&&i!==e&&E(r,i,{get:()=>t[i],enumerable:!(n=k(t,i))||n.enumerable});return r};var R=r=>L(E({},"__esModule",{value:!0}),r);var M={};F(M,{Client:()=>v,Container:()=>b,isSubjectOnEventId:()=>O,isSubjectPristine:()=>C});module.exports=R(M);var y=r=>({...r,time:new Date(r.time)});var u=(r,t)=>{if(typeof t!=typeof r)return!1;if(t===null||r===null)return t===null&&r===null;if(typeof t=="object"){if(Array.isArray(t)||Array.isArray(r))return!1;let e=t,n=r;for(let i of Object.keys(e))if(!(i in n)||!u(n[i],e[i]))return!1;return!0}return!0};var H={specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"},S=r=>u(r,H);var l=async function*(r,t){let e=r.getReader(),n=new TextDecoder("utf-8"),i="",p=()=>{e.cancel().catch(()=>{})};if(t.aborted){await e.cancel().catch(()=>{});return}t.addEventListener("abort",p);try{for(;!t.aborted;){let{done:c,value:d}=await e.read();if(c)break;i+=n.decode(d,{stream:!0});let a;for(;a=i.indexOf(`
2
+ `),a!==-1;){let o=i.slice(0,a).trim();i=i.slice(a+1),o&&(yield JSON.parse(o))}}}finally{t.removeEventListener("abort",p),await e.cancel().catch(()=>{})}};var B={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},g=r=>!(!u(r,B)||r.type!=="event");var I={type:"error",payload:{error:"string"}},m=r=>!(!u(r,I)||r.type!=="error");var N={type:"eventType",payload:{eventType:"string",isPhantom:!0}},T=r=>!(!u(r,N)||r.type!=="eventType");var J={type:"heartbeat"},h=r=>!(!u(r,J)||r.type!=="heartbeat");var U={type:"row"},j=r=>!(!u(r,U)||r.type!=="row"||!("payload"in r));var z={type:"subject",payload:{subject:"string"}},A=r=>!(!u(r,z)||r.type!=="subject");var v=class{#o;#e;#t(t){return new URL(t,this.#o).toString()}constructor(t,e){this.#o=t,this.#e=e}async ping(){let t=this.#t("/api/v1/ping"),e=await fetch(t,{method:"get"});if(e.status!==200)throw new Error(`Failed to ping, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#t("/api/v1/verify-api-token"),e=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#e}`}});if(e.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let n=this.#t("/api/v1/write-events"),i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({events:t,preconditions:e})});if(i.status!==200)throw new Error(`Failed to write events, got HTTP status code '${i.status}', expected '200'.`);let p=await i.json();if(!Array.isArray(p))throw new Error("Failed to parse response.");return p.map(d=>{if(!S(d))throw new Error("Failed to parse response item.");return y(d)})}readEvents(t,e,n){let i=this.#t("/api/v1/read-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,o;if(n&&!n.aborted){let s=()=>c.abort();n.addEventListener("abort",s,{once:!0}),o=()=>n.removeEventListener("abort",s)}try{let s=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(s.status!==200)throw new Error(`Failed to read events, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to read events.");for await(let f of l(s.body,d)){if(m(f))throw new Error(`${f.payload.error}.`);if(g(f)){yield y(f.payload);continue}throw new Error("Failed to read events.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{o&&o(),a&&c.abort()}}()}runEventQlQuery(t,e){let n=this.#t("/api/v1/run-eventql-query"),i=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let o=()=>p.abort();e.addEventListener("abort",o,{once:!0}),a=()=>e.removeEventListener("abort",o)}try{let o=await fetch(n,{method:"post",headers:{authorization:`Bearer ${i}`,"content-type":"application/json"},body:JSON.stringify({query:t}),signal:c});if(o.status!==200)throw new Error(`Failed to run EventQL query, got HTTP status code '${o.status}', expected '200'.`);if(!o.body)throw new Error("Failed to run EventQL query.");for await(let s of l(o.body,c))if(!h(s)){if(m(s))throw new Error(`${s.payload.error}.`);if(j(s)){yield s.payload;continue}throw new Error("Failed to run EventQL query.")}}catch(o){if(o instanceof DOMException&&o.name==="AbortError")return;throw o}finally{a&&a(),d&&p.abort()}}()}observeEvents(t,e,n){let i=this.#t("/api/v1/observe-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,o;if(n&&!n.aborted){let s=()=>c.abort();n.addEventListener("abort",s,{once:!0}),o=()=>n.removeEventListener("abort",s)}try{let s=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(s.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to observe events.");for await(let f of l(s.body,d))if(!h(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(g(f)){yield y(f.payload);continue}throw new Error("Failed to observe events.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{o&&o(),a&&c.abort()}}()}async registerEventSchema(t,e){let n=this.#t("/api/v1/register-event-schema"),i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({eventType:t,schema:e})});if(i.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${i.status}', expected '200'.`)}readSubjects(t,e){let n=this.#t("/api/v1/read-subjects"),i=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let o=()=>p.abort();e.addEventListener("abort",o,{once:!0}),a=()=>e.removeEventListener("abort",o)}try{let o=await fetch(n,{method:"post",headers:{authorization:`Bearer ${i}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:c});if(o.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${o.status}', expected '200'.`);if(!o.body)throw new Error("Failed to read subjects.");for await(let s of l(o.body,c)){if(m(s))throw new Error(`${s.payload.error}.`);if(A(s)){yield s.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(o){if(o instanceof DOMException&&o.name==="AbortError")return;throw o}finally{a&&a(),d&&p.abort()}}()}readEventTypes(t){let e=this.#t("/api/v1/read-event-types"),n=this.#e;return async function*(){let i=new AbortController,p=t??i.signal,c=!t,d;if(t&&!t.aborted){let a=()=>i.abort();t.addEventListener("abort",a,{once:!0}),d=()=>t.removeEventListener("abort",a)}try{let a=await fetch(e,{method:"post",headers:{authorization:`Bearer ${n}`},signal:p});if(a.status!==200)throw new Error(`Failed to read event types, got HTTP status code '${a.status}', expected '200'.`);if(!a.body)throw new Error("Failed to read event types.");for await(let o of l(a.body,p))if(!h(o)){if(m(o))throw new Error(`${o.payload.error}.`);if(T(o)){yield o.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{d&&d(),c&&i.abort()}}()}};var w=require("testcontainers");var b=class{#o="thenativeweb/eventsourcingdb";#e="latest";#t=3e3;#n="secret";#r;withImageTag(t){return this.#e=t,this}withApiToken(t){return this.#n=t,this}withPort(t){return this.#t=t,this}async start(){this.#r=await new w.GenericContainer(`${this.#o}:${this.#e}`).withExposedPorts(this.#t).withCommand(["run","--api-token",this.#n,"--data-directory-temporary","--http-enabled","--https-enabled=false"]).withWaitStrategy(w.Wait.forHttp("/api/v1/ping",this.#t).withStartupTimeout(1e4)).start()}getHost(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getHost()}getMappedPort(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getMappedPort(this.#t)}getBaseUrl(){if(this.#r===void 0)throw new Error("Container must be running.");let t=this.getHost(),e=this.getMappedPort();return new URL(`http://${t}:${e}`)}getApiToken(){return this.#n}isRunning(){return this.#r!==void 0}async stop(){this.#r!==void 0&&(await this.#r.stop(),this.#r=void 0)}getClient(){return new v(this.getBaseUrl(),this.getApiToken())}};var O=(r,t)=>({type:"isSubjectOnEventId",payload:{subject:r,eventId:t}});var C=r=>({type:"isSubjectPristine",payload:{subject:r}});0&&(module.exports={Client,Container,isSubjectOnEventId,isSubjectPristine});
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
1
  var y=o=>({...o,time:new Date(o.time)});var u=(o,t)=>{if(typeof t!=typeof o)return!1;if(t===null||o===null)return t===null&&o===null;if(typeof t=="object"){if(Array.isArray(t)||Array.isArray(o))return!1;let e=t,n=o;for(let s of Object.keys(e))if(!(s in n)||!u(n[s],e[s]))return!1;return!0}return!0};var A={specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"},E=o=>u(o,A);var l=async function*(o,t){let e=o.getReader(),n=new TextDecoder("utf-8"),s="",p=()=>{e.cancel().catch(()=>{})};if(t.aborted){await e.cancel().catch(()=>{});return}t.addEventListener("abort",p);try{for(;!t.aborted;){let{done:c,value:d}=await e.read();if(c)break;s+=n.decode(d,{stream:!0});let a;for(;a=s.indexOf(`
2
- `),a!==-1;){let r=s.slice(0,a).trim();s=s.slice(a+1),r&&(yield JSON.parse(r))}}}finally{t.removeEventListener("abort",p),await e.cancel().catch(()=>{})}};var O={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},h=o=>!(!u(o,O)||o.type!=="event");var C={type:"error",payload:{error:"string"}},m=o=>!(!u(o,C)||o.type!=="error");var P={type:"eventType",payload:{eventType:"string",isPhantom:!0}},g=o=>!(!u(o,P)||o.type!=="eventType");var k={type:"heartbeat"},b=o=>!(!u(o,k)||o.type!=="heartbeat");var x={type:"row"},S=o=>!(!u(o,x)||o.type!=="row"||!("payload"in o));var $={type:"subject",payload:{subject:"string"}},T=o=>!(!u(o,$)||o.type!=="subject");var v=class{#o;#e;#t(t){return new URL(t,this.#o).toString()}constructor(t,e){this.#o=t,this.#e=e}async ping(){let t=this.#t("/api/v1/ping"),e=await fetch(t,{method:"get"});if(e.status!==200)throw new Error(`Failed to ping, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#t("/api/v1/verify-api-token"),e=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#e}`}});if(e.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let n=this.#t("/api/v1/write-events"),s=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({events:t,preconditions:e})});if(s.status!==200)throw new Error(`Failed to write events, got HTTP status code '${s.status}', expected '200'.`);let p=await s.json();if(!Array.isArray(p))throw new Error("Failed to parse response.");return p.map(d=>{if(!E(d))throw new Error("Failed to parse response item.");return y(d)})}readEvents(t,e,n){let s=this.#t("/api/v1/read-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,r;if(n&&!n.aborted){let i=()=>c.abort();n.addEventListener("abort",i,{once:!0}),r=()=>n.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(i.status!==200)throw new Error(`Failed to read events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to read events.");for await(let f of l(i.body,d)){if(m(f))throw new Error(`${f.payload.error}.`);if(h(f)){yield y(f.payload);continue}throw new Error("Failed to read events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{r&&r(),a&&c.abort()}}()}runEventQlQuery(t,e){let n=this.#t("/api/v1/run-eventql-query"),s=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let r=()=>p.abort();e.addEventListener("abort",r,{once:!0}),a=()=>e.removeEventListener("abort",r)}try{let r=await fetch(n,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({query:t}),signal:c});if(r.status!==200)throw new Error(`Failed to run EventQL query, got HTTP status code '${r.status}', expected '200'.`);if(!r.body)throw new Error("Failed to run EventQL query.");for await(let i of l(r.body,c)){if(m(i))throw new Error(`${i.payload.error}.`);if(S(i)){yield i.payload;continue}throw new Error("Failed to run EventQL query.")}}catch(r){if(r instanceof DOMException&&r.name==="AbortError")return;throw r}finally{a&&a(),d&&p.abort()}}()}observeEvents(t,e,n){let s=this.#t("/api/v1/observe-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,r;if(n&&!n.aborted){let i=()=>c.abort();n.addEventListener("abort",i,{once:!0}),r=()=>n.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(i.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to observe events.");for await(let f of l(i.body,d))if(!b(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(h(f)){yield y(f.payload);continue}throw new Error("Failed to observe events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{r&&r(),a&&c.abort()}}()}async registerEventSchema(t,e){let n=this.#t("/api/v1/register-event-schema"),s=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({eventType:t,schema:e})});if(s.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${s.status}', expected '200'.`)}readSubjects(t,e){let n=this.#t("/api/v1/read-subjects"),s=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let r=()=>p.abort();e.addEventListener("abort",r,{once:!0}),a=()=>e.removeEventListener("abort",r)}try{let r=await fetch(n,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:c});if(r.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${r.status}', expected '200'.`);if(!r.body)throw new Error("Failed to read subjects.");for await(let i of l(r.body,c)){if(m(i))throw new Error(`${i.payload.error}.`);if(T(i)){yield i.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(r){if(r instanceof DOMException&&r.name==="AbortError")return;throw r}finally{a&&a(),d&&p.abort()}}()}readEventTypes(t){let e=this.#t("/api/v1/read-event-types"),n=this.#e;return async function*(){let s=new AbortController,p=t??s.signal,c=!t,d;if(t&&!t.aborted){let a=()=>s.abort();t.addEventListener("abort",a,{once:!0}),d=()=>t.removeEventListener("abort",a)}try{let a=await fetch(e,{method:"post",headers:{authorization:`Bearer ${n}`},signal:p});if(a.status!==200)throw new Error(`Failed to read event types, got HTTP status code '${a.status}', expected '200'.`);if(!a.body)throw new Error("Failed to read event types.");for await(let r of l(a.body,p))if(!b(r)){if(m(r))throw new Error(`${r.payload.error}.`);if(g(r)){yield r.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{d&&d(),c&&s.abort()}}()}};import{GenericContainer as F,Wait as L}from"testcontainers";var w=class{#o="thenativeweb/eventsourcingdb";#e="latest";#t=3e3;#n="secret";#r;withImageTag(t){return this.#e=t,this}withApiToken(t){return this.#n=t,this}withPort(t){return this.#t=t,this}async start(){this.#r=await new F(`${this.#o}:${this.#e}`).withExposedPorts(this.#t).withCommand(["run","--api-token",this.#n,"--data-directory-temporary","--http-enabled","--https-enabled=false"]).withWaitStrategy(L.forHttp("/api/v1/ping",this.#t).withStartupTimeout(1e4)).start()}getHost(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getHost()}getMappedPort(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getMappedPort(this.#t)}getBaseUrl(){if(this.#r===void 0)throw new Error("Container must be running.");let t=this.getHost(),e=this.getMappedPort();return new URL(`http://${t}:${e}`)}getApiToken(){return this.#n}isRunning(){return this.#r!==void 0}async stop(){this.#r!==void 0&&(await this.#r.stop(),this.#r=void 0)}getClient(){return new v(this.getBaseUrl(),this.getApiToken())}};var R=(o,t)=>({type:"isSubjectOnEventId",payload:{subject:o,eventId:t}});var H=o=>({type:"isSubjectPristine",payload:{subject:o}});export{v as Client,w as Container,R as isSubjectOnEventId,H as isSubjectPristine};
2
+ `),a!==-1;){let r=s.slice(0,a).trim();s=s.slice(a+1),r&&(yield JSON.parse(r))}}}finally{t.removeEventListener("abort",p),await e.cancel().catch(()=>{})}};var O={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},b=o=>!(!u(o,O)||o.type!=="event");var C={type:"error",payload:{error:"string"}},m=o=>!(!u(o,C)||o.type!=="error");var P={type:"eventType",payload:{eventType:"string",isPhantom:!0}},g=o=>!(!u(o,P)||o.type!=="eventType");var k={type:"heartbeat"},h=o=>!(!u(o,k)||o.type!=="heartbeat");var x={type:"row"},S=o=>!(!u(o,x)||o.type!=="row"||!("payload"in o));var $={type:"subject",payload:{subject:"string"}},T=o=>!(!u(o,$)||o.type!=="subject");var v=class{#o;#e;#t(t){return new URL(t,this.#o).toString()}constructor(t,e){this.#o=t,this.#e=e}async ping(){let t=this.#t("/api/v1/ping"),e=await fetch(t,{method:"get"});if(e.status!==200)throw new Error(`Failed to ping, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#t("/api/v1/verify-api-token"),e=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#e}`}});if(e.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${e.status}', expected '200'.`);let n=await e.json();if(!u(n,{type:"string"}))throw new Error("Failed to parse response.");if(n.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let n=this.#t("/api/v1/write-events"),s=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({events:t,preconditions:e})});if(s.status!==200)throw new Error(`Failed to write events, got HTTP status code '${s.status}', expected '200'.`);let p=await s.json();if(!Array.isArray(p))throw new Error("Failed to parse response.");return p.map(d=>{if(!E(d))throw new Error("Failed to parse response item.");return y(d)})}readEvents(t,e,n){let s=this.#t("/api/v1/read-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,r;if(n&&!n.aborted){let i=()=>c.abort();n.addEventListener("abort",i,{once:!0}),r=()=>n.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(i.status!==200)throw new Error(`Failed to read events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to read events.");for await(let f of l(i.body,d)){if(m(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield y(f.payload);continue}throw new Error("Failed to read events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{r&&r(),a&&c.abort()}}()}runEventQlQuery(t,e){let n=this.#t("/api/v1/run-eventql-query"),s=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let r=()=>p.abort();e.addEventListener("abort",r,{once:!0}),a=()=>e.removeEventListener("abort",r)}try{let r=await fetch(n,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({query:t}),signal:c});if(r.status!==200)throw new Error(`Failed to run EventQL query, got HTTP status code '${r.status}', expected '200'.`);if(!r.body)throw new Error("Failed to run EventQL query.");for await(let i of l(r.body,c))if(!h(i)){if(m(i))throw new Error(`${i.payload.error}.`);if(S(i)){yield i.payload;continue}throw new Error("Failed to run EventQL query.")}}catch(r){if(r instanceof DOMException&&r.name==="AbortError")return;throw r}finally{a&&a(),d&&p.abort()}}()}observeEvents(t,e,n){let s=this.#t("/api/v1/observe-events"),p=this.#e;return async function*(){let c=new AbortController,d=n??c.signal,a=!n,r;if(n&&!n.aborted){let i=()=>c.abort();n.addEventListener("abort",i,{once:!0}),r=()=>n.removeEventListener("abort",i)}try{let i=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(i.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${i.status}', expected '200'.`);if(!i.body)throw new Error("Failed to observe events.");for await(let f of l(i.body,d))if(!h(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield y(f.payload);continue}throw new Error("Failed to observe events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{r&&r(),a&&c.abort()}}()}async registerEventSchema(t,e){let n=this.#t("/api/v1/register-event-schema"),s=await fetch(n,{method:"post",headers:{authorization:`Bearer ${this.#e}`,"content-type":"application/json"},body:JSON.stringify({eventType:t,schema:e})});if(s.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${s.status}', expected '200'.`)}readSubjects(t,e){let n=this.#t("/api/v1/read-subjects"),s=this.#e;return async function*(){let p=new AbortController,c=e??p.signal,d=!e,a;if(e&&!e.aborted){let r=()=>p.abort();e.addEventListener("abort",r,{once:!0}),a=()=>e.removeEventListener("abort",r)}try{let r=await fetch(n,{method:"post",headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:c});if(r.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${r.status}', expected '200'.`);if(!r.body)throw new Error("Failed to read subjects.");for await(let i of l(r.body,c)){if(m(i))throw new Error(`${i.payload.error}.`);if(T(i)){yield i.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(r){if(r instanceof DOMException&&r.name==="AbortError")return;throw r}finally{a&&a(),d&&p.abort()}}()}readEventTypes(t){let e=this.#t("/api/v1/read-event-types"),n=this.#e;return async function*(){let s=new AbortController,p=t??s.signal,c=!t,d;if(t&&!t.aborted){let a=()=>s.abort();t.addEventListener("abort",a,{once:!0}),d=()=>t.removeEventListener("abort",a)}try{let a=await fetch(e,{method:"post",headers:{authorization:`Bearer ${n}`},signal:p});if(a.status!==200)throw new Error(`Failed to read event types, got HTTP status code '${a.status}', expected '200'.`);if(!a.body)throw new Error("Failed to read event types.");for await(let r of l(a.body,p))if(!h(r)){if(m(r))throw new Error(`${r.payload.error}.`);if(g(r)){yield r.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{d&&d(),c&&s.abort()}}()}};import{GenericContainer as F,Wait as L}from"testcontainers";var w=class{#o="thenativeweb/eventsourcingdb";#e="latest";#t=3e3;#n="secret";#r;withImageTag(t){return this.#e=t,this}withApiToken(t){return this.#n=t,this}withPort(t){return this.#t=t,this}async start(){this.#r=await new F(`${this.#o}:${this.#e}`).withExposedPorts(this.#t).withCommand(["run","--api-token",this.#n,"--data-directory-temporary","--http-enabled","--https-enabled=false"]).withWaitStrategy(L.forHttp("/api/v1/ping",this.#t).withStartupTimeout(1e4)).start()}getHost(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getHost()}getMappedPort(){if(this.#r===void 0)throw new Error("Container must be running.");return this.#r.getMappedPort(this.#t)}getBaseUrl(){if(this.#r===void 0)throw new Error("Container must be running.");let t=this.getHost(),e=this.getMappedPort();return new URL(`http://${t}:${e}`)}getApiToken(){return this.#n}isRunning(){return this.#r!==void 0}async stop(){this.#r!==void 0&&(await this.#r.stop(),this.#r=void 0)}getClient(){return new v(this.getBaseUrl(),this.getApiToken())}};var R=(o,t)=>({type:"isSubjectOnEventId",payload:{subject:o,eventId:t}});var H=o=>({type:"isSubjectPristine",payload:{subject:o}});export{v as Client,w as Container,R as isSubjectOnEventId,H as isSubjectPristine};
package/package.json CHANGED
@@ -1,48 +1,48 @@
1
1
  {
2
- "name": "eventsourcingdb",
3
- "version": "1.0.1",
4
- "description": "The official JavaScript client SDK for EventSourcingDB.",
5
- "author": {
6
- "name": "the native web GmbH",
7
- "email": "hello@thenativeweb.io"
8
- },
9
- "exports": {
10
- ".": {
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js"
13
- }
14
- },
15
- "types": "./dist/index.d.ts",
16
- "dependencies": {
17
- "testcontainers": "10.25.0"
18
- },
19
- "devDependencies": {
20
- "@biomejs/biome": "1.9.4",
21
- "@types/node": "22.15.17",
22
- "get-port": "7.1.0",
23
- "tsup": "8.4.0",
24
- "tsx": "4.19.4",
25
- "typescript": "5.8.3"
26
- },
27
- "scripts": {
28
- "analyze": "npx biome check --error-on-warnings .",
29
- "build": "npx tsc --noEmit && npx tsup --clean --dts --format cjs,esm --minify --out-dir=./dist/ ./src/index.ts",
30
- "format": "npx biome format --write .",
31
- "qa": "npm run analyze && npm run test",
32
- "test": "node --test --import tsx \"./src/**/*.test.ts\""
33
- },
34
- "repository": {
35
- "type": "git",
36
- "url": "git://github.com/thenativeweb/eventsourcingdb-client-javascript.git"
37
- },
38
- "license": "MIT",
39
- "keywords": [
40
- "eventsourcingdb",
41
- "esdb",
42
- "event",
43
- "events",
44
- "event sourcing",
45
- "event store",
46
- "cqrs"
47
- ]
2
+ "name": "eventsourcingdb",
3
+ "version": "1.1.0",
4
+ "description": "The official JavaScript client SDK for EventSourcingDB.",
5
+ "author": {
6
+ "name": "the native web GmbH",
7
+ "email": "hello@thenativeweb.io"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "types": "./dist/index.d.ts",
16
+ "dependencies": {
17
+ "testcontainers": "11.3.1"
18
+ },
19
+ "devDependencies": {
20
+ "@biomejs/biome": "2.1.2",
21
+ "@types/node": "24.0.15",
22
+ "get-port": "7.1.0",
23
+ "tsup": "8.5.0",
24
+ "tsx": "4.20.3",
25
+ "typescript": "5.8.3"
26
+ },
27
+ "scripts": {
28
+ "analyze": "npx biome check --error-on-warnings .",
29
+ "build": "npx tsc --noEmit && npx tsup --clean --dts --format cjs,esm --minify --out-dir=./dist/ ./src/index.ts",
30
+ "format": "npx biome check --write .",
31
+ "qa": "npm run analyze && npm run test",
32
+ "test": "node --test --import tsx \"./src/**/*.test.ts\""
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git://github.com/thenativeweb/eventsourcingdb-client-javascript.git"
37
+ },
38
+ "license": "MIT",
39
+ "keywords": [
40
+ "eventsourcingdb",
41
+ "esdb",
42
+ "event",
43
+ "events",
44
+ "event sourcing",
45
+ "event store",
46
+ "cqrs"
47
+ ]
48
48
  }