@nmtjs/client 0.12.4 → 0.12.6
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.d.ts +32 -0
- package/dist/common.js +42 -38
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.js +58 -55
- package/dist/static.d.ts +7 -0
- package/dist/static.js +28 -18
- package/dist/types.d.ts +70 -0
- package/dist/types.js +0 -2
- package/package.json +16 -13
- package/dist/common.js.map +0 -1
- package/dist/runtime.js.map +0 -1
- package/dist/static.js.map +0 -1
- package/dist/types.js.map +0 -1
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { TAnyAPIContract } from '@nmtjs/contract';
|
|
2
|
+
import { EventEmitter, type ProtocolBaseClientCallOptions, type ProtocolBaseTransformer, ProtocolError, type ProtocolTransport } from '@nmtjs/protocol/client';
|
|
3
|
+
import type { ClientCallers, ResolveAPIContract, ResolveClientEvents, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider } from './types.ts';
|
|
4
|
+
export { ErrorCode, ProtocolBlob, type ProtocolBlobMetadata, TransportType, } from '@nmtjs/protocol';
|
|
5
|
+
export * from './types.ts';
|
|
6
|
+
export declare class ClientError extends ProtocolError {
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class BaseClient<APIContract extends TAnyAPIContract = TAnyAPIContract, SafeCall extends boolean = false, API extends ResolveAPIContract<APIContract, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider> = ResolveAPIContract<APIContract, RuntimeInputContractTypeProvider, RuntimeOutputContractTypeProvider>> extends EventEmitter<ResolveClientEvents<API>> {
|
|
9
|
+
readonly transport: ProtocolTransport;
|
|
10
|
+
readonly options: {
|
|
11
|
+
timeout: number;
|
|
12
|
+
autoreconnect?: boolean;
|
|
13
|
+
safe?: SafeCall;
|
|
14
|
+
};
|
|
15
|
+
_: {
|
|
16
|
+
api: API;
|
|
17
|
+
safe: SafeCall;
|
|
18
|
+
};
|
|
19
|
+
protected abstract transformer: ProtocolBaseTransformer;
|
|
20
|
+
protected callers: ClientCallers<API, SafeCall>;
|
|
21
|
+
protected auth: any;
|
|
22
|
+
constructor(transport: ProtocolTransport, options: {
|
|
23
|
+
timeout: number;
|
|
24
|
+
autoreconnect?: boolean;
|
|
25
|
+
safe?: SafeCall;
|
|
26
|
+
});
|
|
27
|
+
protected _call(namespace: string, procedure: string, payload: any, options: ProtocolBaseClientCallOptions): Promise<any>;
|
|
28
|
+
get call(): ClientCallers<API, SafeCall>;
|
|
29
|
+
setAuth(auth: any): void;
|
|
30
|
+
connect(): Promise<void>;
|
|
31
|
+
disconnect(): Promise<void>;
|
|
32
|
+
}
|
package/dist/common.js
CHANGED
|
@@ -1,41 +1,45 @@
|
|
|
1
|
-
import { EventEmitter, ProtocolError } from
|
|
2
|
-
export { ErrorCode, ProtocolBlob, TransportType } from
|
|
1
|
+
import { EventEmitter, ProtocolError, } from '@nmtjs/protocol/client';
|
|
2
|
+
export { ErrorCode, ProtocolBlob, TransportType, } from '@nmtjs/protocol';
|
|
3
3
|
export * from "./types.js";
|
|
4
|
-
export class ClientError extends ProtocolError {
|
|
4
|
+
export class ClientError extends ProtocolError {
|
|
5
|
+
}
|
|
5
6
|
export class BaseClient extends EventEmitter {
|
|
6
|
-
|
|
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
|
-
|
|
7
|
+
transport;
|
|
8
|
+
options;
|
|
9
|
+
_;
|
|
10
|
+
callers;
|
|
11
|
+
auth;
|
|
12
|
+
constructor(transport, options) {
|
|
13
|
+
super();
|
|
14
|
+
this.transport = transport;
|
|
15
|
+
this.options = options;
|
|
16
|
+
if (this.options.autoreconnect) {
|
|
17
|
+
this.transport.on('disconnected', () => setTimeout(this.connect.bind(this), 1000));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async _call(namespace, procedure, payload, options) {
|
|
21
|
+
const call = await this.transport.call(namespace, procedure, payload, options, this.transformer);
|
|
22
|
+
if (this.options.safe) {
|
|
23
|
+
return await call.promise
|
|
24
|
+
.then((result) => ({ result }))
|
|
25
|
+
.catch((error) => ({ error }));
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return await call.promise.catch((error) => {
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
get call() {
|
|
34
|
+
return this.callers;
|
|
35
|
+
}
|
|
36
|
+
setAuth(auth) {
|
|
37
|
+
this.auth = auth;
|
|
38
|
+
}
|
|
39
|
+
connect() {
|
|
40
|
+
return this.transport.connect(this.auth, this.transformer);
|
|
41
|
+
}
|
|
42
|
+
disconnect() {
|
|
43
|
+
return this.transport.disconnect();
|
|
44
|
+
}
|
|
39
45
|
}
|
|
40
|
-
|
|
41
|
-
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TAnyAPIContract } from '@nmtjs/contract';
|
|
2
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client';
|
|
3
|
+
import { BaseClient } from './common.ts';
|
|
4
|
+
export declare class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
5
|
+
protected contract: TAnyAPIContract;
|
|
6
|
+
constructor(contract: TAnyAPIContract);
|
|
7
|
+
decodeEvent(namespace: string, event: string, payload: any): unknown;
|
|
8
|
+
decodeRPC(namespace: string, procedure: string, payload: any): unknown;
|
|
9
|
+
decodeRPCChunk(namespace: string, procedure: string, payload: any): unknown;
|
|
10
|
+
encodeRPC(namespace: string, procedure: string, payload: any): unknown;
|
|
11
|
+
}
|
|
12
|
+
export declare class RuntimeClient<APIContract extends TAnyAPIContract, SafeCall extends boolean> extends BaseClient<APIContract, SafeCall> {
|
|
13
|
+
contract: APIContract;
|
|
14
|
+
protected transformer: RuntimeContractTransformer;
|
|
15
|
+
constructor(contract: APIContract, ...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>);
|
|
16
|
+
}
|
package/dist/runtime.js
CHANGED
|
@@ -1,60 +1,63 @@
|
|
|
1
|
-
import { ErrorCode } from
|
|
2
|
-
import { ProtocolBaseTransformer } from
|
|
3
|
-
import { NeemataTypeError, t } from
|
|
1
|
+
import { ErrorCode } from '@nmtjs/protocol';
|
|
2
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client';
|
|
3
|
+
import { NeemataTypeError, t } from '@nmtjs/type';
|
|
4
4
|
import { BaseClient, ClientError } from "./common.js";
|
|
5
5
|
export class RuntimeContractTransformer extends ProtocolBaseTransformer {
|
|
6
|
-
|
|
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
|
-
|
|
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 t.NeverType)
|
|
18
|
+
return undefined;
|
|
19
|
+
return type.decode(payload);
|
|
20
|
+
}
|
|
21
|
+
decodeRPCChunk(namespace, procedure, payload) {
|
|
22
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].stream;
|
|
23
|
+
if (!type || type instanceof t.NeverType)
|
|
24
|
+
return undefined;
|
|
25
|
+
return type.decode(payload);
|
|
26
|
+
}
|
|
27
|
+
encodeRPC(namespace, procedure, payload) {
|
|
28
|
+
const type = this.contract.namespaces[namespace].procedures[procedure].input;
|
|
29
|
+
if (type instanceof t.NeverType)
|
|
30
|
+
return undefined;
|
|
31
|
+
try {
|
|
32
|
+
return type.encode(payload);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof NeemataTypeError) {
|
|
36
|
+
throw new ClientError(ErrorCode.ValidationError, `Invalid payload for ${namespace}.${procedure}: ${error.message}`, error.issues);
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
37
41
|
}
|
|
38
42
|
export class RuntimeClient extends BaseClient {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
contract;
|
|
44
|
+
transformer;
|
|
45
|
+
constructor(contract, ...args) {
|
|
46
|
+
super(...args);
|
|
47
|
+
this.contract = contract;
|
|
48
|
+
this.transformer = new RuntimeContractTransformer(this.contract);
|
|
49
|
+
const callers = {};
|
|
50
|
+
const namespaces = Object.entries(this.contract.namespaces);
|
|
51
|
+
for (const [namespaceKey, namespace] of namespaces) {
|
|
52
|
+
callers[namespaceKey] = {};
|
|
53
|
+
const procedures = Object.entries(namespace.procedures);
|
|
54
|
+
for (const [procedureKey, procedure] of procedures) {
|
|
55
|
+
callers[namespaceKey][procedureKey] = (payload, options) => this._call(namespace.name, procedure.name, payload, {
|
|
56
|
+
timeout: procedure.timeout || namespace.timeout || options.timeout,
|
|
57
|
+
...options,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.callers = callers;
|
|
62
|
+
}
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
//# sourceMappingURL=runtime.js.map
|
package/dist/static.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { TAnyAPIContract } from '@nmtjs/contract';
|
|
2
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client';
|
|
3
|
+
import { BaseClient } from './common.ts';
|
|
4
|
+
export declare class StaticClient<APIContract extends TAnyAPIContract, SafeCall extends boolean = false> extends BaseClient<APIContract, SafeCall> {
|
|
5
|
+
protected transformer: ProtocolBaseTransformer;
|
|
6
|
+
constructor(...args: ConstructorParameters<typeof BaseClient<APIContract, SafeCall>>);
|
|
7
|
+
}
|
package/dist/static.js
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
import { ProtocolBaseTransformer } from
|
|
1
|
+
import { ProtocolBaseTransformer } from '@nmtjs/protocol/client';
|
|
2
2
|
import { BaseClient } from "./common.js";
|
|
3
3
|
export class StaticClient extends BaseClient {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
4
|
+
transformer;
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(...args);
|
|
7
|
+
this.transformer = new ProtocolBaseTransformer();
|
|
8
|
+
this.callers = new Proxy(Object(), {
|
|
9
|
+
get: (target, namespace) => {
|
|
10
|
+
// `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`
|
|
11
|
+
// without explicitly calling a function implicitly calls .then() on target
|
|
12
|
+
// FIXME: this basically makes "then" a reserved word
|
|
13
|
+
if (namespace === 'then')
|
|
14
|
+
return target;
|
|
15
|
+
return new Proxy(Object(), {
|
|
16
|
+
get: (target, procedure) => {
|
|
17
|
+
// `await client.call.namespaceName` or `await client.call.namespaceName.procedureName`
|
|
18
|
+
// without explicitly calling a function implicitly calls .then() on target
|
|
19
|
+
// FIXME: this basically makes "then" a reserved word
|
|
20
|
+
if (procedure === 'then')
|
|
21
|
+
return target;
|
|
22
|
+
return (payload, options) => this._call(namespace, procedure, payload, {
|
|
23
|
+
...options,
|
|
24
|
+
timeout: options?.timeout ?? this.options.timeout,
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
19
31
|
}
|
|
20
|
-
|
|
21
|
-
//# sourceMappingURL=static.js.map
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { CallTypeProvider, OneOf, TypeProvider } from '@nmtjs/common';
|
|
2
|
+
import type { TAnyAPIContract, TAnyProcedureContract } from '@nmtjs/contract';
|
|
3
|
+
import type { InputType, OutputType, ProtocolBaseClientCallOptions, ProtocolError, ProtocolServerStreamInterface } from '@nmtjs/protocol/client';
|
|
4
|
+
import type { BaseTypeAny, t } from '@nmtjs/type';
|
|
5
|
+
export interface StaticInputContractTypeProvider extends TypeProvider {
|
|
6
|
+
output: this['input'] extends BaseTypeAny ? t.infer.encoded.input<this['input']> : never;
|
|
7
|
+
}
|
|
8
|
+
export interface RuntimeInputContractTypeProvider extends TypeProvider {
|
|
9
|
+
output: this['input'] extends BaseTypeAny ? t.infer.decoded.input<this['input']> : never;
|
|
10
|
+
}
|
|
11
|
+
export interface StaticOutputContractTypeProvider extends TypeProvider {
|
|
12
|
+
output: this['input'] extends BaseTypeAny ? t.infer.encoded.output<this['input']> : never;
|
|
13
|
+
}
|
|
14
|
+
export interface RuntimeOutputContractTypeProvider extends TypeProvider {
|
|
15
|
+
output: this['input'] extends BaseTypeAny ? t.infer.decoded.output<this['input']> : never;
|
|
16
|
+
}
|
|
17
|
+
export type AnyResolvedAPIContract = Record<string, {
|
|
18
|
+
procedures: Record<string, {
|
|
19
|
+
contract: TAnyProcedureContract;
|
|
20
|
+
input: any;
|
|
21
|
+
output: any;
|
|
22
|
+
}>;
|
|
23
|
+
events: Record<string, {
|
|
24
|
+
payload: any;
|
|
25
|
+
}>;
|
|
26
|
+
}>;
|
|
27
|
+
export type ResolveAPIContract<C extends TAnyAPIContract = TAnyAPIContract, InputTypeProvider extends TypeProvider = TypeProvider, OutputTypeProvider extends TypeProvider = TypeProvider> = {
|
|
28
|
+
[N in keyof C['namespaces']]: {
|
|
29
|
+
procedures: {
|
|
30
|
+
[P in keyof C['namespaces'][N]['procedures']]: {
|
|
31
|
+
contract: C['namespaces'][N]['procedures'][P];
|
|
32
|
+
input: InputType<CallTypeProvider<InputTypeProvider, C['namespaces'][N]['procedures'][P]['input']>>;
|
|
33
|
+
output: C['namespaces'][N]['procedures'][P]['stream'] extends undefined | t.NeverType ? OutputType<CallTypeProvider<OutputTypeProvider, C['namespaces'][N]['procedures'][P]['output']>> : {
|
|
34
|
+
result: OutputType<CallTypeProvider<OutputTypeProvider, C['namespaces'][N]['procedures'][P]['output']>>;
|
|
35
|
+
stream: ProtocolServerStreamInterface<CallTypeProvider<OutputTypeProvider, C['namespaces'][N]['procedures'][P]['stream']>>;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
events: {
|
|
40
|
+
[KE in keyof C['namespaces'][N]['events']]: {
|
|
41
|
+
payload: OutputType<CallTypeProvider<OutputTypeProvider, C['namespaces'][N]['events'][KE]['payload']>>;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export type ResolveClientEvents<C extends AnyResolvedAPIContract = AnyResolvedAPIContract> = {
|
|
47
|
+
[N in keyof C]: {
|
|
48
|
+
[E in keyof C[N]['events'] as `${Extract<N, string>}/${Extract<E, string>}`]: [
|
|
49
|
+
C[N]['events'][E]['payload']
|
|
50
|
+
];
|
|
51
|
+
};
|
|
52
|
+
}[keyof C];
|
|
53
|
+
export type ClientCallers<Resolved extends AnyResolvedAPIContract, SafeCall extends boolean> = {
|
|
54
|
+
[N in keyof Resolved]: {
|
|
55
|
+
[P in keyof Resolved[N]['procedures']]: (...args: Resolved[N]['procedures'][P]['input'] extends t.NeverType ? [data?: undefined, options?: Partial<ProtocolBaseClientCallOptions>] : undefined extends t.infer.encoded.input<Resolved[N]['procedures'][P]['contract']['input']> ? [
|
|
56
|
+
data?: Resolved[N]['procedures'][P]['input'],
|
|
57
|
+
options?: Partial<ProtocolBaseClientCallOptions>
|
|
58
|
+
] : [
|
|
59
|
+
data: Resolved[N]['procedures'][P]['input'],
|
|
60
|
+
options?: Partial<ProtocolBaseClientCallOptions>
|
|
61
|
+
]) => SafeCall extends true ? Promise<OneOf<[
|
|
62
|
+
{
|
|
63
|
+
output: Resolved[N]['procedures'][P]['output'];
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
error: ProtocolError;
|
|
67
|
+
}
|
|
68
|
+
]>> : Promise<Resolved[N]['procedures'][P]['output']>;
|
|
69
|
+
};
|
|
70
|
+
};
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -3,29 +3,32 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
|
+
"types": "./dist/common.d.ts",
|
|
6
7
|
"import": "./dist/common.js",
|
|
7
|
-
"
|
|
8
|
+
"module-sync": "./dist/common.js"
|
|
8
9
|
},
|
|
9
10
|
"./runtime": {
|
|
11
|
+
"types": "./dist/runtime.d.ts",
|
|
10
12
|
"import": "./dist/runtime.js",
|
|
11
|
-
"
|
|
13
|
+
"module-sync": "./dist/runtime.js"
|
|
12
14
|
},
|
|
13
15
|
"./static": {
|
|
16
|
+
"types": "./dist/static.d.ts",
|
|
14
17
|
"import": "./dist/static.js",
|
|
15
|
-
"
|
|
18
|
+
"module-sync": "./dist/static.js"
|
|
16
19
|
}
|
|
17
20
|
},
|
|
18
21
|
"peerDependencies": {
|
|
19
|
-
"@nmtjs/type": "0.12.
|
|
20
|
-
"@nmtjs/
|
|
21
|
-
"@nmtjs/
|
|
22
|
-
"@nmtjs/protocol": "0.12.
|
|
22
|
+
"@nmtjs/type": "0.12.6",
|
|
23
|
+
"@nmtjs/contract": "0.12.6",
|
|
24
|
+
"@nmtjs/common": "0.12.6",
|
|
25
|
+
"@nmtjs/protocol": "0.12.6"
|
|
23
26
|
},
|
|
24
27
|
"devDependencies": {
|
|
25
|
-
"@nmtjs/
|
|
26
|
-
"@nmtjs/
|
|
27
|
-
"@nmtjs/
|
|
28
|
-
"@nmtjs/protocol": "0.12.
|
|
28
|
+
"@nmtjs/type": "0.12.6",
|
|
29
|
+
"@nmtjs/common": "0.12.6",
|
|
30
|
+
"@nmtjs/contract": "0.12.6",
|
|
31
|
+
"@nmtjs/protocol": "0.12.6"
|
|
29
32
|
},
|
|
30
33
|
"files": [
|
|
31
34
|
"src",
|
|
@@ -33,9 +36,9 @@
|
|
|
33
36
|
"LICENSE.md",
|
|
34
37
|
"README.md"
|
|
35
38
|
],
|
|
36
|
-
"version": "0.12.
|
|
39
|
+
"version": "0.12.6",
|
|
37
40
|
"scripts": {
|
|
38
|
-
"build": "
|
|
41
|
+
"build": "tsc",
|
|
39
42
|
"type-check": "tsc --noEmit"
|
|
40
43
|
}
|
|
41
44
|
}
|
package/dist/common.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,YACWA,WACAC,SAKT;AACA,SAAO;OAPE;OACA;AAQT,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 readonly transport: ProtocolTransport,\n readonly 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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/static.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|