@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 CHANGED
@@ -1,5 +1,4 @@
1
- import { ProtocolError } from '@nmtjs/protocol/client';
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";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/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"],"names":["ProtocolError","ClientError","ErrorCode","ProtocolBlob","TransportType"],"mappings":"AAAA,SAASA,aAAa,QAAQ,yBAAwB;AAEtD,cAAc,aAAY;AAE1B,OAAO,MAAMC,oBAAoBD;AAAe;AAEhD,SACEE,SAAS,EACTC,YAAY,EAEZC,aAAa,QACR,yBAAwB"}
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 '@nmtjs/protocol/client';
2
- import { ErrorCode } from '@nmtjs/protocol/common';
3
- import { NeverType } from '@nmtjs/type';
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
- #contract;
8
- #types = new Set();
9
- #compiled = new Map();
10
- constructor(contract){
11
- super();
12
- this.#contract = contract;
13
- for (const namespace of Object.values(contract.namespaces)){
14
- for (const procedure of Object.values(namespace.procedures)){
15
- const { input, output, stream } = procedure;
16
- this.#registerType(input);
17
- this.#registerType(output);
18
- this.#registerType(stream);
19
- }
20
- for (const subscription of Object.values(namespace.subscriptions)){
21
- const { input, output } = subscription;
22
- this.#registerType(input);
23
- this.#registerType(output);
24
- for (const event of Object.values(subscription.events)){
25
- this.#registerType(event.payload);
26
- }
27
- }
28
- for (const event of Object.values(namespace.events)){
29
- this.#registerType(event.payload);
30
- }
31
- }
32
- this.#compile();
33
- }
34
- decodeEvent(namespace, event, payload) {
35
- const type = this.#contract.namespaces[namespace].events[event].payload;
36
- if (type instanceof NeverType) return undefined;
37
- const compiled = this.#compiled.get(type);
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
- #callers = {};
74
- constructor(contract, options){
75
- super({
76
- ...options,
77
- transformer: new RuntimeContractTransformer(contract)
78
- });
79
- const callers = {};
80
- for (const [namespaceKey, namespace] of Object.entries(contract.namespaces)){
81
- namespace.procedures;
82
- callers[namespaceKey] = {};
83
- for (const [procedureKey, procedure] of Object.entries(namespace.procedures)){
84
- callers[namespaceKey][procedureKey] = (payload, options)=>this._call(namespace.name, procedure.name, payload, {
85
- timeout: namespace.timeout,
86
- ...options
87
- });
88
- }
89
- }
90
- this.#callers = callers;
91
- }
92
- get call() {
93
- return this.#callers;
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
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/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, NeverType } from '@nmtjs/type'\nimport { type Compiled, compile } from '@nmtjs/type/compiler'\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 #types = new Set<BaseTypeAny>()\n #compiled = new Map<BaseTypeAny, Compiled>()\n\n constructor(contract: TAnyAPIContract) {\n super()\n\n this.#contract = contract\n\n for (const namespace of Object.values(contract.namespaces)) {\n for (const procedure of Object.values(namespace.procedures)) {\n const { input, output, stream } = procedure\n this.#registerType(input)\n this.#registerType(output)\n this.#registerType(stream)\n }\n\n for (const subscription of Object.values(namespace.subscriptions)) {\n const { input, output } = subscription\n this.#registerType(input)\n this.#registerType(output)\n\n for (const event of Object.values(subscription.events)) {\n this.#registerType(event.payload)\n }\n }\n\n for (const event of Object.values(namespace.events)) {\n this.#registerType(event.payload)\n }\n }\n\n this.#compile()\n }\n\n decodeEvent(namespace: string, event: string, payload: any) {\n const type = this.#contract.namespaces[namespace].events[event].payload\n if (type instanceof NeverType) return undefined\n const compiled = this.#compiled.get(type)!\n return compiled.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 const compiled = this.#compiled.get(type)!\n return compiled.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\n const compiled = this.#compiled.get(type)!\n return compiled.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\n const compiled = this.#compiled.get(type)!\n if (!compiled.check(payload)) {\n const errors = compiled.errors(payload)\n throw new ClientError(\n ErrorCode.ValidationError,\n 'Invalid RPC payload',\n errors,\n )\n }\n\n return compiled.encode(payload)\n }\n\n #registerType(type: BaseTypeAny) {\n this.#types.add(type)\n }\n\n #compile() {\n for (const type of this.#types) {\n this.#compiled.set(type, compile(type))\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"],"names":["ProtocolBaseClient","ProtocolBaseTransformer","ErrorCode","NeverType","compile","ClientError","RuntimeContractTransformer","Set","Map","constructor","contract","namespace","Object","values","namespaces","procedure","procedures","input","output","stream","subscription","subscriptions","event","events","payload","decodeEvent","type","undefined","compiled","get","decode","decodeRPC","decodeRPCChunk","encodeRPC","check","errors","ValidationError","encode","add","set","RuntimeClient","_","options","transformer","callers","namespaceKey","entries","procedureKey","_call","name","timeout","call"],"mappings":"AAEA,SACEA,kBAAkB,EAClBC,uBAAuB,QAElB,yBAAwB;AAC/B,SAASC,SAAS,QAAQ,yBAAwB;AAClD,SAA2BC,SAAS,QAAQ,cAAa;AACzD,SAAwBC,OAAO,QAAQ,uBAAsB;AAC7D,SAASC,WAAW,QAAQ,cAAa;AAQzC,OAAO,MAAMC,mCAAmCL;IAC9C,CAAA,QAAS,CAAiB;IAC1B,CAAA,KAAM,GAAG,IAAIM,MAAkB;IAC/B,CAAA,QAAS,GAAG,IAAIC,MAA4B;IAE5CC,YAAYC,QAAyB,CAAE;QACrC,KAAK;QAEL,IAAI,CAAC,CAAA,QAAS,GAAGA;QAEjB,KAAK,MAAMC,aAAaC,OAAOC,MAAM,CAACH,SAASI,UAAU,EAAG;YAC1D,KAAK,MAAMC,aAAaH,OAAOC,MAAM,CAACF,UAAUK,UAAU,EAAG;gBAC3D,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,MAAM,EAAE,GAAGJ;gBAClC,IAAI,CAAC,CAAA,YAAa,CAACE;gBACnB,IAAI,CAAC,CAAA,YAAa,CAACC;gBACnB,IAAI,CA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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 '@nmtjs/protocol/client';
1
+ import { ProtocolBaseClient } from "@nmtjs/protocol/client";
2
2
  export class StaticClient extends ProtocolBaseClient {
3
- _;
4
- #callers;
5
- constructor(options){
6
- super(options);
7
- this.#callers = new Proxy(Object(), {
8
- get: (target, namespace)=>{
9
- if (namespace === 'then') return target;
10
- return new Proxy(Object(), {
11
- get: (target, procedure)=>{
12
- if (procedure === 'then') return target;
13
- return (payload, options)=>this._call(namespace, procedure, payload, options);
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
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../lib/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"],"names":["ProtocolBaseClient","StaticClient","_","constructor","options","Proxy","Object","get","target","namespace","procedure","payload","_call","call"],"mappings":"AACA,SAEEA,kBAAkB,QAEb,yBAAwB;AAS/B,OAAO,MAAMC,qBAMHD;IACRE,EAAuB;IAEvB,CAAA,OAAQ,CAAoC;IAE5CC,YAAYC,OAIX,CAAE;QACD,KAAK,CAACA;QAEN,IAAI,CAAC,CAAA,OAAQ,GAAG,IAAIC,MAAMC,UAAU;YAClCC,KAAK,CAACC,QAAQC;gBAIZ,IAAIA,cAAc,QAAQ,OAAOD;gBACjC,OAAO,IAAIH,MAAMC,UAAU;oBACzBC,KAAK,CAACC,QAAQE;wBAIZ,IAAIA,cAAc,QAAQ,OAAOF;wBACjC,OAAO,CAACG,SAASP,UACf,IAAI,CAACQ,KAAK,CACRH,WACAC,WACAC,SACAP;oBAEN;gBACF;YACF;QACF;IACF;IAEA,IAAIS,OAAO;QACT,OAAO,IAAI,CAAC,CAAA,OAAQ;IACtB;AACF"}
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
- {"version":3,"sources":["../../lib/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"],"names":[],"mappings":"AAsEA,WAkBC"}
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
- "bun": "./lib/common.ts",
7
- "default": "./dist/common.js",
8
- "types": "./lib/common.ts"
6
+ "import": "./dist/common.js",
7
+ "types": "./src/common.ts"
9
8
  },
10
9
  "./runtime": {
11
- "bun": "./lib/runtime.ts",
12
- "default": "./dist/runtime.js",
13
- "types": "./lib/runtime.ts"
10
+ "import": "./dist/runtime.js",
11
+ "types": "./src/runtime.ts"
14
12
  },
15
13
  "./static": {
16
- "bun": "./lib/static.ts",
17
- "default": "./dist/static.js",
18
- "types": "./lib/static.ts"
14
+ "import": "./dist/static.js",
15
+ "types": "./src/static.ts"
19
16
  }
20
17
  },
21
18
  "dependencies": {
22
- "@nmtjs/contract": "0.6.4",
23
- "@nmtjs/common": "0.6.4",
24
- "@nmtjs/protocol": "0.6.4",
25
- "@nmtjs/type": "0.6.4"
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
- "index.ts",
29
- "lib",
25
+ "src",
30
26
  "dist",
31
27
  "LICENSE.md",
32
28
  "README.md"
33
29
  ],
34
- "version": "0.6.4",
30
+ "version": "0.7.0",
35
31
  "scripts": {
36
- "build": "neemata-build -p neutral --root=./lib './**/*.ts'",
32
+ "build": "neemata-build --root=./src './**/*.ts'",
37
33
  "type-check": "tsc --noEmit"
38
34
  }
39
35
  }
@@ -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
- if (type instanceof NeverType) return undefined
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
- const compiled = this.#compiled.get(type)!
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
- const compiled = this.#compiled.get(type)!
85
- if (!compiled.check(payload)) {
86
- const errors = compiled.errors(payload)
87
- throw new ClientError(
88
- ErrorCode.ValidationError,
89
- 'Invalid RPC payload',
90
- errors,
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
  }
File without changes
File without changes
File without changes