@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.
@@ -1,21 +1,22 @@
1
- import { compile } from '@nmtjs/contract/compiler';
2
- import { ContractGuard } from '@nmtjs/contract/guards';
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, serviceContract] of Object.entries(services)){
10
- if (!serviceContract.contract.transports[this.transport.type]) throw new Error(`Transport [${this.transport.type}] not supported for service [${serviceContract.contract.name}]`);
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 serviceContract.contract.procedures){
13
- const { input, output } = serviceContract.contract.procedures[procedureName];
14
- callers[serviceKey][procedureName] = this.createCaller(serviceContract.contract.name, procedureName, {
15
- timeout: serviceContract.contract.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 (ContractGuard.IsNever(data)) return undefined;
18
- const compiled = serviceContract.compiled.get(input);
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 (ContractGuard.IsNever(data)) return undefined;
31
- const compiled = serviceContract.compiled.get(output);
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 procedureContract of Object.values(contract.procedures)){
54
- const { input, output, events } = procedureContract;
55
- if (ContractGuard.IsSubscription(procedureContract)) {
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 eventContract of Object.values(contract.events)){
64
- compiled.set(eventContract, compile(eventContract));
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 Decoded,\n TEventContract,\n TSchema,\n TServiceContract,\n TSubscriptionContract,\n} from '@nmtjs/contract'\nimport { type Compiled, compile } from '@nmtjs/contract/compiler'\nimport { ContractGuard } from '@nmtjs/contract/guards'\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<TSchema, 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: Decoded<\n Services[K]['contract']['procedures'][P]['input']\n > extends never\n ? [options?: ClientCallOptions]\n : [\n data: InputType<\n 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: Decoded<\n Services[K]['contract']['procedures'][P]['output']\n > extends never\n ? undefined\n : Decoded<Services[K]['contract']['procedures'][P]['output']>\n subscription: Subscription<Services[K]['contract']['procedures'][P]>\n }\n : Decoded<\n Services[K]['contract']['procedures'][P]['static']\n > extends never\n ? void\n : OutputType<\n Decoded<Services[K]['contract']['procedures'][P]['output']>\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, serviceContract] of Object.entries(services)) {\n if (!serviceContract.contract.transports[this.transport.type])\n throw new Error(\n `Transport [${this.transport.type}] not supported for service [${serviceContract.contract.name}]`,\n )\n\n callers[serviceKey] = {} as any\n\n for (const procedureName in serviceContract.contract.procedures) {\n const { input, output } =\n serviceContract.contract.procedures[procedureName]\n\n callers[serviceKey][procedureName] = this.createCaller(\n serviceContract.contract.name,\n procedureName,\n {\n timeout: serviceContract.contract.timeout,\n transformInput: (data: any) => {\n if (ContractGuard.IsNever(data)) return undefined\n const compiled = serviceContract.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 (ContractGuard.IsNever(data)) return undefined\n const compiled = serviceContract.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<TSchema, Compiled>()\n for (const procedureContract of Object.values(contract.procedures)) {\n const { input, output, events } = procedureContract\n if (ContractGuard.IsSubscription(procedureContract)) {\n for (const event of Object.values(events) as TEventContract[]) {\n compiled.set(event, compile(event))\n }\n }\n compiled.set(input, compile(input))\n compiled.set(output, compile(output))\n }\n for (const eventContract of Object.values(contract.events)) {\n compiled.set(eventContract, compile(eventContract))\n }\n\n return {\n compiled,\n contract,\n }\n}\n"],"names":["compile","ContractGuard","Client","RuntimeClient","constructor","services","options","Object","values","map","s","contract","name","callers","serviceKey","serviceContract","entries","transports","transport","type","Error","procedureName","procedures","input","output","createCaller","timeout","transformInput","data","IsNever","undefined","compiled","get","result","encode","success","value","console","dir","error","cause","transformOutput","decode","call","compileContract","Map","procedureContract","events","IsSubscription","event","set","eventContract"],"mappings":"AAOA,SAAwBA,OAAO,QAAQ,2BAA0B;AACjE,SAASC,aAAa,QAAQ,yBAAwB;AACtD,SAASC,MAAM,QAA4B,cAAa;AA6CxD,OAAO,MAAMC,sBAAuDD;IAClE,CAAA,OAAQ,CAAyB;IAEjCE,YAAYC,QAAkB,EAAEC,OAAsB,CAAE;QACtD,KAAK,CACHA,SACAC,OAAOC,MAAM,CAACH,UAAUI,GAAG,CAAC,CAACC,IAAMA,EAAEC,QAAQ,CAACC,IAAI;QAGpD,MAAMC,UAAU,CAAC;QACjB,KAAK,MAAM,CAACC,YAAYC,gBAAgB,IAAIR,OAAOS,OAAO,CAACX,UAAW;YACpE,IAAI,CAACU,gBAAgBJ,QAAQ,CAACM,UAAU,CAAC,IAAI,CAACC,SAAS,CAACC,IAAI,CAAC,EAC3D,MAAM,IAAIC,MACR,CAAC,WAAW,EAAE,IAAI,CAACF,SAAS,CAACC,IAAI,CAAC,6BAA6B,EAAEJ,gBAAgBJ,QAAQ,CAACC,IAAI,CAAC,CAAC,CAAC;YAGrGC,OAAO,CAACC,WAAW,GAAG,CAAC;YAEvB,IAAK,MAAMO,iBAAiBN,gBAAgBJ,QAAQ,CAACW,UAAU,CAAE;gBAC/D,MAAM,EAAE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AG;QAClE,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEuB,MAAM,EAAE,GAAGD;QAClC,IAAI7C,cAAc+C,cAAc,CAACF,oBAAoB;YACnD,KAAK,MAAMG,SAAS1C,OAAOC,MAAM,CAACuC,QAA6B;gBAC7DhB,SAASmB,GAAG,CAACD,OAAOjD,QAAQiD;YAC9B;QACF;QACAlB,SAASmB,GAAG,CAAC3B,OAAOvB,QAAQuB;QAC5BQ,SAASmB,GAAG,CAAC1B,QAAQxB,QAAQwB;IAC/B;IACA,KAAK,MAAM2B,iBAAiB5C,OAAOC,MAAM,CAACG,SAASoC,MAAM,EAAG;QAC1DhB,SAASmB,GAAG,CAACC,eAAenD,QAAQmD;IACtC;IAEA,OAAO;QACLpB;QACApB;IACF;AACF,EAAC"}
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":"AAMA,SAAwBA,SAAS,QAAgB,cAAa;AAC9D,SAAwBC,OAAO,QAAQ,uBAAsB;AAE7D,SAASC,MAAM,QAA4B,cAAa;AAiDxD,OAAO,MAAMC,sBAAuDD;IAClE,CAAA,OAAQ,CAAyB;IAEjCE,YAAYC,QAAkB,EAAEC,OAAsB,CAAE;QACtD,KAAK,CACHA,SACAC,OAAOC,MAAM,CAACH,UAAUI,GAAG,CAAC,CAACC,IAAMA,EAAEC,QAAQ,CAACC,IAAI;QAGpD,MAAMC,UAAU,CAAC;QACjB,KAAK,MAAM,CAACC,YAAYC,QAAQ,IAAIR,OAAOS,OAAO,CAACX,UAAW;YAC5DU,QAAQJ,QAAQ,CAACM,UAAU;YAC3B,IAAI,CAACF,QAAQJ,QAAQ,CAACO,UAAU,CAAC,IAAI,CAACC,SAAS,CAACC,IAAI,CAAC,EACnD,MAAM,IAAIC,MACR,CAAC,WAAW,EAAE,IAAI,CAACF,SAAS,CAACC,IAAI,CAAC,6BAA6B,EAAEL,QAAQJ,QAAQ,CAACC,IAAI,CAAC,CAAC,CAAC;YAG7FC,OAAO,CAACC,WAAW,GAAG,CAAC;YAEvB,IAAK,MAAMQ,iBAAiBP,QAAQJ,QAAQ,CAACM,UAAU,CAAE;gBACvD,MAAM,EAAEM,KAAK,EAAEC,MAAM,EAAE,GAAGT,QAAQJ,QAAQ,CAACM,UAAU,CAACK,cAAc;gBAEpET,OAAO,CAACC,WAAW,CAACQ,cAAc,GAAG,IA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ASxC,OAAOC,MAAM,CAACsC,QAA6B;gBAC7DhB,SAASkB,GAAG,CAACD,MAAME,OAAO,EAAEhD,QAAQ8C,MAAME,OAAO;YACnD;QACF;QACAnB,SAASkB,GAAG,CAACzB,OAAOtB,QAAQsB;QAC5BO,SAASkB,GAAG,CAACxB,QAAQvB,QAAQuB;IAC/B;IACA,KAAK,MAAMuB,SAASxC,OAAOC,MAAM,CAACG,SAASmC,MAAM,EAAG;QAClDhB,SAASkB,GAAG,CAACD,MAAME,OAAO,EAAEhD,QAAQ8C,MAAME,OAAO;IACnD;IAEA,OAAO;QACLnB;QACAnB;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/client-static.ts"],"sourcesContent":["import type {\n Encoded,\n TServiceContract,\n TSubscriptionContract,\n} from '@nmtjs/contract'\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: Encoded<Services[K]['procedures'][P]['input']> extends never\n ? [options?: ClientCallOptions]\n : [\n data: InputType<Encoded<Services[K]['procedures'][P]['input']>>,\n options?: ClientCallOptions,\n ]\n ) => Promise<\n Services[K]['procedures'][P] extends TSubscriptionContract\n ? {\n payload: Encoded<\n Services[K]['procedures'][P]['output']\n > extends never\n ? undefined\n : Encoded<Services[K]['procedures'][P]['output']>\n subscription: Subscription<Services[K]['procedures'][P]>\n }\n : Encoded<Services[K]['procedures'][P]['static']> extends never\n ? void\n : OutputType<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":"AAKA,SAASA,MAAM,QAA4B,cAAa;AAgCxD,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
+ {"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 type { TSubscriptionContract } from '@nmtjs/contract'\nimport { EventEmitter } from './utils.ts'\n\nexport class Subscription<\n Contact extends TSubscriptionContract = TSubscriptionContract,\n> extends EventEmitter<{\n [K in keyof Contact['events']]: [Contact['events'][K]['static']['payload']]\n}> {\n constructor(\n readonly key: string,\n readonly unsubscribe: () => void,\n ) {\n super()\n }\n}\n"],"names":["EventEmitter","Subscription","constructor","key","unsubscribe"],"mappings":"AACA,SAASA,YAAY,QAAQ,aAAY;AAEzC,OAAO,MAAMC,qBAEHD;;;IAGRE,YACE,AAASC,GAAW,EACpB,AAASC,WAAuB,CAChC;QACA,KAAK;aAHID,MAAAA;aACAC,cAAAA;IAGX;AACF"}
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"}
@@ -1,18 +1,18 @@
1
1
  import type {
2
- Decoded,
3
2
  TEventContract,
4
- TSchema,
3
+ TProcedureContract,
5
4
  TServiceContract,
6
5
  TSubscriptionContract,
7
6
  } from '@nmtjs/contract'
8
- import { type Compiled, compile } from '@nmtjs/contract/compiler'
9
- import { ContractGuard } from '@nmtjs/contract/guards'
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<TSchema, Compiled>
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: Decoded<
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
- Decoded<Services[K]['contract']['procedures'][P]['input']>
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: Decoded<
38
- Services[K]['contract']['procedures'][P]['output']
39
- > extends never
35
+ payload: Services[K]['contract']['procedures'][P]['output'] extends NeverType
40
36
  ? undefined
41
- : Decoded<Services[K]['contract']['procedures'][P]['output']>
42
- subscription: Subscription<Services[K]['contract']['procedures'][P]>
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
- : Decoded<
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
- Decoded<Services[K]['contract']['procedures'][P]['output']>
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, serviceContract] of Object.entries(services)) {
66
- if (!serviceContract.contract.transports[this.transport.type])
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 [${serviceContract.contract.name}]`,
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 serviceContract.contract.procedures) {
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
- serviceContract.contract.name,
82
+ service.contract.name,
79
83
  procedureName,
80
84
  {
81
- timeout: serviceContract.contract.timeout,
85
+ timeout: service.contract.timeout,
82
86
  transformInput: (data: any) => {
83
- if (ContractGuard.IsNever(data)) return undefined
84
- const compiled = serviceContract.compiled.get(input)!
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 (ContractGuard.IsNever(data)) return undefined
97
- const compiled = serviceContract.compiled.get(output)!
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<TSchema, Compiled>()
124
- for (const procedureContract of Object.values(contract.procedures)) {
125
- const { input, output, events } = procedureContract
126
- if (ContractGuard.IsSubscription(procedureContract)) {
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 eventContract of Object.values(contract.events)) {
135
- compiled.set(eventContract, compile(eventContract))
140
+ for (const event of Object.values(contract.events)) {
141
+ compiled.set(event.payload, compile(event.payload))
136
142
  }
137
143
 
138
144
  return {
@@ -1,8 +1,5 @@
1
- import type {
2
- Encoded,
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: Encoded<Services[K]['procedures'][P]['input']> extends never
12
+ ...args: Services[K]['procedures'][P]['input'] extends NeverType
16
13
  ? [options?: ClientCallOptions]
17
14
  : [
18
- data: InputType<Encoded<Services[K]['procedures'][P]['input']>>,
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: Encoded<
25
- Services[K]['procedures'][P]['output']
26
- > extends never
23
+ payload: Services[K]['procedures'][P]['output'] extends NeverType
27
24
  ? undefined
28
- : Encoded<Services[K]['procedures'][P]['output']>
29
- subscription: Subscription<Services[K]['procedures'][P]>
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
- : Encoded<Services[K]['procedures'][P]['static']> extends never
34
+ : Services[K]['procedures'][P]['output'] extends NeverType
32
35
  ? void
33
- : OutputType<Encoded<Services[K]['procedures'][P]['output']>>
36
+ : OutputType<t.infer.encoded<Services[K]['procedures'][P]['output']>>
34
37
  >
35
38
  }
36
39
  }
@@ -1,11 +1,8 @@
1
- import type { TSubscriptionContract } from '@nmtjs/contract'
2
- import { EventEmitter } from './utils.ts'
1
+ import { EventEmitter, type EventMap } from './utils.ts'
3
2
 
4
3
  export class Subscription<
5
- Contact extends TSubscriptionContract = TSubscriptionContract,
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/common": "0.0.3",
23
- "@nmtjs/contract": "0.0.3"
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/common": "0.0.3",
27
- "@nmtjs/contract": "0.0.3"
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.3",
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"