@nmtjs/client 0.15.3 → 0.16.0-beta.10

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 (60) hide show
  1. package/dist/client.d.ts +68 -0
  2. package/dist/client.js +101 -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 +36 -83
  11. package/dist/core.js +315 -690
  12. package/dist/core.js.map +1 -1
  13. package/dist/events.d.ts +1 -2
  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 +557 -0
  24. package/dist/layers/rpc.js.map +1 -0
  25. package/dist/layers/streams.d.ts +25 -0
  26. package/dist/layers/streams.js +207 -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/logging.js.map +1 -1
  36. package/dist/plugins/reconnect.js +11 -94
  37. package/dist/plugins/reconnect.js.map +1 -1
  38. package/dist/plugins/types.d.ts +27 -11
  39. package/dist/streams.js.map +1 -1
  40. package/dist/transformers.js.map +1 -1
  41. package/dist/transport.d.ts +49 -31
  42. package/dist/types.d.ts +23 -13
  43. package/dist/types.js.map +1 -1
  44. package/package.json +9 -10
  45. package/src/client.ts +232 -0
  46. package/src/clients/runtime.ts +93 -79
  47. package/src/clients/static.ts +46 -38
  48. package/src/core.ts +408 -901
  49. package/src/events.ts +113 -14
  50. package/src/index.ts +4 -0
  51. package/src/layers/ping.ts +99 -0
  52. package/src/layers/rpc.ts +767 -0
  53. package/src/layers/streams.ts +320 -0
  54. package/src/plugins/browser.ts +39 -9
  55. package/src/plugins/heartbeat.ts +10 -10
  56. package/src/plugins/index.ts +8 -1
  57. package/src/plugins/reconnect.ts +12 -119
  58. package/src/plugins/types.ts +30 -13
  59. package/src/transport.ts +75 -46
  60. package/src/types.ts +34 -19
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
@@ -1,29 +1,25 @@
1
1
  import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'
2
2
  import type { TAnyProcedureContract, TAnyRouterContract } from '@nmtjs/contract'
3
- import type { ProtocolBlobInterface } from '@nmtjs/protocol'
4
- import type {
5
- ProtocolError,
6
- ProtocolServerBlobStream,
7
- } from '@nmtjs/protocol/client'
8
- import type { BaseTypeAny, PlainType, t } from '@nmtjs/type'
3
+ import type { ProtocolError } from '@nmtjs/protocol/client'
4
+ import type { BaseTypeAny, t } from '@nmtjs/type'
9
5
 
10
6
  export const ResolvedType: unique symbol = Symbol('ResolvedType')
11
7
  export type ResolvedType = typeof ResolvedType
12
8
 
13
- export type ClientCallOptions = {
14
- timeout?: number
15
- signal?: AbortSignal
9
+ export type RpcCallOptions = { timeout?: number; signal?: AbortSignal }
10
+
11
+ export type StreamCallOptions = RpcCallOptions & { autoReconnect?: boolean }
12
+
13
+ export type ClientCallOptions = StreamCallOptions & {
16
14
  /**
17
15
  * @internal
18
16
  */
19
17
  _stream_response?: boolean
20
18
  }
21
19
 
22
- export type ClientOutputType<T> = T extends ProtocolBlobInterface
23
- ? (options?: { signal?: AbortSignal }) => ProtocolServerBlobStream
24
- : T extends { [PlainType]?: true }
25
- ? { [K in keyof Omit<T, PlainType>]: ClientOutputType<T[K]> }
26
- : T
20
+ export type BlobSubscriptionOptions = { signal?: AbortSignal }
21
+
22
+ export type StreamSubscriptionOptions = Partial<StreamCallOptions>
27
23
 
28
24
  export interface StaticInputContractTypeProvider extends TypeProvider {
29
25
  output: this['input'] extends BaseTypeAny
@@ -39,13 +35,13 @@ export interface RuntimeInputContractTypeProvider extends TypeProvider {
39
35
 
40
36
  export interface StaticOutputContractTypeProvider extends TypeProvider {
41
37
  output: this['input'] extends BaseTypeAny
42
- ? ClientOutputType<t.infer.encodeRaw.output<this['input']>>
38
+ ? t.infer.encode.output<this['input']>
43
39
  : never
44
40
  }
45
41
 
46
42
  export interface RuntimeOutputContractTypeProvider extends TypeProvider {
47
43
  output: this['input'] extends BaseTypeAny
48
- ? ClientOutputType<t.infer.decodeRaw.output<this['input']>>
44
+ ? t.infer.decode.output<this['input']>
49
45
  : never
50
46
  }
51
47
 
@@ -99,10 +95,29 @@ export type ClientCaller<
99
95
  SafeCall extends boolean,
100
96
  > = (
101
97
  ...args: Procedure['input'] extends t.NeverType
102
- ? [data?: undefined, options?: Partial<ClientCallOptions>]
98
+ ? [
99
+ data?: undefined,
100
+ options?: Partial<
101
+ Procedure['stream'] extends true ? StreamCallOptions : RpcCallOptions
102
+ >,
103
+ ]
103
104
  : undefined extends t.infer.encode.input<Procedure['contract']['input']>
104
- ? [data?: Procedure['input'], options?: Partial<ClientCallOptions>]
105
- : [data: Procedure['input'], options?: Partial<ClientCallOptions>]
105
+ ? [
106
+ data?: Procedure['input'],
107
+ options?: Partial<
108
+ Procedure['stream'] extends true
109
+ ? StreamCallOptions
110
+ : RpcCallOptions
111
+ >,
112
+ ]
113
+ : [
114
+ data: Procedure['input'],
115
+ options?: Partial<
116
+ Procedure['stream'] extends true
117
+ ? StreamCallOptions
118
+ : RpcCallOptions
119
+ >,
120
+ ]
106
121
  ) => SafeCall extends true
107
122
  ? Promise<OneOf<[{ result: Procedure['output'] }, { error: ProtocolError }]>>
108
123
  : Promise<Procedure['output']>