@nmtjs/client 0.3.10 → 0.3.11
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 +3 -2
- package/dist/lib/client-runtime.js.map +1 -1
- package/dist/lib/client-static.js +1 -0
- package/dist/lib/client-static.js.map +1 -1
- package/dist/lib/client.js.map +1 -1
- package/lib/client-runtime.ts +42 -38
- package/lib/client-static.ts +40 -28
- package/lib/client.ts +3 -0
- package/package.json +7 -7
|
@@ -3,6 +3,7 @@ import { compile } from '@nmtjs/type/compiler';
|
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
4
|
export class RuntimeClient extends Client {
|
|
5
5
|
contracts;
|
|
6
|
+
$types;
|
|
6
7
|
#callers;
|
|
7
8
|
constructor(contracts, options){
|
|
8
9
|
super(options, Object.values(contracts).map((s)=>s.contract.name));
|
|
@@ -16,7 +17,7 @@ export class RuntimeClient extends Client {
|
|
|
16
17
|
function decodeOutput(data) {
|
|
17
18
|
if (output instanceof NeverType) return undefined;
|
|
18
19
|
const compiled = service.compiled.get(output);
|
|
19
|
-
const result = compiled.
|
|
20
|
+
const result = compiled.decodeSafe(data);
|
|
20
21
|
if (result.success) {
|
|
21
22
|
return result.value;
|
|
22
23
|
} else {
|
|
@@ -31,7 +32,7 @@ export class RuntimeClient extends Client {
|
|
|
31
32
|
transformInput: (data)=>{
|
|
32
33
|
if (input instanceof NeverType) return undefined;
|
|
33
34
|
const compiled = service.compiled.get(input);
|
|
34
|
-
const result = compiled.
|
|
35
|
+
const result = compiled.encodeSafe(data);
|
|
35
36
|
if (result.success) {
|
|
36
37
|
return result.value;
|
|
37
38
|
} else {
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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 ClientServicesResolved<Services extends ClientServices> = {\n [K in keyof Services]: {\n [P in keyof Services[K]['contract']['procedures']]: {\n contract: Services[K]['contract']['procedures'][P]\n input: InputType<\n t.infer.decoded<Services[K]['contract']['procedures'][P]['input']>\n >\n output: OutputType<\n t.infer.decoded<Services[K]['contract']['procedures'][P]['output']>\n >\n events: Services[K]['contract']['procedures'][P] extends TSubscriptionContract\n ? {\n [KE in keyof Services[K]['contract']['procedures'][P]['events']]: {\n payload: OutputType<\n t.infer.decoded<\n Services[K]['contract']['procedures'][P]['events'][KE]['payload']\n >\n >\n }\n }\n : {}\n }\n }\n}\n\ntype ClientServices = Record<string, CompiledContract>\n\ntype ClientCallers<Services extends ClientServicesResolved<ClientServices>> = {\n [K in keyof Services]: {\n [P in keyof Services[K]]: (\n ...args: Services[K][P]['input'] extends NeverType\n ? [options?: ClientCallOptions]\n : t.infer.staticType<\n Services[K][P]['contract']['input']\n >['isOptional'] extends true\n ? [data?: Services[K][P]['input'], options?: ClientCallOptions]\n : [data: Services[K][P]['input'], options?: ClientCallOptions]\n ) => Promise<\n Services[K][P] extends TSubscriptionContract\n ? {\n payload: Services[K][P]['output'] extends never\n ? undefined\n : Services[K][P]['output']\n subscription: Subscription<{\n [KE in keyof Services[K][P]['events']]: [\n t.infer.decoded<Services[K][P]['events'][KE]['payload']>,\n ]\n }>\n }\n : Services[K][P]['output'] extends never\n ? void\n : Services[K][P]['output']\n >\n }\n}\n\nexport class RuntimeClient<Services extends ClientServices> extends Client {\n $types!: ClientServicesResolved<Services>\n #callers: ClientCallers<this['$types']>\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.decodeSafe(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.encodeSafe(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 as TSubscriptionContract\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","$types","constructor","contracts","options","Object","values","map","s","contract","name","callers","serviceKey","service","entries","procedures","procedureName","input","output","decodeOutput","data","undefined","compiled","get","result","decodeSafe","success","value","console","dir","error","Error","cause","createCaller","timeout","transformInput","encodeSafe","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;AAkExD,OAAO,MAAMC,sBAAuDD;;IAClEE,OAAyC;IACzC,CAAA,OAAQ,CAA+B;IAEvCC,YACE,AAAmBC,SAAmB,EACtCC,OAAsB,CACtB;QACA,KAAK,CACHA,SACAC,OAAOC,MAAM,CAACH,WAAWI,GAAG,CAAC,CAACC,IAAMA,EAAEC,QAAQ,CAACC,IAAI;aALlCP,YAAAA;QAQnB,MAAMQ,UAAU,CAAC;QACjB,KAAK,MAAM,CAACC,YAAYC,QAAQ,IAAIR,OAAOS,OAAO,CAACX,WAAY;YAC7DU,QAAQJ,QAAQ,CAACM,UAAU;YAE3BJ,OAAO,CAACC,WAAW,GAAG,CAAC;YAEvB,IAAK,MAAMI,iBAAiBH,QAAQJ,QAAQ,CAACM,UAAU,CAAE;gBACvD,MAAM,EAAEE,KAAK,EAAEC,M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IAAI,CAAC,CAAA,OAAQ;IACtB;IAEUC,eAAeC,SAA0B,EAAQ;QACzD,KAAK,MAAM,EAAEjC,QAAQ,EAAE,IAAIJ,OAAOC,MAAM,CAAC,IAAI,CAACH,SAAS,EAAG;YACxD,IAAI,CAACM,SAASkC,UAAU,CAACD,UAAUJ,IAAI,CAAC,EACtC,MAAM,IAAIP,MACR,CAAC,WAAW,EAAEW,UAAUJ,IAAI,CAAC,6BAA6B,EAAE7B,SAASC,IAAI,CAAC,CAAC,CAAC;QAElF;IACF;AACF;AAEA,OAAO,MAAMkC,kBAAkB,CAC7BnC;IAEA,MAAMa,WAAW,IAAIuB;IAErB,KAAK,MAAMC,aAAazC,OAAOC,MAAM,CAACG,SAASM,UAAU,EAAG;QAC1D,MAAM,EAAEE,KAAK,EAAEC,MAAM,EAAE,GAAG4B;QAC1B,IAAIA,UAAUR,IAAI,KAAK,wBAAwB;YAC7C,MAAM,EAAES,MAAM,EAAE,GAAGD;YACnB,KAAK,MAAME,SAAS3C,OAAOC,MAAM,CAACyC,QAA6B;gBAC7DzB,SAAS2B,GAAG,CAACD,MAAMT,OAAO,EAAEzC,QAAQkD,MAAMT,OAAO;YACnD;QACF;QACAjB,SAAS2B,GAAG,CAAChC,OAAOnB,QAAQmB;QAC5BK,SAAS2B,GAAG,CAAC/B,QAAQpB,QAAQoB;IAC/B;IACA,KAAK,MAAM8B,SAAS3C,OAAOC,MAAM,CAACG,SAASsC,MAAM,EAAG;QAClDzB,SAAS2B,GAAG,CAACD,MAAMT,OAAO,EAAEzC,QAAQkD,MAAMT,OAAO;IACnD;IAEA,OAAO;QACLjB;QACAb;IACF;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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 ClientServicesResolved<Services extends ClientServices> = {\n [K in keyof Services]: {\n [P in keyof Services[K]['procedures']]: {\n contract: Services[K]['procedures'][P]\n input: InputType<t.infer.encoded<Services[K]['procedures'][P]['input']>>\n output: OutputType<\n t.infer.encoded<Services[K]['procedures'][P]['output']>\n >\n events: Services[K]['procedures'][P] extends TSubscriptionContract\n ? {\n [KE in keyof Services[K]['procedures'][P]['events']]: {\n payload: OutputType<\n t.infer.encoded<\n Services[K]['procedures'][P]['events'][KE]['payload']\n >\n >\n }\n }\n : {}\n }\n }\n}\n\ntype ClientCallers<\n Services extends ClientServicesResolved<Record<string, TServiceContract>>,\n> = {\n [K in keyof Services]: {\n [P in keyof Services[K]]: (\n ...args: Services[K][P]['input'] extends never\n ? [options?: ClientCallOptions]\n : Services[K][P]['input'] extends undefined\n ? [data?: Services[K][P]['input'], options?: ClientCallOptions]\n : [data: Services[K][P]['input'], options?: ClientCallOptions]\n ) => Promise<\n Services[K][P]['contract'] extends TSubscriptionContract\n ? {\n payload: Services[K][P]['output'] extends never\n ? undefined\n : Services[K][P]['output']\n subscription: Subscription<{\n [KE in keyof Services[K][P]['events']]: [\n Services[K][P]['events'][KE],\n ]\n }>\n }\n : Services[K][P]['output'] extends never\n ? void\n : Services[K][P]['output']\n >\n }\n}\n\nexport class StaticClient<Services extends ClientServices> extends Client {\n $types!: ClientServicesResolved<Services>\n #callers: ClientCallers<this['$types']>\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","$types","constructor","services","options","Object","values","callers","serviceKey","serviceName","entries","Proxy","get","target","prop","receiver","createCaller","call"],"mappings":"AAEA,SAASA,MAAM,QAA4B,cAAa;AA0DxD,OAAO,MAAMC,qBAAsDD;IACjEE,OAAyC;IACzC,CAAA,OAAQ,CAA+B;IAEvCC,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"}
|
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 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;
|
|
1
|
+
{"version":3,"sources":["../../../lib/client.ts"],"sourcesContent":["import { type BaseClientFormat, ErrorCode } from '@nmtjs/common'\n\nimport type { TServiceContract } from '@nmtjs/contract'\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 type ClientServices = Record<string, TServiceContract>\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;AAGhE,SAASC,WAAW,QA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AAO,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"}
|
package/lib/client-runtime.ts
CHANGED
|
@@ -16,61 +16,65 @@ type CompiledContract<T extends TServiceContract = TServiceContract> = {
|
|
|
16
16
|
contract: T
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
type ClientServicesResolved<Services extends ClientServices> = {
|
|
20
|
+
[K in keyof Services]: {
|
|
21
|
+
[P in keyof Services[K]['contract']['procedures']]: {
|
|
22
|
+
contract: Services[K]['contract']['procedures'][P]
|
|
23
|
+
input: InputType<
|
|
24
|
+
t.infer.decoded<Services[K]['contract']['procedures'][P]['input']>
|
|
25
|
+
>
|
|
26
|
+
output: OutputType<
|
|
27
|
+
t.infer.decoded<Services[K]['contract']['procedures'][P]['output']>
|
|
28
|
+
>
|
|
29
|
+
events: Services[K]['contract']['procedures'][P] extends TSubscriptionContract
|
|
30
|
+
? {
|
|
31
|
+
[KE in keyof Services[K]['contract']['procedures'][P]['events']]: {
|
|
32
|
+
payload: OutputType<
|
|
33
|
+
t.infer.decoded<
|
|
34
|
+
Services[K]['contract']['procedures'][P]['events'][KE]['payload']
|
|
35
|
+
>
|
|
36
|
+
>
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
: {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
19
44
|
type ClientServices = Record<string, CompiledContract>
|
|
20
45
|
|
|
21
|
-
type ClientCallers<Services extends ClientServices
|
|
46
|
+
type ClientCallers<Services extends ClientServicesResolved<ClientServices>> = {
|
|
22
47
|
[K in keyof Services]: {
|
|
23
|
-
[P in keyof Services[K]
|
|
24
|
-
...args: Services[K][
|
|
48
|
+
[P in keyof Services[K]]: (
|
|
49
|
+
...args: Services[K][P]['input'] extends NeverType
|
|
25
50
|
? [options?: ClientCallOptions]
|
|
26
51
|
: t.infer.staticType<
|
|
27
|
-
Services[K][
|
|
52
|
+
Services[K][P]['contract']['input']
|
|
28
53
|
>['isOptional'] extends true
|
|
29
|
-
? [
|
|
30
|
-
|
|
31
|
-
t.infer.decoded<
|
|
32
|
-
Services[K]['contract']['procedures'][P]['input']
|
|
33
|
-
>
|
|
34
|
-
>,
|
|
35
|
-
options?: ClientCallOptions,
|
|
36
|
-
]
|
|
37
|
-
: [
|
|
38
|
-
data: InputType<
|
|
39
|
-
t.infer.decoded<
|
|
40
|
-
Services[K]['contract']['procedures'][P]['input']
|
|
41
|
-
>
|
|
42
|
-
>,
|
|
43
|
-
options?: ClientCallOptions,
|
|
44
|
-
]
|
|
54
|
+
? [data?: Services[K][P]['input'], options?: ClientCallOptions]
|
|
55
|
+
: [data: Services[K][P]['input'], options?: ClientCallOptions]
|
|
45
56
|
) => Promise<
|
|
46
|
-
Services[K][
|
|
57
|
+
Services[K][P] extends TSubscriptionContract
|
|
47
58
|
? {
|
|
48
|
-
payload: Services[K][
|
|
59
|
+
payload: Services[K][P]['output'] extends never
|
|
49
60
|
? undefined
|
|
50
|
-
:
|
|
51
|
-
Services[K]['contract']['procedures'][P]['output']
|
|
52
|
-
>
|
|
61
|
+
: Services[K][P]['output']
|
|
53
62
|
subscription: Subscription<{
|
|
54
|
-
[KE in keyof Services[K][
|
|
55
|
-
t.infer.decoded<
|
|
56
|
-
Services[K]['contract']['procedures'][P]['events'][KE]['payload']
|
|
57
|
-
>,
|
|
63
|
+
[KE in keyof Services[K][P]['events']]: [
|
|
64
|
+
t.infer.decoded<Services[K][P]['events'][KE]['payload']>,
|
|
58
65
|
]
|
|
59
66
|
}>
|
|
60
67
|
}
|
|
61
|
-
: Services[K][
|
|
68
|
+
: Services[K][P]['output'] extends never
|
|
62
69
|
? void
|
|
63
|
-
:
|
|
64
|
-
t.infer.decoded<
|
|
65
|
-
Services[K]['contract']['procedures'][P]['output']
|
|
66
|
-
>
|
|
67
|
-
>
|
|
70
|
+
: Services[K][P]['output']
|
|
68
71
|
>
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
73
|
-
|
|
76
|
+
$types!: ClientServicesResolved<Services>
|
|
77
|
+
#callers: ClientCallers<this['$types']>
|
|
74
78
|
|
|
75
79
|
constructor(
|
|
76
80
|
protected readonly contracts: Services,
|
|
@@ -93,7 +97,7 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
93
97
|
function decodeOutput(data) {
|
|
94
98
|
if (output instanceof NeverType) return undefined
|
|
95
99
|
const compiled = service.compiled.get(output)!
|
|
96
|
-
const result = compiled.
|
|
100
|
+
const result = compiled.decodeSafe(data)
|
|
97
101
|
if (result.success) {
|
|
98
102
|
return result.value
|
|
99
103
|
} else {
|
|
@@ -112,7 +116,7 @@ export class RuntimeClient<Services extends ClientServices> extends Client {
|
|
|
112
116
|
transformInput: (data: any) => {
|
|
113
117
|
if (input instanceof NeverType) return undefined
|
|
114
118
|
const compiled = service.compiled.get(input)!
|
|
115
|
-
const result = compiled.
|
|
119
|
+
const result = compiled.encodeSafe(data)
|
|
116
120
|
if (result.success) {
|
|
117
121
|
return result.value
|
|
118
122
|
} else {
|
package/lib/client-static.ts
CHANGED
|
@@ -6,49 +6,61 @@ import type { ClientCallOptions, InputType, OutputType } from './types.ts'
|
|
|
6
6
|
|
|
7
7
|
type ClientServices = Record<string, TServiceContract>
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type ClientServicesResolved<Services extends ClientServices> = {
|
|
10
10
|
[K in keyof Services]: {
|
|
11
|
-
[P in keyof Services[K]['procedures']]:
|
|
12
|
-
|
|
11
|
+
[P in keyof Services[K]['procedures']]: {
|
|
12
|
+
contract: Services[K]['procedures'][P]
|
|
13
|
+
input: InputType<t.infer.encoded<Services[K]['procedures'][P]['input']>>
|
|
14
|
+
output: OutputType<
|
|
15
|
+
t.infer.encoded<Services[K]['procedures'][P]['output']>
|
|
16
|
+
>
|
|
17
|
+
events: Services[K]['procedures'][P] extends TSubscriptionContract
|
|
18
|
+
? {
|
|
19
|
+
[KE in keyof Services[K]['procedures'][P]['events']]: {
|
|
20
|
+
payload: OutputType<
|
|
21
|
+
t.infer.encoded<
|
|
22
|
+
Services[K]['procedures'][P]['events'][KE]['payload']
|
|
23
|
+
>
|
|
24
|
+
>
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
: {}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type ClientCallers<
|
|
33
|
+
Services extends ClientServicesResolved<Record<string, TServiceContract>>,
|
|
34
|
+
> = {
|
|
35
|
+
[K in keyof Services]: {
|
|
36
|
+
[P in keyof Services[K]]: (
|
|
37
|
+
...args: Services[K][P]['input'] extends never
|
|
13
38
|
? [options?: ClientCallOptions]
|
|
14
|
-
:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
? [
|
|
18
|
-
data?: InputType<
|
|
19
|
-
t.infer.encoded<Services[K]['procedures'][P]['input']>
|
|
20
|
-
>,
|
|
21
|
-
options?: ClientCallOptions,
|
|
22
|
-
]
|
|
23
|
-
: [
|
|
24
|
-
data: InputType<
|
|
25
|
-
t.infer.encoded<Services[K]['procedures'][P]['input']>
|
|
26
|
-
>,
|
|
27
|
-
options?: ClientCallOptions,
|
|
28
|
-
]
|
|
39
|
+
: Services[K][P]['input'] extends undefined
|
|
40
|
+
? [data?: Services[K][P]['input'], options?: ClientCallOptions]
|
|
41
|
+
: [data: Services[K][P]['input'], options?: ClientCallOptions]
|
|
29
42
|
) => Promise<
|
|
30
|
-
Services[K]['
|
|
43
|
+
Services[K][P]['contract'] extends TSubscriptionContract
|
|
31
44
|
? {
|
|
32
|
-
payload: Services[K][
|
|
45
|
+
payload: Services[K][P]['output'] extends never
|
|
33
46
|
? undefined
|
|
34
|
-
:
|
|
47
|
+
: Services[K][P]['output']
|
|
35
48
|
subscription: Subscription<{
|
|
36
|
-
[KE in keyof Services[K][
|
|
37
|
-
|
|
38
|
-
Services[K]['procedures'][P]['events'][KE]['payload']
|
|
39
|
-
>,
|
|
49
|
+
[KE in keyof Services[K][P]['events']]: [
|
|
50
|
+
Services[K][P]['events'][KE],
|
|
40
51
|
]
|
|
41
52
|
}>
|
|
42
53
|
}
|
|
43
|
-
: Services[K][
|
|
54
|
+
: Services[K][P]['output'] extends never
|
|
44
55
|
? void
|
|
45
|
-
:
|
|
56
|
+
: Services[K][P]['output']
|
|
46
57
|
>
|
|
47
58
|
}
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
export class StaticClient<Services extends ClientServices> extends Client {
|
|
51
|
-
|
|
62
|
+
$types!: ClientServicesResolved<Services>
|
|
63
|
+
#callers: ClientCallers<this['$types']>
|
|
52
64
|
|
|
53
65
|
constructor(
|
|
54
66
|
services: { [K in keyof Services]: Services[K]['name'] },
|
package/lib/client.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type BaseClientFormat, ErrorCode } from '@nmtjs/common'
|
|
2
2
|
|
|
3
|
+
import type { TServiceContract } from '@nmtjs/contract'
|
|
3
4
|
import { ClientError } from './common.ts'
|
|
4
5
|
import type { ClientTransport } from './transport.ts'
|
|
5
6
|
import type { ClientCallOptions } from './types.ts'
|
|
@@ -10,6 +11,8 @@ export type ClientOptions = {
|
|
|
10
11
|
debug?: boolean
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
export type ClientServices = Record<string, TServiceContract>
|
|
15
|
+
|
|
13
16
|
export abstract class Client extends utils.EventEmitter {
|
|
14
17
|
protected transport!: ClientTransport
|
|
15
18
|
protected format!: BaseClientFormat
|
package/package.json
CHANGED
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/contract": "0.3.
|
|
24
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/type": "0.3.11",
|
|
23
|
+
"@nmtjs/contract": "0.3.11",
|
|
24
|
+
"@nmtjs/common": "0.3.11"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@nmtjs/contract": "0.3.
|
|
28
|
-
"@nmtjs/
|
|
29
|
-
"@nmtjs/
|
|
27
|
+
"@nmtjs/contract": "0.3.11",
|
|
28
|
+
"@nmtjs/common": "0.3.11",
|
|
29
|
+
"@nmtjs/type": "0.3.11"
|
|
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.3.
|
|
39
|
+
"version": "0.3.11",
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
42
42
|
"type-check": "tsc --noEmit"
|