@nmtjs/client 0.6.4 → 0.7.0
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 +15 -58
- /package/{lib → src}/common.ts +0 -0
- /package/{lib → src}/static.ts +0 -0
- /package/{lib → src}/types.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;AAQzC,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,sBAMH,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 RuntimeContractTypeProvider,\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 RuntimeContractTypeProvider\n > = ResolveAPIContract<APIContract, RuntimeContractTypeProvider>,\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;AAS/B,OAAO,MAAM,qBAMH,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 StaticContractTypeProvider,\n} from './types.ts'\n\nexport class StaticClient<\n APIContract extends TAnyAPIContract,\n ResolvedAPIContract extends ResolveAPIContract<\n APIContract,\n StaticContractTypeProvider\n > = ResolveAPIContract<APIContract, StaticContractTypeProvider>,\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 StaticContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.input.decoded<this['input']>\n : never\n}\n\nexport interface RuntimeContractTypeProvider 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 T 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<T, C['namespaces'][N]['procedures'][P]['input']>\n >\n output: C['namespaces'][N]['procedures'][P]['stream'] extends NeverType\n ? OutputType<\n CallTypeProvider<T, C['namespaces'][N]['procedures'][P]['output']>\n >\n : {\n response: OutputType<\n CallTypeProvider<\n T,\n C['namespaces'][N]['procedures'][P]['output']\n >\n >\n stream: ProtocolServerStreamInterface<\n CallTypeProvider<\n T,\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<T, C['namespaces'][N]['events'][KE]['payload']>\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.0",
|
|
20
|
+
"@nmtjs/protocol": "0.7.0",
|
|
21
|
+
"@nmtjs/contract": "0.7.0",
|
|
22
|
+
"@nmtjs/common": "0.7.0"
|
|
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.0",
|
|
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,8 +6,7 @@ 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,
|
|
@@ -18,89 +17,47 @@ import type {
|
|
|
18
17
|
|
|
19
18
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
20
19
|
#contract: TAnyAPIContract
|
|
21
|
-
#types = new Set<BaseTypeAny>()
|
|
22
|
-
#compiled = new Map<BaseTypeAny, Compiled>()
|
|
23
20
|
|
|
24
21
|
constructor(contract: TAnyAPIContract) {
|
|
25
22
|
super()
|
|
26
23
|
|
|
27
24
|
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
25
|
}
|
|
54
26
|
|
|
55
27
|
decodeEvent(namespace: string, event: string, payload: any) {
|
|
56
28
|
const type = this.#contract.namespaces[namespace].events[event].payload
|
|
57
|
-
|
|
58
|
-
const compiled = this.#compiled.get(type)!
|
|
59
|
-
return compiled.decode(payload)
|
|
29
|
+
return type.decode(payload)
|
|
60
30
|
}
|
|
61
31
|
|
|
62
32
|
decodeRPC(namespace: string, procedure: string, payload: any) {
|
|
63
33
|
const type =
|
|
64
34
|
this.#contract.namespaces[namespace].procedures[procedure].output
|
|
65
35
|
if (type instanceof NeverType) return undefined
|
|
66
|
-
|
|
67
|
-
return compiled.decode(payload)
|
|
36
|
+
return type.decode(payload)
|
|
68
37
|
}
|
|
69
38
|
|
|
70
39
|
decodeRPCChunk(namespace: string, procedure: string, payload: any) {
|
|
71
40
|
const type =
|
|
72
41
|
this.#contract.namespaces[namespace].procedures[procedure].stream
|
|
73
42
|
if (type instanceof NeverType) return undefined
|
|
74
|
-
|
|
75
|
-
const compiled = this.#compiled.get(type)!
|
|
76
|
-
return compiled.decode(payload)
|
|
43
|
+
return type.decode(payload)
|
|
77
44
|
}
|
|
78
45
|
|
|
79
46
|
encodeRPC(namespace: string, procedure: string, payload: any) {
|
|
80
47
|
const type =
|
|
81
48
|
this.#contract.namespaces[namespace].procedures[procedure].input
|
|
82
49
|
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))
|
|
50
|
+
try {
|
|
51
|
+
return type.encode(payload)
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof NeemataTypeError) {
|
|
54
|
+
throw new ClientError(
|
|
55
|
+
ErrorCode.ValidationError,
|
|
56
|
+
`Invalid payload for ${namespace}.${procedure}: ${error.message}`,
|
|
57
|
+
error.issues,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
throw error
|
|
104
61
|
}
|
|
105
62
|
}
|
|
106
63
|
}
|
/package/{lib → src}/common.ts
RENAMED
|
File without changes
|
/package/{lib → src}/static.ts
RENAMED
|
File without changes
|
/package/{lib → src}/types.ts
RENAMED
|
File without changes
|