@nmtjs/client 0.15.3 → 0.16.0-beta.1

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.
Files changed (56) hide show
  1. package/dist/client.d.ts +64 -0
  2. package/dist/client.js +97 -0
  3. package/dist/client.js.map +1 -0
  4. package/dist/clients/runtime.d.ts +6 -12
  5. package/dist/clients/runtime.js +58 -57
  6. package/dist/clients/runtime.js.map +1 -1
  7. package/dist/clients/static.d.ts +4 -9
  8. package/dist/clients/static.js +20 -20
  9. package/dist/clients/static.js.map +1 -1
  10. package/dist/core.d.ts +33 -83
  11. package/dist/core.js +305 -690
  12. package/dist/core.js.map +1 -1
  13. package/dist/events.d.ts +0 -1
  14. package/dist/events.js +74 -11
  15. package/dist/events.js.map +1 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.js +4 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/layers/ping.d.ts +6 -0
  20. package/dist/layers/ping.js +65 -0
  21. package/dist/layers/ping.js.map +1 -0
  22. package/dist/layers/rpc.d.ts +19 -0
  23. package/dist/layers/rpc.js +521 -0
  24. package/dist/layers/rpc.js.map +1 -0
  25. package/dist/layers/streams.d.ts +20 -0
  26. package/dist/layers/streams.js +194 -0
  27. package/dist/layers/streams.js.map +1 -0
  28. package/dist/plugins/browser.js +28 -9
  29. package/dist/plugins/browser.js.map +1 -1
  30. package/dist/plugins/heartbeat.js +10 -10
  31. package/dist/plugins/heartbeat.js.map +1 -1
  32. package/dist/plugins/index.d.ts +1 -1
  33. package/dist/plugins/index.js +0 -1
  34. package/dist/plugins/index.js.map +1 -1
  35. package/dist/plugins/reconnect.js +11 -94
  36. package/dist/plugins/reconnect.js.map +1 -1
  37. package/dist/plugins/types.d.ts +27 -11
  38. package/dist/transport.d.ts +49 -31
  39. package/dist/types.d.ts +21 -5
  40. package/package.json +10 -10
  41. package/src/client.ts +216 -0
  42. package/src/clients/runtime.ts +93 -79
  43. package/src/clients/static.ts +46 -38
  44. package/src/core.ts +394 -901
  45. package/src/events.ts +113 -14
  46. package/src/index.ts +4 -0
  47. package/src/layers/ping.ts +99 -0
  48. package/src/layers/rpc.ts +725 -0
  49. package/src/layers/streams.ts +277 -0
  50. package/src/plugins/browser.ts +39 -9
  51. package/src/plugins/heartbeat.ts +10 -10
  52. package/src/plugins/index.ts +8 -1
  53. package/src/plugins/reconnect.ts +12 -119
  54. package/src/plugins/types.ts +30 -13
  55. package/src/transport.ts +75 -46
  56. package/src/types.ts +33 -8
package/src/transport.ts CHANGED
@@ -5,59 +5,84 @@ import type {
5
5
  } from '@nmtjs/protocol'
6
6
  import type { BaseClientFormat } from '@nmtjs/protocol/client'
7
7
 
8
- export type ClientTransportMessageOptions = {
9
- signal?: AbortSignal
10
- _stream_response?: boolean
11
- }
8
+ export type ClientDisconnectReason = 'client' | 'server' | (string & {})
12
9
 
13
- export interface ClientTransportStartParams {
10
+ export interface TransportConnectParams {
14
11
  auth?: string
15
12
  application?: string
16
- onMessage: (message: ArrayBufferView) => any
17
- onConnect: () => any
18
- onDisconnect: (reason: 'client' | 'server' | (string & {})) => any
13
+ onMessage: (message: ArrayBufferView) => void
14
+ onConnect: () => void
15
+ onDisconnect: (reason: ClientDisconnectReason) => void
19
16
  }
20
17
 
21
- export interface ClientTransportRpcParams {
22
- format: BaseClientFormat
18
+ export interface TransportSendOptions {
19
+ signal?: AbortSignal
20
+ }
21
+
22
+ export interface TransportCallContext {
23
+ contentType: string
23
24
  auth?: string
24
25
  application?: string
25
26
  }
26
27
 
27
- export type ClientCallResponse =
28
- | { type: 'rpc'; result: ArrayBufferView }
29
- | { type: 'rpc_stream'; stream: ReadableStream<ArrayBufferView> }
30
- | {
31
- type: 'blob'
32
- metadata: ProtocolBlobMetadata
33
- source: ReadableStream<ArrayBufferView>
34
- }
35
-
36
- export type ClientTransport<T extends ConnectionType = ConnectionType> =
37
- T extends ConnectionType.Bidirectional
38
- ? {
39
- type: ConnectionType.Bidirectional
40
- connect(params: ClientTransportStartParams): Promise<void>
41
- disconnect(): Promise<void>
42
- send(
43
- message: ArrayBufferView,
44
- options: ClientTransportMessageOptions,
45
- ): Promise<void>
46
- }
47
- : {
48
- type: ConnectionType.Unidirectional
49
- connect?(params: ClientTransportStartParams): Promise<void>
50
- disconnect?(): Promise<void>
51
- call(
52
- client: {
53
- format: BaseClientFormat
54
- auth?: string
55
- application?: string
56
- },
57
- rpc: { callId: number; procedure: string; payload: any },
58
- options: ClientTransportMessageOptions,
59
- ): Promise<ClientCallResponse>
60
- }
28
+ export interface TransportRpcParams {
29
+ callId: number
30
+ procedure: string
31
+ payload: ArrayBufferView
32
+ blob?: { source: ReadableStream; metadata: ProtocolBlobMetadata }
33
+ }
34
+
35
+ export interface TransportCallOptions {
36
+ signal?: AbortSignal
37
+ streamResponse?: boolean
38
+ }
39
+
40
+ export interface TransportRpcResponse {
41
+ type: 'rpc'
42
+ result: ArrayBufferView
43
+ }
44
+
45
+ export interface TransportRpcStreamResponse {
46
+ type: 'rpc_stream'
47
+ stream: ReadableStream<ArrayBufferView>
48
+ }
49
+
50
+ export interface TransportBlobResponse {
51
+ type: 'blob'
52
+ metadata: ProtocolBlobMetadata
53
+ source: ReadableStream<ArrayBufferView>
54
+ }
55
+
56
+ export interface TransportErrorResponse {
57
+ type: 'error'
58
+ error: ArrayBufferView
59
+ status?: number
60
+ statusText?: string
61
+ }
62
+
63
+ export type TransportCallResponse =
64
+ | TransportRpcResponse
65
+ | TransportRpcStreamResponse
66
+ | TransportBlobResponse
67
+ | TransportErrorResponse
68
+
69
+ export interface BidirectionalTransport {
70
+ type: ConnectionType.Bidirectional
71
+ connect(params: TransportConnectParams): Promise<void>
72
+ disconnect(): Promise<void>
73
+ send(message: ArrayBufferView, options: TransportSendOptions): Promise<void>
74
+ }
75
+
76
+ export interface UnidirectionalTransport {
77
+ type: ConnectionType.Unidirectional
78
+ call(
79
+ context: TransportCallContext,
80
+ rpc: TransportRpcParams,
81
+ options: TransportCallOptions,
82
+ ): Promise<TransportCallResponse>
83
+ }
84
+
85
+ export type ClientTransport = BidirectionalTransport | UnidirectionalTransport
61
86
 
62
87
  export interface ClientTransportParams {
63
88
  protocol: ProtocolVersion
@@ -65,7 +90,11 @@ export interface ClientTransportParams {
65
90
  }
66
91
 
67
92
  export type ClientTransportFactory<
68
- Type extends ConnectionType,
93
+ Transport extends ClientTransport = ClientTransport,
69
94
  Options = unknown,
70
- Transport extends ClientTransport<Type> = ClientTransport<Type>,
71
95
  > = (params: ClientTransportParams, options: Options) => Transport
96
+
97
+ export type ClientTransportMessageOptions = TransportSendOptions
98
+ export type ClientTransportStartParams = TransportConnectParams
99
+ export type ClientTransportRpcParams = TransportCallContext
100
+ export type ClientCallResponse = TransportCallResponse
package/src/types.ts CHANGED
@@ -3,24 +3,30 @@ import type { TAnyProcedureContract, TAnyRouterContract } from '@nmtjs/contract'
3
3
  import type { ProtocolBlobInterface } from '@nmtjs/protocol'
4
4
  import type {
5
5
  ProtocolError,
6
- ProtocolServerBlobStream,
6
+ ProtocolServerBlobConsumer,
7
7
  } from '@nmtjs/protocol/client'
8
8
  import type { BaseTypeAny, PlainType, t } from '@nmtjs/type'
9
9
 
10
10
  export const ResolvedType: unique symbol = Symbol('ResolvedType')
11
11
  export type ResolvedType = typeof ResolvedType
12
12
 
13
- export type ClientCallOptions = {
14
- timeout?: number
15
- signal?: AbortSignal
13
+ export type RpcCallOptions = { timeout?: number; signal?: AbortSignal }
14
+
15
+ export type StreamCallOptions = RpcCallOptions & { autoReconnect?: boolean }
16
+
17
+ export type ClientCallOptions = StreamCallOptions & {
16
18
  /**
17
19
  * @internal
18
20
  */
19
21
  _stream_response?: boolean
20
22
  }
21
23
 
24
+ export type BlobSubscriptionOptions = { signal?: AbortSignal }
25
+
26
+ export type StreamSubscriptionOptions = Partial<StreamCallOptions>
27
+
22
28
  export type ClientOutputType<T> = T extends ProtocolBlobInterface
23
- ? (options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
29
+ ? ProtocolServerBlobConsumer
24
30
  : T extends { [PlainType]?: true }
25
31
  ? { [K in keyof Omit<T, PlainType>]: ClientOutputType<T[K]> }
26
32
  : T
@@ -99,10 +105,29 @@ export type ClientCaller<
99
105
  SafeCall extends boolean,
100
106
  > = (
101
107
  ...args: Procedure['input'] extends t.NeverType
102
- ? [data?: undefined, options?: Partial<ClientCallOptions>]
108
+ ? [
109
+ data?: undefined,
110
+ options?: Partial<
111
+ Procedure['stream'] extends true ? StreamCallOptions : RpcCallOptions
112
+ >,
113
+ ]
103
114
  : undefined extends t.infer.encode.input<Procedure['contract']['input']>
104
- ? [data?: Procedure['input'], options?: Partial<ClientCallOptions>]
105
- : [data: Procedure['input'], options?: Partial<ClientCallOptions>]
115
+ ? [
116
+ data?: Procedure['input'],
117
+ options?: Partial<
118
+ Procedure['stream'] extends true
119
+ ? StreamCallOptions
120
+ : RpcCallOptions
121
+ >,
122
+ ]
123
+ : [
124
+ data: Procedure['input'],
125
+ options?: Partial<
126
+ Procedure['stream'] extends true
127
+ ? StreamCallOptions
128
+ : RpcCallOptions
129
+ >,
130
+ ]
106
131
  ) => SafeCall extends true
107
132
  ? Promise<OneOf<[{ result: Procedure['output'] }, { error: ProtocolError }]>>
108
133
  : Promise<Procedure['output']>