@nmtjs/client 0.11.9 → 0.12.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.map +1 -1
- package/dist/runtime.js +6 -4
- package/dist/runtime.js.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +9 -9
- package/src/common.ts +12 -15
- package/src/runtime.ts +6 -5
- package/src/types.ts +32 -11
package/dist/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AACA,SACE,cAGA,qBAEK,wBAAwB;AAS/B,SACE,WACA,cAEA,qBACK;AACP,cAAc;AAEd,OAAO,MAAM,oBAAoB,cAAc,CAAE;AAEjD,OAAO,MAAe,
|
|
1
|
+
{"mappings":"AACA,SACE,cAGA,qBAEK,wBAAwB;AAS/B,SACE,WACA,cAEA,qBACK;AACP,cAAc;AAEd,OAAO,MAAM,oBAAoB,cAAc,CAAE;AAEjD,OAAO,MAAe,mBAYZ,aAAuC;CAC/C;CAMA,AAAU;CACV,AAAU;CAEV,YACYA,WACAC,SAKV;AACA,SAAO;OAPG;OACA;AAQV,MAAI,KAAK,QAAQ,eAAe;AAC9B,QAAK,UAAU,GAAG,gBAAgB,MAChC,WAAW,KAAK,QAAQ,KAAK,KAAK,EAAE,IAAK,CAC1C;EACF;CACF;CAED,MAAgB,MACdC,WACAC,WACAC,SACAC,SACA;EACA,MAAM,OAAO,MAAM,KAAK,UAAU,KAChC,WACA,WACA,SACA,SACA,KAAK,YACN;AACD,MAAI,KAAK,QAAQ,MAAM;AACrB,UAAO,MAAM,KAAK,QACf,KAAK,CAAC,YAAY,EAAE,OAAQ,GAAE,CAC9B,MAAM,CAAC,WAAW,EAAE,MAAO,GAAE;EACjC,OAAM;AACL,UAAO,MAAM,KAAK,QAAQ,MAAM,CAAC,UAAU;AACzC,UAAM;GACP,EAAC;EACH;CACF;CAED,IAAI,OAAO;AACT,SAAO,KAAK;CACb;CAED,QAAQC,MAAW;AACjB,OAAK,OAAO;CACb;CAED,UAAU;AACR,SAAO,KAAK,UAAU,QAAQ,KAAK,MAAM,KAAK,YAAY;CAC3D;CAED,aAAa;AACX,SAAO,KAAK,UAAU,YAAY;CACnC;AACF","names":["transport: ProtocolTransport","options: {\n timeout: number\n autoreconnect?: boolean\n safe?: SafeCall\n }","namespace: string","procedure: string","payload: any","options: ProtocolBaseClientCallOptions","auth: any"],"sources":["../src/common.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport {\n EventEmitter,\n type ProtocolBaseClientCallOptions,\n type ProtocolBaseTransformer,\n ProtocolError,\n type ProtocolTransport,\n} from '@nmtjs/protocol/client'\nimport type {\n ClientCallers,\n ResolveAPIContract,\n ResolveClientEvents,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider,\n} from './types.ts'\n\nexport {\n ErrorCode,\n ProtocolBlob,\n type ProtocolBlobMetadata,\n TransportType,\n} from '@nmtjs/protocol'\nexport * from './types.ts'\n\nexport class ClientError extends ProtocolError {}\n\nexport abstract class BaseClient<\n APIContract extends TAnyAPIContract = TAnyAPIContract,\n SafeCall extends boolean = false,\n API extends ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n > = ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n >,\n> extends EventEmitter<ResolveClientEvents<API>> {\n _!: {\n api: API\n safe: SafeCall\n }\n\n protected abstract transformer: ProtocolBaseTransformer\n protected callers!: ClientCallers<API, SafeCall>\n protected auth: any\n\n constructor(\n protected transport: ProtocolTransport,\n protected options: {\n timeout: number\n autoreconnect?: boolean\n safe?: SafeCall\n },\n ) {\n super()\n\n if (this.options.autoreconnect) {\n this.transport.on('disconnected', () =>\n setTimeout(this.connect.bind(this), 1000),\n )\n }\n }\n\n protected async _call(\n namespace: string,\n procedure: string,\n payload: any,\n options: ProtocolBaseClientCallOptions,\n ) {\n const call = await this.transport.call(\n namespace,\n procedure,\n payload,\n options,\n this.transformer,\n )\n if (this.options.safe) {\n return await call.promise\n .then((result) => ({ result }))\n .catch((error) => ({ error }))\n } else {\n return await call.promise.catch((error) => {\n throw error\n })\n }\n }\n\n get call() {\n return this.callers\n }\n\n setAuth(auth: any) {\n this.auth = auth\n }\n\n connect() {\n return this.transport.connect(this.auth, this.transformer)\n }\n\n disconnect() {\n return this.transport.disconnect()\n }\n}\n"],"version":3,"file":"common.js"}
|
package/dist/runtime.js
CHANGED
|
@@ -14,12 +14,12 @@ export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
|
14
14
|
}
|
|
15
15
|
decodeRPC(namespace, procedure, payload) {
|
|
16
16
|
const type = this.contract.namespaces[namespace].procedures[procedure].output;
|
|
17
|
-
if (type instanceof
|
|
17
|
+
if (type instanceof t.NeverType) return undefined;
|
|
18
18
|
return type.decode(payload);
|
|
19
19
|
}
|
|
20
20
|
decodeRPCChunk(namespace, procedure, payload) {
|
|
21
21
|
const type = this.contract.namespaces[namespace].procedures[procedure].stream;
|
|
22
|
-
if (type instanceof t.NeverType) return undefined;
|
|
22
|
+
if (!type || type instanceof t.NeverType) return undefined;
|
|
23
23
|
return type.decode(payload);
|
|
24
24
|
}
|
|
25
25
|
encodeRPC(namespace, procedure, payload) {
|
|
@@ -41,17 +41,19 @@ export class RuntimeClient extends BaseClient {
|
|
|
41
41
|
super(...args);
|
|
42
42
|
this.contract = contract;
|
|
43
43
|
this.transformer = new RuntimeContractTransformer(this.contract);
|
|
44
|
+
const callers = {};
|
|
44
45
|
const namespaces = Object.entries(this.contract.namespaces);
|
|
45
46
|
for (const [namespaceKey, namespace] of namespaces) {
|
|
46
|
-
|
|
47
|
+
callers[namespaceKey] = {};
|
|
47
48
|
const procedures = Object.entries(namespace.procedures);
|
|
48
49
|
for (const [procedureKey, procedure] of procedures) {
|
|
49
|
-
|
|
50
|
+
callers[namespaceKey][procedureKey] = (payload, options) => this._call(namespace.name, procedure.name, payload, {
|
|
50
51
|
timeout: procedure.timeout || namespace.timeout || options.timeout,
|
|
51
52
|
...options
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
}
|
|
56
|
+
this.callers = callers;
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
|
package/dist/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AACA,SAAS,iBAAiB,iBAAiB;AAC3C,SAAS,+BAA+B,wBAAwB;AAChE,SAAS,kBAAkB,SAAS,aAAa;AACjD,SAAS,YAAY,mBAAmB,aAAa;AAErD,OAAO,MAAM,mCAAmC,wBAAwB;CACtE,AAAU;CAEV,YAAYA,UAA2B;AACrC,SAAO;AAEP,OAAK,WAAW;CACjB;CAED,YAAYC,WAAmBC,OAAeC,SAAc;EAC1D,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,OAAO,OAAO;AAC/D,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUF,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OACJ,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AAC5D,MAAI,gBAAgB,
|
|
1
|
+
{"mappings":"AACA,SAAS,iBAAiB,iBAAiB;AAC3C,SAAS,+BAA+B,wBAAwB;AAChE,SAAS,kBAAkB,SAAS,aAAa;AACjD,SAAS,YAAY,mBAAmB,aAAa;AAErD,OAAO,MAAM,mCAAmC,wBAAwB;CACtE,AAAU;CAEV,YAAYA,UAA2B;AACrC,SAAO;AAEP,OAAK,WAAW;CACjB;CAED,YAAYC,WAAmBC,OAAeC,SAAc;EAC1D,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,OAAO,OAAO;AAC/D,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUF,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OACJ,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AAC5D,MAAI,gBAAgB,EAAE,UAAW,QAAO;AACxC,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,eAAeF,WAAmBG,WAAmBD,SAAc;EACjE,MAAM,OACJ,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AAC5D,OAAK,QAAQ,gBAAgB,EAAE,UAAW,QAAO;AACjD,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUF,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AACvE,MAAI,gBAAgB,EAAE,UAAW,QAAO;AACxC,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,sBAGH,WAAkC;CAC1C,AAAU;CAEV,YACSE,UACP,GAAG,MACH;AACA,QAAM,GAAG,KAAK;OAHP;AAKP,OAAK,cAAc,IAAI,2BAA2B,KAAK;EACvD,MAAMC,UAAe,CAAE;EACvB,MAAM,aAAa,OAAO,QAAQ,KAAK,SAAS,WAAW;AAC3D,OAAK,MAAM,CAAC,cAAc,UAAU,IAAI,YAAY;AAClD,WAAQ,gBAAgB,CAAE;GAE1B,MAAM,aAAa,OAAO,QAAQ,UAAU,WAAW;AAEvD,QAAK,MAAM,CAAC,cAAc,UAAU,IAAI,YAAY;AAClD,YAAQ,cAAc,gBAAgB,CAAC,SAAS,YAC9C,KAAK,MAAM,UAAU,MAAM,UAAU,MAAM,SAAS;KAClD,SAAS,UAAU,WAAW,UAAU,WAAW,QAAQ;KAC3D,GAAG;IACJ,EAAC;GACL;EACF;AACD,OAAK,UAAU;CAChB;AACF","names":["contract: TAnyAPIContract","namespace: string","event: string","payload: any","procedure: string","contract: APIContract","callers: any"],"sources":["../src/runtime.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport { ErrorCode } from '@nmtjs/protocol'\nimport { ProtocolBaseTransformer } from '@nmtjs/protocol/client'\nimport { NeemataTypeError, t } from '@nmtjs/type'\nimport { BaseClient, ClientError } from './common.ts'\n\nexport class RuntimeContractTransformer extends ProtocolBaseTransformer {\n protected 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 t.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 || type instanceof t.NeverType) return undefined\n return type.decode(payload)\n }\n\n encodeRPC(namespace: string, procedure: string, payload: any) {\n const type = this.contract.namespaces[namespace].procedures[procedure].input\n if (type instanceof t.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 SafeCall extends boolean,\n> extends BaseClient<APIContract, SafeCall> {\n protected transformer: RuntimeContractTransformer\n\n constructor(\n public contract: APIContract,\n ...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>\n ) {\n super(...args)\n\n this.transformer = new RuntimeContractTransformer(this.contract)\n const callers: any = {}\n const namespaces = Object.entries(this.contract.namespaces)\n for (const [namespaceKey, namespace] of namespaces) {\n callers[namespaceKey] = {} as any\n\n const procedures = Object.entries(namespace.procedures)\n\n for (const [procedureKey, procedure] of procedures) {\n callers[namespaceKey][procedureKey] = (payload, options) =>\n this._call(namespace.name, procedure.name, payload, {\n timeout: procedure.timeout || namespace.timeout || options.timeout,\n ...options,\n })\n }\n }\n this.callers = callers\n }\n}\n"],"version":3,"file":"runtime.js"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'\nimport type { TAnyAPIContract } from '@nmtjs/contract'\nimport type {\n InputType,\n OutputType,\n ProtocolBaseClientCallOptions,\n ProtocolError,\n ProtocolServerStreamInterface,\n} from '@nmtjs/protocol/client'\nimport type { BaseTypeAny, t } from '@nmtjs/type'\n\nexport interface StaticInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.encoded.input<this['input']>\n : never\n}\n\nexport interface RuntimeInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded.input<this['input']>\n : never\n}\n\nexport interface StaticOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.encoded.output<this['input']>\n : never\n}\n\nexport interface RuntimeOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded.output<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']
|
|
1
|
+
{"mappings":"","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'\nimport type { TAnyAPIContract, TAnyProcedureContract } from '@nmtjs/contract'\nimport type {\n InputType,\n OutputType,\n ProtocolBaseClientCallOptions,\n ProtocolError,\n ProtocolServerStreamInterface,\n} from '@nmtjs/protocol/client'\nimport type { BaseTypeAny, t } from '@nmtjs/type'\n\nexport interface StaticInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.encoded.input<this['input']>\n : never\n}\n\nexport interface RuntimeInputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded.input<this['input']>\n : never\n}\n\nexport interface StaticOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.encoded.output<this['input']>\n : never\n}\n\nexport interface RuntimeOutputContractTypeProvider extends TypeProvider {\n output: this['input'] extends BaseTypeAny\n ? t.infer.decoded.output<this['input']>\n : never\n}\n\nexport type AnyResolvedAPIContract = Record<\n string,\n {\n procedures: Record<\n string,\n {\n contract: TAnyProcedureContract\n input: any\n output: any\n }\n >\n events: Record<\n string,\n {\n payload: any\n }\n >\n }\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']]: {\n procedures: {\n [P in keyof C['namespaces'][N]['procedures']]: {\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\n | undefined\n | t.NeverType\n ? OutputType<\n CallTypeProvider<\n OutputTypeProvider,\n C['namespaces'][N]['procedures'][P]['output']\n >\n >\n : {\n result: 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']]: {\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 AnyResolvedAPIContract = AnyResolvedAPIContract,\n> = {\n [N in keyof C]: {\n [E in keyof C[N]['events'] as `${Extract<N, string>}/${Extract<E, string>}`]: [\n C[N]['events'][E]['payload'],\n ]\n }\n}[keyof C]\n\nexport type ClientCallers<\n Resolved extends AnyResolvedAPIContract,\n SafeCall extends boolean,\n> = {\n [N in keyof Resolved]: {\n [P in keyof Resolved[N]['procedures']]: (\n ...args: Resolved[N]['procedures'][P]['input'] extends t.NeverType\n ? [data?: undefined, options?: Partial<ProtocolBaseClientCallOptions>]\n : undefined extends t.infer.encoded.input<\n Resolved[N]['procedures'][P]['contract']['input']\n >\n ? [\n data?: Resolved[N]['procedures'][P]['input'],\n options?: Partial<ProtocolBaseClientCallOptions>,\n ]\n : [\n data: Resolved[N]['procedures'][P]['input'],\n options?: Partial<ProtocolBaseClientCallOptions>,\n ]\n ) => SafeCall extends true\n ? Promise<\n OneOf<\n [\n {\n output: Resolved[N]['procedures'][P]['output']\n },\n { error: ProtocolError },\n ]\n >\n >\n : Promise<Resolved[N]['procedures'][P]['output']>\n }\n}\n"],"version":3,"file":"types.js"}
|
package/package.json
CHANGED
|
@@ -16,16 +16,16 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@nmtjs/
|
|
20
|
-
"@nmtjs/common": "0.
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/
|
|
19
|
+
"@nmtjs/type": "0.12.0",
|
|
20
|
+
"@nmtjs/common": "0.12.0",
|
|
21
|
+
"@nmtjs/protocol": "0.12.0",
|
|
22
|
+
"@nmtjs/contract": "0.12.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@nmtjs/
|
|
26
|
-
"@nmtjs/
|
|
27
|
-
"@nmtjs/
|
|
28
|
-
"@nmtjs/
|
|
25
|
+
"@nmtjs/type": "0.12.0",
|
|
26
|
+
"@nmtjs/contract": "0.12.0",
|
|
27
|
+
"@nmtjs/protocol": "0.12.0",
|
|
28
|
+
"@nmtjs/common": "0.12.0"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
31
|
"src",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"LICENSE.md",
|
|
34
34
|
"README.md"
|
|
35
35
|
],
|
|
36
|
-
"version": "0.
|
|
36
|
+
"version": "0.12.0",
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "neemata-build --root=./src './**/*.ts'",
|
|
39
39
|
"type-check": "tsc --noEmit"
|
package/src/common.ts
CHANGED
|
@@ -27,26 +27,23 @@ export class ClientError extends ProtocolError {}
|
|
|
27
27
|
export abstract class BaseClient<
|
|
28
28
|
APIContract extends TAnyAPIContract = TAnyAPIContract,
|
|
29
29
|
SafeCall extends boolean = false,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
API extends ResolveAPIContract<
|
|
31
|
+
APIContract,
|
|
32
|
+
RuntimeInputContractTypeProvider,
|
|
33
|
+
RuntimeOutputContractTypeProvider
|
|
34
|
+
> = ResolveAPIContract<
|
|
35
|
+
APIContract,
|
|
36
|
+
RuntimeInputContractTypeProvider,
|
|
37
|
+
RuntimeOutputContractTypeProvider
|
|
38
|
+
>,
|
|
39
|
+
> extends EventEmitter<ResolveClientEvents<API>> {
|
|
39
40
|
_!: {
|
|
40
|
-
api:
|
|
41
|
-
APIContract,
|
|
42
|
-
RuntimeInputContractTypeProvider,
|
|
43
|
-
RuntimeOutputContractTypeProvider
|
|
44
|
-
>
|
|
41
|
+
api: API
|
|
45
42
|
safe: SafeCall
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
protected abstract transformer: ProtocolBaseTransformer
|
|
49
|
-
protected callers!: ClientCallers<
|
|
46
|
+
protected callers!: ClientCallers<API, SafeCall>
|
|
50
47
|
protected auth: any
|
|
51
48
|
|
|
52
49
|
constructor(
|
package/src/runtime.ts
CHANGED
|
@@ -21,14 +21,14 @@ export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
|
21
21
|
decodeRPC(namespace: string, procedure: string, payload: any) {
|
|
22
22
|
const type =
|
|
23
23
|
this.contract.namespaces[namespace].procedures[procedure].output
|
|
24
|
-
if (type instanceof
|
|
24
|
+
if (type instanceof t.NeverType) return undefined
|
|
25
25
|
return type.decode(payload)
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
decodeRPCChunk(namespace: string, procedure: string, payload: any) {
|
|
29
29
|
const type =
|
|
30
30
|
this.contract.namespaces[namespace].procedures[procedure].stream
|
|
31
|
-
if (type instanceof t.NeverType) return undefined
|
|
31
|
+
if (!type || type instanceof t.NeverType) return undefined
|
|
32
32
|
return type.decode(payload)
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -63,20 +63,21 @@ export class RuntimeClient<
|
|
|
63
63
|
super(...args)
|
|
64
64
|
|
|
65
65
|
this.transformer = new RuntimeContractTransformer(this.contract)
|
|
66
|
-
|
|
66
|
+
const callers: any = {}
|
|
67
67
|
const namespaces = Object.entries(this.contract.namespaces)
|
|
68
68
|
for (const [namespaceKey, namespace] of namespaces) {
|
|
69
|
-
|
|
69
|
+
callers[namespaceKey] = {} as any
|
|
70
70
|
|
|
71
71
|
const procedures = Object.entries(namespace.procedures)
|
|
72
72
|
|
|
73
73
|
for (const [procedureKey, procedure] of procedures) {
|
|
74
|
-
|
|
74
|
+
callers[namespaceKey][procedureKey] = (payload, options) =>
|
|
75
75
|
this._call(namespace.name, procedure.name, payload, {
|
|
76
76
|
timeout: procedure.timeout || namespace.timeout || options.timeout,
|
|
77
77
|
...options,
|
|
78
78
|
})
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
+
this.callers = callers
|
|
81
82
|
}
|
|
82
83
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'
|
|
2
|
-
import type { TAnyAPIContract } from '@nmtjs/contract'
|
|
2
|
+
import type { TAnyAPIContract, TAnyProcedureContract } from '@nmtjs/contract'
|
|
3
3
|
import type {
|
|
4
4
|
InputType,
|
|
5
5
|
OutputType,
|
|
@@ -33,14 +33,34 @@ export interface RuntimeOutputContractTypeProvider extends TypeProvider {
|
|
|
33
33
|
: never
|
|
34
34
|
}
|
|
35
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
|
+
|
|
36
56
|
export type ResolveAPIContract<
|
|
37
57
|
C extends TAnyAPIContract = TAnyAPIContract,
|
|
38
58
|
InputTypeProvider extends TypeProvider = TypeProvider,
|
|
39
59
|
OutputTypeProvider extends TypeProvider = TypeProvider,
|
|
40
60
|
> = {
|
|
41
|
-
[N in keyof C['namespaces']
|
|
61
|
+
[N in keyof C['namespaces']]: {
|
|
42
62
|
procedures: {
|
|
43
|
-
[P in keyof C['namespaces'][N]['procedures']
|
|
63
|
+
[P in keyof C['namespaces'][N]['procedures']]: {
|
|
44
64
|
contract: C['namespaces'][N]['procedures'][P]
|
|
45
65
|
input: InputType<
|
|
46
66
|
CallTypeProvider<
|
|
@@ -48,7 +68,9 @@ export type ResolveAPIContract<
|
|
|
48
68
|
C['namespaces'][N]['procedures'][P]['input']
|
|
49
69
|
>
|
|
50
70
|
>
|
|
51
|
-
output: C['namespaces'][N]['procedures'][P]['stream'] extends
|
|
71
|
+
output: C['namespaces'][N]['procedures'][P]['stream'] extends
|
|
72
|
+
| undefined
|
|
73
|
+
| t.NeverType
|
|
52
74
|
? OutputType<
|
|
53
75
|
CallTypeProvider<
|
|
54
76
|
OutputTypeProvider,
|
|
@@ -72,7 +94,7 @@ export type ResolveAPIContract<
|
|
|
72
94
|
}
|
|
73
95
|
}
|
|
74
96
|
events: {
|
|
75
|
-
[KE in keyof C['namespaces'][N]['events']
|
|
97
|
+
[KE in keyof C['namespaces'][N]['events']]: {
|
|
76
98
|
payload: OutputType<
|
|
77
99
|
CallTypeProvider<
|
|
78
100
|
OutputTypeProvider,
|
|
@@ -85,17 +107,17 @@ export type ResolveAPIContract<
|
|
|
85
107
|
}
|
|
86
108
|
|
|
87
109
|
export type ResolveClientEvents<
|
|
88
|
-
C extends
|
|
110
|
+
C extends AnyResolvedAPIContract = AnyResolvedAPIContract,
|
|
89
111
|
> = {
|
|
90
112
|
[N in keyof C]: {
|
|
91
|
-
[
|
|
92
|
-
C[N]['events'][
|
|
113
|
+
[E in keyof C[N]['events'] as `${Extract<N, string>}/${Extract<E, string>}`]: [
|
|
114
|
+
C[N]['events'][E]['payload'],
|
|
93
115
|
]
|
|
94
116
|
}
|
|
95
117
|
}[keyof C]
|
|
96
118
|
|
|
97
119
|
export type ClientCallers<
|
|
98
|
-
Resolved extends
|
|
120
|
+
Resolved extends AnyResolvedAPIContract,
|
|
99
121
|
SafeCall extends boolean,
|
|
100
122
|
> = {
|
|
101
123
|
[N in keyof Resolved]: {
|
|
@@ -118,10 +140,9 @@ export type ClientCallers<
|
|
|
118
140
|
OneOf<
|
|
119
141
|
[
|
|
120
142
|
{
|
|
121
|
-
error?: undefined
|
|
122
143
|
output: Resolved[N]['procedures'][P]['output']
|
|
123
144
|
},
|
|
124
|
-
{ error: ProtocolError
|
|
145
|
+
{ error: ProtocolError },
|
|
125
146
|
]
|
|
126
147
|
>
|
|
127
148
|
>
|