@nmtjs/client 0.6.5 → 0.7.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.
- package/dist/common.js +3 -4
- package/dist/common.js.map +1 -1
- package/dist/runtime.js +57 -90
- package/dist/runtime.js.map +1 -1
- package/dist/static.js +16 -20
- package/dist/static.js.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +13 -17
- package/{lib → src}/runtime.ts +24 -61
- package/{lib → src}/static.ts +9 -3
- package/{lib → src}/types.ts +30 -8
- /package/{lib → src}/common.ts +0 -0
package/dist/common.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { ProtocolError } from
|
|
1
|
+
import { ProtocolError } from "@nmtjs/protocol/client";
|
|
2
2
|
export * from "./types.js";
|
|
3
|
-
export class ClientError extends ProtocolError {
|
|
4
|
-
}
|
|
5
|
-
export { ErrorCode, ProtocolBlob, TransportType } from '@nmtjs/protocol/common';
|
|
3
|
+
export class ClientError extends ProtocolError {}
|
|
4
|
+
export { ErrorCode, ProtocolBlob, TransportType } from "@nmtjs/protocol/common";
|
package/dist/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAAA,SAAS,qBAAqB,wBAAwB;AAEtD,cAAc;AAEd,OAAO,MAAM,oBAAoB,cAAc,CAAE;AAEjD,SACE,WACA,cAEA,qBACK","names":[],"sources":["src/common.ts"],"sourcesContent":["import { ProtocolError } from '@nmtjs/protocol/client'\n\nexport * from './types.ts'\n\nexport class ClientError extends ProtocolError {}\n\nexport {\n ErrorCode,\n ProtocolBlob,\n type ProtocolBlobMetadata,\n TransportType,\n} from '@nmtjs/protocol/common'\n"],"version":3}
|
package/dist/runtime.js
CHANGED
|
@@ -1,95 +1,62 @@
|
|
|
1
|
-
import { ProtocolBaseClient, ProtocolBaseTransformer } from
|
|
2
|
-
import { ErrorCode } from
|
|
3
|
-
import { NeverType } from
|
|
4
|
-
import { compile } from '@nmtjs/type/compiler';
|
|
1
|
+
import { ProtocolBaseClient, ProtocolBaseTransformer } from "@nmtjs/protocol/client";
|
|
2
|
+
import { ErrorCode } from "@nmtjs/protocol/common";
|
|
3
|
+
import { NeemataTypeError, NeverType } from "@nmtjs/type";
|
|
5
4
|
import { ClientError } from "./common.js";
|
|
6
5
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return compiled.decode(payload);
|
|
39
|
-
}
|
|
40
|
-
decodeRPC(namespace, procedure, payload) {
|
|
41
|
-
const type = this.#contract.namespaces[namespace].procedures[procedure].output;
|
|
42
|
-
if (type instanceof NeverType) return undefined;
|
|
43
|
-
const compiled = this.#compiled.get(type);
|
|
44
|
-
return compiled.decode(payload);
|
|
45
|
-
}
|
|
46
|
-
decodeRPCChunk(namespace, procedure, payload) {
|
|
47
|
-
const type = this.#contract.namespaces[namespace].procedures[procedure].stream;
|
|
48
|
-
if (type instanceof NeverType) return undefined;
|
|
49
|
-
const compiled = this.#compiled.get(type);
|
|
50
|
-
return compiled.decode(payload);
|
|
51
|
-
}
|
|
52
|
-
encodeRPC(namespace, procedure, payload) {
|
|
53
|
-
const type = this.#contract.namespaces[namespace].procedures[procedure].input;
|
|
54
|
-
if (type instanceof NeverType) return undefined;
|
|
55
|
-
const compiled = this.#compiled.get(type);
|
|
56
|
-
if (!compiled.check(payload)) {
|
|
57
|
-
const errors = compiled.errors(payload);
|
|
58
|
-
throw new ClientError(ErrorCode.ValidationError, 'Invalid RPC payload', errors);
|
|
59
|
-
}
|
|
60
|
-
return compiled.encode(payload);
|
|
61
|
-
}
|
|
62
|
-
#registerType(type) {
|
|
63
|
-
this.#types.add(type);
|
|
64
|
-
}
|
|
65
|
-
#compile() {
|
|
66
|
-
for (const type of this.#types){
|
|
67
|
-
this.#compiled.set(type, compile(type));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
6
|
+
#contract;
|
|
7
|
+
constructor(contract) {
|
|
8
|
+
super();
|
|
9
|
+
this.#contract = contract;
|
|
10
|
+
}
|
|
11
|
+
decodeEvent(namespace, event, payload) {
|
|
12
|
+
const type = this.#contract.namespaces[namespace].events[event].payload;
|
|
13
|
+
return type.decode(payload);
|
|
14
|
+
}
|
|
15
|
+
decodeRPC(namespace, procedure, payload) {
|
|
16
|
+
const type = this.#contract.namespaces[namespace].procedures[procedure].output;
|
|
17
|
+
if (type instanceof NeverType) return undefined;
|
|
18
|
+
return type.decode(payload);
|
|
19
|
+
}
|
|
20
|
+
decodeRPCChunk(namespace, procedure, payload) {
|
|
21
|
+
const type = this.#contract.namespaces[namespace].procedures[procedure].stream;
|
|
22
|
+
if (type instanceof NeverType) return undefined;
|
|
23
|
+
return type.decode(payload);
|
|
24
|
+
}
|
|
25
|
+
encodeRPC(namespace, procedure, payload) {
|
|
26
|
+
const type = this.#contract.namespaces[namespace].procedures[procedure].input;
|
|
27
|
+
if (type instanceof NeverType) return undefined;
|
|
28
|
+
try {
|
|
29
|
+
return type.encode(payload);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error instanceof NeemataTypeError) {
|
|
32
|
+
throw new ClientError(ErrorCode.ValidationError, `Invalid payload for ${namespace}.${procedure}: ${error.message}`, error.issues);
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
70
37
|
}
|
|
71
38
|
export class RuntimeClient extends ProtocolBaseClient {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
39
|
+
_;
|
|
40
|
+
#callers = {};
|
|
41
|
+
constructor(contract, options) {
|
|
42
|
+
super({
|
|
43
|
+
...options,
|
|
44
|
+
transformer: new RuntimeContractTransformer(contract)
|
|
45
|
+
});
|
|
46
|
+
const callers = {};
|
|
47
|
+
for (const [namespaceKey, namespace] of Object.entries(contract.namespaces)) {
|
|
48
|
+
namespace.procedures;
|
|
49
|
+
callers[namespaceKey] = {};
|
|
50
|
+
for (const [procedureKey, procedure] of Object.entries(namespace.procedures)) {
|
|
51
|
+
callers[namespaceKey][procedureKey] = (payload, options) => this._call(namespace.name, procedure.name, payload, {
|
|
52
|
+
timeout: namespace.timeout,
|
|
53
|
+
...options
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.#callers = callers;
|
|
58
|
+
}
|
|
59
|
+
get call() {
|
|
60
|
+
return this.#callers;
|
|
61
|
+
}
|
|
95
62
|
}
|
package/dist/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AAEA,SACE,oBACA,+BAEK,wBAAwB;AAC/B,SAAS,iBAAiB,wBAAwB;AAClD,SAA2B,kBAAkB,iBAAiB,aAAa;AAC3E,SAAS,mBAAmB,aAAa;AASzC,OAAO,MAAM,mCAAmC,wBAAwB;CACtE;CAEA,YAAYA,UAA2B;AACrC,SAAO;AAEP,OAAKC,YAAY;CAClB;CAED,YAAYC,WAAmBC,OAAeC,SAAc;EAC1D,MAAM,OAAO,KAAKH,UAAU,WAAW,WAAW,OAAO,OAAO;AAChE,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUC,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OACJ,KAAKH,UAAU,WAAW,WAAW,WAAW,WAAW;AAC7D,MAAI,gBAAgB,UAAW,QAAO;AACtC,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,eAAeC,WAAmBG,WAAmBD,SAAc;EACjE,MAAM,OACJ,KAAKH,UAAU,WAAW,WAAW,WAAW,WAAW;AAC7D,MAAI,gBAAgB,UAAW,QAAO;AACtC,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUC,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OACJ,KAAKH,UAAU,WAAW,WAAW,WAAW,WAAW;AAC7D,MAAI,gBAAgB,UAAW,QAAO;AACtC,MAAI;AACF,UAAO,KAAK,OAAO,QAAQ;EAC5B,SAAQ,OAAO;AACd,OAAI,iBAAiB,kBAAkB;AACrC,UAAM,IAAI,YACR,UAAU,kBACT,sBAAsB,UAAU,GAAG,UAAU,IAAI,MAAM,QAAQ,GAChE,MAAM;GAET;AACD,SAAM;EACP;CACF;AACF;AAED,OAAO,MAAM,sBAWH,mBAA6D;CACrE;CACA,WAAW,CAAE;CAEb,YACEK,UACAC,SAKA;AACA,QAAM;GACJ,GAAG;GACH,aAAa,IAAI,2BAA2B;EAC7C,EAAC;EAEF,MAAM,UAAU,CAAE;AAElB,OAAK,MAAM,CAAC,cAAc,UAAU,IAAI,OAAO,QAC7C,SAAS,WACV,EAAE;AACD,aAAU;AAEV,WAAQ,gBAAgB,CAAE;AAE1B,QAAK,MAAM,CAAC,cAAc,UAAU,IAAI,OAAO,QAC7C,UAAU,WACX,EAAE;AACD,YAAQ,cAAc,gBAAgB,CAAC,SAAS,YAC9C,KAAK,MAAM,UAAU,MAAM,UAAU,MAAM,SAAS;KAClD,SAAS,UAAU;KACnB,GAAG;IACJ,EAAC;GACL;EACF;AACD,OAAKC,WAAW;CACjB;CAED,IAAI,OAAO;AACT,SAAO,KAAKA;CACb;AACF","names":["contract: TAnyAPIContract","#contract","namespace: string","event: string","payload: any","procedure: string","contract: APIContract","options: {\n transport: ProtocolTransport\n format: BaseClientFormat\n timeout?: number\n }","#callers"],"sources":["src/runtime.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport type { BaseClientFormat } from '@nmtjs/protocol/client'\nimport {\n ProtocolBaseClient,\n ProtocolBaseTransformer,\n type ProtocolTransport,\n} from '@nmtjs/protocol/client'\nimport { ErrorCode } from '@nmtjs/protocol/common'\nimport { type BaseTypeAny, NeemataTypeError, NeverType } from '@nmtjs/type'\nimport { ClientError } from './common.ts'\nimport type {\n ClientCallers,\n ResolveAPIContract,\n ResolveClientEvents,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider,\n} from './types.ts'\n\nexport class RuntimeContractTransformer extends ProtocolBaseTransformer {\n #contract: TAnyAPIContract\n\n constructor(contract: TAnyAPIContract) {\n super()\n\n this.#contract = contract\n }\n\n decodeEvent(namespace: string, event: string, payload: any) {\n const type = this.#contract.namespaces[namespace].events[event].payload\n return type.decode(payload)\n }\n\n decodeRPC(namespace: string, procedure: string, payload: any) {\n const type =\n this.#contract.namespaces[namespace].procedures[procedure].output\n if (type instanceof NeverType) return undefined\n return type.decode(payload)\n }\n\n decodeRPCChunk(namespace: string, procedure: string, payload: any) {\n const type =\n this.#contract.namespaces[namespace].procedures[procedure].stream\n if (type instanceof NeverType) return undefined\n return type.decode(payload)\n }\n\n encodeRPC(namespace: string, procedure: string, payload: any) {\n const type =\n this.#contract.namespaces[namespace].procedures[procedure].input\n if (type instanceof NeverType) return undefined\n try {\n return type.encode(payload)\n } catch (error) {\n if (error instanceof NeemataTypeError) {\n throw new ClientError(\n ErrorCode.ValidationError,\n `Invalid payload for ${namespace}.${procedure}: ${error.message}`,\n error.issues,\n )\n }\n throw error\n }\n }\n}\n\nexport class RuntimeClient<\n APIContract extends TAnyAPIContract,\n ResolvedAPIContract extends ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n > = ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n >,\n> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {\n _!: ResolvedAPIContract\n #callers = {} as ClientCallers<ResolvedAPIContract>\n\n constructor(\n contract: APIContract,\n options: {\n transport: ProtocolTransport\n format: BaseClientFormat\n timeout?: number\n },\n ) {\n super({\n ...options,\n transformer: new RuntimeContractTransformer(contract),\n })\n\n const callers = {} as any\n\n for (const [namespaceKey, namespace] of Object.entries(\n contract.namespaces,\n )) {\n namespace.procedures\n\n callers[namespaceKey] = {} as any\n\n for (const [procedureKey, procedure] of Object.entries(\n namespace.procedures,\n )) {\n callers[namespaceKey][procedureKey] = (payload, options) =>\n this._call(namespace.name, procedure.name, payload, {\n timeout: namespace.timeout,\n ...options,\n })\n }\n }\n this.#callers = callers\n }\n\n get call() {\n return this.#callers\n }\n}\n"],"version":3}
|
package/dist/static.js
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
import { ProtocolBaseClient } from
|
|
1
|
+
import { ProtocolBaseClient } from "@nmtjs/protocol/client";
|
|
2
2
|
export class StaticClient extends ProtocolBaseClient {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
get call() {
|
|
20
|
-
return this.#callers;
|
|
21
|
-
}
|
|
3
|
+
_;
|
|
4
|
+
#callers;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super(options);
|
|
7
|
+
this.#callers = new Proxy(Object(), { get: (target, namespace) => {
|
|
8
|
+
if (namespace === "then") return target;
|
|
9
|
+
return new Proxy(Object(), { get: (target, procedure) => {
|
|
10
|
+
if (procedure === "then") return target;
|
|
11
|
+
return (payload, options) => this._call(namespace, procedure, payload, options);
|
|
12
|
+
} });
|
|
13
|
+
} });
|
|
14
|
+
}
|
|
15
|
+
get call() {
|
|
16
|
+
return this.#callers;
|
|
17
|
+
}
|
|
22
18
|
}
|
package/dist/static.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"AACA,SAEE,0BAEK,wBAAwB;AAU/B,OAAO,MAAM,qBAWH,mBAA6D;CACrE;CAEA;CAEA,YAAYA,SAIT;AACD,QAAM,QAAQ;AAEd,OAAKC,WAAW,IAAI,MAAM,QAAQ,EAAE,EAClC,KAAK,CAAC,QAAQ,cAAc;AAI1B,OAAI,cAAc,OAAQ,QAAO;AACjC,UAAO,IAAI,MAAM,QAAQ,EAAE,EACzB,KAAK,CAAC,QAAQ,cAAc;AAI1B,QAAI,cAAc,OAAQ,QAAO;AACjC,WAAO,CAAC,SAAS,YACf,KAAK,MACH,WACA,WACA,SACA,QACD;GACJ,EACF;EACF,EACF;CACF;CAED,IAAI,OAAO;AACT,SAAO,KAAKA;CACb;AACF","names":["options: {\n transport: ProtocolTransport\n format: BaseClientFormat\n timeout?: number\n }","#callers"],"sources":["src/static.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport {\n type BaseClientFormat,\n ProtocolBaseClient,\n type ProtocolTransport,\n} from '@nmtjs/protocol/client'\n\nimport type {\n ClientCallers,\n ResolveAPIContract,\n ResolveClientEvents,\n StaticInputContractTypeProvider,\n StaticOutputContractTypeProvider,\n} from './types.ts'\n\nexport class StaticClient<\n APIContract extends TAnyAPIContract,\n ResolvedAPIContract extends ResolveAPIContract<\n APIContract,\n StaticInputContractTypeProvider,\n StaticOutputContractTypeProvider\n > = ResolveAPIContract<\n APIContract,\n StaticInputContractTypeProvider,\n StaticOutputContractTypeProvider\n >,\n> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {\n _!: ResolvedAPIContract\n\n #callers: ClientCallers<ResolvedAPIContract>\n\n constructor(options: {\n transport: ProtocolTransport\n format: BaseClientFormat\n timeout?: number\n }) {\n super(options)\n\n this.#callers = new Proxy(Object(), {\n get: (target, namespace) => {\n // `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`\n // without explicitly calling a function implicitly calls .then() on target\n // FIXME: this basically makes \"then\" a reserved word\n if (namespace === 'then') return target\n return new Proxy(Object(), {\n get: (target, procedure) => {\n // `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`\n // without explicitly calling a function implicitly calls .then() on target\n // FIXME: this basically makes \"then\" a reserved word\n if (procedure === 'then') return target\n return (payload, options) =>\n this._call(\n namespace as string,\n procedure as string,\n payload,\n options,\n )\n },\n })\n },\n })\n }\n\n get call() {\n return this.#callers\n }\n}\n"],"version":3}
|
package/dist/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {};
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":"","names":[],"sources":["src/types.ts"],"sourcesContent":["import type { CallTypeProvider, TypeProvider } from '@nmtjs/common'\nimport type { TAnyAPIContract } from '@nmtjs/contract'\nimport type {\n ProtocolBaseClientCallOptions,\n ProtocolServerStreamInterface,\n} from '@nmtjs/protocol/client'\nimport type { InputType, OutputType } from '@nmtjs/protocol/common'\nimport type { BaseTypeAny, NeverType, t } from '@nmtjs/type'\n\nexport interface StaticInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.input.encoded<this['input']>\n : never\n}\n\nexport interface RuntimeInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.input.decoded<this['input']>\n : never\n}\n\nexport interface StaticOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded<this['input']>\n : never\n}\n\nexport interface RuntimeOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded<this['input']>\n : never\n}\n\nexport type ResolveAPIContract<\n C extends TAnyAPIContract = TAnyAPIContract,\n InputTypeProvider extends TypeProvider = TypeProvider,\n OutputTypeProvider extends TypeProvider = TypeProvider,\n> = {\n [N in keyof C['namespaces'] as C['namespaces'][N]['name']]: {\n procedures: {\n [P in keyof C['namespaces'][N]['procedures'] as C['namespaces'][N]['procedures'][P]['name']]: {\n contract: C['namespaces'][N]['procedures'][P]\n input: InputType<\n CallTypeProvider<\n InputTypeProvider,\n C['namespaces'][N]['procedures'][P]['input']\n >\n >\n output: C['namespaces'][N]['procedures'][P]['stream'] extends NeverType\n ? OutputType<\n CallTypeProvider<\n OutputTypeProvider,\n C['namespaces'][N]['procedures'][P]['output']\n >\n >\n : {\n response: OutputType<\n CallTypeProvider<\n OutputTypeProvider,\n C['namespaces'][N]['procedures'][P]['output']\n >\n >\n stream: ProtocolServerStreamInterface<\n CallTypeProvider<\n OutputTypeProvider,\n C['namespaces'][N]['procedures'][P]['stream']\n >\n >\n }\n }\n }\n events: {\n [KE in keyof C['namespaces'][N]['events'] as C['namespaces'][N]['events'][KE]['name']]: {\n payload: OutputType<\n CallTypeProvider<\n OutputTypeProvider,\n C['namespaces'][N]['events'][KE]['payload']\n >\n >\n }\n }\n }\n}\n\nexport type ResolveClientEvents<\n C extends ResolveAPIContract = ResolveAPIContract,\n> = {\n [N in keyof C]: {\n [KE in keyof C[N]['events']]: C[N]['events'][KE]['payload']\n }\n}\n\nexport type ClientCallers<Resolved extends ResolveAPIContract> = {\n [N in keyof Resolved]: {\n [P in keyof Resolved[N]['procedures']]: (\n ...args: Resolved[N]['procedures'][P]['input'] extends NeverType\n ? [options?: ProtocolBaseClientCallOptions]\n : t.infer.input.encoded<\n Resolved[N]['procedures'][P]['contract']['input']\n > extends undefined\n ? [\n data?: Resolved[N]['procedures'][P]['input'],\n options?: ProtocolBaseClientCallOptions,\n ]\n : [\n data: Resolved[N]['procedures'][P]['input'],\n options?: ProtocolBaseClientCallOptions,\n ]\n ) => Promise<Resolved[N]['procedures'][P]['output']>\n }\n}\n"],"version":3}
|
package/package.json
CHANGED
|
@@ -3,37 +3,33 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"types": "./lib/common.ts"
|
|
6
|
+
"import": "./dist/common.js",
|
|
7
|
+
"types": "./src/common.ts"
|
|
9
8
|
},
|
|
10
9
|
"./runtime": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"types": "./lib/runtime.ts"
|
|
10
|
+
"import": "./dist/runtime.js",
|
|
11
|
+
"types": "./src/runtime.ts"
|
|
14
12
|
},
|
|
15
13
|
"./static": {
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"types": "./lib/static.ts"
|
|
14
|
+
"import": "./dist/static.js",
|
|
15
|
+
"types": "./src/static.ts"
|
|
19
16
|
}
|
|
20
17
|
},
|
|
21
18
|
"dependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/
|
|
24
|
-
"@nmtjs/
|
|
25
|
-
"@nmtjs/
|
|
19
|
+
"@nmtjs/type": "0.7.1",
|
|
20
|
+
"@nmtjs/contract": "0.7.1",
|
|
21
|
+
"@nmtjs/common": "0.7.1",
|
|
22
|
+
"@nmtjs/protocol": "0.7.1"
|
|
26
23
|
},
|
|
27
24
|
"files": [
|
|
28
|
-
"
|
|
29
|
-
"lib",
|
|
25
|
+
"src",
|
|
30
26
|
"dist",
|
|
31
27
|
"LICENSE.md",
|
|
32
28
|
"README.md"
|
|
33
29
|
],
|
|
34
|
-
"version": "0.
|
|
30
|
+
"version": "0.7.1",
|
|
35
31
|
"scripts": {
|
|
36
|
-
"build": "neemata-build
|
|
32
|
+
"build": "neemata-build --root=./src './**/*.ts'",
|
|
37
33
|
"type-check": "tsc --noEmit"
|
|
38
34
|
}
|
|
39
35
|
}
|
package/{lib → src}/runtime.ts
RENAMED
|
@@ -6,101 +6,59 @@ import {
|
|
|
6
6
|
type ProtocolTransport,
|
|
7
7
|
} from '@nmtjs/protocol/client'
|
|
8
8
|
import { ErrorCode } from '@nmtjs/protocol/common'
|
|
9
|
-
import { type BaseTypeAny, NeverType } from '@nmtjs/type'
|
|
10
|
-
import { type Compiled, compile } from '@nmtjs/type/compiler'
|
|
9
|
+
import { type BaseTypeAny, NeemataTypeError, NeverType } from '@nmtjs/type'
|
|
11
10
|
import { ClientError } from './common.ts'
|
|
12
11
|
import type {
|
|
13
12
|
ClientCallers,
|
|
14
13
|
ResolveAPIContract,
|
|
15
14
|
ResolveClientEvents,
|
|
16
|
-
|
|
15
|
+
RuntimeInputContractTypeProvider,
|
|
16
|
+
RuntimeOutputContractTypeProvider,
|
|
17
17
|
} from './types.ts'
|
|
18
18
|
|
|
19
19
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
20
20
|
#contract: TAnyAPIContract
|
|
21
|
-
#types = new Set<BaseTypeAny>()
|
|
22
|
-
#compiled = new Map<BaseTypeAny, Compiled>()
|
|
23
21
|
|
|
24
22
|
constructor(contract: TAnyAPIContract) {
|
|
25
23
|
super()
|
|
26
24
|
|
|
27
25
|
this.#contract = contract
|
|
28
|
-
|
|
29
|
-
for (const namespace of Object.values(contract.namespaces)) {
|
|
30
|
-
for (const procedure of Object.values(namespace.procedures)) {
|
|
31
|
-
const { input, output, stream } = procedure
|
|
32
|
-
this.#registerType(input)
|
|
33
|
-
this.#registerType(output)
|
|
34
|
-
this.#registerType(stream)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
for (const subscription of Object.values(namespace.subscriptions)) {
|
|
38
|
-
const { input, output } = subscription
|
|
39
|
-
this.#registerType(input)
|
|
40
|
-
this.#registerType(output)
|
|
41
|
-
|
|
42
|
-
for (const event of Object.values(subscription.events)) {
|
|
43
|
-
this.#registerType(event.payload)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
for (const event of Object.values(namespace.events)) {
|
|
48
|
-
this.#registerType(event.payload)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.#compile()
|
|
53
26
|
}
|
|
54
27
|
|
|
55
28
|
decodeEvent(namespace: string, event: string, payload: any) {
|
|
56
29
|
const type = this.#contract.namespaces[namespace].events[event].payload
|
|
57
|
-
|
|
58
|
-
const compiled = this.#compiled.get(type)!
|
|
59
|
-
return compiled.decode(payload)
|
|
30
|
+
return type.decode(payload)
|
|
60
31
|
}
|
|
61
32
|
|
|
62
33
|
decodeRPC(namespace: string, procedure: string, payload: any) {
|
|
63
34
|
const type =
|
|
64
35
|
this.#contract.namespaces[namespace].procedures[procedure].output
|
|
65
36
|
if (type instanceof NeverType) return undefined
|
|
66
|
-
|
|
67
|
-
return compiled.decode(payload)
|
|
37
|
+
return type.decode(payload)
|
|
68
38
|
}
|
|
69
39
|
|
|
70
40
|
decodeRPCChunk(namespace: string, procedure: string, payload: any) {
|
|
71
41
|
const type =
|
|
72
42
|
this.#contract.namespaces[namespace].procedures[procedure].stream
|
|
73
43
|
if (type instanceof NeverType) return undefined
|
|
74
|
-
|
|
75
|
-
const compiled = this.#compiled.get(type)!
|
|
76
|
-
return compiled.decode(payload)
|
|
44
|
+
return type.decode(payload)
|
|
77
45
|
}
|
|
78
46
|
|
|
79
47
|
encodeRPC(namespace: string, procedure: string, payload: any) {
|
|
80
48
|
const type =
|
|
81
49
|
this.#contract.namespaces[namespace].procedures[procedure].input
|
|
82
50
|
if (type instanceof NeverType) return undefined
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return compiled.encode(payload)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
#registerType(type: BaseTypeAny) {
|
|
98
|
-
this.#types.add(type)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
#compile() {
|
|
102
|
-
for (const type of this.#types) {
|
|
103
|
-
this.#compiled.set(type, compile(type))
|
|
51
|
+
try {
|
|
52
|
+
return type.encode(payload)
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof NeemataTypeError) {
|
|
55
|
+
throw new ClientError(
|
|
56
|
+
ErrorCode.ValidationError,
|
|
57
|
+
`Invalid payload for ${namespace}.${procedure}: ${error.message}`,
|
|
58
|
+
error.issues,
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
throw error
|
|
104
62
|
}
|
|
105
63
|
}
|
|
106
64
|
}
|
|
@@ -109,8 +67,13 @@ export class RuntimeClient<
|
|
|
109
67
|
APIContract extends TAnyAPIContract,
|
|
110
68
|
ResolvedAPIContract extends ResolveAPIContract<
|
|
111
69
|
APIContract,
|
|
112
|
-
|
|
113
|
-
|
|
70
|
+
RuntimeInputContractTypeProvider,
|
|
71
|
+
RuntimeOutputContractTypeProvider
|
|
72
|
+
> = ResolveAPIContract<
|
|
73
|
+
APIContract,
|
|
74
|
+
RuntimeInputContractTypeProvider,
|
|
75
|
+
RuntimeOutputContractTypeProvider
|
|
76
|
+
>,
|
|
114
77
|
> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {
|
|
115
78
|
_!: ResolvedAPIContract
|
|
116
79
|
#callers = {} as ClientCallers<ResolvedAPIContract>
|
package/{lib → src}/static.ts
RENAMED
|
@@ -9,15 +9,21 @@ import type {
|
|
|
9
9
|
ClientCallers,
|
|
10
10
|
ResolveAPIContract,
|
|
11
11
|
ResolveClientEvents,
|
|
12
|
-
|
|
12
|
+
StaticInputContractTypeProvider,
|
|
13
|
+
StaticOutputContractTypeProvider,
|
|
13
14
|
} from './types.ts'
|
|
14
15
|
|
|
15
16
|
export class StaticClient<
|
|
16
17
|
APIContract extends TAnyAPIContract,
|
|
17
18
|
ResolvedAPIContract extends ResolveAPIContract<
|
|
18
19
|
APIContract,
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
StaticInputContractTypeProvider,
|
|
21
|
+
StaticOutputContractTypeProvider
|
|
22
|
+
> = ResolveAPIContract<
|
|
23
|
+
APIContract,
|
|
24
|
+
StaticInputContractTypeProvider,
|
|
25
|
+
StaticOutputContractTypeProvider
|
|
26
|
+
>,
|
|
21
27
|
> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {
|
|
22
28
|
_!: ResolvedAPIContract
|
|
23
29
|
|
package/{lib → src}/types.ts
RENAMED
|
@@ -7,13 +7,25 @@ import type {
|
|
|
7
7
|
import type { InputType, OutputType } from '@nmtjs/protocol/common'
|
|
8
8
|
import type { BaseTypeAny, NeverType, t } from '@nmtjs/type'
|
|
9
9
|
|
|
10
|
-
export interface
|
|
10
|
+
export interface StaticInputContractTypeProvider extends TypeProvider {
|
|
11
|
+
output: this['input'] extends BaseTypeAny
|
|
12
|
+
? t.infer.input.encoded<this['input']>
|
|
13
|
+
: never
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RuntimeInputContractTypeProvider extends TypeProvider {
|
|
11
17
|
output: this['input'] extends BaseTypeAny
|
|
12
18
|
? t.infer.input.decoded<this['input']>
|
|
13
19
|
: never
|
|
14
20
|
}
|
|
15
21
|
|
|
16
|
-
export interface
|
|
22
|
+
export interface StaticOutputContractTypeProvider extends TypeProvider {
|
|
23
|
+
output: this['input'] extends BaseTypeAny
|
|
24
|
+
? t.infer.decoded<this['input']>
|
|
25
|
+
: never
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RuntimeOutputContractTypeProvider extends TypeProvider {
|
|
17
29
|
output: this['input'] extends BaseTypeAny
|
|
18
30
|
? t.infer.decoded<this['input']>
|
|
19
31
|
: never
|
|
@@ -21,29 +33,36 @@ export interface RuntimeContractTypeProvider extends TypeProvider {
|
|
|
21
33
|
|
|
22
34
|
export type ResolveAPIContract<
|
|
23
35
|
C extends TAnyAPIContract = TAnyAPIContract,
|
|
24
|
-
|
|
36
|
+
InputTypeProvider extends TypeProvider = TypeProvider,
|
|
37
|
+
OutputTypeProvider extends TypeProvider = TypeProvider,
|
|
25
38
|
> = {
|
|
26
39
|
[N in keyof C['namespaces'] as C['namespaces'][N]['name']]: {
|
|
27
40
|
procedures: {
|
|
28
41
|
[P in keyof C['namespaces'][N]['procedures'] as C['namespaces'][N]['procedures'][P]['name']]: {
|
|
29
42
|
contract: C['namespaces'][N]['procedures'][P]
|
|
30
43
|
input: InputType<
|
|
31
|
-
CallTypeProvider<
|
|
44
|
+
CallTypeProvider<
|
|
45
|
+
InputTypeProvider,
|
|
46
|
+
C['namespaces'][N]['procedures'][P]['input']
|
|
47
|
+
>
|
|
32
48
|
>
|
|
33
49
|
output: C['namespaces'][N]['procedures'][P]['stream'] extends NeverType
|
|
34
50
|
? OutputType<
|
|
35
|
-
CallTypeProvider<
|
|
51
|
+
CallTypeProvider<
|
|
52
|
+
OutputTypeProvider,
|
|
53
|
+
C['namespaces'][N]['procedures'][P]['output']
|
|
54
|
+
>
|
|
36
55
|
>
|
|
37
56
|
: {
|
|
38
57
|
response: OutputType<
|
|
39
58
|
CallTypeProvider<
|
|
40
|
-
|
|
59
|
+
OutputTypeProvider,
|
|
41
60
|
C['namespaces'][N]['procedures'][P]['output']
|
|
42
61
|
>
|
|
43
62
|
>
|
|
44
63
|
stream: ProtocolServerStreamInterface<
|
|
45
64
|
CallTypeProvider<
|
|
46
|
-
|
|
65
|
+
OutputTypeProvider,
|
|
47
66
|
C['namespaces'][N]['procedures'][P]['stream']
|
|
48
67
|
>
|
|
49
68
|
>
|
|
@@ -53,7 +72,10 @@ export type ResolveAPIContract<
|
|
|
53
72
|
events: {
|
|
54
73
|
[KE in keyof C['namespaces'][N]['events'] as C['namespaces'][N]['events'][KE]['name']]: {
|
|
55
74
|
payload: OutputType<
|
|
56
|
-
CallTypeProvider<
|
|
75
|
+
CallTypeProvider<
|
|
76
|
+
OutputTypeProvider,
|
|
77
|
+
C['namespaces'][N]['events'][KE]['payload']
|
|
78
|
+
>
|
|
57
79
|
>
|
|
58
80
|
}
|
|
59
81
|
}
|
/package/{lib → src}/common.ts
RENAMED
|
File without changes
|