eventsourcingdb 0.10.6 → 0.12.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
@@ -6,6 +6,8 @@ EventSourcingDB enables you to build and operate event-driven applications with
6
6
 
7
7
  For more information on EventSourcingDB, see its [official documentation](https://docs.eventsourcingdb.io/).
8
8
 
9
+ This client SDK includes support for [Testcontainers](https://testcontainers.com/) to spin up EventSourcingDB instances in integration tests. For details, see [Using Testcontainers](#using-testcontainers).
10
+
9
11
  ## Getting Started
10
12
 
11
13
  Import the `Client` class and create an instance by providing the URL of your EventSourcingDB instance and the API token to use:
@@ -186,6 +188,42 @@ for await (const event of client.readEvents('/books/42', {
186
188
  controller.abort();
187
189
  ```
188
190
 
191
+ ### Running EventQL Queries
192
+
193
+ To run an EventQL query, call the `runEventQlQuery` function and provide the query as argument. The function returns an asynchronous iterator, which you can use e.g. inside a `for await` loop:
194
+
195
+ ```typescript
196
+ for await (const row of client.runEventQlQuery(`
197
+ FROM e IN events
198
+ PROJECT INTO e
199
+ `)) {
200
+ // ...
201
+ }
202
+ ```
203
+
204
+ #### Aborting a Query
205
+
206
+ If you need to abort a query use `break` or `return` within the `await for` loop. However, this only works if there is currently an iteration going on.
207
+
208
+ To abort the query independently of that, hand over an abort signal as second argument when calling `runEventQlQuery`, and abort the appropriate AbortController:
209
+
210
+ ```typescript
211
+ const controller = new AbortController();
212
+
213
+ for await (const row of client.runEventQlQuery(`
214
+ FROM e IN events
215
+ PROJECT INTO e
216
+ `, controller.signal)) {
217
+ // ...
218
+ }
219
+
220
+ // Somewhere else, abort the controller, which will cause
221
+ // reading to end.
222
+ controller.abort();
223
+ ```
224
+
225
+ *Note that each row returned by the iterator matches the projection specified in your query.*
226
+
189
227
  ### Observing Events
190
228
 
191
229
  To observe all events of a subject, call the `observeEvents` 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.
@@ -356,3 +394,52 @@ for await (const eventType of client.readEventTypes()) {
356
394
  // reading to end.
357
395
  controller.abort();
358
396
  ```
397
+
398
+ ### Using Testcontainers
399
+
400
+ Import the `EventSourcingDbContainer` class, create an instance, call the `start` function to run a test container, get a client, run your test code, and finally call the `stop` function to stop the test container:
401
+
402
+ ```typescript
403
+ import { EventSourcingDbContainer } from 'eventsourcingdb';
404
+
405
+ const container = new EventSourcingDbContainer();
406
+ await container.start();
407
+
408
+ const client = container.getClient();
409
+
410
+ // ...
411
+
412
+ await container.stop();
413
+ ```
414
+
415
+ To check if the test container is running, call the `isRunning` function:
416
+
417
+ ```typescript
418
+ const isRunning = container.isRunning();
419
+ ```
420
+
421
+ #### Configuring the Container Instance
422
+
423
+ By default, `EventSourcingDbContainer` uses the `latest` tag of the official EventSourcingDB Docker image. To change that, call the `withImageTag` function:
424
+
425
+ ```typescript
426
+ const container = new EventSourcingDbContainer()
427
+ .withImageTag('1.0.0');
428
+ ```
429
+
430
+ Similarly, you can configure the port to use and the API token. Call the `withPort` or the `withApiToken` function respectively:
431
+
432
+ ```typescript
433
+ const container = new EventSourcingDbContainer()
434
+ .withPort(4000)
435
+ .withApiToken('secret');
436
+ ```
437
+
438
+ #### Configuring the Client Manually
439
+
440
+ In case you need to set up the client yourself, use the following functions to get details on the container:
441
+
442
+ - `getHost()` returns the host name.
443
+ - `getMappedPort()` returns the port.
444
+ - `getBaseUrl()` returns the full URL of the container.
445
+ - `getApiToken()` returns the API token.
package/dist/index.d.mts CHANGED
@@ -83,14 +83,30 @@ declare class Client {
83
83
  verifyApiToken(): Promise<void>;
84
84
  writeEvents(events: EventCandidate[], preconditions?: Precondition[]): Promise<Event[]>;
85
85
  readEvents(subject: string, options: ReadEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
86
+ runEventQlQuery(query: string, signal?: AbortSignal): AsyncGenerator<unknown, void, void>;
86
87
  observeEvents(subject: string, options: ObserveEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
87
88
  registerEventSchema(eventType: string, schema: Record<string, unknown>): Promise<void>;
88
89
  readSubjects(baseSubject: string, signal?: AbortSignal): AsyncGenerator<string, void, void>;
89
90
  readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void>;
90
91
  }
91
92
 
93
+ declare class EventSourcingDbContainer {
94
+ #private;
95
+ withImageTag(tag: string): this;
96
+ withApiToken(token: string): this;
97
+ withPort(port: number): this;
98
+ start(): Promise<void>;
99
+ getHost(): string;
100
+ getMappedPort(): number;
101
+ getBaseUrl(): URL;
102
+ getApiToken(): string;
103
+ isRunning(): boolean;
104
+ stop(): Promise<void>;
105
+ getClient(): Client;
106
+ }
107
+
92
108
  declare const isSubjectOnEventId: (subject: string, eventId: string) => Precondition;
93
109
 
94
110
  declare const isSubjectPristine: (subject: string) => Precondition;
95
111
 
96
- export { Client, type Event, type EventCandidate, type EventType, type ObserveEventsOptions, type ReadEventsOptions, isSubjectOnEventId, isSubjectPristine };
112
+ export { Client, type Event, type EventCandidate, EventSourcingDbContainer, type EventType, type ObserveEventsOptions, type ReadEventsOptions, isSubjectOnEventId, isSubjectPristine };
package/dist/index.d.ts CHANGED
@@ -83,14 +83,30 @@ declare class Client {
83
83
  verifyApiToken(): Promise<void>;
84
84
  writeEvents(events: EventCandidate[], preconditions?: Precondition[]): Promise<Event[]>;
85
85
  readEvents(subject: string, options: ReadEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
86
+ runEventQlQuery(query: string, signal?: AbortSignal): AsyncGenerator<unknown, void, void>;
86
87
  observeEvents(subject: string, options: ObserveEventsOptions, signal?: AbortSignal): AsyncGenerator<Event, void, void>;
87
88
  registerEventSchema(eventType: string, schema: Record<string, unknown>): Promise<void>;
88
89
  readSubjects(baseSubject: string, signal?: AbortSignal): AsyncGenerator<string, void, void>;
89
90
  readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void>;
90
91
  }
91
92
 
93
+ declare class EventSourcingDbContainer {
94
+ #private;
95
+ withImageTag(tag: string): this;
96
+ withApiToken(token: string): this;
97
+ withPort(port: number): this;
98
+ start(): Promise<void>;
99
+ getHost(): string;
100
+ getMappedPort(): number;
101
+ getBaseUrl(): URL;
102
+ getApiToken(): string;
103
+ isRunning(): boolean;
104
+ stop(): Promise<void>;
105
+ getClient(): Client;
106
+ }
107
+
92
108
  declare const isSubjectOnEventId: (subject: string, eventId: string) => Precondition;
93
109
 
94
110
  declare const isSubjectPristine: (subject: string) => Precondition;
95
111
 
96
- export { Client, type Event, type EventCandidate, type EventType, type ObserveEventsOptions, type ReadEventsOptions, isSubjectOnEventId, isSubjectPristine };
112
+ export { Client, type Event, type EventCandidate, EventSourcingDbContainer, type EventType, type ObserveEventsOptions, type ReadEventsOptions, isSubjectOnEventId, isSubjectPristine };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var h=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var P=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},k=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of C(t))!x.call(e,n)&&n!==r&&h(e,n,{get:()=>t[n],enumerable:!(o=O(t,n))||o.enumerable});return e};var F=e=>k(h({},"__esModule",{value:!0}),e);var J={};P(J,{Client:()=>b,isSubjectOnEventId:()=>j,isSubjectPristine:()=>T});module.exports=F(J);var m=e=>({...e,time:new Date(e.time)});var d=(e,t)=>{if(typeof t!=typeof e)return!1;if(t===null||e===null)return t===null&&e===null;if(typeof t=="object"){if(Array.isArray(t)||Array.isArray(e))return!1;let r=t,o=e;for(let n of Object.keys(r))if(!(n in o)||!d(o[n],r[n]))return!1;return!0}return!0};var $={specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"},w=e=>d(e,$);var v=async function*(e,t){let r=e.getReader(),o=new TextDecoder("utf-8"),n="",p=()=>{r.cancel().catch(()=>{})};if(t.aborted){await r.cancel().catch(()=>{});return}t.addEventListener("abort",p);try{for(;!t.aborted;){let{done:u,value:c}=await r.read();if(u)break;n+=o.decode(c,{stream:!0});let a;for(;a=n.indexOf(`
2
- `),a!==-1;){let s=n.slice(0,a).trim();n=n.slice(a+1),s&&(yield JSON.parse(s))}}}finally{t.removeEventListener("abort",p),await r.cancel().catch(()=>{})}};var L={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},E=e=>!(!d(e,L)||e.type!=="event");var R={type:"error",payload:{error:"string"}},l=e=>!(!d(e,R)||e.type!=="error");var H={type:"eventType",payload:{eventType:"string",isPhantom:!0}},S=e=>!(!d(e,H)||e.type!=="eventType");var B={type:"heartbeat"},y=e=>!(!d(e,B)||e.type!=="heartbeat");var I={type:"subject",payload:{subject:"string"}},g=e=>!(!d(e,I)||e.type!=="subject");var b=class{#r;#t;#e(t){return new URL(t,this.#r).toString()}constructor(t,r){this.#r=t,this.#t=r}async ping(){let t=this.#e("/api/v1/ping"),r=await fetch(t,{method:"get"});if(r.status!==200)throw new Error(`Failed to ping, got HTTP status code '${r.status}', expected '200'.`);let o=await r.json();if(!d(o,{type:"string"}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#e("/api/v1/verify-api-token"),r=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#t}`}});if(r.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${r.status}', expected '200'.`);let o=await r.json();if(!d(o,{type:"string"}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,r=[]){let o=this.#e("/api/v1/write-events"),n=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"content-type":"application/json"},body:JSON.stringify({events:t,preconditions:r})});if(n.status!==200)throw new Error(`Failed to write events, got HTTP status code '${n.status}', expected '200'.`);let p=await n.json();if(!Array.isArray(p))throw new Error("Failed to parse response.");return p.map(c=>{if(!w(c))throw new Error("Failed to parse response item.");return m(c)})}readEvents(t,r,o){let n=this.#e("/api/v1/read-events"),p=this.#t;return async function*(){let u=new AbortController,c=o??u.signal,a=!o,s;if(o&&!o.aborted){let i=()=>u.abort();o.addEventListener("abort",i,{once:!0}),s=()=>o.removeEventListener("abort",i)}try{let i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:r}),signal:c});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 v(i.body,c))if(!y(f)){if(l(f))throw new Error(`${f.payload.error}.`);if(E(f)){yield m(f.payload);continue}throw new Error("Failed to read events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{s&&s(),a&&u.abort()}}()}observeEvents(t,r,o){let n=this.#e("/api/v1/observe-events"),p=this.#t;return async function*(){let u=new AbortController,c=o??u.signal,a=!o,s;if(o&&!o.aborted){let i=()=>u.abort();o.addEventListener("abort",i,{once:!0}),s=()=>o.removeEventListener("abort",i)}try{let i=await fetch(n,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:r}),signal:c});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 v(i.body,c))if(!y(f)){if(l(f))throw new Error(`${f.payload.error}.`);if(E(f)){yield m(f.payload);continue}throw new Error("Failed to observe events.")}}catch(i){if(i instanceof DOMException&&i.name==="AbortError")return;throw i}finally{s&&s(),a&&u.abort()}}()}async registerEventSchema(t,r){let o=this.#e("/api/v1/register-event-schema"),n=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"content-type":"application/json"},body:JSON.stringify({eventType:t,schema:r})});if(n.status!==200)throw new Error(`Failed to register event schema, got HTTP status code '${n.status}', expected '200'.`)}readSubjects(t,r){let o=this.#e("/api/v1/read-subjects"),n=this.#t;return async function*(){let p=new AbortController,u=r??p.signal,c=!r,a;if(r&&!r.aborted){let s=()=>p.abort();r.addEventListener("abort",s,{once:!0}),a=()=>r.removeEventListener("abort",s)}try{let s=await fetch(o,{method:"post",headers:{authorization:`Bearer ${n}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:u});if(s.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to read subjects.");for await(let i of v(s.body,u))if(!y(i)){if(l(i))throw new Error(`${i.payload.error}.`);if(g(i)){yield i.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{a&&a(),c&&p.abort()}}()}readEventTypes(t){let r=this.#e("/api/v1/read-event-types"),o=this.#t;return async function*(){let n=new AbortController,p=t??n.signal,u=!t,c;if(t&&!t.aborted){let a=()=>n.abort();t.addEventListener("abort",a,{once:!0}),c=()=>t.removeEventListener("abort",a)}try{let a=await fetch(r,{method:"post",headers:{authorization:`Bearer ${o}`},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 s of v(a.body,p))if(!y(s)){if(l(s))throw new Error(`${s.payload.error}.`);if(S(s)){yield s.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{c&&c(),u&&n.abort()}}()}};var j=(e,t)=>({type:"isSubjectOnEventId",payload:{subject:e,eventId:t}});var T=e=>({type:"isSubjectPristine",payload:{subject:e}});0&&(module.exports={Client,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:()=>y,EventSourcingDbContainer:()=>b,isSubjectOnEventId:()=>O,isSubjectPristine:()=>C});module.exports=R(M);var h=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"},v=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 y=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 h(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(!v(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(g(f)){yield h(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(!v(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(!v(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(g(f)){yield h(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(!v(s)){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(!v(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 y(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,EventSourcingDbContainer,isSubjectOnEventId,isSubjectPristine});
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var m=r=>({...r,time:new Date(r.time)});var d=(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,o=r;for(let i of Object.keys(e))if(!(i in o)||!d(o[i],e[i]))return!1;return!0}return!0};var j={specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"},E=r=>d(r,j);var v=async function*(r,t){let e=r.getReader(),o=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:u,value:c}=await e.read();if(u)break;i+=o.decode(c,{stream:!0});let a;for(;a=i.indexOf(`
2
- `),a!==-1;){let s=i.slice(0,a).trim();i=i.slice(a+1),s&&(yield JSON.parse(s))}}}finally{t.removeEventListener("abort",p),await e.cancel().catch(()=>{})}};var T={type:"event",payload:{specversion:"string",id:"string",time:"string",source:"string",subject:"string",type:"string",datacontenttype:"string",data:{},hash:"string",predecessorhash:"string"}},b=r=>!(!d(r,T)||r.type!=="event");var A={type:"error",payload:{error:"string"}},l=r=>!(!d(r,A)||r.type!=="error");var O={type:"eventType",payload:{eventType:"string",isPhantom:!0}},w=r=>!(!d(r,O)||r.type!=="eventType");var C={type:"heartbeat"},y=r=>!(!d(r,C)||r.type!=="heartbeat");var x={type:"subject",payload:{subject:"string"}},S=r=>!(!d(r,x)||r.type!=="subject");var h=class{#r;#t;#e(t){return new URL(t,this.#r).toString()}constructor(t,e){this.#r=t,this.#t=e}async ping(){let t=this.#e("/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 o=await e.json();if(!d(o,{type:"string"}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.ping-received")throw new Error("Failed to ping.")}async verifyApiToken(){let t=this.#e("/api/v1/verify-api-token"),e=await fetch(t,{method:"post",headers:{authorization:`Bearer ${this.#t}`}});if(e.status!==200)throw new Error(`Failed to verify API token, got HTTP status code '${e.status}', expected '200'.`);let o=await e.json();if(!d(o,{type:"string"}))throw new Error("Failed to parse response.");if(o.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let o=this.#e("/api/v1/write-events"),i=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"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(c=>{if(!E(c))throw new Error("Failed to parse response item.");return m(c)})}readEvents(t,e,o){let i=this.#e("/api/v1/read-events"),p=this.#t;return async function*(){let u=new AbortController,c=o??u.signal,a=!o,s;if(o&&!o.aborted){let n=()=>u.abort();o.addEventListener("abort",n,{once:!0}),s=()=>o.removeEventListener("abort",n)}try{let n=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:c});if(n.status!==200)throw new Error(`Failed to read events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to read events.");for await(let f of v(n.body,c))if(!y(f)){if(l(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield m(f.payload);continue}throw new Error("Failed to read events.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{s&&s(),a&&u.abort()}}()}observeEvents(t,e,o){let i=this.#e("/api/v1/observe-events"),p=this.#t;return async function*(){let u=new AbortController,c=o??u.signal,a=!o,s;if(o&&!o.aborted){let n=()=>u.abort();o.addEventListener("abort",n,{once:!0}),s=()=>o.removeEventListener("abort",n)}try{let n=await fetch(i,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:c});if(n.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to observe events.");for await(let f of v(n.body,c))if(!y(f)){if(l(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield m(f.payload);continue}throw new Error("Failed to observe events.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{s&&s(),a&&u.abort()}}()}async registerEventSchema(t,e){let o=this.#e("/api/v1/register-event-schema"),i=await fetch(o,{method:"post",headers:{authorization:`Bearer ${this.#t}`,"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 o=this.#e("/api/v1/read-subjects"),i=this.#t;return async function*(){let p=new AbortController,u=e??p.signal,c=!e,a;if(e&&!e.aborted){let s=()=>p.abort();e.addEventListener("abort",s,{once:!0}),a=()=>e.removeEventListener("abort",s)}try{let s=await fetch(o,{method:"post",headers:{authorization:`Bearer ${i}`,"content-type":"application/json"},body:JSON.stringify({baseSubject:t}),signal:u});if(s.status!==200)throw new Error(`Failed to read subjects, got HTTP status code '${s.status}', expected '200'.`);if(!s.body)throw new Error("Failed to read subjects.");for await(let n of v(s.body,u))if(!y(n)){if(l(n))throw new Error(`${n.payload.error}.`);if(S(n)){yield n.payload.subject;continue}throw new Error("Failed to read subjects.")}}catch(s){if(s instanceof DOMException&&s.name==="AbortError")return;throw s}finally{a&&a(),c&&p.abort()}}()}readEventTypes(t){let e=this.#e("/api/v1/read-event-types"),o=this.#t;return async function*(){let i=new AbortController,p=t??i.signal,u=!t,c;if(t&&!t.aborted){let a=()=>i.abort();t.addEventListener("abort",a,{once:!0}),c=()=>t.removeEventListener("abort",a)}try{let a=await fetch(e,{method:"post",headers:{authorization:`Bearer ${o}`},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 s of v(a.body,p))if(!y(s)){if(l(s))throw new Error(`${s.payload.error}.`);if(w(s)){yield s.payload;continue}throw new Error("Failed to read event types.")}}catch(a){if(a instanceof DOMException&&a.name==="AbortError")return;throw a}finally{c&&c(),u&&i.abort()}}()}};var P=(r,t)=>({type:"isSubjectOnEventId",payload:{subject:r,eventId:t}});var k=r=>({type:"isSubjectPristine",payload:{subject:r}});export{h as Client,P as isSubjectOnEventId,k as isSubjectPristine};
1
+ var h=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,i=o;for(let s of Object.keys(e))if(!(s in i)||!u(i[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(),i=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+=i.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"}},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"},v=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 y=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 i=await e.json();if(!u(i,{type:"string"}))throw new Error("Failed to parse response.");if(i.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 i=await e.json();if(!u(i,{type:"string"}))throw new Error("Failed to parse response.");if(i.type!=="io.eventsourcingdb.api.api-token-verified")throw new Error("Failed to verify API token.")}async writeEvents(t,e=[]){let i=this.#t("/api/v1/write-events"),s=await fetch(i,{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 h(d)})}readEvents(t,e,i){let s=this.#t("/api/v1/read-events"),p=this.#e;return async function*(){let c=new AbortController,d=i??c.signal,a=!i,r;if(i&&!i.aborted){let n=()=>c.abort();i.addEventListener("abort",n,{once:!0}),r=()=>i.removeEventListener("abort",n)}try{let n=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(n.status!==200)throw new Error(`Failed to read events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to read events.");for await(let f of l(n.body,d))if(!v(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield h(f.payload);continue}throw new Error("Failed to read events.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{r&&r(),a&&c.abort()}}()}runEventQlQuery(t,e){let i=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(i,{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 n of l(r.body,c))if(!v(n)){if(m(n))throw new Error(`${n.payload.error}.`);if(S(n)){yield n.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,i){let s=this.#t("/api/v1/observe-events"),p=this.#e;return async function*(){let c=new AbortController,d=i??c.signal,a=!i,r;if(i&&!i.aborted){let n=()=>c.abort();i.addEventListener("abort",n,{once:!0}),r=()=>i.removeEventListener("abort",n)}try{let n=await fetch(s,{method:"post",headers:{authorization:`Bearer ${p}`,"content-type":"application/json"},body:JSON.stringify({subject:t,options:e}),signal:d});if(n.status!==200)throw new Error(`Failed to observe events, got HTTP status code '${n.status}', expected '200'.`);if(!n.body)throw new Error("Failed to observe events.");for await(let f of l(n.body,d))if(!v(f)){if(m(f))throw new Error(`${f.payload.error}.`);if(b(f)){yield h(f.payload);continue}throw new Error("Failed to observe events.")}}catch(n){if(n instanceof DOMException&&n.name==="AbortError")return;throw n}finally{r&&r(),a&&c.abort()}}()}async registerEventSchema(t,e){let i=this.#t("/api/v1/register-event-schema"),s=await fetch(i,{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 i=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(i,{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 n of l(r.body,c))if(!v(n)){if(m(n))throw new Error(`${n.payload.error}.`);if(T(n)){yield n.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"),i=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 ${i}`},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(!v(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 y(this.getBaseUrl(),this.getApiToken())}};var R=(o,t)=>({type:"isSubjectOnEventId",payload:{subject:o,eventId:t}});var H=o=>({type:"isSubjectPristine",payload:{subject:o}});export{y as Client,w as EventSourcingDbContainer,R as isSubjectOnEventId,H as isSubjectPristine};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eventsourcingdb",
3
- "version": "0.10.6",
3
+ "version": "0.12.0",
4
4
  "description": "The official JavaScript client SDK for EventSourcingDB.",
5
5
  "author": {
6
6
  "name": "the native web GmbH",
@@ -43,5 +43,8 @@
43
43
  "event sourcing",
44
44
  "event store",
45
45
  "cqrs"
46
- ]
46
+ ],
47
+ "dependencies": {
48
+ "testcontainers": "10.24.2"
49
+ }
47
50
  }