@nmtjs/client 0.0.3 → 0.1.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/lib/client-runtime.js +20 -18
- package/dist/lib/client-runtime.js.map +1 -1
- package/dist/lib/client-static.js.map +1 -1
- package/dist/lib/subscription.js.map +1 -1
- package/lib/client-runtime.ts +43 -37
- package/lib/client-static.ts +17 -14
- package/lib/subscription.ts +3 -6
- package/package.json +7 -5
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { NeverType } from '@nmtjs/type';
|
|
2
|
+
import { compile } from '@nmtjs/type/compiler';
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
4
|
export class RuntimeClient extends Client {
|
|
5
5
|
#callers;
|
|
6
6
|
constructor(services, options){
|
|
7
7
|
super(options, Object.values(services).map((s)=>s.contract.name));
|
|
8
8
|
const callers = {};
|
|
9
|
-
for (const [serviceKey,
|
|
10
|
-
|
|
9
|
+
for (const [serviceKey, service] of Object.entries(services)){
|
|
10
|
+
service.contract.procedures;
|
|
11
|
+
if (!service.contract.transports[this.transport.type]) throw new Error(`Transport [${this.transport.type}] not supported for service [${service.contract.name}]`);
|
|
11
12
|
callers[serviceKey] = {};
|
|
12
|
-
for(const procedureName in
|
|
13
|
-
const { input, output } =
|
|
14
|
-
callers[serviceKey][procedureName] = this.createCaller(
|
|
15
|
-
timeout:
|
|
13
|
+
for(const procedureName in service.contract.procedures){
|
|
14
|
+
const { input, output } = service.contract.procedures[procedureName];
|
|
15
|
+
callers[serviceKey][procedureName] = this.createCaller(service.contract.name, procedureName, {
|
|
16
|
+
timeout: service.contract.timeout,
|
|
16
17
|
transformInput: (data)=>{
|
|
17
|
-
if (
|
|
18
|
-
const compiled =
|
|
18
|
+
if (input instanceof NeverType) return undefined;
|
|
19
|
+
const compiled = service.compiled.get(input);
|
|
19
20
|
const result = compiled.encode(data);
|
|
20
21
|
if (result.success) {
|
|
21
22
|
return result.value;
|
|
@@ -27,8 +28,8 @@ export class RuntimeClient extends Client {
|
|
|
27
28
|
}
|
|
28
29
|
},
|
|
29
30
|
transformOutput: (data)=>{
|
|
30
|
-
if (
|
|
31
|
-
const compiled =
|
|
31
|
+
if (output instanceof NeverType) return undefined;
|
|
32
|
+
const compiled = service.compiled.get(output);
|
|
32
33
|
const result = compiled.decode(data);
|
|
33
34
|
if (result.success) {
|
|
34
35
|
return result.value;
|
|
@@ -50,18 +51,19 @@ export class RuntimeClient extends Client {
|
|
|
50
51
|
}
|
|
51
52
|
export const compileContract = (contract)=>{
|
|
52
53
|
const compiled = new Map();
|
|
53
|
-
for (const
|
|
54
|
-
const { input, output
|
|
55
|
-
if (
|
|
54
|
+
for (const procedure of Object.values(contract.procedures)){
|
|
55
|
+
const { input, output } = procedure;
|
|
56
|
+
if (procedure.type === 'neemata:subscription') {
|
|
57
|
+
const { events } = procedure;
|
|
56
58
|
for (const event of Object.values(events)){
|
|
57
|
-
compiled.set(event, compile(event));
|
|
59
|
+
compiled.set(event.payload, compile(event.payload));
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
compiled.set(input, compile(input));
|
|
61
63
|
compiled.set(output, compile(output));
|
|
62
64
|
}
|
|
63
|
-
for (const
|
|
64
|
-
compiled.set(
|
|
65
|
+
for (const event of Object.values(contract.events)){
|
|
66
|
+
compiled.set(event.payload, compile(event.payload));
|
|
65
67
|
}
|
|
66
68
|
return {
|
|
67
69
|
compiled,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/client-runtime.ts"],"sourcesContent":["import type {\n
|
|
1
|
+
{"version":3,"sources":["../../../lib/client-runtime.ts"],"sourcesContent":["import type {\n TEventContract,\n TProcedureContract,\n TServiceContract,\n TSubscriptionContract,\n} from '@nmtjs/contract'\nimport { type BaseType, NeverType, type t } from '@nmtjs/type'\nimport { type Compiled, compile } from '@nmtjs/type/compiler'\n\nimport { Client, type ClientOptions } from './client.ts'\nimport type { Subscription } from './subscription.ts'\nimport type { ClientCallOptions, InputType, OutputType } from './types.ts'\n\ntype CompiledContract<T extends TServiceContract = TServiceContract> = {\n compiled: Map<BaseType, Compiled>\n contract: T\n}\n\ntype ClientServices = Record<string, CompiledContract>\n\ntype ClientCallers<Services extends ClientServices> = {\n [K in keyof Services]: {\n [P in keyof Services[K]['contract']['procedures']]: (\n ...args: Services[K]['contract']['procedures'][P]['input'] extends NeverType\n ? [options?: ClientCallOptions]\n : [\n data: InputType<\n t.infer.decoded<Services[K]['contract']['procedures'][P]['input']>\n >,\n options?: ClientCallOptions,\n ]\n ) => Promise<\n Services[K]['contract']['procedures'][P] extends TSubscriptionContract\n ? {\n payload: Services[K]['contract']['procedures'][P]['output'] extends NeverType\n ? undefined\n : t.infer.decoded<\n Services[K]['contract']['procedures'][P]['output']\n >\n subscription: Subscription<{\n [KE in keyof Services[K]['contract']['procedures'][P]['events']]: [\n t.infer.decoded<\n Services[K]['contract']['procedures'][P]['events'][KE]['payload']\n >,\n ]\n }>\n }\n : Services[K]['contract']['procedures'][P]['output'] extends NeverType\n ? void\n : OutputType<\n t.infer.decoded<\n Services[K]['contract']['procedures'][P]['output']\n >\n >\n >\n }\n}\n\nexport class RuntimeClient<Services extends ClientServices> extends Client {\n #callers: ClientCallers<Services>\n\n constructor(services: Services, options: ClientOptions) {\n super(\n options,\n Object.values(services).map((s) => s.contract.name),\n )\n\n const callers = {} as any\n for (const [serviceKey, service] of Object.entries(services)) {\n service.contract.procedures\n if (!service.contract.transports[this.transport.type])\n throw new Error(\n `Transport [${this.transport.type}] not supported for service [${service.contract.name}]`,\n )\n\n callers[serviceKey] = {} as any\n\n for (const procedureName in service.contract.procedures) {\n const { input, output } = service.contract.procedures[procedureName]\n\n callers[serviceKey][procedureName] = this.createCaller(\n service.contract.name,\n procedureName,\n {\n timeout: service.contract.timeout,\n transformInput: (data: any) => {\n if (input instanceof NeverType) return undefined\n const compiled = service.compiled.get(input)!\n const result = compiled.encode(data)\n if (result.success) {\n return result.value\n } else {\n console.dir(result.error)\n throw new Error('Failed to encode input', {\n cause: result.error,\n })\n }\n },\n transformOutput: (data: any) => {\n if (output instanceof NeverType) return undefined\n const compiled = service.compiled.get(output)!\n const result = compiled.decode(data)\n if (result.success) {\n return result.value\n } else {\n console.dir(result.error)\n throw new Error('Failed to decode output', {\n cause: result.error,\n })\n }\n },\n },\n )\n }\n }\n this.#callers = callers\n }\n\n get call() {\n return this.#callers\n }\n}\n\nexport const compileContract = <T extends TServiceContract>(\n contract: T,\n): CompiledContract<T> => {\n const compiled = new Map<BaseType, Compiled>()\n\n for (const procedure of Object.values(contract.procedures)) {\n const { input, output } = procedure\n if (procedure.type === 'neemata:subscription') {\n const { events } = procedure\n for (const event of Object.values(events) as TEventContract[]) {\n compiled.set(event.payload, compile(event.payload))\n }\n }\n compiled.set(input, compile(input))\n compiled.set(output, compile(output))\n }\n for (const event of Object.values(contract.events)) {\n compiled.set(event.payload, compile(event.payload))\n }\n\n return {\n compiled,\n contract,\n }\n}\n"],"names":["NeverType","compile","Client","RuntimeClient","constructor","services","options","Object","values","map","s","contract","name","callers","serviceKey","service","entries","procedures","transports","transport","type","Error","procedureName","input","output","createCaller","timeout","transformInput","data","undefined","compiled","get","result","encode","success","value","console","dir","error","cause","transformOutput","decode","call","compileContract","Map","procedure","events","event","set","payload"],"mappings":"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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/client-static.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../../lib/client-static.ts"],"sourcesContent":["import type { TServiceContract, TSubscriptionContract } from '@nmtjs/contract'\nimport type { NeverType, t } from '@nmtjs/type'\nimport { Client, type ClientOptions } from './client.ts'\nimport type { Subscription } from './subscription.ts'\nimport type { ClientCallOptions, InputType, OutputType } from './types.ts'\n\ntype ClientServices = Record<string, TServiceContract>\n\ntype ClientCallers<Services extends ClientServices> = {\n [K in keyof Services]: {\n [P in keyof Services[K]['procedures']]: (\n ...args: Services[K]['procedures'][P]['input'] extends NeverType\n ? [options?: ClientCallOptions]\n : [\n data: InputType<\n t.infer.encoded<Services[K]['procedures'][P]['input']>\n >,\n options?: ClientCallOptions,\n ]\n ) => Promise<\n Services[K]['procedures'][P] extends TSubscriptionContract\n ? {\n payload: Services[K]['procedures'][P]['output'] extends NeverType\n ? undefined\n : t.infer.encoded<Services[K]['procedures'][P]['output']>\n subscription: Subscription<{\n [KE in keyof Services[K]['procedures'][P]['events']]: [\n t.infer.encoded<\n Services[K]['procedures'][P]['events'][KE]['payload']\n >,\n ]\n }>\n }\n : Services[K]['procedures'][P]['output'] extends NeverType\n ? void\n : OutputType<t.infer.encoded<Services[K]['procedures'][P]['output']>>\n >\n }\n}\n\nexport class StaticClient<Services extends ClientServices> extends Client {\n #callers: ClientCallers<Services>\n\n constructor(\n services: { [K in keyof Services]: Services[K]['name'] },\n options: ClientOptions,\n ) {\n super(options, Object.values(services))\n\n const callers = {} as any\n\n for (const [serviceKey, serviceName] of Object.entries(services)) {\n callers[serviceKey] = new Proxy(Object(), {\n get: (target, prop, receiver) => {\n return this.createCaller(serviceName, prop as string)\n },\n })\n }\n\n this.#callers = callers\n }\n\n get call() {\n return this.#callers\n }\n}\n"],"names":["Client","StaticClient","constructor","services","options","Object","values","callers","serviceKey","serviceName","entries","Proxy","get","target","prop","receiver","createCaller","call"],"mappings":"AAEA,SAASA,MAAM,QAA4B,cAAa;AAsCxD,OAAO,MAAMC,qBAAsDD;IACjE,CAAA,OAAQ,CAAyB;IAEjCE,YACEC,QAAwD,EACxDC,OAAsB,CACtB;QACA,KAAK,CAACA,SAASC,OAAOC,MAAM,CAACH;QAE7B,MAAMI,UAAU,CAAC;QAEjB,KAAK,MAAM,CAACC,YAAYC,YAAY,IAAIJ,OAAOK,OAAO,CAACP,UAAW;YAChEI,OAAO,CAACC,WAAW,GAAG,IAAIG,MAAMN,UAAU;gBACxCO,KAAK,CAACC,QAAQC,MAAMC;oBAClB,OAAO,IAAI,CAACC,YAAY,CAACP,aAAaK;gBACxC;YACF;QACF;QAEA,IAAI,CAAC,CAAA,OAAQ,GAAGP;IAClB;IAEA,IAAIU,OAAO;QACT,OAAO,IAAI,CAAC,CAAA,OAAQ;IACtB;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/subscription.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../lib/subscription.ts"],"sourcesContent":["import { EventEmitter, type EventMap } from './utils.ts'\n\nexport class Subscription<\n Events extends EventMap = EventMap,\n> extends EventEmitter<Events> {\n constructor(\n readonly key: string,\n readonly unsubscribe: () => void,\n ) {\n super()\n }\n}\n"],"names":["EventEmitter","Subscription","constructor","key","unsubscribe"],"mappings":"AAAA,SAASA,YAAY,QAAuB,aAAY;AAExD,OAAO,MAAMC,qBAEHD;;;IACRE,YACE,AAASC,GAAW,EACpB,AAASC,WAAuB,CAChC;QACA,KAAK;aAHID,MAAAA;aACAC,cAAAA;IAGX;AACF"}
|
package/lib/client-runtime.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
Decoded,
|
|
3
2
|
TEventContract,
|
|
4
|
-
|
|
3
|
+
TProcedureContract,
|
|
5
4
|
TServiceContract,
|
|
6
5
|
TSubscriptionContract,
|
|
7
6
|
} from '@nmtjs/contract'
|
|
8
|
-
import { type
|
|
9
|
-
import {
|
|
7
|
+
import { type BaseType, NeverType, type t } from '@nmtjs/type'
|
|
8
|
+
import { type Compiled, compile } from '@nmtjs/type/compiler'
|
|
9
|
+
|
|
10
10
|
import { Client, type ClientOptions } from './client.ts'
|
|
11
11
|
import type { Subscription } from './subscription.ts'
|
|
12
12
|
import type { ClientCallOptions, InputType, OutputType } from './types.ts'
|
|
13
13
|
|
|
14
14
|
type CompiledContract<T extends TServiceContract = TServiceContract> = {
|
|
15
|
-
compiled: Map<
|
|
15
|
+
compiled: Map<BaseType, Compiled>
|
|
16
16
|
contract: T
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -21,32 +21,36 @@ type ClientServices = Record<string, CompiledContract>
|
|
|
21
21
|
type ClientCallers<Services extends ClientServices> = {
|
|
22
22
|
[K in keyof Services]: {
|
|
23
23
|
[P in keyof Services[K]['contract']['procedures']]: (
|
|
24
|
-
...args:
|
|
25
|
-
Services[K]['contract']['procedures'][P]['input']
|
|
26
|
-
> extends never
|
|
24
|
+
...args: Services[K]['contract']['procedures'][P]['input'] extends NeverType
|
|
27
25
|
? [options?: ClientCallOptions]
|
|
28
26
|
: [
|
|
29
27
|
data: InputType<
|
|
30
|
-
|
|
28
|
+
t.infer.decoded<Services[K]['contract']['procedures'][P]['input']>
|
|
31
29
|
>,
|
|
32
30
|
options?: ClientCallOptions,
|
|
33
31
|
]
|
|
34
32
|
) => Promise<
|
|
35
33
|
Services[K]['contract']['procedures'][P] extends TSubscriptionContract
|
|
36
34
|
? {
|
|
37
|
-
payload:
|
|
38
|
-
Services[K]['contract']['procedures'][P]['output']
|
|
39
|
-
> extends never
|
|
35
|
+
payload: Services[K]['contract']['procedures'][P]['output'] extends NeverType
|
|
40
36
|
? undefined
|
|
41
|
-
:
|
|
42
|
-
|
|
37
|
+
: t.infer.decoded<
|
|
38
|
+
Services[K]['contract']['procedures'][P]['output']
|
|
39
|
+
>
|
|
40
|
+
subscription: Subscription<{
|
|
41
|
+
[KE in keyof Services[K]['contract']['procedures'][P]['events']]: [
|
|
42
|
+
t.infer.decoded<
|
|
43
|
+
Services[K]['contract']['procedures'][P]['events'][KE]['payload']
|
|
44
|
+
>,
|
|
45
|
+
]
|
|
46
|
+
}>
|
|
43
47
|
}
|
|
44
|
-
:
|
|
45
|
-
Services[K]['contract']['procedures'][P]['static']
|
|
46
|
-
> extends never
|
|
48
|
+
: Services[K]['contract']['procedures'][P]['output'] extends NeverType
|
|
47
49
|
? void
|
|
48
50
|
: OutputType<
|
|
49
|
-
|
|
51
|
+
t.infer.decoded<
|
|
52
|
+
Services[K]['contract']['procedures'][P]['output']
|
|
53
|
+
>
|
|
50
54
|
>
|
|
51
55
|
>
|
|
52
56
|
}
|
|
@@ -62,26 +66,26 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
62
66
|
)
|
|
63
67
|
|
|
64
68
|
const callers = {} as any
|
|
65
|
-
for (const [serviceKey,
|
|
66
|
-
|
|
69
|
+
for (const [serviceKey, service] of Object.entries(services)) {
|
|
70
|
+
service.contract.procedures
|
|
71
|
+
if (!service.contract.transports[this.transport.type])
|
|
67
72
|
throw new Error(
|
|
68
|
-
`Transport [${this.transport.type}] not supported for service [${
|
|
73
|
+
`Transport [${this.transport.type}] not supported for service [${service.contract.name}]`,
|
|
69
74
|
)
|
|
70
75
|
|
|
71
76
|
callers[serviceKey] = {} as any
|
|
72
77
|
|
|
73
|
-
for (const procedureName in
|
|
74
|
-
const { input, output } =
|
|
75
|
-
serviceContract.contract.procedures[procedureName]
|
|
78
|
+
for (const procedureName in service.contract.procedures) {
|
|
79
|
+
const { input, output } = service.contract.procedures[procedureName]
|
|
76
80
|
|
|
77
81
|
callers[serviceKey][procedureName] = this.createCaller(
|
|
78
|
-
|
|
82
|
+
service.contract.name,
|
|
79
83
|
procedureName,
|
|
80
84
|
{
|
|
81
|
-
timeout:
|
|
85
|
+
timeout: service.contract.timeout,
|
|
82
86
|
transformInput: (data: any) => {
|
|
83
|
-
if (
|
|
84
|
-
const compiled =
|
|
87
|
+
if (input instanceof NeverType) return undefined
|
|
88
|
+
const compiled = service.compiled.get(input)!
|
|
85
89
|
const result = compiled.encode(data)
|
|
86
90
|
if (result.success) {
|
|
87
91
|
return result.value
|
|
@@ -93,8 +97,8 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
93
97
|
}
|
|
94
98
|
},
|
|
95
99
|
transformOutput: (data: any) => {
|
|
96
|
-
if (
|
|
97
|
-
const compiled =
|
|
100
|
+
if (output instanceof NeverType) return undefined
|
|
101
|
+
const compiled = service.compiled.get(output)!
|
|
98
102
|
const result = compiled.decode(data)
|
|
99
103
|
if (result.success) {
|
|
100
104
|
return result.value
|
|
@@ -120,19 +124,21 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
120
124
|
export const compileContract = <T extends TServiceContract>(
|
|
121
125
|
contract: T,
|
|
122
126
|
): CompiledContract<T> => {
|
|
123
|
-
const compiled = new Map<
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
const compiled = new Map<BaseType, Compiled>()
|
|
128
|
+
|
|
129
|
+
for (const procedure of Object.values(contract.procedures)) {
|
|
130
|
+
const { input, output } = procedure
|
|
131
|
+
if (procedure.type === 'neemata:subscription') {
|
|
132
|
+
const { events } = procedure
|
|
127
133
|
for (const event of Object.values(events) as TEventContract[]) {
|
|
128
|
-
compiled.set(event, compile(event))
|
|
134
|
+
compiled.set(event.payload, compile(event.payload))
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
137
|
compiled.set(input, compile(input))
|
|
132
138
|
compiled.set(output, compile(output))
|
|
133
139
|
}
|
|
134
|
-
for (const
|
|
135
|
-
compiled.set(
|
|
140
|
+
for (const event of Object.values(contract.events)) {
|
|
141
|
+
compiled.set(event.payload, compile(event.payload))
|
|
136
142
|
}
|
|
137
143
|
|
|
138
144
|
return {
|
package/lib/client-static.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
TServiceContract,
|
|
4
|
-
TSubscriptionContract,
|
|
5
|
-
} from '@nmtjs/contract'
|
|
1
|
+
import type { TServiceContract, TSubscriptionContract } from '@nmtjs/contract'
|
|
2
|
+
import type { NeverType, t } from '@nmtjs/type'
|
|
6
3
|
import { Client, type ClientOptions } from './client.ts'
|
|
7
4
|
import type { Subscription } from './subscription.ts'
|
|
8
5
|
import type { ClientCallOptions, InputType, OutputType } from './types.ts'
|
|
@@ -12,25 +9,31 @@ type ClientServices = Record<string, TServiceContract>
|
|
|
12
9
|
type ClientCallers<Services extends ClientServices> = {
|
|
13
10
|
[K in keyof Services]: {
|
|
14
11
|
[P in keyof Services[K]['procedures']]: (
|
|
15
|
-
...args:
|
|
12
|
+
...args: Services[K]['procedures'][P]['input'] extends NeverType
|
|
16
13
|
? [options?: ClientCallOptions]
|
|
17
14
|
: [
|
|
18
|
-
data: InputType<
|
|
15
|
+
data: InputType<
|
|
16
|
+
t.infer.encoded<Services[K]['procedures'][P]['input']>
|
|
17
|
+
>,
|
|
19
18
|
options?: ClientCallOptions,
|
|
20
19
|
]
|
|
21
20
|
) => Promise<
|
|
22
21
|
Services[K]['procedures'][P] extends TSubscriptionContract
|
|
23
22
|
? {
|
|
24
|
-
payload:
|
|
25
|
-
Services[K]['procedures'][P]['output']
|
|
26
|
-
> extends never
|
|
23
|
+
payload: Services[K]['procedures'][P]['output'] extends NeverType
|
|
27
24
|
? undefined
|
|
28
|
-
:
|
|
29
|
-
subscription: Subscription<
|
|
25
|
+
: t.infer.encoded<Services[K]['procedures'][P]['output']>
|
|
26
|
+
subscription: Subscription<{
|
|
27
|
+
[KE in keyof Services[K]['procedures'][P]['events']]: [
|
|
28
|
+
t.infer.encoded<
|
|
29
|
+
Services[K]['procedures'][P]['events'][KE]['payload']
|
|
30
|
+
>,
|
|
31
|
+
]
|
|
32
|
+
}>
|
|
30
33
|
}
|
|
31
|
-
:
|
|
34
|
+
: Services[K]['procedures'][P]['output'] extends NeverType
|
|
32
35
|
? void
|
|
33
|
-
: OutputType<
|
|
36
|
+
: OutputType<t.infer.encoded<Services[K]['procedures'][P]['output']>>
|
|
34
37
|
>
|
|
35
38
|
}
|
|
36
39
|
}
|
package/lib/subscription.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { EventEmitter } from './utils.ts'
|
|
1
|
+
import { EventEmitter, type EventMap } from './utils.ts'
|
|
3
2
|
|
|
4
3
|
export class Subscription<
|
|
5
|
-
|
|
6
|
-
> extends EventEmitter<{
|
|
7
|
-
[K in keyof Contact['events']]: [Contact['events'][K]['static']['payload']]
|
|
8
|
-
}> {
|
|
4
|
+
Events extends EventMap = EventMap,
|
|
5
|
+
> extends EventEmitter<Events> {
|
|
9
6
|
constructor(
|
|
10
7
|
readonly key: string,
|
|
11
8
|
readonly unsubscribe: () => void,
|
package/package.json
CHANGED
|
@@ -19,12 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/type": "0.1.0",
|
|
23
|
+
"@nmtjs/common": "0.1.0",
|
|
24
|
+
"@nmtjs/contract": "0.1.0"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@nmtjs/
|
|
27
|
-
"@nmtjs/contract": "0.0
|
|
27
|
+
"@nmtjs/type": "0.1.0",
|
|
28
|
+
"@nmtjs/contract": "0.1.0",
|
|
29
|
+
"@nmtjs/common": "0.1.0"
|
|
28
30
|
},
|
|
29
31
|
"files": [
|
|
30
32
|
"index.ts",
|
|
@@ -34,7 +36,7 @@
|
|
|
34
36
|
"LICENSE.md",
|
|
35
37
|
"README.md"
|
|
36
38
|
],
|
|
37
|
-
"version": "0.0
|
|
39
|
+
"version": "0.1.0",
|
|
38
40
|
"scripts": {
|
|
39
41
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
40
42
|
"type-check": "tsc --noEmit"
|