From 60f1d7252b54648b5add1f0a01a4816728b3288d Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Sun, 13 Nov 2016 23:21:01 -0800 Subject: [PATCH 1/2] Minimalistic TypeScript declaration file, wip for issue #13 --- index.d.ts | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + 2 files changed, 143 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..ce30d4e --- /dev/null +++ b/index.d.ts @@ -0,0 +1,141 @@ +/// +/// + +export namespace expectedVersion { + const any: number; + const noStream: number; + const emptyStream: number; +} + +export namespace positions { + const start: Position; + const end: Position; +} + +export interface EventData { +} + +export function createJsonEventData(eventId: string, event: any, metadata?: any, type?: string): EventData; + +export function createEventData(eventId: string, type: string, isJson: boolean, data: Buffer, metadata?: Buffer): EventData; + +export interface TcpEndPoint { + port: number; + hostname: string; +} + +export interface GossipSeed { + new (endPoint: TcpEndPoint, hostHeader: string); + readonly endPoint: TcpEndPoint; + readonly hostHeader: string; +} + +export interface Logger { + debug(fmt: string, ...args: any[]): void; + info(fmt: string, ...args: any[]): void; + error(fmt: string, ...args: any[]): void; +} + +export interface UserCredentials { + new (username: string, password: string); +} + +export interface ConnectionSettings { + log?: Logger, + verboseLogging?: boolean, + + maxQueueSize?: number, + maxConcurrentItems?: number, + maxRetries?: number, + maxReconnections?: number, + + requireMaster?: boolean, + + reconnectionDelay?: number, + operationTimeout?: number, + operationTimeoutCheckPeriod?: number, + + defaultUserCredentials?: UserCredentials, + useSslConnection?: boolean, + targetHost?: TcpEndPoint, + validateServer?: boolean, + + failOnNoServerResponse?: boolean, + heartbeatInterval?: number, + heartbeatTimeout?: number, + clientConnectionTimeout?: number, + + // Cluster Settings + clusterDns?: string, + maxDiscoverAttempts?: number, + externalGossipPort?: number, + gossipTimeout?: number +} + +export interface WriteResult { + readonly nextExpectedVersion: number; + readonly logPosition: Position; +} + +export interface RecordedEvent { + readonly eventStreamId: string; + readonly eventId: string; + readonly eventNumber: number; + readonly eventType: string; + readonly createdEpoch: number; + readonly data?: Buffer; + readonly metadata?: Buffer; + readonly isJson: boolean; +} + +export interface ResolvedEvent { + readonly event?: RecordedEvent; + readonly link?: RecordedEvent; + readonly originalEvent?: RecordedEvent; + readonly isResolved: boolean; + readonly originalPosition?: Position; + readonly originalStreamId: string; + readonly originalEventNumber: number; +} + +export interface StreamEventsSlice { + readonly status: string; // TODO: enum + readonly stream: string; + readonly fromEventNumber: number; + readonly readDirection: string; // TODO: enum + readonly events: ResolvedEvent[]; + readonly nextEventNumber: number; + readonly lastEventNumber: number; + readonly isEndOfStream: boolean; +} + +export interface EventStoreSubscription { + readonly isSubscribedToAll: boolean; + readonly streamId: string; + readonly lastCommitPosition: Position; + readonly lastEventNumber: number; + + close(): void; + unsubscribe(): void; +} + +export interface EventAppearedCallback { + (subscription: EventStoreSubscription, event: EventData); +} + +export interface SubscriptionDroppedCallback { + (subscription: EventStoreSubscription, reason: string, error?: Error); +} + +export interface EventStoreNodeConnection { + connect(): Promise; + close(): void; + appendToStream(stream: string, expectedVersion: number, events: EventData[], userCredentials?: UserCredentials): Promise; + readStreamEventsForward(stream: string, start: number, count: number, resolveLinkTos: boolean, userCredentials?: UserCredentials): Promise; + subscribeToStream(stream: string, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials): Promise; + + on(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: Function): this; + once(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: Function): this; +} + +export function createConnection(settings: ConnectionSettings, endPointOrGossipSeed: string | TcpEndPoint | GossipSeed[], connectionName?: string): EventStoreNodeConnection; diff --git a/package.json b/package.json index 0f184f8..872c3f1 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.10", "description": "A port of the EventStore .Net ClientAPI to Node.js", "main": "index.js", + "types": "index.d.ts", "scripts": { "clean": "rm lib/dist.js", "build": "webpack", @@ -37,6 +38,7 @@ }, "homepage": "https://github.com/nicdex/eventstore-node#readme", "dependencies": { + "@types/es6-promise": "0.0.32", "long": "^3.2", "protobufjs": "^5.0", "uuid": "^2.0" From 9b9e202cec9b29d36b758900c2771f3171d3ed16 Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Mon, 14 Nov 2016 22:33:33 -0800 Subject: [PATCH 2/2] Completing TypeScript declaration file (only missing persistent subscriptions) --- index.d.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index ce30d4e..9d35295 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,6 +13,11 @@ export namespace positions { } export interface EventData { + readonly eventId: string; + readonly type: string; + readonly isJson: boolean; + readonly data: Buffer; + readonly metadata: Buffer; } export function createJsonEventData(eventId: string, event: any, metadata?: any, type?: string): EventData; @@ -38,6 +43,9 @@ export interface Logger { export interface UserCredentials { new (username: string, password: string); + + readonly username: string; + readonly password: string; } export interface ConnectionSettings { @@ -109,6 +117,32 @@ export interface StreamEventsSlice { readonly isEndOfStream: boolean; } +export interface AllEventsSlice { + readonly readDirection: string; // TODO enum + readonly fromPosition: Position; + readonly nextPosition: Position; + readonly events: ResolvedEvent[]; + readonly isEndOfStream: boolean; +} + +export interface DeleteResult { + readonly logPosition: Position; +} + +export interface EventStoreTransaction { + readonly transactionId: number; + commit(): Promise; + write(eventOrEvents: EventData | EventData[]): Promise; + rollback(): void; +} + +export interface EventReadResult { + readonly status: string; + readonly stream: string; + readonly eventNumber: number; + readonly event: ResolvedEvent | null; +} + export interface EventStoreSubscription { readonly isSubscribedToAll: boolean; readonly streamId: string; @@ -119,23 +153,56 @@ export interface EventStoreSubscription { unsubscribe(): void; } -export interface EventAppearedCallback { - (subscription: EventStoreSubscription, event: EventData); +export interface EventStoreCatchUpSubscription { + start(): void; + stop(): void; } -export interface SubscriptionDroppedCallback { - (subscription: EventStoreSubscription, reason: string, error?: Error); +export interface RawStreamMetadataResult { + readonly stream: string; + readonly isStreamDeleted: boolean; + readonly metastreamVersion: number; + readonly streamMetadata: any; +} + +// Callbacks +export interface EventAppearedCallback { + (subscription: TSubscription, event: EventData): void; +} + +export interface LiveProcessingStartedCallback { + (subscription: EventStoreCatchUpSubscription): void; +} + +export interface SubscriptionDroppedCallback { + (subscription: TSubscription, reason: string, error?: Error); } export interface EventStoreNodeConnection { connect(): Promise; close(): void; - appendToStream(stream: string, expectedVersion: number, events: EventData[], userCredentials?: UserCredentials): Promise; - readStreamEventsForward(stream: string, start: number, count: number, resolveLinkTos: boolean, userCredentials?: UserCredentials): Promise; - subscribeToStream(stream: string, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials): Promise; + // write actions + deleteStream(stream: string, expectedVersion: number, hardDelete?: boolean, userCredentials?: UserCredentials): Promise; + appendToStream(stream: string, expectedVersion: number, eventOrEvents: EventData | EventData[], userCredentials?: UserCredentials): Promise; + startTransaction(stream: string, expectedVersion: number, userCredentials?: UserCredentials): Promise; + continueTransaction(transactionId: number, userCredentials?: UserCredentials): EventStoreTransaction; + // read actions + readEvent(stream: string, eventNumber: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise; + readStreamEventsForward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise; + readStreamEventsBackward(stream: string, start: number, count: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise; + readAllEventsForward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise; + readAllEventsBackward(position: Position, maxCount: number, resolveLinkTos?: boolean, userCredentials?: UserCredentials): Promise; + // subscription actions + subscribeToStream(stream: string, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials): Promise; + subscribeToStreamFrom(stream: string, lastCheckpoint: number | null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription; + subscribeToAll(resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials): Promise; + subscribeToAllFrom(lastCheckpoint: Position | null, resolveLinkTos: boolean, eventAppeared: EventAppearedCallback, liveProcessingStarted?: LiveProcessingStartedCallback, subscriptionDropped?: SubscriptionDroppedCallback, userCredentials?: UserCredentials, readBatchSize?: number): EventStoreCatchUpSubscription; + // metadata actions + setStreamMetadataRaw(stream: string, expectedMetastreamVersion: number, metadata: any, userCredentials?: UserCredentials): Promise; + getStreamMetadataRaw(stream: string, userCredentials?: UserCredentials): Promise; - on(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: Function): this; - once(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: Function): this; + on(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: (arg: Error | string | TcpEndPoint) => void): this; + once(event: "connected" | "disconnected" | "reconnecting" | "closed" | "error", listener: (arg: Error | string | TcpEndPoint) => void): this; } export function createConnection(settings: ConnectionSettings, endPointOrGossipSeed: string | TcpEndPoint | GossipSeed[], connectionName?: string): EventStoreNodeConnection;