@nmtjs/client 0.1.0 → 0.2.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 +27 -13
- package/dist/lib/client-runtime.js.map +1 -1
- package/dist/lib/client.js +2 -0
- package/dist/lib/client.js.map +1 -1
- package/dist/lib/transport.js.map +1 -1
- package/lib/client-runtime.ts +37 -17
- package/lib/client.ts +3 -0
- package/lib/transport.ts +1 -1
- package/package.json +7 -7
|
@@ -2,16 +2,30 @@ import { NeverType } from '@nmtjs/type';
|
|
|
2
2
|
import { compile } from '@nmtjs/type/compiler';
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
4
|
export class RuntimeClient extends Client {
|
|
5
|
+
contracts;
|
|
5
6
|
#callers;
|
|
6
|
-
constructor(
|
|
7
|
-
super(options, Object.values(
|
|
7
|
+
constructor(contracts, options){
|
|
8
|
+
super(options, Object.values(contracts).map((s)=>s.contract.name));
|
|
9
|
+
this.contracts = contracts;
|
|
8
10
|
const callers = {};
|
|
9
|
-
for (const [serviceKey, service] of Object.entries(
|
|
11
|
+
for (const [serviceKey, service] of Object.entries(contracts)){
|
|
10
12
|
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}]`);
|
|
12
13
|
callers[serviceKey] = {};
|
|
13
14
|
for(const procedureName in service.contract.procedures){
|
|
14
15
|
const { input, output } = service.contract.procedures[procedureName];
|
|
16
|
+
function decodeOutput(data) {
|
|
17
|
+
if (output instanceof NeverType) return undefined;
|
|
18
|
+
const compiled = service.compiled.get(output);
|
|
19
|
+
const result = compiled.decode(data);
|
|
20
|
+
if (result.success) {
|
|
21
|
+
return result.value;
|
|
22
|
+
} else {
|
|
23
|
+
console.dir(result.error);
|
|
24
|
+
throw new Error('Failed to decode output', {
|
|
25
|
+
cause: result.error
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
15
29
|
callers[serviceKey][procedureName] = this.createCaller(service.contract.name, procedureName, {
|
|
16
30
|
timeout: service.contract.timeout,
|
|
17
31
|
transformInput: (data)=>{
|
|
@@ -28,16 +42,11 @@ export class RuntimeClient extends Client {
|
|
|
28
42
|
}
|
|
29
43
|
},
|
|
30
44
|
transformOutput: (data)=>{
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (result.success) {
|
|
35
|
-
return result.value;
|
|
45
|
+
if (service.contract.procedures[procedureName].type === 'neemata:subscription') {
|
|
46
|
+
data.payload = decodeOutput(data.payload);
|
|
47
|
+
return data;
|
|
36
48
|
} else {
|
|
37
|
-
|
|
38
|
-
throw new Error('Failed to decode output', {
|
|
39
|
-
cause: result.error
|
|
40
|
-
});
|
|
49
|
+
return decodeOutput(data);
|
|
41
50
|
}
|
|
42
51
|
}
|
|
43
52
|
});
|
|
@@ -48,6 +57,11 @@ export class RuntimeClient extends Client {
|
|
|
48
57
|
get call() {
|
|
49
58
|
return this.#callers;
|
|
50
59
|
}
|
|
60
|
+
checkTransport(transport) {
|
|
61
|
+
for (const { contract } of Object.values(this.contracts)){
|
|
62
|
+
if (!contract.transports[transport.type]) throw new Error(`Transport [${transport.type}] not supported for service [${contract.name}]`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
51
65
|
}
|
|
52
66
|
export const compileContract = (contract)=>{
|
|
53
67
|
const compiled = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/client-runtime.ts"],"sourcesContent":["import type {\n TEventContract,\n
|
|
1
|
+
{"version":3,"sources":["../../../lib/client-runtime.ts"],"sourcesContent":["import type {\n TEventContract,\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 { ClientTransport } from './transport.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(\n protected readonly contracts: Services,\n options: ClientOptions,\n ) {\n super(\n options,\n Object.values(contracts).map((s) => s.contract.name),\n )\n\n const callers = {} as any\n for (const [serviceKey, service] of Object.entries(contracts)) {\n service.contract.procedures\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 function decodeOutput(data) {\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 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 (\n service.contract.procedures[procedureName].type ===\n 'neemata:subscription'\n ) {\n data.payload = decodeOutput(data.payload)\n return data\n } else {\n return decodeOutput(data)\n }\n },\n },\n )\n }\n }\n this.#callers = callers\n }\n\n get call() {\n return this.#callers\n }\n\n protected checkTransport(transport: ClientTransport): void {\n for (const { contract } of Object.values(this.contracts)) {\n if (!contract.transports[transport.type])\n throw new Error(\n `Transport [${transport.type}] not supported for service [${contract.name}]`,\n )\n }\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","contracts","options","Object","values","map","s","contract","name","callers","serviceKey","service","entries","procedures","procedureName","input","output","decodeOutput","data","undefined","compiled","get","result","decode","success","value","console","dir","error","Error","cause","createCaller","timeout","transformInput","encode","transformOutput","type","payload","call","checkTransport","transport","transports","compileContract","Map","procedure","events","event","set"],"mappings":"AAKA,SAAwBA,SAAS,QAAgB,cAAa;AAC9D,SAAwBC,OAAO,QAAQ,uBAAsB;AAE7D,SAASC,MAAM,QAA4B,cAAa;AAkDxD,OAAO,MAAMC,sBAAuDD;;IAClE,CAAA,OAAQ,CAAyB;IAEjCE,YACE,AAAmBC,SAAmB,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nB,QAAQmB;IAC/B;IACA,KAAK,MAAM8B,SAAS3C,OAAOC,MAAM,CAACG,SAASsC,MAAM,EAAG;QAClDzB,SAAS2B,GAAG,CAACD,MAAMT,OAAO,EAAExC,QAAQiD,MAAMT,OAAO;IACnD;IAEA,OAAO;QACLjB;QACAb;IACF;AACF,EAAC"}
|
package/dist/lib/client.js
CHANGED
|
@@ -25,6 +25,7 @@ export class Client extends utils.EventEmitter {
|
|
|
25
25
|
format: this.format,
|
|
26
26
|
auth: this.auth
|
|
27
27
|
});
|
|
28
|
+
this.checkTransport(this.transport);
|
|
28
29
|
return this;
|
|
29
30
|
}
|
|
30
31
|
useFormat(format) {
|
|
@@ -41,6 +42,7 @@ export class Client extends utils.EventEmitter {
|
|
|
41
42
|
await this.disconnect();
|
|
42
43
|
await this.connect();
|
|
43
44
|
}
|
|
45
|
+
checkTransport(transport) {}
|
|
44
46
|
createCaller(service, procedure, { timeout = this.options.defaultTimeout, transformInput, transformOutput } = {}) {
|
|
45
47
|
return async (payload, options = {})=>{
|
|
46
48
|
const { signal } = options;
|
package/dist/lib/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/client.ts"],"sourcesContent":["import { type BaseClientFormat, ErrorCode } from '@nmtjs/common'\n\nimport { ClientError } from './common.ts'\nimport type { ClientTransport } from './transport.ts'\nimport type { ClientCallOptions } from './types.ts'\nimport * as utils from './utils.ts'\n\nexport type ClientOptions = {\n defaultTimeout: number\n debug?: boolean\n}\n\nexport abstract class Client extends utils.EventEmitter {\n protected transport!: ClientTransport\n protected format!: BaseClientFormat\n\n auth?: string\n\n private ids = {\n call: 0,\n stream: 0,\n }\n\n constructor(\n protected readonly options: ClientOptions,\n protected services: string[],\n ) {\n super()\n if (!options.defaultTimeout) options.defaultTimeout = 15000\n }\n\n useTransport<T extends new (...args: any[]) => ClientTransport>(\n transportClass: T,\n ...options: ConstructorParameters<T>\n ) {\n this.transport = new transportClass(...options)\n this.transport.client = Object.freeze({\n services: this.services,\n format: this.format,\n auth: this.auth,\n })\n return this as Omit<this, 'useTransport'>\n }\n\n useFormat(format: BaseClientFormat) {\n this.format = format\n return this as Omit<this, 'useFormat'>\n }\n\n async connect() {\n await this.transport.connect()\n }\n\n async disconnect() {\n await this.transport.disconnect()\n }\n\n async reconnect() {\n await this.disconnect()\n await this.connect()\n }\n\n protected createCaller(\n service: string,\n procedure: string,\n {\n timeout = this.options.defaultTimeout,\n transformInput,\n transformOutput,\n }: {\n timeout?: number\n transformInput?: (input: any) => any\n transformOutput?: (output: any) => any\n } = {},\n ) {\n return async (payload: any, options: ClientCallOptions = {}) => {\n const { signal } = options\n\n const abortSignal = signal\n ? AbortSignal.any([signal, AbortSignal.timeout(timeout)])\n : AbortSignal.timeout(timeout)\n\n const callId = ++this.ids.call\n\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] ${service}/${procedure}`)\n console.log(payload)\n console.groupEnd()\n }\n\n const callExecution = this.transport\n .rpc({\n callId,\n service,\n procedure,\n payload: transformInput ? transformInput(payload) : payload,\n signal: abortSignal,\n })\n .then((result) => {\n if (result.success) return result.value\n throw new ClientError(\n result.error.code,\n result.error.message,\n result.error.data,\n )\n })\n\n const callTimeout = utils.forAborted(abortSignal).catch(() => {\n const error = new ClientError(ErrorCode.RequestTimeout)\n return Promise.reject(error)\n })\n\n try {\n const response = await Promise.race([callTimeout, callExecution])\n\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] Success`)\n console.log(response)\n console.groupEnd()\n }\n\n return transformOutput ? transformOutput(response) : response\n } catch (error) {\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] Error`)\n console.log(error)\n console.groupEnd()\n }\n\n throw error\n }\n }\n }\n}\n"],"names":["ErrorCode","ClientError","utils","Client","EventEmitter","transport","format","auth","ids","constructor","options","services","call","stream","defaultTimeout","useTransport","transportClass","client","Object","freeze","useFormat","connect","disconnect","reconnect","createCaller","service","procedure","timeout","transformInput","transformOutput","payload","signal","abortSignal","AbortSignal","any","callId","debug","console","groupCollapsed","log","groupEnd","callExecution","rpc","then","result","success","value","error","code","message","data","callTimeout","forAborted","catch","RequestTimeout","Promise","reject","response","race"],"mappings":"AAAA,SAAgCA,SAAS,QAAQ,gBAAe;AAEhE,SAASC,WAAW,QAAQ,cAAa;AAGzC,YAAYC,WAAW,aAAY;AAOnC,OAAO,MAAeC,eAAeD,MAAME,YAAY;;;IAC3CC,UAA2B;IAC3BC,OAAyB;IAEnCC,KAAa;IAELC,IAGP;IAEDC,YACE,AAAmBC,OAAsB,EACzC,AAAUC,QAAkB,CAC5B;QACA,KAAK;aAHcD,UAAAA;aACTC,WAAAA;aAPJH,MAAM;YACZI,MAAM;YACNC,QAAQ;QACV;QAOE,IAAI,CAACH,QAAQI,cAAc,EAAEJ,QAAQI,cAAc,GAAG;IACxD;IAEAC,aACEC,cAAiB,EACjB,GAAGN,OAAiC,EACpC;QACA,IAAI,CAACL,SAAS,GAAG,IAAIW,kBAAkBN;QACvC,IAAI,CAACL,SAAS,CAACY,MAAM,GAAGC,OAAOC,MAAM,CAAC;YACpCR,UAAU,IAAI,CAACA,QAAQ;YACvBL,QAAQ,IAAI,CAACA,MAAM;YACnBC,MAAM,IAAI,CAACA,IAAI;QACjB;QACA,OAAO,IAAI;IACb;
|
|
1
|
+
{"version":3,"sources":["../../../lib/client.ts"],"sourcesContent":["import { type BaseClientFormat, ErrorCode } from '@nmtjs/common'\n\nimport { ClientError } from './common.ts'\nimport type { ClientTransport } from './transport.ts'\nimport type { ClientCallOptions } from './types.ts'\nimport * as utils from './utils.ts'\n\nexport type ClientOptions = {\n defaultTimeout: number\n debug?: boolean\n}\n\nexport abstract class Client extends utils.EventEmitter {\n protected transport!: ClientTransport\n protected format!: BaseClientFormat\n\n auth?: string\n\n private ids = {\n call: 0,\n stream: 0,\n }\n\n constructor(\n protected readonly options: ClientOptions,\n protected services: string[],\n ) {\n super()\n if (!options.defaultTimeout) options.defaultTimeout = 15000\n }\n\n useTransport<T extends new (...args: any[]) => ClientTransport>(\n transportClass: T,\n ...options: ConstructorParameters<T>\n ) {\n this.transport = new transportClass(...options)\n this.transport.client = Object.freeze({\n services: this.services,\n format: this.format,\n auth: this.auth,\n })\n this.checkTransport(this.transport)\n return this as Omit<this, 'useTransport'>\n }\n\n useFormat(format: BaseClientFormat) {\n this.format = format\n return this as Omit<this, 'useFormat'>\n }\n\n async connect() {\n await this.transport.connect()\n }\n\n async disconnect() {\n await this.transport.disconnect()\n }\n\n async reconnect() {\n await this.disconnect()\n await this.connect()\n }\n\n protected checkTransport(transport: ClientTransport) {}\n\n protected createCaller(\n service: string,\n procedure: string,\n {\n timeout = this.options.defaultTimeout,\n transformInput,\n transformOutput,\n }: {\n timeout?: number\n transformInput?: (input: any) => any\n transformOutput?: (output: any) => any\n } = {},\n ) {\n return async (payload: any, options: ClientCallOptions = {}) => {\n const { signal } = options\n\n const abortSignal = signal\n ? AbortSignal.any([signal, AbortSignal.timeout(timeout)])\n : AbortSignal.timeout(timeout)\n\n const callId = ++this.ids.call\n\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] ${service}/${procedure}`)\n console.log(payload)\n console.groupEnd()\n }\n\n const callExecution = this.transport\n .rpc({\n callId,\n service,\n procedure,\n payload: transformInput ? transformInput(payload) : payload,\n signal: abortSignal,\n })\n .then((result) => {\n if (result.success) return result.value\n throw new ClientError(\n result.error.code,\n result.error.message,\n result.error.data,\n )\n })\n\n const callTimeout = utils.forAborted(abortSignal).catch(() => {\n const error = new ClientError(ErrorCode.RequestTimeout)\n return Promise.reject(error)\n })\n\n try {\n const response = await Promise.race([callTimeout, callExecution])\n\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] Success`)\n console.log(response)\n console.groupEnd()\n }\n\n return transformOutput ? transformOutput(response) : response\n } catch (error) {\n if (this.options.debug) {\n console.groupCollapsed(`RPC [${callId}] Error`)\n console.log(error)\n console.groupEnd()\n }\n\n throw error\n }\n }\n }\n}\n"],"names":["ErrorCode","ClientError","utils","Client","EventEmitter","transport","format","auth","ids","constructor","options","services","call","stream","defaultTimeout","useTransport","transportClass","client","Object","freeze","checkTransport","useFormat","connect","disconnect","reconnect","createCaller","service","procedure","timeout","transformInput","transformOutput","payload","signal","abortSignal","AbortSignal","any","callId","debug","console","groupCollapsed","log","groupEnd","callExecution","rpc","then","result","success","value","error","code","message","data","callTimeout","forAborted","catch","RequestTimeout","Promise","reject","response","race"],"mappings":"AAAA,SAAgCA,SAAS,QAAQ,gBAAe;AAEhE,SAASC,WAAW,QAAQ,cAAa;AAGzC,YAAYC,WAAW,aAAY;AAOnC,OAAO,MAAeC,eAAeD,MAAME,YAAY;;;IAC3CC,UAA2B;IAC3BC,OAAyB;IAEnCC,KAAa;IAELC,IAGP;IAEDC,YACE,AAAmBC,OAAsB,EACzC,AAAUC,QAAkB,CAC5B;QACA,KAAK;aAHcD,UAAAA;aACTC,WAAAA;aAPJH,MAAM;YACZI,MAAM;YACNC,QAAQ;QACV;QAOE,IAAI,CAACH,QAAQI,cAAc,EAAEJ,QAAQI,cAAc,GAAG;IACxD;IAEAC,aACEC,cAAiB,EACjB,GAAGN,OAAiC,EACpC;QACA,IAAI,CAACL,SAAS,GAAG,IAAIW,kBAAkBN;QACvC,IAAI,CAACL,SAAS,CAACY,MAAM,GAAGC,OAAOC,MAAM,CAAC;YACpCR,UAAU,IAAI,CAACA,QAAQ;YACvBL,QAAQ,IAAI,CAACA,MAAM;YACnBC,MAAM,IAAI,CAACA,IAAI;QACjB;QACA,IAAI,CAACa,cAAc,CAAC,IAAI,CAACf,SAAS;QAClC,OAAO,IAAI;IACb;IAEAgB,UAAUf,MAAwB,EAAE;QAClC,IAAI,CAACA,MAAM,GAAGA;QACd,OAAO,IAAI;IACb;IAEA,MAAMgB,UAAU;QACd,MAAM,IAAI,CAACjB,SAAS,CAACiB,OAAO;IAC9B;IAEA,MAAMC,aAAa;QACjB,MAAM,IAAI,CAAClB,SAAS,CAACkB,UAAU;IACjC;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AI;gBACF,MAAMU,WAAW,MAAMF,QAAQG,IAAI,CAAC;oBAACP;oBAAaV;iBAAc;gBAEhE,IAAI,IAAI,CAAChC,OAAO,CAAC2B,KAAK,EAAE;oBACtBC,QAAQC,cAAc,CAAC,CAAC,KAAK,EAAEH,OAAO,SAAS,CAAC;oBAChDE,QAAQE,GAAG,CAACkB;oBACZpB,QAAQG,QAAQ;gBAClB;gBAEA,OAAOX,kBAAkBA,gBAAgB4B,YAAYA;YACvD,EAAE,OAAOV,OAAO;gBACd,IAAI,IAAI,CAACtC,OAAO,CAAC2B,KAAK,EAAE;oBACtBC,QAAQC,cAAc,CAAC,CAAC,KAAK,EAAEH,OAAO,OAAO,CAAC;oBAC9CE,QAAQE,GAAG,CAACQ;oBACZV,QAAQG,QAAQ;gBAClB;gBAEA,MAAMO;YACR;QACF;IACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/transport.ts"],"sourcesContent":["import type { BaseClientFormat } from '@nmtjs/common'\nimport { EventEmitter, type EventMap } from './utils.ts'\n\nexport type ClientTransportRpcCall = {\n service: string\n procedure: string\n callId: number\n payload: any\n signal: AbortSignal\n}\n\nexport type ClientTransportRpcResult =\n | {\n success: false\n error: { code: string; message?: string; data?: any }\n }\n | {\n success: true\n value: any\n }\n\nexport abstract class ClientTransport<\n T extends EventMap =
|
|
1
|
+
{"version":3,"sources":["../../../lib/transport.ts"],"sourcesContent":["import type { BaseClientFormat } from '@nmtjs/common'\nimport { EventEmitter, type EventMap } from './utils.ts'\n\nexport type ClientTransportRpcCall = {\n service: string\n procedure: string\n callId: number\n payload: any\n signal: AbortSignal\n}\n\nexport type ClientTransportRpcResult =\n | {\n success: false\n error: { code: string; message?: string; data?: any }\n }\n | {\n success: true\n value: any\n }\n\nexport abstract class ClientTransport<\n T extends EventMap = EventMap,\n> extends EventEmitter<\n T & { event: [service: string, event: string, payload: any] }\n> {\n abstract type: string\n\n client!: {\n readonly services: string[]\n readonly format: BaseClientFormat\n readonly auth?: string\n }\n\n abstract connect(): Promise<void>\n abstract disconnect(): Promise<void>\n abstract rpc(\n params: ClientTransportRpcCall,\n ): Promise<ClientTransportRpcResult>\n}\n"],"names":["EventEmitter","ClientTransport","client"],"mappings":"AACA,SAASA,YAAY,QAAuB,aAAY;AAoBxD,OAAO,MAAeC,wBAEZD;IAKRE,OAIC;AAOH"}
|
package/lib/client-runtime.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
TEventContract,
|
|
3
|
-
TProcedureContract,
|
|
4
3
|
TServiceContract,
|
|
5
4
|
TSubscriptionContract,
|
|
6
5
|
} from '@nmtjs/contract'
|
|
@@ -9,6 +8,7 @@ import { type Compiled, compile } from '@nmtjs/type/compiler'
|
|
|
9
8
|
|
|
10
9
|
import { Client, type ClientOptions } from './client.ts'
|
|
11
10
|
import type { Subscription } from './subscription.ts'
|
|
11
|
+
import type { ClientTransport } from './transport.ts'
|
|
12
12
|
import type { ClientCallOptions, InputType, OutputType } from './types.ts'
|
|
13
13
|
|
|
14
14
|
type CompiledContract<T extends TServiceContract = TServiceContract> = {
|
|
@@ -59,25 +59,38 @@ type ClientCallers<Services extends ClientServices> = {
|
|
|
59
59
|
export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
60
60
|
#callers: ClientCallers<Services>
|
|
61
61
|
|
|
62
|
-
constructor(
|
|
62
|
+
constructor(
|
|
63
|
+
protected readonly contracts: Services,
|
|
64
|
+
options: ClientOptions,
|
|
65
|
+
) {
|
|
63
66
|
super(
|
|
64
67
|
options,
|
|
65
|
-
Object.values(
|
|
68
|
+
Object.values(contracts).map((s) => s.contract.name),
|
|
66
69
|
)
|
|
67
70
|
|
|
68
71
|
const callers = {} as any
|
|
69
|
-
for (const [serviceKey, service] of Object.entries(
|
|
72
|
+
for (const [serviceKey, service] of Object.entries(contracts)) {
|
|
70
73
|
service.contract.procedures
|
|
71
|
-
if (!service.contract.transports[this.transport.type])
|
|
72
|
-
throw new Error(
|
|
73
|
-
`Transport [${this.transport.type}] not supported for service [${service.contract.name}]`,
|
|
74
|
-
)
|
|
75
74
|
|
|
76
75
|
callers[serviceKey] = {} as any
|
|
77
76
|
|
|
78
77
|
for (const procedureName in service.contract.procedures) {
|
|
79
78
|
const { input, output } = service.contract.procedures[procedureName]
|
|
80
79
|
|
|
80
|
+
function decodeOutput(data) {
|
|
81
|
+
if (output instanceof NeverType) return undefined
|
|
82
|
+
const compiled = service.compiled.get(output)!
|
|
83
|
+
const result = compiled.decode(data)
|
|
84
|
+
if (result.success) {
|
|
85
|
+
return result.value
|
|
86
|
+
} else {
|
|
87
|
+
console.dir(result.error)
|
|
88
|
+
throw new Error('Failed to decode output', {
|
|
89
|
+
cause: result.error,
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
81
94
|
callers[serviceKey][procedureName] = this.createCaller(
|
|
82
95
|
service.contract.name,
|
|
83
96
|
procedureName,
|
|
@@ -97,16 +110,14 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
97
110
|
}
|
|
98
111
|
},
|
|
99
112
|
transformOutput: (data: any) => {
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
113
|
+
if (
|
|
114
|
+
service.contract.procedures[procedureName].type ===
|
|
115
|
+
'neemata:subscription'
|
|
116
|
+
) {
|
|
117
|
+
data.payload = decodeOutput(data.payload)
|
|
118
|
+
return data
|
|
105
119
|
} else {
|
|
106
|
-
|
|
107
|
-
throw new Error('Failed to decode output', {
|
|
108
|
-
cause: result.error,
|
|
109
|
-
})
|
|
120
|
+
return decodeOutput(data)
|
|
110
121
|
}
|
|
111
122
|
},
|
|
112
123
|
},
|
|
@@ -119,6 +130,15 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
119
130
|
get call() {
|
|
120
131
|
return this.#callers
|
|
121
132
|
}
|
|
133
|
+
|
|
134
|
+
protected checkTransport(transport: ClientTransport): void {
|
|
135
|
+
for (const { contract } of Object.values(this.contracts)) {
|
|
136
|
+
if (!contract.transports[transport.type])
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Transport [${transport.type}] not supported for service [${contract.name}]`,
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
122
142
|
}
|
|
123
143
|
|
|
124
144
|
export const compileContract = <T extends TServiceContract>(
|
package/lib/client.ts
CHANGED
|
@@ -39,6 +39,7 @@ export abstract class Client extends utils.EventEmitter {
|
|
|
39
39
|
format: this.format,
|
|
40
40
|
auth: this.auth,
|
|
41
41
|
})
|
|
42
|
+
this.checkTransport(this.transport)
|
|
42
43
|
return this as Omit<this, 'useTransport'>
|
|
43
44
|
}
|
|
44
45
|
|
|
@@ -60,6 +61,8 @@ export abstract class Client extends utils.EventEmitter {
|
|
|
60
61
|
await this.connect()
|
|
61
62
|
}
|
|
62
63
|
|
|
64
|
+
protected checkTransport(transport: ClientTransport) {}
|
|
65
|
+
|
|
63
66
|
protected createCaller(
|
|
64
67
|
service: string,
|
|
65
68
|
procedure: string,
|
package/lib/transport.ts
CHANGED
package/package.json
CHANGED
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/
|
|
24
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/contract": "0.2.0",
|
|
23
|
+
"@nmtjs/type": "0.2.0",
|
|
24
|
+
"@nmtjs/common": "0.2.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@nmtjs/type": "0.
|
|
28
|
-
"@nmtjs/
|
|
29
|
-
"@nmtjs/
|
|
27
|
+
"@nmtjs/type": "0.2.0",
|
|
28
|
+
"@nmtjs/common": "0.2.0",
|
|
29
|
+
"@nmtjs/contract": "0.2.0"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"index.ts",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"LICENSE.md",
|
|
37
37
|
"README.md"
|
|
38
38
|
],
|
|
39
|
-
"version": "0.
|
|
39
|
+
"version": "0.2.0",
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
42
42
|
"type-check": "tsc --noEmit"
|