@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
@@ -1,16 +1,48 @@
1
1
  import type { TAnyRouterContract } from '@nmtjs/contract'
2
2
 
3
- import type { BaseClientOptions } from '../core.ts'
3
+ import type { BaseClientOptions } from '../client.ts'
4
+ import type { RpcLayerApi } from '../layers/rpc.ts'
4
5
  import type { ClientTransportFactory } from '../transport.ts'
5
6
  import type {
6
- ClientCallers,
7
7
  ClientCallOptions,
8
8
  StaticInputContractTypeProvider,
9
9
  StaticOutputContractTypeProvider,
10
10
  } from '../types.ts'
11
- import { BaseClient } from '../core.ts'
11
+ import { Client } from '../client.ts'
12
12
  import { BaseClientTransformer } from '../transformers.ts'
13
13
 
14
+ const buildStaticCallers = (
15
+ rpc: RpcLayerApi,
16
+ isStream: boolean,
17
+ path: string[] = [],
18
+ ): Record<string, unknown> => {
19
+ const createProxy = <T>(
20
+ target: Record<string, unknown>,
21
+ current: string[],
22
+ ) => {
23
+ return new Proxy(target, {
24
+ get: (obj, prop) => {
25
+ if (prop === 'then') return obj
26
+
27
+ const nextPath = [...current, String(prop)]
28
+ const caller = (
29
+ payload?: unknown,
30
+ options?: Partial<ClientCallOptions>,
31
+ ) => {
32
+ return rpc.call(nextPath.join('/'), payload, {
33
+ ...options,
34
+ _stream_response: isStream || options?._stream_response,
35
+ })
36
+ }
37
+
38
+ return createProxy(caller as any, nextPath)
39
+ },
40
+ }) as T
41
+ }
42
+
43
+ return createProxy(Object.create(null), path)
44
+ }
45
+
14
46
  export class StaticClient<
15
47
  Transport extends ClientTransportFactory<any, any> = ClientTransportFactory<
16
48
  any,
@@ -18,15 +50,13 @@ export class StaticClient<
18
50
  >,
19
51
  RouterContract extends TAnyRouterContract = TAnyRouterContract,
20
52
  SafeCall extends boolean = false,
21
- > extends BaseClient<
53
+ > extends Client<
22
54
  Transport,
23
55
  RouterContract,
24
56
  SafeCall,
25
57
  StaticInputContractTypeProvider,
26
58
  StaticOutputContractTypeProvider
27
59
  > {
28
- protected readonly transformer: BaseClientTransformer
29
-
30
60
  constructor(
31
61
  options: BaseClientOptions<RouterContract, SafeCall>,
32
62
  transport: Transport,
@@ -37,37 +67,15 @@ export class StaticClient<
37
67
  ? Options
38
68
  : never,
39
69
  ) {
40
- super(options, transport, transportOptions)
41
- this.transformer = new BaseClientTransformer()
42
- }
43
-
44
- override get call(): ClientCallers<this['_']['routes'], SafeCall, false> {
45
- return this.createProxy(Object.create(null), false)
46
- }
47
-
48
- override get stream(): ClientCallers<this['_']['routes'], SafeCall, true> {
49
- return this.createProxy(Object.create(null), true)
50
- }
51
-
52
- protected createProxy<T>(
53
- target: Record<string, unknown>,
54
- isStream: boolean,
55
- path: string[] = [],
56
- ) {
57
- return new Proxy(target, {
58
- get: (obj, prop) => {
59
- if (prop === 'then') return obj
60
- const newPath = [...path, String(prop)]
61
- const caller = (
62
- payload?: unknown,
63
- options?: Partial<ClientCallOptions>,
64
- ) =>
65
- this._call(newPath.join('/'), payload, {
66
- ...options,
67
- _stream_response: isStream || options?._stream_response,
68
- })
69
- return this.createProxy(caller as any, isStream, newPath)
70
- },
71
- }) as T
70
+ super(
71
+ options,
72
+ transport,
73
+ transportOptions,
74
+ new BaseClientTransformer(),
75
+ (rpc) => ({
76
+ call: buildStaticCallers(rpc, false),
77
+ stream: buildStaticCallers(rpc, true),
78
+ }),
79
+ )
72
80
  }
73
81
  }