@nmtjs/client 0.4.8 → 0.5.1
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 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.
|
|
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.input.decoded<\n Services[K][P]['contract']['input']\n > extends undefined\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 : t.infer.decoded<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,MAAM,EAAE,GAAGL,QAAQJ,QAAQ,CAACM,UAAU,CAACC,cAAc;gBAEpE,SAASG,aAAaC,IAAI;oBACxB,IAAIF,kBAAkBrB,WAAW,OAAOwB;oBACxC,MAAMC,WAAWT,QAAQS,QAAQ,CAACC,GAAG,CAACL;oBACtC,MAAMM,SAASF,SAASG,UAAU,CAACL;oBACnC,IAAII,OAAOE,OAAO,EAAE;wBAClB,OAAOF,OAAOG,KAAK;oBACrB,OAAO;wBACLC,QAAQC,GAAG,CAACL,OAAOM,KAAK;wBACxB,MAAM,IAAIC,MAAM,2BAA2B;4BACzCC,OAAOR,OAA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a,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 ClientServicesResolved<Services extends ClientServices> = {\n [K in keyof Services]: {\n [P in keyof Services[K]['procedures']]: {\n contract: Services[K]['procedures'][P]\n input:
|
|
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: t.infer.input.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 // `await client.call.serviceName` or `await client.call.serviceName.procedureName`\n // without explicitly calling a function implicitly calls .then() on target\n // FIXME: this basically makes \"then\" a reserved word\n if (prop === 'then') return target\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;oBAIlB,IAAID,SAAS,QAAQ,OAAOD;oBAC5B,OAAO,IAAI,CAACG,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/lib/client-runtime.ts
CHANGED
|
@@ -48,9 +48,9 @@ type ClientCallers<Services extends ClientServicesResolved<ClientServices>> = {
|
|
|
48
48
|
[P in keyof Services[K]]: (
|
|
49
49
|
...args: Services[K][P]['input'] extends NeverType
|
|
50
50
|
? [options?: ClientCallOptions]
|
|
51
|
-
: t.infer.
|
|
51
|
+
: t.infer.input.decoded<
|
|
52
52
|
Services[K][P]['contract']['input']
|
|
53
|
-
>
|
|
53
|
+
> extends undefined
|
|
54
54
|
? [data?: Services[K][P]['input'], options?: ClientCallOptions]
|
|
55
55
|
: [data: Services[K][P]['input'], options?: ClientCallOptions]
|
|
56
56
|
) => Promise<
|
|
@@ -58,7 +58,7 @@ type ClientCallers<Services extends ClientServicesResolved<ClientServices>> = {
|
|
|
58
58
|
? {
|
|
59
59
|
payload: Services[K][P]['output'] extends never
|
|
60
60
|
? undefined
|
|
61
|
-
: Services[K][P]['output']
|
|
61
|
+
: t.infer.decoded<Services[K][P]['output']>
|
|
62
62
|
subscription: Subscription<{
|
|
63
63
|
[KE in keyof Services[K][P]['events']]: [
|
|
64
64
|
t.infer.decoded<Services[K][P]['events'][KE]['payload']>,
|
package/lib/client-static.ts
CHANGED
|
@@ -10,7 +10,7 @@ type ClientServicesResolved<Services extends ClientServices> = {
|
|
|
10
10
|
[K in keyof Services]: {
|
|
11
11
|
[P in keyof Services[K]['procedures']]: {
|
|
12
12
|
contract: Services[K]['procedures'][P]
|
|
13
|
-
input:
|
|
13
|
+
input: t.infer.input.encoded<Services[K]['procedures'][P]['input']>
|
|
14
14
|
output: OutputType<
|
|
15
15
|
t.infer.encoded<Services[K]['procedures'][P]['output']>
|
|
16
16
|
>
|
package/package.json
CHANGED
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/common": "0.
|
|
24
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/contract": "0.5.1",
|
|
23
|
+
"@nmtjs/common": "0.5.1",
|
|
24
|
+
"@nmtjs/type": "0.5.1"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"index.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"LICENSE.md",
|
|
32
32
|
"README.md"
|
|
33
33
|
],
|
|
34
|
-
"version": "0.
|
|
34
|
+
"version": "0.5.1",
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
37
37
|
"type-check": "tsc --noEmit"
|