@nxtedition/deepstream.io-client-js 32.0.11 → 32.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/deepstream.io-client-js",
3
- "version": "32.0.11",
3
+ "version": "32.0.13",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
package/src/client.d.ts CHANGED
@@ -7,10 +7,21 @@ import type { EventStats } from './event/event-handler.js'
7
7
  import type RpcHandler from './rpc/rpc-handler.js'
8
8
  import type { RpcStats, RpcMethodDef } from './rpc/rpc-handler.js'
9
9
 
10
+ export interface DeepstreamClientOptions {
11
+ reconnectIntervalIncrement?: number
12
+ maxReconnectInterval?: number
13
+ maxReconnectAttempts?: number
14
+ maxPacketSize?: number
15
+ batchSize?: number
16
+ schedule?: ((fn: () => void) => void) | null
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ logger?: any
19
+ }
20
+
10
21
  export default function <
11
22
  Records extends Record<string, unknown> = Record<string, unknown>,
12
23
  Methods extends Record<string, RpcMethodDef> = Record<string, RpcMethodDef>,
13
- >(url: string, options?: unknown): DeepstreamClient<Records, Methods>
24
+ >(url: string, options?: DeepstreamClientOptions): DeepstreamClient<Records, Methods>
14
25
 
15
26
  export type {
16
27
  DsRecord,
@@ -22,6 +33,8 @@ export type {
22
33
  SyncOptions,
23
34
  Paths,
24
35
  Get,
36
+ ConnectionStateName,
37
+ DeepstreamErrorEventName,
25
38
  }
26
39
 
27
40
  type RecordStateConstants = Readonly<{
@@ -77,6 +90,36 @@ type EventConstants = Readonly<{
77
90
  }>
78
91
  type EventKey = keyof EventConstants
79
92
  type EventName = EventConstants[EventKey]
93
+ type DeepstreamErrorEventName = Exclude<
94
+ EventName,
95
+ 'connectionStateChanged' | 'connected' | 'MAX_RECONNECTION_ATTEMPTS_REACHED'
96
+ >
97
+
98
+ export interface DeepstreamError extends Error {
99
+ topic?: string
100
+ event?: EventName | null
101
+ data?: unknown
102
+ }
103
+
104
+ export interface DeepstreamMessage {
105
+ raw: string | null
106
+ topic: string | null
107
+ action: string | null
108
+ data: string[]
109
+ }
110
+
111
+ export interface DeepstreamClientEventMap {
112
+ connectionStateChanged: (state: ConnectionStateName) => void
113
+ connected: (connected: boolean) => void
114
+ MAX_RECONNECTION_ATTEMPTS_REACHED: (attempt: number) => void
115
+ error: (error: DeepstreamError) => void
116
+ recv: (message: DeepstreamMessage) => void
117
+ send: (message: DeepstreamMessage) => void
118
+ }
119
+
120
+ type DeepstreamErrorEventMap = {
121
+ [K in DeepstreamErrorEventName]: (error: DeepstreamError) => void
122
+ }
80
123
 
81
124
  export interface DeepstreamClient<
82
125
  Records extends Record<string, unknown> = Record<string, unknown>,
@@ -87,12 +130,21 @@ export interface DeepstreamClient<
87
130
  rpc: RpcHandler<Methods>
88
131
  record: RecordHandler<Records>
89
132
  user: string | null
90
- on: (evt: EventName | 'error', callback: (...args: unknown[]) => void) => this
91
- off: (evt: EventName | 'error', callback: (...args: unknown[]) => void) => this
133
+ on<K extends keyof (DeepstreamClientEventMap & DeepstreamErrorEventMap)>(
134
+ evt: K,
135
+ callback: (DeepstreamClientEventMap & DeepstreamErrorEventMap)[K],
136
+ ): this
137
+ off<K extends keyof (DeepstreamClientEventMap & DeepstreamErrorEventMap)>(
138
+ evt: K,
139
+ callback: (DeepstreamClientEventMap & DeepstreamErrorEventMap)[K],
140
+ ): this
92
141
  getConnectionState: () => ConnectionStateName
93
142
  close: () => void
94
143
  login(callback: (success: boolean, authData: unknown) => void): this
95
- login(authParams: object, callback: (success: boolean, authData: unknown) => void): this
144
+ login(
145
+ authParams: Record<string, unknown>,
146
+ callback: (success: boolean, authData: unknown) => void,
147
+ ): this
96
148
  stats: {
97
149
  record: RecordStats
98
150
  rpc: RpcStats
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import make, { type DeepstreamClient } from './client.js'
2
+ import make, { type DeepstreamClient, type DeepstreamError } from './client.js'
3
3
  import { expectAssignable, expectError, expectType } from 'tsd'
4
4
  import type { Observable } from 'rxjs'
5
5
  import type { EmptyObject } from 'type-fest'
@@ -287,3 +287,34 @@ expectAssignable<(() => void) | void>(ds.event.provide('pattern*', () => {}, {})
287
287
  ds.on('error', (err) => {})
288
288
  ds.off('error', (err) => {})
289
289
  expectError(ds.on('unknownEvent', () => {}))
290
+
291
+ // client.on: callback arg types per event
292
+ ds.on('error', (err) => {
293
+ expectType<DeepstreamError>(err)
294
+ })
295
+ ds.on('connectionError', (err) => {
296
+ expectType<DeepstreamError>(err)
297
+ })
298
+ ds.on('connectionStateChanged', (state) => {
299
+ expectType<
300
+ | 'CLOSED'
301
+ | 'AWAITING_CONNECTION'
302
+ | 'CHALLENGING'
303
+ | 'AWAITING_AUTHENTICATION'
304
+ | 'AUTHENTICATING'
305
+ | 'OPEN'
306
+ | 'ERROR'
307
+ | 'RECONNECTING'
308
+ >(state)
309
+ })
310
+ ds.on('connected', (connected) => {
311
+ expectType<boolean>(connected)
312
+ })
313
+ ds.on('MAX_RECONNECTION_ATTEMPTS_REACHED', (attempt) => {
314
+ expectType<number>(attempt)
315
+ })
316
+
317
+ // client.on: wrong callback arg types are errors
318
+ expectError(ds.on('connectionStateChanged', (state: number) => {}))
319
+ expectError(ds.on('connected', (connected: string) => {}))
320
+ expectError(ds.on('MAX_RECONNECTION_ATTEMPTS_REACHED', (attempt: string) => {}))
@@ -78,7 +78,7 @@ EventHandler.on = function (name, callback) {
78
78
 
79
79
  EventHandler.once = function (name, callback) {
80
80
  const fn = (...args) => {
81
- this.unsubscribe(fn)
81
+ this.unsubscribe(name, fn)
82
82
  callback(...args)
83
83
  }
84
84
  this.subscribe(name, fn)
@@ -249,7 +249,7 @@ class Record {
249
249
  when(stateOrNil, optionsOrNil) {
250
250
  invariant(this._refs > 0, 'missing refs')
251
251
 
252
- if (stateOrNil != null && stateOrNil === 'object') {
252
+ if (stateOrNil != null && typeof stateOrNil === 'object') {
253
253
  optionsOrNil = stateOrNil
254
254
  stateOrNil = optionsOrNil?.state
255
255
  }
@@ -85,7 +85,7 @@ export function setTimeout(callback, timeoutDuration) {
85
85
 
86
86
  export function setInterval(callback, intervalDuration) {
87
87
  if (intervalDuration !== null) {
88
- return setInterval(callback, intervalDuration)
88
+ return globalThis.setInterval(callback, intervalDuration)
89
89
  } else {
90
90
  return -1
91
91
  }
@@ -1,9 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(npm run test:types:*)"
5
- ],
6
- "deny": [],
7
- "ask": []
8
- }
9
- }