@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.
- package/dist/client.d.ts +68 -0
- package/dist/client.js +101 -0
- package/dist/client.js.map +1 -0
- package/dist/clients/runtime.d.ts +6 -12
- package/dist/clients/runtime.js +58 -57
- package/dist/clients/runtime.js.map +1 -1
- package/dist/clients/static.d.ts +4 -9
- package/dist/clients/static.js +20 -20
- package/dist/clients/static.js.map +1 -1
- package/dist/core.d.ts +36 -83
- package/dist/core.js +315 -690
- package/dist/core.js.map +1 -1
- package/dist/events.d.ts +1 -2
- package/dist/events.js +74 -11
- package/dist/events.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/layers/ping.d.ts +6 -0
- package/dist/layers/ping.js +65 -0
- package/dist/layers/ping.js.map +1 -0
- package/dist/layers/rpc.d.ts +19 -0
- package/dist/layers/rpc.js +557 -0
- package/dist/layers/rpc.js.map +1 -0
- package/dist/layers/streams.d.ts +25 -0
- package/dist/layers/streams.js +207 -0
- package/dist/layers/streams.js.map +1 -0
- package/dist/plugins/browser.js +28 -9
- package/dist/plugins/browser.js.map +1 -1
- package/dist/plugins/heartbeat.js +10 -10
- package/dist/plugins/heartbeat.js.map +1 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +0 -1
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/logging.js.map +1 -1
- package/dist/plugins/reconnect.js +11 -94
- package/dist/plugins/reconnect.js.map +1 -1
- package/dist/plugins/types.d.ts +27 -11
- package/dist/streams.js.map +1 -1
- package/dist/transformers.js.map +1 -1
- package/dist/transport.d.ts +49 -31
- package/dist/types.d.ts +23 -13
- package/dist/types.js.map +1 -1
- package/package.json +9 -10
- package/src/client.ts +232 -0
- package/src/clients/runtime.ts +93 -79
- package/src/clients/static.ts +46 -38
- package/src/core.ts +408 -901
- package/src/events.ts +113 -14
- package/src/index.ts +4 -0
- package/src/layers/ping.ts +99 -0
- package/src/layers/rpc.ts +767 -0
- package/src/layers/streams.ts +320 -0
- package/src/plugins/browser.ts +39 -9
- package/src/plugins/heartbeat.ts +10 -10
- package/src/plugins/index.ts +8 -1
- package/src/plugins/reconnect.ts +12 -119
- package/src/plugins/types.ts +30 -13
- package/src/transport.ts +75 -46
- package/src/types.ts +34 -19
package/src/clients/static.ts
CHANGED
|
@@ -1,16 +1,48 @@
|
|
|
1
1
|
import type { TAnyRouterContract } from '@nmtjs/contract'
|
|
2
2
|
|
|
3
|
-
import type { BaseClientOptions } from '../
|
|
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 {
|
|
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
|
|
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(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
}
|