@nmtjs/client 0.8.1 → 0.10.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 +36 -2
- package/dist/common.js.map +1 -1
- package/dist/runtime.js +24 -28
- package/dist/runtime.js.map +1 -1
- package/dist/static.js +14 -11
- package/dist/static.js.map +1 -1
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -1
- package/package.json +9 -9
- package/src/common.ts +92 -4
- package/src/runtime.ts +24 -61
- package/src/static.ts +15 -42
- package/src/types.ts +28 -10
package/dist/common.js
CHANGED
|
@@ -1,4 +1,38 @@
|
|
|
1
|
-
import { ProtocolError } from "@nmtjs/protocol/client";
|
|
1
|
+
import { EventEmitter, ProtocolError } from "@nmtjs/protocol/client";
|
|
2
2
|
export * from "./types.js";
|
|
3
|
+
export { ErrorCode, ProtocolBlob, TransportType } from "@nmtjs/protocol";
|
|
3
4
|
export class ClientError extends ProtocolError {}
|
|
4
|
-
export
|
|
5
|
+
export class BaseClient extends EventEmitter {
|
|
6
|
+
_;
|
|
7
|
+
auth;
|
|
8
|
+
constructor(transport, options) {
|
|
9
|
+
super();
|
|
10
|
+
this.transport = transport;
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
callers = {};
|
|
14
|
+
async _call(namespace, procedure, payload, options) {
|
|
15
|
+
const call = await this.transport.call(namespace, procedure, payload, options, this.transformer);
|
|
16
|
+
if (this.options.safe) {
|
|
17
|
+
return await call.promise.then((result) => ({ result })).catch((error) => ({ error }));
|
|
18
|
+
} else {
|
|
19
|
+
return await call.promise.catch((error) => {
|
|
20
|
+
throw error;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
get call() {
|
|
25
|
+
return this.callers;
|
|
26
|
+
}
|
|
27
|
+
setAuth(auth) {
|
|
28
|
+
this.auth = auth;
|
|
29
|
+
}
|
|
30
|
+
connect() {
|
|
31
|
+
return this.transport.connect(this.auth, this.transformer);
|
|
32
|
+
}
|
|
33
|
+
disconnect() {
|
|
34
|
+
return this.transport.disconnect();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=common.js.map
|
package/dist/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":"AACA,SACE,cAGA,qBAEK,wBAAwB;AAS/B,cAAc;AAEd,SACE,WACA,cAEA,qBACK;AAEP,OAAO,MAAM,oBAAoB,cAAc,CAAE;AAEjD,OAAO,MAAe,mBAGZ,aAQR;CACA;CAUA,AAAU;CAEV,YACYA,WACAC,SACV;AACA,SAAO;OAHG;OACA;CAGX;CAED,AAAU,UAAU,CAAE;CAEtB,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: { timeout: number; safe?: SafeCall }","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 * from './types.ts'\n\nexport {\n ErrorCode,\n ProtocolBlob,\n type ProtocolBlobMetadata,\n TransportType,\n} from '@nmtjs/protocol'\n\nexport class ClientError extends ProtocolError {}\n\nexport abstract class BaseClient<\n APIContract extends TAnyAPIContract = TAnyAPIContract,\n SafeCall extends boolean = false,\n> extends EventEmitter<\n ResolveClientEvents<\n ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n >\n >\n> {\n _!: {\n api: ResolveAPIContract<\n APIContract,\n RuntimeInputContractTypeProvider,\n RuntimeOutputContractTypeProvider\n >\n safe: SafeCall\n }\n\n protected abstract transformer: ProtocolBaseTransformer\n protected auth: any\n\n constructor(\n protected transport: ProtocolTransport,\n protected options: { timeout: number; safe?: SafeCall },\n ) {\n super()\n }\n\n protected callers = {} as ClientCallers<this['_']['api'], this['_']['safe']>\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
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ErrorCode } from "@nmtjs/protocol";
|
|
2
|
+
import { ProtocolBaseTransformer } from "@nmtjs/protocol/client";
|
|
3
3
|
import { NeemataTypeError, NeverType } from "@nmtjs/type";
|
|
4
|
-
import { ClientError } from "./common.js";
|
|
4
|
+
import { BaseClient, ClientError } from "./common.js";
|
|
5
5
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
6
|
-
|
|
6
|
+
contract;
|
|
7
7
|
constructor(contract) {
|
|
8
8
|
super();
|
|
9
|
-
this
|
|
9
|
+
this.contract = contract;
|
|
10
10
|
}
|
|
11
11
|
decodeEvent(namespace, event, payload) {
|
|
12
|
-
const type = this
|
|
12
|
+
const type = this.contract.namespaces[namespace].events[event].payload;
|
|
13
13
|
return type.decode(payload);
|
|
14
14
|
}
|
|
15
15
|
decodeRPC(namespace, procedure, payload) {
|
|
16
|
-
const type = this
|
|
16
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].output;
|
|
17
17
|
if (type instanceof NeverType) return undefined;
|
|
18
18
|
return type.decode(payload);
|
|
19
19
|
}
|
|
20
20
|
decodeRPCChunk(namespace, procedure, payload) {
|
|
21
|
-
const type = this
|
|
21
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].stream;
|
|
22
22
|
if (type instanceof NeverType) return undefined;
|
|
23
23
|
return type.decode(payload);
|
|
24
24
|
}
|
|
25
25
|
encodeRPC(namespace, procedure, payload) {
|
|
26
|
-
const type = this
|
|
26
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].input;
|
|
27
27
|
if (type instanceof NeverType) return undefined;
|
|
28
28
|
try {
|
|
29
29
|
return type.encode(payload);
|
|
@@ -35,28 +35,24 @@ export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
export class RuntimeClient extends
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
callers[namespaceKey][procedureKey] = (payload, options) => this._call(namespace.name, procedure.name, payload, {
|
|
52
|
-
timeout: namespace.timeout,
|
|
38
|
+
export class RuntimeClient extends BaseClient {
|
|
39
|
+
transformer;
|
|
40
|
+
constructor(contract, ...args) {
|
|
41
|
+
super(...args);
|
|
42
|
+
this.contract = contract;
|
|
43
|
+
this.transformer = new RuntimeContractTransformer(this.contract);
|
|
44
|
+
const namespaces = Object.entries(this.contract.namespaces);
|
|
45
|
+
for (const [namespaceKey, namespace] of namespaces) {
|
|
46
|
+
this.callers[namespaceKey] = {};
|
|
47
|
+
const procedures = Object.entries(namespace.procedures);
|
|
48
|
+
for (const [procedureKey, procedure] of procedures) {
|
|
49
|
+
this.callers[namespaceKey][procedureKey] = (payload, options) => this._call(namespace.name, procedure.name, payload, {
|
|
50
|
+
timeout: procedure.timeout || namespace.timeout || options.timeout,
|
|
53
51
|
...options
|
|
54
52
|
});
|
|
55
53
|
}
|
|
56
54
|
}
|
|
57
|
-
this.#callers = callers;
|
|
58
|
-
}
|
|
59
|
-
get call() {
|
|
60
|
-
return this.#callers;
|
|
61
55
|
}
|
|
62
56
|
}
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=runtime.js.map
|
package/dist/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":"AACA,SAAS,iBAAiB,iBAAiB;AAC3C,SAAS,+BAA+B,wBAAwB;AAChE,SAAS,kBAAkB,iBAAiB,aAAa;AACzD,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,UAAW,QAAO;AACtC,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,eAAeF,WAAmBG,WAAmBD,SAAc;EACjE,MAAM,OACJ,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AAC5D,MAAI,gBAAgB,UAAW,QAAO;AACtC,SAAO,KAAK,OAAO,QAAQ;CAC5B;CAED,UAAUF,WAAmBG,WAAmBD,SAAc;EAC5D,MAAM,OAAO,KAAK,SAAS,WAAW,WAAW,WAAW,WAAW;AACvE,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,sBAGH,WAAkC;CAC1C,AAAU;CAEV,YACSE,UACP,GAAG,MACH;AACA,QAAM,GAAG,KAAK;OAHP;AAKP,OAAK,cAAc,IAAI,2BAA2B,KAAK;EAEvD,MAAM,aAAa,OAAO,QAAQ,KAAK,SAAS,WAAW;AAC3D,OAAK,MAAM,CAAC,cAAc,UAAU,IAAI,YAAY;AAClD,QAAK,QAAQ,gBAAgB,CAAE;GAE/B,MAAM,aAAa,OAAO,QAAQ,UAAU,WAAW;AAEvD,QAAK,MAAM,CAAC,cAAc,UAAU,IAAI,YAAY;AAClD,SAAK,QAAQ,cAAc,gBAAgB,CAAC,SAAS,YACnD,KAAK,MAAM,UAAU,MAAM,UAAU,MAAM,SAAS;KAClD,SAAS,UAAU,WAAW,UAAU,WAAW,QAAQ;KAC3D,GAAG;IACJ,EAAC;GACL;EACF;CACF;AACF","names":["contract: TAnyAPIContract","namespace: string","event: string","payload: any","procedure: string","contract: APIContract"],"sources":["../src/runtime.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport { ErrorCode } from '@nmtjs/protocol'\nimport { ProtocolBaseTransformer } from '@nmtjs/protocol/client'\nimport { NeemataTypeError, NeverType } 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 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 = 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 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\n const namespaces = Object.entries(this.contract.namespaces)\n for (const [namespaceKey, namespace] of namespaces) {\n this.callers[namespaceKey] = {} as any\n\n const procedures = Object.entries(namespace.procedures)\n\n for (const [procedureKey, procedure] of procedures) {\n this.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 }\n}\n"],"version":3,"file":"runtime.js"}
|
package/dist/static.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(
|
|
6
|
-
super(
|
|
7
|
-
this
|
|
1
|
+
import { ProtocolBaseTransformer } from "@nmtjs/protocol/client";
|
|
2
|
+
import { BaseClient } from "./common.js";
|
|
3
|
+
export class StaticClient extends BaseClient {
|
|
4
|
+
transformer;
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(...args);
|
|
7
|
+
this.transformer = new ProtocolBaseTransformer();
|
|
8
|
+
this.callers = new Proxy(Object(), { get: (target, namespace) => {
|
|
8
9
|
if (namespace === "then") return target;
|
|
9
10
|
return new Proxy(Object(), { get: (target, procedure) => {
|
|
10
11
|
if (procedure === "then") return target;
|
|
11
|
-
return (payload, options) => this._call(namespace, procedure, payload,
|
|
12
|
+
return (payload, options) => this._call(namespace, procedure, payload, {
|
|
13
|
+
...options,
|
|
14
|
+
timeout: options?.timeout ?? this.options.timeout
|
|
15
|
+
});
|
|
12
16
|
} });
|
|
13
17
|
} });
|
|
14
18
|
}
|
|
15
|
-
get call() {
|
|
16
|
-
return this.#callers;
|
|
17
|
-
}
|
|
18
19
|
}
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=static.js.map
|
package/dist/static.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AACA,
|
|
1
|
+
{"mappings":"AACA,SAAS,+BAA+B,wBAAwB;AAChE,SAAS,kBAAkB,aAAa;AAExC,OAAO,MAAM,qBAGH,WAAkC;CAC1C,AAAU;CAEV,YACE,GAAG,MACH;AACA,QAAM,GAAG,KAAK;AAEd,OAAK,cAAc,IAAI;AAEvB,OAAK,UAAU,IAAI,MAAM,QAAQ,EAAE,EACjC,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,MAAM,WAAqB,WAAqB,SAAS;KAC5D,GAAG;KACH,SAAS,SAAS,WAAW,KAAK,QAAQ;IAC3C,EAAC;GACL,EACF;EACF,EACF;CACF;AACF","names":[],"sources":["../src/static.ts"],"sourcesContent":["import type { TAnyAPIContract } from '@nmtjs/contract'\nimport { ProtocolBaseTransformer } from '@nmtjs/protocol/client'\nimport { BaseClient } from './common.ts'\n\nexport class StaticClient<\n APIContract extends TAnyAPIContract,\n SafeCall extends boolean = false,\n> extends BaseClient<APIContract, SafeCall> {\n protected transformer: ProtocolBaseTransformer\n\n constructor(\n ...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>\n ) {\n super(...args)\n\n this.transformer = new ProtocolBaseTransformer()\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(namespace as string, procedure as string, payload, {\n ...options,\n timeout: options?.timeout ?? this.options.timeout,\n })\n },\n })\n },\n })\n }\n}\n"],"version":3,"file":"static.js"}
|
package/dist/types.js
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -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/
|
|
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 ProtocolBaseClientCallOptions,\n ProtocolError,\n ProtocolServerStreamInterface,\n} from '@nmtjs/protocol/client'\nimport type { InputType, OutputType } from '@nmtjs/protocol/client'\nimport type { BaseTypeAny, NeverType, 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'] 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 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'] 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'] as `${Extract<N, string>}/${Extract<KE, string>}`]: [\n C[N]['events'][KE]['payload'],\n ]\n }\n}[keyof C]\n\nexport type ClientCallers<\n Resolved extends ResolveAPIContract,\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 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 error?: undefined\n output: Resolved[N]['procedures'][P]['output']\n },\n { error: ProtocolError; output?: undefined },\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/type": "0.
|
|
20
|
-
"@nmtjs/
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/
|
|
19
|
+
"@nmtjs/type": "0.10.0",
|
|
20
|
+
"@nmtjs/contract": "0.10.0",
|
|
21
|
+
"@nmtjs/common": "0.10.0",
|
|
22
|
+
"@nmtjs/protocol": "0.10.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@nmtjs/type": "0.
|
|
26
|
-
"@nmtjs/
|
|
27
|
-
"@nmtjs/
|
|
28
|
-
"@nmtjs/protocol": "0.
|
|
25
|
+
"@nmtjs/type": "0.10.0",
|
|
26
|
+
"@nmtjs/contract": "0.10.0",
|
|
27
|
+
"@nmtjs/common": "0.10.0",
|
|
28
|
+
"@nmtjs/protocol": "0.10.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.10.0",
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "neemata-build --root=./src './**/*.ts'",
|
|
39
39
|
"type-check": "tsc --noEmit"
|
package/src/common.ts
CHANGED
|
@@ -1,12 +1,100 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TAnyAPIContract } from '@nmtjs/contract'
|
|
2
|
+
import {
|
|
3
|
+
EventEmitter,
|
|
4
|
+
type ProtocolBaseClientCallOptions,
|
|
5
|
+
type ProtocolBaseTransformer,
|
|
6
|
+
ProtocolError,
|
|
7
|
+
type ProtocolTransport,
|
|
8
|
+
} from '@nmtjs/protocol/client'
|
|
9
|
+
import type {
|
|
10
|
+
ClientCallers,
|
|
11
|
+
ResolveAPIContract,
|
|
12
|
+
ResolveClientEvents,
|
|
13
|
+
RuntimeInputContractTypeProvider,
|
|
14
|
+
RuntimeOutputContractTypeProvider,
|
|
15
|
+
} from './types.ts'
|
|
2
16
|
|
|
3
17
|
export * from './types.ts'
|
|
4
18
|
|
|
5
|
-
export class ClientError extends ProtocolError {}
|
|
6
|
-
|
|
7
19
|
export {
|
|
8
20
|
ErrorCode,
|
|
9
21
|
ProtocolBlob,
|
|
10
22
|
type ProtocolBlobMetadata,
|
|
11
23
|
TransportType,
|
|
12
|
-
} from '@nmtjs/protocol
|
|
24
|
+
} from '@nmtjs/protocol'
|
|
25
|
+
|
|
26
|
+
export class ClientError extends ProtocolError {}
|
|
27
|
+
|
|
28
|
+
export abstract class BaseClient<
|
|
29
|
+
APIContract extends TAnyAPIContract = TAnyAPIContract,
|
|
30
|
+
SafeCall extends boolean = false,
|
|
31
|
+
> extends EventEmitter<
|
|
32
|
+
ResolveClientEvents<
|
|
33
|
+
ResolveAPIContract<
|
|
34
|
+
APIContract,
|
|
35
|
+
RuntimeInputContractTypeProvider,
|
|
36
|
+
RuntimeOutputContractTypeProvider
|
|
37
|
+
>
|
|
38
|
+
>
|
|
39
|
+
> {
|
|
40
|
+
_!: {
|
|
41
|
+
api: ResolveAPIContract<
|
|
42
|
+
APIContract,
|
|
43
|
+
RuntimeInputContractTypeProvider,
|
|
44
|
+
RuntimeOutputContractTypeProvider
|
|
45
|
+
>
|
|
46
|
+
safe: SafeCall
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected abstract transformer: ProtocolBaseTransformer
|
|
50
|
+
protected auth: any
|
|
51
|
+
|
|
52
|
+
constructor(
|
|
53
|
+
protected transport: ProtocolTransport,
|
|
54
|
+
protected options: { timeout: number; safe?: SafeCall },
|
|
55
|
+
) {
|
|
56
|
+
super()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected callers = {} as ClientCallers<this['_']['api'], this['_']['safe']>
|
|
60
|
+
|
|
61
|
+
protected async _call(
|
|
62
|
+
namespace: string,
|
|
63
|
+
procedure: string,
|
|
64
|
+
payload: any,
|
|
65
|
+
options: ProtocolBaseClientCallOptions,
|
|
66
|
+
) {
|
|
67
|
+
const call = await this.transport.call(
|
|
68
|
+
namespace,
|
|
69
|
+
procedure,
|
|
70
|
+
payload,
|
|
71
|
+
options,
|
|
72
|
+
this.transformer,
|
|
73
|
+
)
|
|
74
|
+
if (this.options.safe) {
|
|
75
|
+
return await call.promise
|
|
76
|
+
.then((result) => ({ result }))
|
|
77
|
+
.catch((error) => ({ error }))
|
|
78
|
+
} else {
|
|
79
|
+
return await call.promise.catch((error) => {
|
|
80
|
+
throw error
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get call() {
|
|
86
|
+
return this.callers
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setAuth(auth: any) {
|
|
90
|
+
this.auth = auth
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
connect() {
|
|
94
|
+
return this.transport.connect(this.auth, this.transformer)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
disconnect() {
|
|
98
|
+
return this.transport.disconnect()
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/runtime.ts
CHANGED
|
@@ -1,52 +1,39 @@
|
|
|
1
1
|
import type { TAnyAPIContract } from '@nmtjs/contract'
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type ProtocolTransport,
|
|
7
|
-
} from '@nmtjs/protocol/client'
|
|
8
|
-
import { ErrorCode } from '@nmtjs/protocol/common'
|
|
9
|
-
import { type BaseTypeAny, NeemataTypeError, NeverType } from '@nmtjs/type'
|
|
10
|
-
import { ClientError } from './common.ts'
|
|
11
|
-
import type {
|
|
12
|
-
ClientCallers,
|
|
13
|
-
ResolveAPIContract,
|
|
14
|
-
ResolveClientEvents,
|
|
15
|
-
RuntimeInputContractTypeProvider,
|
|
16
|
-
RuntimeOutputContractTypeProvider,
|
|
17
|
-
} from './types.ts'
|
|
2
|
+
import { ErrorCode } from '@nmtjs/protocol'
|
|
3
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client'
|
|
4
|
+
import { NeemataTypeError, NeverType } from '@nmtjs/type'
|
|
5
|
+
import { BaseClient, ClientError } from './common.ts'
|
|
18
6
|
|
|
19
7
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
20
|
-
|
|
8
|
+
protected contract: TAnyAPIContract
|
|
21
9
|
|
|
22
10
|
constructor(contract: TAnyAPIContract) {
|
|
23
11
|
super()
|
|
24
12
|
|
|
25
|
-
this
|
|
13
|
+
this.contract = contract
|
|
26
14
|
}
|
|
27
15
|
|
|
28
16
|
decodeEvent(namespace: string, event: string, payload: any) {
|
|
29
|
-
const type = this
|
|
17
|
+
const type = this.contract.namespaces[namespace].events[event].payload
|
|
30
18
|
return type.decode(payload)
|
|
31
19
|
}
|
|
32
20
|
|
|
33
21
|
decodeRPC(namespace: string, procedure: string, payload: any) {
|
|
34
22
|
const type =
|
|
35
|
-
this
|
|
23
|
+
this.contract.namespaces[namespace].procedures[procedure].output
|
|
36
24
|
if (type instanceof NeverType) return undefined
|
|
37
25
|
return type.decode(payload)
|
|
38
26
|
}
|
|
39
27
|
|
|
40
28
|
decodeRPCChunk(namespace: string, procedure: string, payload: any) {
|
|
41
29
|
const type =
|
|
42
|
-
this
|
|
30
|
+
this.contract.namespaces[namespace].procedures[procedure].stream
|
|
43
31
|
if (type instanceof NeverType) return undefined
|
|
44
32
|
return type.decode(payload)
|
|
45
33
|
}
|
|
46
34
|
|
|
47
35
|
encodeRPC(namespace: string, procedure: string, payload: any) {
|
|
48
|
-
const type =
|
|
49
|
-
this.#contract.namespaces[namespace].procedures[procedure].input
|
|
36
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].input
|
|
50
37
|
if (type instanceof NeverType) return undefined
|
|
51
38
|
try {
|
|
52
39
|
return type.encode(payload)
|
|
@@ -65,55 +52,31 @@ export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
|
65
52
|
|
|
66
53
|
export class RuntimeClient<
|
|
67
54
|
APIContract extends TAnyAPIContract,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
RuntimeOutputContractTypeProvider
|
|
72
|
-
> = ResolveAPIContract<
|
|
73
|
-
APIContract,
|
|
74
|
-
RuntimeInputContractTypeProvider,
|
|
75
|
-
RuntimeOutputContractTypeProvider
|
|
76
|
-
>,
|
|
77
|
-
> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {
|
|
78
|
-
_!: ResolvedAPIContract
|
|
79
|
-
#callers = {} as ClientCallers<ResolvedAPIContract>
|
|
55
|
+
SafeCall extends boolean,
|
|
56
|
+
> extends BaseClient<APIContract, SafeCall> {
|
|
57
|
+
protected transformer: RuntimeContractTransformer
|
|
80
58
|
|
|
81
59
|
constructor(
|
|
82
|
-
contract: APIContract,
|
|
83
|
-
|
|
84
|
-
transport: ProtocolTransport
|
|
85
|
-
format: BaseClientFormat
|
|
86
|
-
timeout?: number
|
|
87
|
-
},
|
|
60
|
+
public contract: APIContract,
|
|
61
|
+
...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>
|
|
88
62
|
) {
|
|
89
|
-
super(
|
|
90
|
-
...options,
|
|
91
|
-
transformer: new RuntimeContractTransformer(contract),
|
|
92
|
-
})
|
|
63
|
+
super(...args)
|
|
93
64
|
|
|
94
|
-
|
|
65
|
+
this.transformer = new RuntimeContractTransformer(this.contract)
|
|
95
66
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
namespace.procedures
|
|
67
|
+
const namespaces = Object.entries(this.contract.namespaces)
|
|
68
|
+
for (const [namespaceKey, namespace] of namespaces) {
|
|
69
|
+
this.callers[namespaceKey] = {} as any
|
|
100
70
|
|
|
101
|
-
|
|
71
|
+
const procedures = Object.entries(namespace.procedures)
|
|
102
72
|
|
|
103
|
-
for (const [procedureKey, procedure] of
|
|
104
|
-
|
|
105
|
-
)) {
|
|
106
|
-
callers[namespaceKey][procedureKey] = (payload, options) =>
|
|
73
|
+
for (const [procedureKey, procedure] of procedures) {
|
|
74
|
+
this.callers[namespaceKey][procedureKey] = (payload, options) =>
|
|
107
75
|
this._call(namespace.name, procedure.name, payload, {
|
|
108
|
-
timeout: namespace.timeout,
|
|
76
|
+
timeout: procedure.timeout || namespace.timeout || options.timeout,
|
|
109
77
|
...options,
|
|
110
78
|
})
|
|
111
79
|
}
|
|
112
80
|
}
|
|
113
|
-
this.#callers = callers
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
get call() {
|
|
117
|
-
return this.#callers
|
|
118
81
|
}
|
|
119
82
|
}
|
package/src/static.ts
CHANGED
|
@@ -1,42 +1,21 @@
|
|
|
1
1
|
import type { TAnyAPIContract } from '@nmtjs/contract'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
ProtocolBaseClient,
|
|
5
|
-
type ProtocolTransport,
|
|
6
|
-
} from '@nmtjs/protocol/client'
|
|
7
|
-
|
|
8
|
-
import type {
|
|
9
|
-
ClientCallers,
|
|
10
|
-
ResolveAPIContract,
|
|
11
|
-
ResolveClientEvents,
|
|
12
|
-
StaticInputContractTypeProvider,
|
|
13
|
-
StaticOutputContractTypeProvider,
|
|
14
|
-
} from './types.ts'
|
|
2
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client'
|
|
3
|
+
import { BaseClient } from './common.ts'
|
|
15
4
|
|
|
16
5
|
export class StaticClient<
|
|
17
6
|
APIContract extends TAnyAPIContract,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
StaticOutputContractTypeProvider
|
|
22
|
-
> = ResolveAPIContract<
|
|
23
|
-
APIContract,
|
|
24
|
-
StaticInputContractTypeProvider,
|
|
25
|
-
StaticOutputContractTypeProvider
|
|
26
|
-
>,
|
|
27
|
-
> extends ProtocolBaseClient<ResolveClientEvents<ResolvedAPIContract>> {
|
|
28
|
-
_!: ResolvedAPIContract
|
|
7
|
+
SafeCall extends boolean = false,
|
|
8
|
+
> extends BaseClient<APIContract, SafeCall> {
|
|
9
|
+
protected transformer: ProtocolBaseTransformer
|
|
29
10
|
|
|
30
|
-
|
|
11
|
+
constructor(
|
|
12
|
+
...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>
|
|
13
|
+
) {
|
|
14
|
+
super(...args)
|
|
31
15
|
|
|
32
|
-
|
|
33
|
-
transport: ProtocolTransport
|
|
34
|
-
format: BaseClientFormat
|
|
35
|
-
timeout?: number
|
|
36
|
-
}) {
|
|
37
|
-
super(options)
|
|
16
|
+
this.transformer = new ProtocolBaseTransformer()
|
|
38
17
|
|
|
39
|
-
this
|
|
18
|
+
this.callers = new Proxy(Object(), {
|
|
40
19
|
get: (target, namespace) => {
|
|
41
20
|
// `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`
|
|
42
21
|
// without explicitly calling a function implicitly calls .then() on target
|
|
@@ -49,19 +28,13 @@ export class StaticClient<
|
|
|
49
28
|
// FIXME: this basically makes "then" a reserved word
|
|
50
29
|
if (procedure === 'then') return target
|
|
51
30
|
return (payload, options) =>
|
|
52
|
-
this._call(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
options,
|
|
57
|
-
)
|
|
31
|
+
this._call(namespace as string, procedure as string, payload, {
|
|
32
|
+
...options,
|
|
33
|
+
timeout: options?.timeout ?? this.options.timeout,
|
|
34
|
+
})
|
|
58
35
|
},
|
|
59
36
|
})
|
|
60
37
|
},
|
|
61
38
|
})
|
|
62
39
|
}
|
|
63
|
-
|
|
64
|
-
get call() {
|
|
65
|
-
return this.#callers
|
|
66
|
-
}
|
|
67
40
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { CallTypeProvider, TypeProvider } from '@nmtjs/common'
|
|
1
|
+
import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common'
|
|
2
2
|
import type { TAnyAPIContract } from '@nmtjs/contract'
|
|
3
3
|
import type {
|
|
4
4
|
ProtocolBaseClientCallOptions,
|
|
5
|
+
ProtocolError,
|
|
5
6
|
ProtocolServerStreamInterface,
|
|
6
7
|
} from '@nmtjs/protocol/client'
|
|
7
|
-
import type { InputType, OutputType } from '@nmtjs/protocol/
|
|
8
|
+
import type { InputType, OutputType } from '@nmtjs/protocol/client'
|
|
8
9
|
import type { BaseTypeAny, NeverType, t } from '@nmtjs/type'
|
|
9
10
|
|
|
10
11
|
export interface StaticInputContractTypeProvider extends TypeProvider {
|
|
@@ -54,7 +55,7 @@ export type ResolveAPIContract<
|
|
|
54
55
|
>
|
|
55
56
|
>
|
|
56
57
|
: {
|
|
57
|
-
|
|
58
|
+
result: OutputType<
|
|
58
59
|
CallTypeProvider<
|
|
59
60
|
OutputTypeProvider,
|
|
60
61
|
C['namespaces'][N]['procedures'][P]['output']
|
|
@@ -86,26 +87,43 @@ export type ResolveClientEvents<
|
|
|
86
87
|
C extends ResolveAPIContract = ResolveAPIContract,
|
|
87
88
|
> = {
|
|
88
89
|
[N in keyof C]: {
|
|
89
|
-
[KE in keyof C[N]['events']
|
|
90
|
+
[KE in keyof C[N]['events'] as `${Extract<N, string>}/${Extract<KE, string>}`]: [
|
|
91
|
+
C[N]['events'][KE]['payload'],
|
|
92
|
+
]
|
|
90
93
|
}
|
|
91
|
-
}
|
|
94
|
+
}[keyof C]
|
|
92
95
|
|
|
93
|
-
export type ClientCallers<
|
|
96
|
+
export type ClientCallers<
|
|
97
|
+
Resolved extends ResolveAPIContract,
|
|
98
|
+
SafeCall extends boolean,
|
|
99
|
+
> = {
|
|
94
100
|
[N in keyof Resolved]: {
|
|
95
101
|
[P in keyof Resolved[N]['procedures']]: (
|
|
96
102
|
...args: Resolved[N]['procedures'][P]['input'] extends NeverType
|
|
97
|
-
? [options?: ProtocolBaseClientCallOptions]
|
|
103
|
+
? [data?: undefined, options?: Partial<ProtocolBaseClientCallOptions>]
|
|
98
104
|
: undefined extends t.infer.encoded.input<
|
|
99
105
|
Resolved[N]['procedures'][P]['contract']['input']
|
|
100
106
|
>
|
|
101
107
|
? [
|
|
102
108
|
data?: Resolved[N]['procedures'][P]['input'],
|
|
103
|
-
options?: ProtocolBaseClientCallOptions
|
|
109
|
+
options?: Partial<ProtocolBaseClientCallOptions>,
|
|
104
110
|
]
|
|
105
111
|
: [
|
|
106
112
|
data: Resolved[N]['procedures'][P]['input'],
|
|
107
|
-
options?: ProtocolBaseClientCallOptions
|
|
113
|
+
options?: Partial<ProtocolBaseClientCallOptions>,
|
|
114
|
+
]
|
|
115
|
+
) => SafeCall extends true
|
|
116
|
+
? Promise<
|
|
117
|
+
OneOf<
|
|
118
|
+
[
|
|
119
|
+
{
|
|
120
|
+
error?: undefined
|
|
121
|
+
output: Resolved[N]['procedures'][P]['output']
|
|
122
|
+
},
|
|
123
|
+
{ error: ProtocolError; output?: undefined },
|
|
108
124
|
]
|
|
109
|
-
|
|
125
|
+
>
|
|
126
|
+
>
|
|
127
|
+
: Promise<Resolved[N]['procedures'][P]['output']>
|
|
110
128
|
}
|
|
111
129
|
}
|