@@ -13,6 +13,7 @@ import { isStreamError } from './stream/isStreamError.js';
1313import { isStreamEventType } from './stream/isStreamEventType.js' ;
1414import { isStreamHeartbeat } from './stream/isStreamHeartbeat.js' ;
1515import { isStreamSubject } from './stream/isStreamSubject.js' ;
16+ import { hasShapeOf } from './types/hasShapeOf.js' ;
1617
1718class Client {
1819 #url: URL ;
@@ -29,35 +30,47 @@ class Client {
2930
3031 public async ping ( ) : Promise < void > {
3132 const url = this . #getUrl( '/api/v1/ping' ) ;
32-
33- const response = await axios ( {
34- url,
33+ const response = await fetch ( url , {
3534 method : 'get' ,
36- responseType : 'json' ,
3735 } ) ;
3836
39- const eventType = 'io.eventsourcingdb.api.ping-received' ;
37+ if ( response . status !== 200 ) {
38+ throw new Error ( `Failed to ping, got HTTP status code '${ response . status } ', expected '200'.` ) ;
39+ }
4040
41- if ( response . data . type !== eventType ) {
41+ const responseBody = await response . json ( ) ;
42+ if ( ! hasShapeOf ( responseBody , { type : 'string' } ) ) {
43+ throw new Error ( 'Failed to parse response.' ) ;
44+ }
45+
46+ const eventType = 'io.eventsourcingdb.api.ping-received' ;
47+ if ( responseBody . type !== eventType ) {
4248 throw new Error ( 'Failed to ping.' ) ;
4349 }
4450 }
4551
4652 public async verifyApiToken ( ) : Promise < void > {
4753 const url = this . #getUrl( '/api/v1/verify-api-token' ) ;
48-
49- const response = await axios ( {
50- url,
54+ const response = await fetch ( url , {
5155 method : 'post' ,
5256 headers : {
5357 authorization : `Bearer ${ this . #apiToken} ` ,
5458 } ,
55- responseType : 'json' ,
5659 } ) ;
5760
58- const eventType = 'io.eventsourcingdb.api.api-token-verified' ;
61+ if ( response . status !== 200 ) {
62+ throw new Error (
63+ `Failed to verify API token, got HTTP status code '${ response . status } ', expected '200'.` ,
64+ ) ;
65+ }
66+
67+ const responseBody = await response . json ( ) ;
68+ if ( ! hasShapeOf ( responseBody , { type : 'string' } ) ) {
69+ throw new Error ( 'Failed to parse response.' ) ;
70+ }
5971
60- if ( response . data . type !== eventType ) {
72+ const eventType = 'io.eventsourcingdb.api.api-token-verified' ;
73+ if ( responseBody . type !== eventType ) {
6174 throw new Error ( 'Failed to verify API token.' ) ;
6275 }
6376 }
@@ -67,22 +80,25 @@ class Client {
6780 preconditions : Precondition [ ] = [ ] ,
6881 ) : Promise < Event [ ] > {
6982 const url = this . #getUrl( '/api/v1/write-events' ) ;
70-
71- const response = await axios ( {
72- url,
83+ const response = await fetch ( url , {
7384 method : 'post' ,
7485 headers : {
7586 authorization : `Bearer ${ this . #apiToken} ` ,
7687 'content-type' : 'application/json' ,
7788 } ,
78- data : {
89+ body : JSON . stringify ( {
7990 events,
8091 preconditions,
81- } ,
82- responseType : 'json' ,
92+ } ) ,
8393 } ) ;
8494
85- const responseBody = response . data ;
95+ if ( response . status !== 200 ) {
96+ throw new Error (
97+ `Failed to write events, got HTTP status code '${ response . status } ', expected '200'.` ,
98+ ) ;
99+ }
100+
101+ const responseBody = await response . json ( ) ;
86102
87103 if ( ! Array . isArray ( responseBody ) ) {
88104 throw new Error ( 'Failed to parse response.' ) ;
0 commit comments