@nmtjs/client 0.3.1 → 0.3.3
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 +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 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,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,MAAM,EAAE,GAAGL,QAAQJ,QAAQ,CAACM,UAAU,CAACC,cAAc;gBAEpE,SAASG,aAAaC,IAAI;oBACxB,IAAIF,kBAAkBpB,WAAW,OAAOuB;oBACxC,MAAMC,WAAWT,QAAQS,QAAQ,CAACC,GAAG,CAACL;oBACtC,MAAMM,SAASF,SAASG,MAAM,CAACL;oBAC/B,IAAII,OAAOE,OAAO,EAAE;wBAClB,OAAOF,OAAOG,KAAK;oBACrB,OAAO;wBACLC,QAAQC,GAAG,CAACL,OAAOM,KAAK;wBACxB,MAAM,IAAIC,MAAM,2BAA2B;4BACzCC,OAAOR,OAAOM,KAAK;wBACrB;oBACF;gBACF;gBAEAnB,OAAO,CAACC,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,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,EAAExC,QAAQiD,MAAMT,OAAO;YACnD;QACF;QACAjB,SAAS2B,GAAG,CAAChC,OAAOlB,QAAQkB;QAC5BK,SAAS2B,GAAG,CAAC/B,QAAQnB,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"}
|
|
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 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","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,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,CAA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T;IAClB;IAEA,IAAI6B,OAAO;QACT,OAAO,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,EAAExC,QAAQiD,MAAMT,OAAO;YACnD;QACF;QACAjB,SAAS2B,GAAG,CAAChC,OAAOlB,QAAQkB;QAC5BK,SAAS2B,GAAG,CAAC/B,QAAQnB,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/lib/client-runtime.ts
CHANGED
|
@@ -149,7 +149,7 @@ export const compileContract = <T extends TServiceContract>(
|
|
|
149
149
|
for (const procedure of Object.values(contract.procedures)) {
|
|
150
150
|
const { input, output } = procedure
|
|
151
151
|
if (procedure.type === 'neemata:subscription') {
|
|
152
|
-
const { events } = procedure
|
|
152
|
+
const { events } = procedure as TSubscriptionContract
|
|
153
153
|
for (const event of Object.values(events) as TEventContract[]) {
|
|
154
154
|
compiled.set(event.payload, compile(event.payload))
|
|
155
155
|
}
|
package/package.json
CHANGED
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@nmtjs/type": "0.3.
|
|
23
|
-
"@nmtjs/
|
|
24
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/type": "0.3.3",
|
|
23
|
+
"@nmtjs/common": "0.3.3",
|
|
24
|
+
"@nmtjs/contract": "0.3.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@nmtjs/type": "0.3.
|
|
28
|
-
"@nmtjs/
|
|
29
|
-
"@nmtjs/
|
|
27
|
+
"@nmtjs/type": "0.3.3",
|
|
28
|
+
"@nmtjs/common": "0.3.3",
|
|
29
|
+
"@nmtjs/contract": "0.3.3"
|
|
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.3",
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
42
42
|
"type-check": "tsc --noEmit"
|