11import { accountDeleteBasin } from "./funcs/accountDeleteBasin" ;
22import { RequestOptions } from "./lib/sdks" ;
3- import { BasinConfig , BasinInfo , CheckTailResponse , CreateBasinRequest , CreateStreamRequest , StreamConfig , StreamInfo } from "./models/components" ;
3+ import { AppendOutput , BasinConfig , BasinInfo , CheckTailResponse , CreateBasinRequest , CreateStreamRequest , Output , ReadResponse , StreamConfig , StreamInfo } from "./models/components" ;
44import { NotFoundError } from "./models/errors" ;
5- import {
5+ import {
66 GetBasinConfigRequest ,
77 ListBasinsRequest ,
88 ListBasinsResponse ,
99 ListStreamsRequest ,
1010 ListStreamsResponse ,
1111 ReconfigureBasinRequest ,
1212 CreateBasinRequest as CreateBasinRequestInner ,
13- CreateStreamRequest as CreateStreamRequestInner
13+ CreateStreamRequest as CreateStreamRequestInner ,
14+ ReadRequest as ReadRequestInner ,
15+ AppendRequest as AppendRequestInner ,
1416} from "./models/operations" ;
1517import { PageIterator } from "./types" ;
1618
17- import { Stream as InnerStream } from "./sdk/stream" ;
19+ import { Stream as InnerStream , ReadAcceptEnum } from "./sdk/stream" ;
1820import { Basin as InnerBasin } from "./sdk/basin" ;
1921import { Account as InnerAccount } from "./sdk/account" ;
2022
2123import { v4 } from 'uuid' ;
2224import { basinDeleteStream } from "./funcs/basinDeleteStream" ;
25+ import { EventStream } from "./lib/event-streams" ;
26+
27+ export type ReadRequest = Omit < ReadRequestInner , 'stream' > ;
28+ export type AppendRequest = Omit < AppendRequestInner , 'stream' > ;
29+ export type {
30+ GetBasinConfigRequest ,
31+ ListBasinsRequest ,
32+ ListBasinsResponse ,
33+ ListStreamsRequest ,
34+ ListStreamsResponse ,
35+ ReconfigureBasinRequest ,
36+ } from "./models/operations" ;
37+ export type { BasinConfig , BasinInfo , CheckTailResponse , CreateBasinRequest , CreateStreamRequest , Output , ReadResponse , StreamConfig , StreamInfo } from "./models/components" ;
2338
2439export function genS2RequestToken ( ) : string {
2540 return v4 ( ) . replace ( / - / g, '' ) ;
@@ -45,9 +60,11 @@ export class S2Client {
4560class S2Account {
4661 private _account : InnerAccount ;
4762 private requestOptions : RequestOptions ;
63+ private authToken : string ;
4864
4965 constructor ( authToken : string ) {
5066 this . _account = new InnerAccount ( { bearerAuth : authToken } ) ;
67+ this . authToken = authToken ;
5168 this . requestOptions = {
5269 timeoutMs : 3000 ,
5370 retries : {
@@ -64,9 +81,10 @@ class S2Account {
6481 } ;
6582 }
6683
67- /**
68- * List basins.
69- */
84+ basin ( basinName : string ) : S2Basin {
85+ return new S2Basin ( this . authToken , basinName ) ;
86+ }
87+
7088 async listBasins (
7189 request ?: ListBasinsRequest ,
7290 ) : Promise < PageIterator < ListBasinsResponse , { cursor : string } > > {
@@ -132,7 +150,7 @@ class S2Basin {
132150 }
133151
134152 stream ( streamName : string ) : Stream {
135- return ( this . _stream ??= new Stream ( streamName , this . basinName ) ) ;
153+ return ( this . _stream ??= new Stream ( this . basinName , streamName ) ) ;
136154 }
137155
138156 async listStreams (
@@ -151,13 +169,13 @@ class S2Basin {
151169
152170 async createStream (
153171 stream : string ,
154- request : CreateStreamRequest ,
172+ request ? : CreateStreamRequest ,
155173 ) : Promise < StreamInfo | undefined > {
156174 const basinURL = `${ this . basinName } .${ this . basinURLSuffx } ` ;
157175 const _request : CreateStreamRequestInner = {
158176 stream,
159177 s2RequestToken : genS2RequestToken ( ) ,
160- createStreamRequest : request ,
178+ createStreamRequest : request ?? { } ,
161179 } ;
162180
163181 return (
@@ -170,7 +188,7 @@ class S2Basin {
170188 const response = await basinDeleteStream ( this . _basin , { stream } , { serverURL : basinURL , ...this . requestOptions } ) ;
171189 if ( if_exists && response instanceof NotFoundError ) return ;
172190 if ( response . error ) throw new Error ( response . error . message ) ;
173- return ;
191+ return ;
174192 }
175193
176194 async reconfigureStream ( stream : string , config : StreamConfig ) : Promise < StreamConfig | undefined > {
@@ -184,15 +202,16 @@ class S2Basin {
184202 }
185203}
186204
205+
187206class Stream {
188207 private _stream : InnerStream ;
189- private streamName : string ;
190208 private basinName : string ;
209+ private streamName : string ;
191210 private readonly basinURLSuffx = 'b.aws.s2.dev/v1alpha' ;
192211
193- constructor ( streamName : string , basinName : string ) {
194- this . streamName = streamName ;
212+ constructor ( basinName : string , streamName : string ) {
195213 this . basinName = basinName ;
214+ this . streamName = streamName ;
196215 this . _stream = new InnerStream ( ) ;
197216 }
198217
@@ -201,4 +220,28 @@ class Stream {
201220 return ( await this . _stream . checkTail ( { stream : this . streamName } , { serverURL : basinURL } ) )
202221 . checkTailResponse ;
203222 }
223+
224+ async read (
225+ request : ReadRequest ,
226+ stream ?: boolean
227+ ) : Promise < EventStream < ReadResponse > | Output | undefined > {
228+ const basinURL = `${ this . basinName } .${ this . basinURLSuffx } ` ;
229+ return ( await this . _stream . read (
230+ { ...request , stream : this . streamName } ,
231+ stream
232+ ? { serverURL : basinURL , acceptHeaderOverride : ReadAcceptEnum . textEventStream }
233+ : { serverURL : basinURL }
234+ ) )
235+ . readResponse ;
236+ }
237+
238+ async append (
239+ request : AppendRequest ,
240+ ) : Promise < AppendOutput | undefined > {
241+ const basinURL = `${ this . basinName } .${ this . basinURLSuffx } ` ;
242+ return ( await this . _stream . append (
243+ { ...request , stream : this . streamName } ,
244+ { serverURL : basinURL }
245+ ) ) . appendOutput ;
246+ }
204247}
0 commit comments