@nmtjs/client 0.12.6 → 0.12.8

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
@@ -19,24 +19,23 @@
19
19
  }
20
20
  },
21
21
  "peerDependencies": {
22
- "@nmtjs/type": "0.12.6",
23
- "@nmtjs/contract": "0.12.6",
24
- "@nmtjs/common": "0.12.6",
25
- "@nmtjs/protocol": "0.12.6"
22
+ "@nmtjs/contract": "0.12.8",
23
+ "@nmtjs/common": "0.12.8",
24
+ "@nmtjs/protocol": "0.12.8",
25
+ "@nmtjs/type": "0.12.8"
26
26
  },
27
27
  "devDependencies": {
28
- "@nmtjs/type": "0.12.6",
29
- "@nmtjs/common": "0.12.6",
30
- "@nmtjs/contract": "0.12.6",
31
- "@nmtjs/protocol": "0.12.6"
28
+ "@nmtjs/type": "0.12.8",
29
+ "@nmtjs/contract": "0.12.8",
30
+ "@nmtjs/common": "0.12.8",
31
+ "@nmtjs/protocol": "0.12.8"
32
32
  },
33
33
  "files": [
34
- "src",
35
34
  "dist",
36
35
  "LICENSE.md",
37
36
  "README.md"
38
37
  ],
39
- "version": "0.12.6",
38
+ "version": "0.12.8",
40
39
  "scripts": {
41
40
  "build": "tsc",
42
41
  "type-check": "tsc --noEmit"
package/src/common.ts DELETED
@@ -1,105 +0,0 @@
1
- import type { TAnyAPIContract } from '@nmtjs/contract'
2
- import {
3
- EventEmitter,
4
- type ProtocolBaseClientCallOptions,
5
- type ProtocolBaseTransformer,
6
- ProtocolError,
7
- type ProtocolTransport,
8
- } from '@nmtjs/protocol/client'
9
- import type {
10
- ClientCallers,
11
- ResolveAPIContract,
12
- ResolveClientEvents,
13
- RuntimeInputContractTypeProvider,
14
- RuntimeOutputContractTypeProvider,
15
- } from './types.ts'
16
-
17
- export {
18
- ErrorCode,
19
- ProtocolBlob,
20
- type ProtocolBlobMetadata,
21
- TransportType,
22
- } from '@nmtjs/protocol'
23
- export * from './types.ts'
24
-
25
- export class ClientError extends ProtocolError {}
26
-
27
- export abstract class BaseClient<
28
- APIContract extends TAnyAPIContract = TAnyAPIContract,
29
- SafeCall extends boolean = false,
30
- API extends ResolveAPIContract<
31
- APIContract,
32
- RuntimeInputContractTypeProvider,
33
- RuntimeOutputContractTypeProvider
34
- > = ResolveAPIContract<
35
- APIContract,
36
- RuntimeInputContractTypeProvider,
37
- RuntimeOutputContractTypeProvider
38
- >,
39
- > extends EventEmitter<ResolveClientEvents<API>> {
40
- _!: {
41
- api: API
42
- safe: SafeCall
43
- }
44
-
45
- protected abstract transformer: ProtocolBaseTransformer
46
- protected callers!: ClientCallers<API, SafeCall>
47
- protected auth: any
48
-
49
- constructor(
50
- readonly transport: ProtocolTransport,
51
- readonly options: {
52
- timeout: number
53
- autoreconnect?: boolean
54
- safe?: SafeCall
55
- },
56
- ) {
57
- super()
58
-
59
- if (this.options.autoreconnect) {
60
- this.transport.on('disconnected', () =>
61
- setTimeout(this.connect.bind(this), 1000),
62
- )
63
- }
64
- }
65
-
66
- protected async _call(
67
- namespace: string,
68
- procedure: string,
69
- payload: any,
70
- options: ProtocolBaseClientCallOptions,
71
- ) {
72
- const call = await this.transport.call(
73
- namespace,
74
- procedure,
75
- payload,
76
- options,
77
- this.transformer,
78
- )
79
- if (this.options.safe) {
80
- return await call.promise
81
- .then((result) => ({ result }))
82
- .catch((error) => ({ error }))
83
- } else {
84
- return await call.promise.catch((error) => {
85
- throw error
86
- })
87
- }
88
- }
89
-
90
- get call() {
91
- return this.callers
92
- }
93
-
94
- setAuth(auth: any) {
95
- this.auth = auth
96
- }
97
-
98
- connect() {
99
- return this.transport.connect(this.auth, this.transformer)
100
- }
101
-
102
- disconnect() {
103
- return this.transport.disconnect()
104
- }
105
- }
package/src/runtime.ts DELETED
@@ -1,83 +0,0 @@
1
- import type { TAnyAPIContract } from '@nmtjs/contract'
2
- import { ErrorCode } from '@nmtjs/protocol'
3
- import { ProtocolBaseTransformer } from '@nmtjs/protocol/client'
4
- import { NeemataTypeError, t } from '@nmtjs/type'
5
- import { BaseClient, ClientError } from './common.ts'
6
-
7
- export class RuntimeContractTransformer extends ProtocolBaseTransformer {
8
- protected contract: TAnyAPIContract
9
-
10
- constructor(contract: TAnyAPIContract) {
11
- super()
12
-
13
- this.contract = contract
14
- }
15
-
16
- decodeEvent(namespace: string, event: string, payload: any) {
17
- const type = this.contract.namespaces[namespace].events[event].payload
18
- return type.decode(payload)
19
- }
20
-
21
- decodeRPC(namespace: string, procedure: string, payload: any) {
22
- const type =
23
- this.contract.namespaces[namespace].procedures[procedure].output
24
- if (type instanceof t.NeverType) return undefined
25
- return type.decode(payload)
26
- }
27
-
28
- decodeRPCChunk(namespace: string, procedure: string, payload: any) {
29
- const type =
30
- this.contract.namespaces[namespace].procedures[procedure].stream
31
- if (!type || type instanceof t.NeverType) return undefined
32
- return type.decode(payload)
33
- }
34
-
35
- encodeRPC(namespace: string, procedure: string, payload: any) {
36
- const type = this.contract.namespaces[namespace].procedures[procedure].input
37
- if (type instanceof t.NeverType) return undefined
38
- try {
39
- return type.encode(payload)
40
- } catch (error) {
41
- if (error instanceof NeemataTypeError) {
42
- throw new ClientError(
43
- ErrorCode.ValidationError,
44
- `Invalid payload for ${namespace}.${procedure}: ${error.message}`,
45
- error.issues,
46
- )
47
- }
48
- throw error
49
- }
50
- }
51
- }
52
-
53
- export class RuntimeClient<
54
- APIContract extends TAnyAPIContract,
55
- SafeCall extends boolean,
56
- > extends BaseClient<APIContract, SafeCall> {
57
- protected transformer: RuntimeContractTransformer
58
-
59
- constructor(
60
- public contract: APIContract,
61
- ...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>
62
- ) {
63
- super(...args)
64
-
65
- this.transformer = new RuntimeContractTransformer(this.contract)
66
- const callers: any = {}
67
- const namespaces = Object.entries(this.contract.namespaces)
68
- for (const [namespaceKey, namespace] of namespaces) {
69
- callers[namespaceKey] = {} as any
70
-
71
- const procedures = Object.entries(namespace.procedures)
72
-
73
- for (const [procedureKey, procedure] of procedures) {
74
- callers[namespaceKey][procedureKey] = (payload, options) =>
75
- this._call(namespace.name, procedure.name, payload, {
76
- timeout: procedure.timeout || namespace.timeout || options.timeout,
77
- ...options,
78
- })
79
- }
80
- }
81
- this.callers = callers
82
- }
83
- }
package/src/static.ts DELETED
@@ -1,40 +0,0 @@
1
- import type { TAnyAPIContract } from '@nmtjs/contract'
2
- import { ProtocolBaseTransformer } from '@nmtjs/protocol/client'
3
- import { BaseClient } from './common.ts'
4
-
5
- export class StaticClient<
6
- APIContract extends TAnyAPIContract,
7
- SafeCall extends boolean = false,
8
- > extends BaseClient<APIContract, SafeCall> {
9
- protected transformer: ProtocolBaseTransformer
10
-
11
- constructor(
12
- ...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>
13
- ) {
14
- super(...args)
15
-
16
- this.transformer = new ProtocolBaseTransformer()
17
-
18
- this.callers = new Proxy(Object(), {
19
- get: (target, namespace) => {
20
- // `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`
21
- // without explicitly calling a function implicitly calls .then() on target
22
- // FIXME: this basically makes "then" a reserved word
23
- if (namespace === 'then') return target
24
- return new Proxy(Object(), {
25
- get: (target, procedure) => {
26
- // `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`
27
- // without explicitly calling a function implicitly calls .then() on target
28
- // FIXME: this basically makes "then" a reserved word
29
- if (procedure === 'then') return target
30
- return (payload, options) =>
31
- this._call(namespace as string, procedure as string, payload, {
32
- ...options,
33
- timeout: options?.timeout ?? this.options.timeout,
34
- })
35
- },
36
- })
37
- },
38
- })
39
- }
40
- }
package/src/types.ts DELETED
@@ -1,151 +0,0 @@
1
- import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'
2
- import type { TAnyAPIContract, TAnyProcedureContract } from '@nmtjs/contract'
3
- import type {
4
- InputType,
5
- OutputType,
6
- ProtocolBaseClientCallOptions,
7
- ProtocolError,
8
- ProtocolServerStreamInterface,
9
- } from '@nmtjs/protocol/client'
10
- import type { BaseTypeAny, t } from '@nmtjs/type'
11
-
12
- export interface StaticInputContractTypeProvider extends TypeProvider {
13
- output: this['input'] extends BaseTypeAny
14
- ? t.infer.encoded.input<this['input']>
15
- : never
16
- }
17
-
18
- export interface RuntimeInputContractTypeProvider extends TypeProvider {
19
- output: this['input'] extends BaseTypeAny
20
- ? t.infer.decoded.input<this['input']>
21
- : never
22
- }
23
-
24
- export interface StaticOutputContractTypeProvider extends TypeProvider {
25
- output: this['input'] extends BaseTypeAny
26
- ? t.infer.encoded.output<this['input']>
27
- : never
28
- }
29
-
30
- export interface RuntimeOutputContractTypeProvider extends TypeProvider {
31
- output: this['input'] extends BaseTypeAny
32
- ? t.infer.decoded.output<this['input']>
33
- : never
34
- }
35
-
36
- export type AnyResolvedAPIContract = Record<
37
- string,
38
- {
39
- procedures: Record<
40
- string,
41
- {
42
- contract: TAnyProcedureContract
43
- input: any
44
- output: any
45
- }
46
- >
47
- events: Record<
48
- string,
49
- {
50
- payload: any
51
- }
52
- >
53
- }
54
- >
55
-
56
- export type ResolveAPIContract<
57
- C extends TAnyAPIContract = TAnyAPIContract,
58
- InputTypeProvider extends TypeProvider = TypeProvider,
59
- OutputTypeProvider extends TypeProvider = TypeProvider,
60
- > = {
61
- [N in keyof C['namespaces']]: {
62
- procedures: {
63
- [P in keyof C['namespaces'][N]['procedures']]: {
64
- contract: C['namespaces'][N]['procedures'][P]
65
- input: InputType<
66
- CallTypeProvider<
67
- InputTypeProvider,
68
- C['namespaces'][N]['procedures'][P]['input']
69
- >
70
- >
71
- output: C['namespaces'][N]['procedures'][P]['stream'] extends
72
- | undefined
73
- | t.NeverType
74
- ? OutputType<
75
- CallTypeProvider<
76
- OutputTypeProvider,
77
- C['namespaces'][N]['procedures'][P]['output']
78
- >
79
- >
80
- : {
81
- result: OutputType<
82
- CallTypeProvider<
83
- OutputTypeProvider,
84
- C['namespaces'][N]['procedures'][P]['output']
85
- >
86
- >
87
- stream: ProtocolServerStreamInterface<
88
- CallTypeProvider<
89
- OutputTypeProvider,
90
- C['namespaces'][N]['procedures'][P]['stream']
91
- >
92
- >
93
- }
94
- }
95
- }
96
- events: {
97
- [KE in keyof C['namespaces'][N]['events']]: {
98
- payload: OutputType<
99
- CallTypeProvider<
100
- OutputTypeProvider,
101
- C['namespaces'][N]['events'][KE]['payload']
102
- >
103
- >
104
- }
105
- }
106
- }
107
- }
108
-
109
- export type ResolveClientEvents<
110
- C extends AnyResolvedAPIContract = AnyResolvedAPIContract,
111
- > = {
112
- [N in keyof C]: {
113
- [E in keyof C[N]['events'] as `${Extract<N, string>}/${Extract<E, string>}`]: [
114
- C[N]['events'][E]['payload'],
115
- ]
116
- }
117
- }[keyof C]
118
-
119
- export type ClientCallers<
120
- Resolved extends AnyResolvedAPIContract,
121
- SafeCall extends boolean,
122
- > = {
123
- [N in keyof Resolved]: {
124
- [P in keyof Resolved[N]['procedures']]: (
125
- ...args: Resolved[N]['procedures'][P]['input'] extends t.NeverType
126
- ? [data?: undefined, options?: Partial<ProtocolBaseClientCallOptions>]
127
- : undefined extends t.infer.encoded.input<
128
- Resolved[N]['procedures'][P]['contract']['input']
129
- >
130
- ? [
131
- data?: Resolved[N]['procedures'][P]['input'],
132
- options?: Partial<ProtocolBaseClientCallOptions>,
133
- ]
134
- : [
135
- data: Resolved[N]['procedures'][P]['input'],
136
- options?: Partial<ProtocolBaseClientCallOptions>,
137
- ]
138
- ) => SafeCall extends true
139
- ? Promise<
140
- OneOf<
141
- [
142
- {
143
- output: Resolved[N]['procedures'][P]['output']
144
- },
145
- { error: ProtocolError },
146
- ]
147
- >
148
- >
149
- : Promise<Resolved[N]['procedures'][P]['output']>
150
- }
151
- }