@nmtjs/client 0.3.6 → 0.3.7
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
|
|
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 : t.infer.staticType<\n Services[K]['contract']['procedures'][P]['input']\n >['isOptional'] extends true\n ? [\n data?: InputType<\n t.infer.decoded<\n Services[K]['contract']['procedures'][P]['input']\n >\n >,\n options?: ClientCallOptions,\n ]\n : [\n data: InputType<\n t.infer.decoded<\n Services[K]['contract']['procedures'][P]['input']\n >\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;AA+DxD,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;QAQ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iD,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 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
|
|
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 : t.infer.staticType<\n Services[K]['procedures'][P]['input']\n >['isOptional'] extends true\n ? [\n data?: InputType<\n t.infer.encoded<Services[K]['procedures'][P]['input']>\n >,\n options?: ClientCallOptions,\n ]\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;AA+CxD,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"}
|
package/lib/client-runtime.ts
CHANGED
|
@@ -23,12 +23,25 @@ type ClientCallers<Services extends ClientServices> = {
|
|
|
23
23
|
[P in keyof Services[K]['contract']['procedures']]: (
|
|
24
24
|
...args: Services[K]['contract']['procedures'][P]['input'] extends NeverType
|
|
25
25
|
? [options?: ClientCallOptions]
|
|
26
|
-
:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
: t.infer.staticType<
|
|
27
|
+
Services[K]['contract']['procedures'][P]['input']
|
|
28
|
+
>['isOptional'] extends true
|
|
29
|
+
? [
|
|
30
|
+
data?: InputType<
|
|
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
|
+
]
|
|
32
45
|
) => Promise<
|
|
33
46
|
Services[K]['contract']['procedures'][P] extends TSubscriptionContract
|
|
34
47
|
? {
|
package/lib/client-static.ts
CHANGED
|
@@ -11,12 +11,21 @@ type ClientCallers<Services extends ClientServices> = {
|
|
|
11
11
|
[P in keyof Services[K]['procedures']]: (
|
|
12
12
|
...args: Services[K]['procedures'][P]['input'] extends NeverType
|
|
13
13
|
? [options?: ClientCallOptions]
|
|
14
|
-
:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
: t.infer.staticType<
|
|
15
|
+
Services[K]['procedures'][P]['input']
|
|
16
|
+
>['isOptional'] extends true
|
|
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
|
+
]
|
|
20
29
|
) => Promise<
|
|
21
30
|
Services[K]['procedures'][P] extends TSubscriptionContract
|
|
22
31
|
? {
|
package/package.json
CHANGED
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@nmtjs/
|
|
23
|
-
"@nmtjs/
|
|
24
|
-
"@nmtjs/
|
|
22
|
+
"@nmtjs/common": "0.3.7",
|
|
23
|
+
"@nmtjs/type": "0.3.7",
|
|
24
|
+
"@nmtjs/contract": "0.3.7"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@nmtjs/
|
|
28
|
-
"@nmtjs/
|
|
29
|
-
"@nmtjs/
|
|
27
|
+
"@nmtjs/contract": "0.3.7",
|
|
28
|
+
"@nmtjs/type": "0.3.7",
|
|
29
|
+
"@nmtjs/common": "0.3.7"
|
|
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.7",
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "neemata-build -p neutral ./index.ts './lib/**/*.ts'",
|
|
42
42
|
"type-check": "tsc --noEmit"
|