@nmtjs/contract 0.11.9 → 0.12.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.
- package/dist/schemas/api.js +13 -28
- package/dist/schemas/api.js.map +1 -1
- package/dist/schemas/event.js +1 -1
- package/dist/schemas/event.js.map +1 -1
- package/dist/schemas/namespace.js +17 -14
- package/dist/schemas/namespace.js.map +1 -1
- package/dist/schemas/procedure.js +6 -3
- package/dist/schemas/procedure.js.map +1 -1
- package/dist/schemas/subscription.js +13 -8
- package/dist/schemas/subscription.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/schemas/api.ts +59 -49
- package/src/schemas/event.ts +7 -7
- package/src/schemas/namespace.ts +82 -62
- package/src/schemas/procedure.ts +43 -31
- package/src/schemas/subscription.ts +65 -31
- package/src/utils.ts +1 -1
package/dist/schemas/api.js
CHANGED
|
@@ -1,50 +1,35 @@
|
|
|
1
1
|
import { Kind } from "../constants.js";
|
|
2
2
|
import { createSchema } from "../utils.js";
|
|
3
|
-
export const APIKind = "NeemataAPI";
|
|
3
|
+
export const APIKind = Symbol("NeemataAPI");
|
|
4
4
|
export const APIContract = (options) => {
|
|
5
|
-
const {
|
|
5
|
+
const { timeout, schemaOptions } = options;
|
|
6
6
|
const _namespaces = {};
|
|
7
|
-
for (const namespaceKey in namespaces) {
|
|
8
|
-
const namespace = namespaces[namespaceKey];
|
|
7
|
+
for (const namespaceKey in options.namespaces) {
|
|
8
|
+
const namespace = options.namespaces[namespaceKey];
|
|
9
9
|
const _procedures = {};
|
|
10
10
|
for (const procedureKey in namespace.procedures) {
|
|
11
11
|
const procedure = namespace.procedures[procedureKey];
|
|
12
|
-
_procedures[procedureKey] =
|
|
12
|
+
_procedures[procedureKey] = createSchema({
|
|
13
|
+
...procedure,
|
|
13
14
|
name: procedureKey,
|
|
14
15
|
namespace: namespaceKey
|
|
15
16
|
});
|
|
16
17
|
}
|
|
17
|
-
const _subscriptions = {};
|
|
18
|
-
for (const subscriptionKey in namespace.subscriptions) {
|
|
19
|
-
const subscription = namespace.subscriptions[subscriptionKey];
|
|
20
|
-
const _events = {};
|
|
21
|
-
for (const eventKey in subscription.events) {
|
|
22
|
-
const event = subscription.events[eventKey];
|
|
23
|
-
_events[eventKey] = Object.assign({}, event, {
|
|
24
|
-
name: eventKey,
|
|
25
|
-
namespace: namespaceKey,
|
|
26
|
-
subscription: subscriptionKey
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
_subscriptions[subscriptionKey] = Object.assign({}, subscription, {
|
|
30
|
-
name: subscriptionKey,
|
|
31
|
-
namespace: namespaceKey,
|
|
32
|
-
events: _events
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
18
|
const _events = {};
|
|
36
19
|
for (const eventKey in namespace.events) {
|
|
37
20
|
const event = namespace.events[eventKey];
|
|
38
|
-
_events[eventKey] =
|
|
21
|
+
_events[eventKey] = createSchema({
|
|
22
|
+
...event,
|
|
23
|
+
subscription: undefined,
|
|
39
24
|
name: eventKey,
|
|
40
25
|
namespace: namespaceKey
|
|
41
26
|
});
|
|
42
27
|
}
|
|
43
|
-
_namespaces[namespaceKey] =
|
|
44
|
-
|
|
28
|
+
_namespaces[namespaceKey] = createSchema({
|
|
29
|
+
...namespace,
|
|
45
30
|
procedures: _procedures,
|
|
46
|
-
|
|
47
|
-
|
|
31
|
+
events: _events,
|
|
32
|
+
name: namespaceKey
|
|
48
33
|
});
|
|
49
34
|
}
|
|
50
35
|
return createSchema({
|
package/dist/schemas/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;
|
|
1
|
+
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAKtE,OAAO,MAAM,UAAU,OAAO,aAAa;AAqB3C,OAAO,MAAM,cAAc,CAOzBA,YACG;CACH,MAAM,EAAE,SAAS,eAAe,GAAG;CAEnC,MAAM,cAAc,CAAE;AAEtB,MAAK,MAAM,gBAAgB,QAAQ,YAAY;EAC7C,MAAM,YAAY,QAAQ,WAAW;EACrC,MAAM,cAAc,CAAE;AACtB,OAAK,MAAM,gBAAgB,UAAU,YAAY;GAC/C,MAAM,YAAY,UAAU,WAAW;AACvC,eAAY,gBAAgB,aAQ1B;IACA,GAAG;IACH,MAAM;IACN,WAAW;GACZ,EAAC;EACH;EAED,MAAM,UAAU,CAAE;AAClB,OAAK,MAAM,YAAY,UAAU,QAAQ;GACvC,MAAM,QAAQ,UAAU,OAAO;AAC/B,WAAQ,YAAY,aAOlB;IACA,GAAG;IACH,cAAc;IACd,MAAM;IACN,WAAW;GACZ,EAAC;EACH;AAED,cAAY,gBAAgB,aAM1B;GACA,GAAG;GACH,YAAY;GACZ,QAAQ;GACR,MAAM;EACP,EAAC;CACH;AAED,QAAO,aAAkD;EACvD,GAAG;GACF,OAAO;EACR,MAAM;EACN,YAAY;EACZ;CACD,EAAC;AACH;AAED,OAAO,SAAS,cAAcC,OAAsC;AAClE,QAAO,QAAQ,SAAS,MAAM,UAAU;AACzC","names":["options: Options","value: any"],"sources":["../../src/schemas/api.ts"],"sourcesContent":["import { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\nimport type { TEventContract } from './event.ts'\nimport type { TAnyNamespaceContract, TNamespaceContract } from './namespace.ts'\nimport type { TProcedureContract } from './procedure.ts'\n\nexport const APIKind = Symbol('NeemataAPI')\n\nexport type TAnyAPIContract = TAPIContract<\n Record<string, TAnyNamespaceContract>\n>\n\nexport interface TAPIContract<\n Namespaces extends Record<string, TAnyNamespaceContract> = {},\n> {\n readonly [Kind]: typeof APIKind\n readonly type: 'neemata:api'\n readonly namespaces: {\n [K in keyof Namespaces]: TNamespaceContract<\n Namespaces[K]['procedures'],\n Namespaces[K]['events'],\n Extract<K, string>\n >\n }\n readonly timeout?: number\n}\n\nexport const APIContract = <\n const Options extends {\n namespaces: Record<string, TAnyNamespaceContract>\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n },\n>(\n options: Options,\n) => {\n const { timeout, schemaOptions } = options\n\n const _namespaces = {} as any\n\n for (const namespaceKey in options.namespaces) {\n const namespace = options.namespaces[namespaceKey]\n const _procedures = {} as any\n for (const procedureKey in namespace.procedures) {\n const procedure = namespace.procedures[procedureKey]\n _procedures[procedureKey] = createSchema<\n TProcedureContract<\n (typeof procedure)['input'],\n (typeof procedure)['output'],\n (typeof procedure)['stream'],\n Extract<typeof procedureKey, string>,\n Extract<typeof namespaceKey, string>\n >\n >({\n ...procedure,\n name: procedureKey,\n namespace: namespaceKey,\n })\n }\n\n const _events = {} as any\n for (const eventKey in namespace.events) {\n const event = namespace.events[eventKey]\n _events[eventKey] = createSchema<\n TEventContract<\n (typeof event)['payload'],\n Extract<typeof eventKey, string>,\n undefined,\n Extract<typeof namespaceKey, string>\n >\n >({\n ...event,\n subscription: undefined,\n name: eventKey,\n namespace: namespaceKey,\n })\n }\n\n _namespaces[namespaceKey] = createSchema<\n TNamespaceContract<\n typeof _procedures,\n typeof _events,\n Extract<typeof namespaceKey, string>\n >\n >({\n ...namespace,\n procedures: _procedures,\n events: _events,\n name: namespaceKey,\n })\n }\n\n return createSchema<TAPIContract<Options['namespaces']>>({\n ...schemaOptions,\n [Kind]: APIKind,\n type: 'neemata:api',\n namespaces: _namespaces,\n timeout,\n })\n}\n\nexport function IsAPIContract(value: any): value is TAnyAPIContract {\n return Kind in value && value[Kind] === APIKind\n}\n"],"version":3,"file":"api.js"}
|
package/dist/schemas/event.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t } from "@nmtjs/type";
|
|
2
2
|
import { Kind } from "../constants.js";
|
|
3
3
|
import { createSchema } from "../utils.js";
|
|
4
|
-
export const EventKind = "NeemataEvent";
|
|
4
|
+
export const EventKind = Symbol("NeemataEvent");
|
|
5
5
|
export const EventContract = (options) => {
|
|
6
6
|
const { payload = t.never(), schemaOptions = {}, name } = options ?? {};
|
|
7
7
|
return createSchema({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAwB,SAAS,aAAa;AAC9C,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAEtE,OAAO,MAAM,YAAY;
|
|
1
|
+
{"mappings":"AAAA,SAAwB,SAAS,aAAa;AAC9C,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAEtE,OAAO,MAAM,YAAY,OAAO,eAAe;AAuB/C,OAAO,MAAM,gBAAgB,CAG3BA,YAII;CACJ,MAAM,EACJ,UAAU,EAAE,OAAO,EACnB,gBAAgB,CAAE,GAClB,MACD,GAAG,WAAW,CAAE;AACjB,QAAO,aAA4C;EACjD,GAAG;GACF,OAAO;EACR,MAAM;EACN;EACM;EACN,cAAc;EACd,WAAW;CACZ,EAAC;AACH;AAED,OAAO,SAAS,gBAAgBC,OAAwC;AACtE,QAAO,QAAQ,SAAS,MAAM,UAAU;AACzC","names":["options?: {\n payload?: Payload\n schemaOptions?: ContractSchemaOptions\n name?: Name\n}","value: any"],"sources":["../../src/schemas/event.ts"],"sourcesContent":["import { type BaseType, t } from '@nmtjs/type'\nimport { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\n\nexport const EventKind = Symbol('NeemataEvent')\n\nexport type TAnyEventContract = TEventContract<\n BaseType,\n string | undefined,\n string | undefined,\n string | undefined\n>\n\nexport interface TEventContract<\n Payload extends BaseType = t.NeverType,\n Name extends string | undefined = undefined,\n Subscription extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> {\n readonly [Kind]: typeof EventKind\n readonly type: 'neemata:event'\n readonly name: Name\n readonly subscription: Subscription\n readonly namespace: Namespace\n readonly payload: Payload\n}\n\nexport const EventContract = <\n Payload extends BaseType,\n Name extends string | undefined = undefined,\n>(options?: {\n payload?: Payload\n schemaOptions?: ContractSchemaOptions\n name?: Name\n}) => {\n const {\n payload = t.never() as unknown as Payload,\n schemaOptions = {},\n name,\n } = options ?? {}\n return createSchema<TEventContract<Payload, Name>>({\n ...schemaOptions,\n [Kind]: EventKind,\n type: 'neemata:event',\n payload,\n name: name as Name,\n subscription: undefined,\n namespace: undefined,\n })\n}\n\nexport function IsEventContract(value: any): value is TAnyEventContract {\n return Kind in value && value[Kind] === EventKind\n}\n"],"version":3,"file":"event.js"}
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { Kind } from "../constants.js";
|
|
2
2
|
import { createSchema } from "../utils.js";
|
|
3
|
-
export const NamespaceKind = "NeemataNamespace";
|
|
3
|
+
export const NamespaceKind = Symbol("NeemataNamespace");
|
|
4
4
|
export const NamespaceContract = (options) => {
|
|
5
|
-
const {
|
|
6
|
-
const
|
|
7
|
-
for (const
|
|
8
|
-
const event = events[
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const { name, timeout, schemaOptions = {} } = options;
|
|
6
|
+
const events = {};
|
|
7
|
+
for (const name in options.events) {
|
|
8
|
+
const event = options.events[name];
|
|
9
|
+
events[name] = createSchema({
|
|
10
|
+
...event,
|
|
11
|
+
subscription: undefined,
|
|
12
|
+
name,
|
|
11
13
|
namespace: options?.name
|
|
12
14
|
});
|
|
13
15
|
}
|
|
14
|
-
const
|
|
15
|
-
for (const
|
|
16
|
-
const procedure = procedures[
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const procedures = {};
|
|
17
|
+
for (const name in options.procedures) {
|
|
18
|
+
const procedure = options.procedures[name];
|
|
19
|
+
procedures[name] = createSchema({
|
|
20
|
+
...procedure,
|
|
21
|
+
name,
|
|
19
22
|
namespace: options?.name
|
|
20
23
|
});
|
|
21
24
|
}
|
|
@@ -24,8 +27,8 @@ export const NamespaceContract = (options) => {
|
|
|
24
27
|
[Kind]: NamespaceKind,
|
|
25
28
|
type: "neemata:namespace",
|
|
26
29
|
name,
|
|
27
|
-
procedures
|
|
28
|
-
events
|
|
30
|
+
procedures,
|
|
31
|
+
events,
|
|
29
32
|
timeout
|
|
30
33
|
});
|
|
31
34
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAItE,OAAO,MAAM,gBAAgB;
|
|
1
|
+
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAItE,OAAO,MAAM,gBAAgB,OAAO,mBAAmB;AAyCvD,OAAO,MAAM,oBAAoB,CAS/BA,YACG;CACH,MAAM,EAAE,MAAM,SAAS,gBAAgB,CAAE,GAA2B,GAAG;CACvE,MAAMC,SAA8B,CAAE;AAEtC,MAAK,MAAM,QAAQ,QAAQ,QAAQ;EACjC,MAAM,QAAQ,QAAQ,OAAO;AAC7B,SAAO,QAAQ,aAOb;GACA,GAAG;GACH,cAAc;GACR;GACN,WAAW,SAAS;EACrB,EAAC;CACH;CAED,MAAMC,aAAkC,CAAE;AAE1C,MAAK,MAAM,QAAQ,QAAQ,YAAY;EACrC,MAAMC,YAAiB,QAAQ,WAAW;AAC1C,aAAW,QAAQ,aAQjB;GACA,GAAG;GACG;GACN,WAAW,SAAS;EACrB,EAAC;CACH;AAED,QAAO,aAML;EACA,GAAG;GACF,OAAO;EACR,MAAM;EACA;EACM;EACJ;EACR;CACD,EAAC;AACH;AAED,OAAO,SAAS,oBACdC,OACgC;AAChC,QAAO,QAAQ,SAAS,MAAM,UAAU;AACzC","names":["options: Options","events: Record<string, any>","procedures: Record<string, any>","procedure: any","value: any"],"sources":["../../src/schemas/namespace.ts"],"sourcesContent":["import { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\nimport type { TAnyEventContract, TEventContract } from './event.ts'\nimport type { TAnyProcedureContract, TProcedureContract } from './procedure.ts'\n\nexport const NamespaceKind = Symbol('NeemataNamespace')\n\nexport type TAnyNamespaceContract<\n Procedures extends Record<string, TAnyProcedureContract> = Record<\n string,\n TAnyProcedureContract\n >,\n> = TNamespaceContract<\n Procedures,\n Record<string, TAnyEventContract>,\n string | undefined\n>\n\nexport interface TNamespaceContract<\n Procedures extends Record<string, TAnyProcedureContract> = {},\n Events extends Record<string, TAnyEventContract> = {},\n Name extends string | undefined = undefined,\n> {\n readonly [Kind]: typeof NamespaceKind\n readonly type: 'neemata:namespace'\n readonly name: Name\n readonly procedures: {\n [K in keyof Procedures]: TProcedureContract<\n Procedures[K]['input'],\n Procedures[K]['output'],\n Procedures[K]['stream'],\n Extract<K, string>,\n Name\n >\n }\n readonly events: {\n [K in keyof Events]: TEventContract<\n Events[K]['payload'],\n Extract<K, string>,\n undefined,\n Name\n >\n }\n readonly timeout?: number\n}\n\nexport const NamespaceContract = <\n const Options extends {\n procedures: Record<string, TAnyProcedureContract>\n events: Record<string, TAnyEventContract>\n name?: string\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n },\n>(\n options: Options,\n) => {\n const { name, timeout, schemaOptions = {} as ContractSchemaOptions } = options\n const events: Record<string, any> = {}\n\n for (const name in options.events) {\n const event = options.events[name]\n events[name] = createSchema<\n TEventContract<\n (typeof event)['payload'],\n Extract<typeof name, string>,\n undefined,\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...event,\n subscription: undefined,\n name: name as any,\n namespace: options?.name as any,\n })\n }\n\n const procedures: Record<string, any> = {}\n\n for (const name in options.procedures) {\n const procedure: any = options.procedures[name]\n procedures[name] = createSchema<\n TProcedureContract<\n (typeof procedure)['input'],\n (typeof procedure)['output'],\n (typeof procedure)['stream'],\n Extract<typeof name, string>,\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...procedure,\n name: name as any,\n namespace: options?.name as any,\n })\n }\n\n return createSchema<\n TNamespaceContract<\n Options['procedures'],\n Options['events'],\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...schemaOptions,\n [Kind]: NamespaceKind,\n type: 'neemata:namespace',\n name: name as any,\n procedures: procedures as any,\n events: events as any,\n timeout,\n })\n}\n\nexport function IsNamespaceContract(\n value: any,\n): value is TAnyNamespaceContract {\n return Kind in value && value[Kind] === NamespaceKind\n}\n"],"version":3,"file":"namespace.js"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { t } from "@nmtjs/type";
|
|
2
2
|
import { Kind } from "../constants.js";
|
|
3
3
|
import { createSchema } from "../utils.js";
|
|
4
|
-
export const ProcedureKind = "NeemataProcedure";
|
|
4
|
+
export const ProcedureKind = Symbol("NeemataProcedure");
|
|
5
5
|
export const ProcedureContract = (options) => {
|
|
6
|
-
const { input = t.never(), output = t.never(), stream =
|
|
6
|
+
const { input = t.never(), output = t.never(), stream = undefined, name = undefined, timeout, schemaOptions = {} } = options;
|
|
7
7
|
return createSchema({
|
|
8
8
|
...schemaOptions,
|
|
9
9
|
[Kind]: ProcedureKind,
|
|
@@ -11,13 +11,16 @@ export const ProcedureContract = (options) => {
|
|
|
11
11
|
input,
|
|
12
12
|
output,
|
|
13
13
|
stream,
|
|
14
|
-
timeout,
|
|
15
14
|
name,
|
|
15
|
+
timeout,
|
|
16
16
|
namespace: undefined
|
|
17
17
|
});
|
|
18
18
|
};
|
|
19
19
|
export function IsProcedureContract(contract) {
|
|
20
20
|
return Kind in contract && contract[Kind] === ProcedureKind;
|
|
21
21
|
}
|
|
22
|
+
export function IsStreamProcedureContract(contract) {
|
|
23
|
+
return IsProcedureContract(contract) && typeof contract.stream !== "undefined";
|
|
24
|
+
}
|
|
22
25
|
|
|
23
26
|
//# sourceMappingURL=procedure.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAwB,SAAS,aAAa;AAC9C,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAUtE,OAAO,MAAM,gBAAgB;
|
|
1
|
+
{"mappings":"AAAA,SAAwB,SAAS,aAAa;AAC9C,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAUtE,OAAO,MAAM,gBAAgB,OAAO,mBAAmB;AAmBvD,OAAO,MAAM,oBAAoB,CAU/BA,YACG;CACH,MAAM,EACJ,QAAQ,EAAE,OAAO,EACjB,SAAS,EAAE,OAAO,EAClB,SAAS,WACT,OAAO,WACP,SACA,gBAAgB,CAAE,GACnB,GAAG;AACJ,QAAO,aAOL;EACA,GAAG;GACF,OAAO;EACR,MAAM;EACN;EACA;EACA;EACA;EACA;EACA,WAAW;CACZ,EAAC;AACH;AAED,OAAO,SAAS,oBACdC,UACmC;AACnC,QAAO,QAAQ,YAAY,SAAS,UAAU;AAC/C;AAED,OAAO,SAAS,0BACdA,UACmC;AACnC,QAAO,oBAAoB,SAAS,WAAW,SAAS,WAAW;AACpE","names":["options: Options","contract: any"],"sources":["../../src/schemas/procedure.ts"],"sourcesContent":["import { type BaseType, t } from '@nmtjs/type'\nimport { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\n\nexport type TAnyProcedureContract = TProcedureContract<\n BaseType,\n BaseType,\n BaseType | undefined,\n string | undefined,\n string | undefined\n>\n\nexport const ProcedureKind = Symbol('NeemataProcedure')\n\nexport interface TProcedureContract<\n Input extends BaseType,\n Output extends BaseType,\n Stream extends BaseType | undefined,\n Name extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> {\n readonly [Kind]: typeof ProcedureKind\n readonly type: 'neemata:procedure'\n readonly name: Name\n readonly namespace: Namespace\n readonly input: Input\n readonly output: Output\n readonly stream: Stream\n readonly timeout?: number\n}\n\nexport const ProcedureContract = <\n const Options extends {\n input?: BaseType\n output?: BaseType\n stream?: BaseType | undefined\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n name?: string\n },\n>(\n options: Options,\n) => {\n const {\n input = t.never() as any,\n output = t.never() as any,\n stream = undefined as any,\n name = undefined as any,\n timeout,\n schemaOptions = {},\n } = options\n return createSchema<\n TProcedureContract<\n Options['input'] extends BaseType ? Options['input'] : t.NeverType,\n Options['output'] extends BaseType ? Options['output'] : t.NeverType,\n Options['stream'] extends BaseType ? Options['stream'] : undefined,\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...schemaOptions,\n [Kind]: ProcedureKind,\n type: 'neemata:procedure',\n input,\n output,\n stream,\n name,\n timeout,\n namespace: undefined,\n })\n}\n\nexport function IsProcedureContract(\n contract: any,\n): contract is TAnyProcedureContract {\n return Kind in contract && contract[Kind] === ProcedureKind\n}\n\nexport function IsStreamProcedureContract(\n contract: any,\n): contract is TAnyProcedureContract {\n return IsProcedureContract(contract) && typeof contract.stream !== 'undefined'\n}\n"],"version":3,"file":"procedure.js"}
|
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import { Kind } from "../constants.js";
|
|
2
2
|
import { createSchema } from "../utils.js";
|
|
3
|
-
export const SubscriptionKind = "NeemataSubscription";
|
|
4
|
-
|
|
5
|
-
const {
|
|
3
|
+
export const SubscriptionKind = Symbol("NeemataSubscription");
|
|
4
|
+
const _SubscriptionContract = (options) => {
|
|
5
|
+
const { schemaOptions = {}, name } = options;
|
|
6
6
|
const _events = {};
|
|
7
|
-
for (const key in events) {
|
|
8
|
-
const event = events[key];
|
|
9
|
-
_events[key] =
|
|
7
|
+
for (const key in options.events) {
|
|
8
|
+
const event = options.events[key];
|
|
9
|
+
_events[key] = createSchema({
|
|
10
|
+
...event,
|
|
10
11
|
name: key,
|
|
12
|
+
namespace: undefined,
|
|
11
13
|
subscription: name
|
|
12
14
|
});
|
|
13
15
|
}
|
|
14
|
-
return
|
|
16
|
+
return createSchema({
|
|
15
17
|
...schemaOptions,
|
|
16
18
|
[Kind]: SubscriptionKind,
|
|
17
19
|
type: "neemata:subscription",
|
|
18
20
|
events: _events,
|
|
19
21
|
name,
|
|
20
22
|
options: undefined
|
|
21
|
-
})
|
|
23
|
+
});
|
|
22
24
|
};
|
|
25
|
+
export const SubscriptionContract = Object.assign(_SubscriptionContract, { withOptions: () => {
|
|
26
|
+
return (options) => _SubscriptionContract(options);
|
|
27
|
+
} });
|
|
23
28
|
export function IsSubscriptionContract(contract) {
|
|
24
29
|
return Kind in contract && contract[Kind] === SubscriptionKind;
|
|
25
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAGtE,OAAO,MAAM,mBAAmB
|
|
1
|
+
{"mappings":"AAAA,SAAS,YAAY,iBAAiB;AACtC,SAAqC,oBAAoB,aAAa;AAGtE,OAAO,MAAM,mBAAmB,OAAO,sBAAsB;AA6B7D,MAAM,wBAAwB,CAQ5BA,YACG;CACH,MAAM,EAAE,gBAAgB,CAAE,GAAE,MAAM,GAAG;CACrC,MAAM,UAAU,CAAE;AAClB,MAAK,MAAM,OAAO,QAAQ,QAAQ;EAChC,MAAM,QAAQ,QAAQ,OAAO;AAC7B,UAAQ,OAAO,aAOb;GACA,GAAG;GACH,MAAM;GACN,WAAW;GACX,cAAc;EACf,EAAC;CACH;AACD,QAAO,aAML;EACA,GAAG;GACF,OAAO;EACR,MAAM;EACN,QAAQ;EACF;EACN,SAAS;CACV,EAAC;AACH;AAED,OAAO,MAAM,uBAAuB,OAAO,OAAO,uBAAuB,EACvE,aAAa,MAA0C;AACrD,QAAO,CAOLC,YACG,sBAAkC,QAAQ;AAChD,EACF,EAAC;AAEF,OAAO,SAAS,uBACdC,UACsC;AACtC,QAAO,QAAQ,YAAY,SAAS,UAAU;AAC/C","names":["options: Options","options: T","contract: any"],"sources":["../../src/schemas/subscription.ts"],"sourcesContent":["import { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\nimport type { TAnyEventContract, TEventContract } from './event.ts'\n\nexport const SubscriptionKind = Symbol('NeemataSubscription')\n\nexport type SubcriptionOptions = Record<\n string,\n string | number | boolean\n> | null\n\nexport type TAnySubscriptionContract = TSubscriptionContract<\n SubcriptionOptions,\n Record<string, TAnyEventContract>,\n string | undefined\n>\n\nexport interface TSubscriptionContract<\n Options extends SubcriptionOptions = null,\n Events extends Record<string, unknown> = {},\n Name extends string | undefined = undefined,\n> {\n readonly [Kind]: typeof SubscriptionKind\n readonly type: 'neemata:subscription'\n readonly name: Name\n readonly options: Options\n readonly events: {\n [K in keyof Events]: Events[K] extends TAnyEventContract\n ? TEventContract<Events[K]['payload'], Extract<K, string>, Name>\n : never\n }\n}\n\nconst _SubscriptionContract = <\n const Options extends {\n events: Record<string, TAnyEventContract>\n name?: string\n schemaOptions?: ContractSchemaOptions\n },\n SubOpt extends SubcriptionOptions = null,\n>(\n options: Options,\n) => {\n const { schemaOptions = {}, name } = options\n const _events = {} as any\n for (const key in options.events) {\n const event = options.events[key]\n _events[key] = createSchema<\n TEventContract<\n (typeof event)['payload'],\n Extract<typeof key, string>,\n undefined,\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...event,\n name: key,\n namespace: undefined,\n subscription: name as any,\n })\n }\n return createSchema<\n TSubscriptionContract<\n SubOpt,\n Options['events'],\n Options['name'] extends string ? Options['name'] : undefined\n >\n >({\n ...schemaOptions,\n [Kind]: SubscriptionKind,\n type: 'neemata:subscription',\n events: _events,\n name: name as any,\n options: undefined as unknown as SubOpt,\n })\n}\n\nexport const SubscriptionContract = Object.assign(_SubscriptionContract, {\n withOptions: <Options extends SubcriptionOptions>() => {\n return <\n T extends {\n events: Record<string, TAnyEventContract>\n name?: string\n schemaOptions?: ContractSchemaOptions\n },\n >(\n options: T,\n ) => _SubscriptionContract<T, Options>(options)\n },\n})\n\nexport function IsSubscriptionContract(\n contract: any,\n): contract is TAnySubscriptionContract {\n return Kind in contract && contract[Kind] === SubscriptionKind\n}\n"],"version":3,"file":"subscription.js"}
|
package/dist/utils.js
CHANGED
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAKA,OAAO,MAAM,aAAa,CACxBA,QACAC,SACG;AACH,QAAO,OAAO,YACZ,OAAO,QAAQ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG;EAAE,GAAG;EAAG,MAAM;EAAG,GAAG;CAAM,CAAC,EAAC,CACxE;AACF;AAED,OAAO,MAAM,eAAe,CAAIC,WAAc","names":["params: T","opts: { serviceName?: string; subscriptionName?: string }","schema: T"],"sources":["../src/utils.ts"],"sourcesContent":["export type ContractSchemaOptions = {\n title?: string\n description?: string\n}\n\nexport const applyNames = <T extends Record<string, { serviceName?: string }>>(\n params: T,\n opts: { serviceName?: string; subscriptionName?: string },\n) => {\n return Object.fromEntries(\n Object.entries(params).map(([k, v]) => [k, { ...v, name: k, ...opts }]),\n )\n}\n\nexport const createSchema = <T>(schema: T) => schema as T\n"],"version":3,"file":"utils.js"}
|
|
1
|
+
{"mappings":"AAKA,OAAO,MAAM,aAAa,CACxBA,QACAC,SACG;AACH,QAAO,OAAO,YACZ,OAAO,QAAQ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG;EAAE,GAAG;EAAG,MAAM;EAAG,GAAG;CAAM,CAAC,EAAC,CACxE;AACF;AAED,OAAO,MAAM,eAAe,CAAIC,WAAc,OAAO,OAAO,OAAO","names":["params: T","opts: { serviceName?: string; subscriptionName?: string }","schema: T"],"sources":["../src/utils.ts"],"sourcesContent":["export type ContractSchemaOptions = {\n title?: string\n description?: string\n}\n\nexport const applyNames = <T extends Record<string, { serviceName?: string }>>(\n params: T,\n opts: { serviceName?: string; subscriptionName?: string },\n) => {\n return Object.fromEntries(\n Object.entries(params).map(([k, v]) => [k, { ...v, name: k, ...opts }]),\n )\n}\n\nexport const createSchema = <T>(schema: T) => Object.freeze(schema) as T\n"],"version":3,"file":"utils.js"}
|
package/package.json
CHANGED
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
}
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@nmtjs/
|
|
12
|
-
"@nmtjs/
|
|
11
|
+
"@nmtjs/protocol": "0.12.1",
|
|
12
|
+
"@nmtjs/type": "0.12.1"
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"src",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"LICENSE.md",
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
|
-
"version": "0.
|
|
20
|
+
"version": "0.12.1",
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "neemata-build --root=./src './**/*.ts'",
|
|
23
23
|
"type-check": "tsc --noEmit"
|
package/src/schemas/api.ts
CHANGED
|
@@ -1,86 +1,96 @@
|
|
|
1
1
|
import { Kind } from '../constants.ts'
|
|
2
2
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
|
+
import type { TEventContract } from './event.ts'
|
|
3
4
|
import type { TAnyNamespaceContract, TNamespaceContract } from './namespace.ts'
|
|
5
|
+
import type { TProcedureContract } from './procedure.ts'
|
|
4
6
|
|
|
5
|
-
export const APIKind = 'NeemataAPI'
|
|
7
|
+
export const APIKind = Symbol('NeemataAPI')
|
|
6
8
|
|
|
7
|
-
export type TAnyAPIContract = TAPIContract<
|
|
9
|
+
export type TAnyAPIContract = TAPIContract<
|
|
10
|
+
Record<string, TAnyNamespaceContract>
|
|
11
|
+
>
|
|
8
12
|
|
|
9
|
-
export interface TAPIContract<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
: never
|
|
13
|
+
export interface TAPIContract<
|
|
14
|
+
Namespaces extends Record<string, TAnyNamespaceContract> = {},
|
|
15
|
+
> {
|
|
16
|
+
readonly [Kind]: typeof APIKind
|
|
17
|
+
readonly type: 'neemata:api'
|
|
18
|
+
readonly namespaces: {
|
|
19
|
+
[K in keyof Namespaces]: TNamespaceContract<
|
|
20
|
+
Namespaces[K]['procedures'],
|
|
21
|
+
Namespaces[K]['events'],
|
|
22
|
+
Extract<K, string>
|
|
23
|
+
>
|
|
21
24
|
}
|
|
22
|
-
timeout?: number
|
|
25
|
+
readonly timeout?: number
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
export const APIContract = <
|
|
26
|
-
|
|
27
|
-
>
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
const Options extends {
|
|
30
|
+
namespaces: Record<string, TAnyNamespaceContract>
|
|
31
|
+
timeout?: number
|
|
32
|
+
schemaOptions?: ContractSchemaOptions
|
|
33
|
+
},
|
|
34
|
+
>(
|
|
35
|
+
options: Options,
|
|
36
|
+
) => {
|
|
37
|
+
const { timeout, schemaOptions } = options
|
|
33
38
|
|
|
34
39
|
const _namespaces = {} as any
|
|
35
40
|
|
|
36
|
-
for (const namespaceKey in namespaces) {
|
|
37
|
-
const namespace = namespaces[namespaceKey]
|
|
41
|
+
for (const namespaceKey in options.namespaces) {
|
|
42
|
+
const namespace = options.namespaces[namespaceKey]
|
|
38
43
|
const _procedures = {} as any
|
|
39
44
|
for (const procedureKey in namespace.procedures) {
|
|
40
45
|
const procedure = namespace.procedures[procedureKey]
|
|
41
|
-
_procedures[procedureKey] =
|
|
46
|
+
_procedures[procedureKey] = createSchema<
|
|
47
|
+
TProcedureContract<
|
|
48
|
+
(typeof procedure)['input'],
|
|
49
|
+
(typeof procedure)['output'],
|
|
50
|
+
(typeof procedure)['stream'],
|
|
51
|
+
Extract<typeof procedureKey, string>,
|
|
52
|
+
Extract<typeof namespaceKey, string>
|
|
53
|
+
>
|
|
54
|
+
>({
|
|
55
|
+
...procedure,
|
|
42
56
|
name: procedureKey,
|
|
43
57
|
namespace: namespaceKey,
|
|
44
58
|
})
|
|
45
59
|
}
|
|
46
60
|
|
|
47
|
-
const _subscriptions = {} as any
|
|
48
|
-
for (const subscriptionKey in namespace.subscriptions) {
|
|
49
|
-
const subscription = namespace.subscriptions[subscriptionKey]
|
|
50
|
-
const _events = {} as any
|
|
51
|
-
for (const eventKey in subscription.events) {
|
|
52
|
-
const event = subscription.events[eventKey]
|
|
53
|
-
_events[eventKey] = Object.assign({}, event, {
|
|
54
|
-
name: eventKey,
|
|
55
|
-
namespace: namespaceKey,
|
|
56
|
-
subscription: subscriptionKey,
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
_subscriptions[subscriptionKey] = Object.assign({}, subscription, {
|
|
60
|
-
name: subscriptionKey,
|
|
61
|
-
namespace: namespaceKey,
|
|
62
|
-
events: _events,
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
61
|
const _events = {} as any
|
|
67
62
|
for (const eventKey in namespace.events) {
|
|
68
63
|
const event = namespace.events[eventKey]
|
|
69
|
-
_events[eventKey] =
|
|
64
|
+
_events[eventKey] = createSchema<
|
|
65
|
+
TEventContract<
|
|
66
|
+
(typeof event)['payload'],
|
|
67
|
+
Extract<typeof eventKey, string>,
|
|
68
|
+
undefined,
|
|
69
|
+
Extract<typeof namespaceKey, string>
|
|
70
|
+
>
|
|
71
|
+
>({
|
|
72
|
+
...event,
|
|
73
|
+
subscription: undefined,
|
|
70
74
|
name: eventKey,
|
|
71
75
|
namespace: namespaceKey,
|
|
72
76
|
})
|
|
73
77
|
}
|
|
74
78
|
|
|
75
|
-
_namespaces[namespaceKey] =
|
|
76
|
-
|
|
79
|
+
_namespaces[namespaceKey] = createSchema<
|
|
80
|
+
TNamespaceContract<
|
|
81
|
+
typeof _procedures,
|
|
82
|
+
typeof _events,
|
|
83
|
+
Extract<typeof namespaceKey, string>
|
|
84
|
+
>
|
|
85
|
+
>({
|
|
86
|
+
...namespace,
|
|
77
87
|
procedures: _procedures,
|
|
78
|
-
subscriptions: _subscriptions,
|
|
79
88
|
events: _events,
|
|
89
|
+
name: namespaceKey,
|
|
80
90
|
})
|
|
81
91
|
}
|
|
82
92
|
|
|
83
|
-
return createSchema<TAPIContract<
|
|
93
|
+
return createSchema<TAPIContract<Options['namespaces']>>({
|
|
84
94
|
...schemaOptions,
|
|
85
95
|
[Kind]: APIKind,
|
|
86
96
|
type: 'neemata:api',
|
package/src/schemas/event.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type BaseType, t } from '@nmtjs/type'
|
|
|
2
2
|
import { Kind } from '../constants.ts'
|
|
3
3
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
4
4
|
|
|
5
|
-
export const EventKind = 'NeemataEvent'
|
|
5
|
+
export const EventKind = Symbol('NeemataEvent')
|
|
6
6
|
|
|
7
7
|
export type TAnyEventContract = TEventContract<
|
|
8
8
|
BaseType,
|
|
@@ -17,12 +17,12 @@ export interface TEventContract<
|
|
|
17
17
|
Subscription extends string | undefined = undefined,
|
|
18
18
|
Namespace extends string | undefined = undefined,
|
|
19
19
|
> {
|
|
20
|
-
[Kind]: typeof EventKind
|
|
21
|
-
type: 'neemata:event'
|
|
22
|
-
name: Name
|
|
23
|
-
subscription: Subscription
|
|
24
|
-
namespace: Namespace
|
|
25
|
-
payload: Payload
|
|
20
|
+
readonly [Kind]: typeof EventKind
|
|
21
|
+
readonly type: 'neemata:event'
|
|
22
|
+
readonly name: Name
|
|
23
|
+
readonly subscription: Subscription
|
|
24
|
+
readonly namespace: Namespace
|
|
25
|
+
readonly payload: Payload
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export const EventContract = <
|
package/src/schemas/namespace.ts
CHANGED
|
@@ -3,90 +3,110 @@ import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
|
3
3
|
import type { TAnyEventContract, TEventContract } from './event.ts'
|
|
4
4
|
import type { TAnyProcedureContract, TProcedureContract } from './procedure.ts'
|
|
5
5
|
|
|
6
|
-
export const NamespaceKind = 'NeemataNamespace'
|
|
6
|
+
export const NamespaceKind = Symbol('NeemataNamespace')
|
|
7
7
|
|
|
8
|
-
export type TAnyNamespaceContract
|
|
9
|
-
Record<string,
|
|
10
|
-
|
|
8
|
+
export type TAnyNamespaceContract<
|
|
9
|
+
Procedures extends Record<string, TAnyProcedureContract> = Record<
|
|
10
|
+
string,
|
|
11
|
+
TAnyProcedureContract
|
|
12
|
+
>,
|
|
13
|
+
> = TNamespaceContract<
|
|
14
|
+
Procedures,
|
|
15
|
+
Record<string, TAnyEventContract>,
|
|
11
16
|
string | undefined
|
|
12
17
|
>
|
|
13
18
|
|
|
14
19
|
export interface TNamespaceContract<
|
|
15
|
-
Procedures extends Record<string,
|
|
16
|
-
Events extends Record<string,
|
|
20
|
+
Procedures extends Record<string, TAnyProcedureContract> = {},
|
|
21
|
+
Events extends Record<string, TAnyEventContract> = {},
|
|
17
22
|
Name extends string | undefined = undefined,
|
|
18
23
|
> {
|
|
19
|
-
[Kind]: typeof NamespaceKind
|
|
20
|
-
type: 'neemata:namespace'
|
|
21
|
-
name: Name
|
|
22
|
-
procedures: {
|
|
23
|
-
[K in keyof Procedures]:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
>
|
|
31
|
-
: never
|
|
24
|
+
readonly [Kind]: typeof NamespaceKind
|
|
25
|
+
readonly type: 'neemata:namespace'
|
|
26
|
+
readonly name: Name
|
|
27
|
+
readonly procedures: {
|
|
28
|
+
[K in keyof Procedures]: TProcedureContract<
|
|
29
|
+
Procedures[K]['input'],
|
|
30
|
+
Procedures[K]['output'],
|
|
31
|
+
Procedures[K]['stream'],
|
|
32
|
+
Extract<K, string>,
|
|
33
|
+
Name
|
|
34
|
+
>
|
|
32
35
|
}
|
|
33
|
-
events: {
|
|
34
|
-
[K in keyof Events]:
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
>
|
|
41
|
-
: never
|
|
36
|
+
readonly events: {
|
|
37
|
+
[K in keyof Events]: TEventContract<
|
|
38
|
+
Events[K]['payload'],
|
|
39
|
+
Extract<K, string>,
|
|
40
|
+
undefined,
|
|
41
|
+
Name
|
|
42
|
+
>
|
|
42
43
|
}
|
|
43
|
-
timeout?: number
|
|
44
|
+
readonly timeout?: number
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
export const NamespaceContract = <
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const {
|
|
58
|
-
|
|
59
|
-
events = {} as Events,
|
|
60
|
-
name,
|
|
61
|
-
timeout,
|
|
62
|
-
schemaOptions = {} as ContractSchemaOptions,
|
|
63
|
-
} = options ?? {}
|
|
64
|
-
const _events = {} as any
|
|
48
|
+
const Options extends {
|
|
49
|
+
procedures: Record<string, TAnyProcedureContract>
|
|
50
|
+
events: Record<string, TAnyEventContract>
|
|
51
|
+
name?: string
|
|
52
|
+
timeout?: number
|
|
53
|
+
schemaOptions?: ContractSchemaOptions
|
|
54
|
+
},
|
|
55
|
+
>(
|
|
56
|
+
options: Options,
|
|
57
|
+
) => {
|
|
58
|
+
const { name, timeout, schemaOptions = {} as ContractSchemaOptions } = options
|
|
59
|
+
const events: Record<string, any> = {}
|
|
65
60
|
|
|
66
|
-
for (const
|
|
67
|
-
const event = events[
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
for (const name in options.events) {
|
|
62
|
+
const event = options.events[name]
|
|
63
|
+
events[name] = createSchema<
|
|
64
|
+
TEventContract<
|
|
65
|
+
(typeof event)['payload'],
|
|
66
|
+
Extract<typeof name, string>,
|
|
67
|
+
undefined,
|
|
68
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
69
|
+
>
|
|
70
|
+
>({
|
|
71
|
+
...event,
|
|
72
|
+
subscription: undefined,
|
|
73
|
+
name: name as any,
|
|
74
|
+
namespace: options?.name as any,
|
|
71
75
|
})
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const procedures: Record<string, any> = {}
|
|
79
|
+
|
|
80
|
+
for (const name in options.procedures) {
|
|
81
|
+
const procedure: any = options.procedures[name]
|
|
82
|
+
procedures[name] = createSchema<
|
|
83
|
+
TProcedureContract<
|
|
84
|
+
(typeof procedure)['input'],
|
|
85
|
+
(typeof procedure)['output'],
|
|
86
|
+
(typeof procedure)['stream'],
|
|
87
|
+
Extract<typeof name, string>,
|
|
88
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
89
|
+
>
|
|
90
|
+
>({
|
|
91
|
+
...procedure,
|
|
92
|
+
name: name as any,
|
|
93
|
+
namespace: options?.name as any,
|
|
80
94
|
})
|
|
81
95
|
}
|
|
82
96
|
|
|
83
|
-
return createSchema<
|
|
97
|
+
return createSchema<
|
|
98
|
+
TNamespaceContract<
|
|
99
|
+
Options['procedures'],
|
|
100
|
+
Options['events'],
|
|
101
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
102
|
+
>
|
|
103
|
+
>({
|
|
84
104
|
...schemaOptions,
|
|
85
105
|
[Kind]: NamespaceKind,
|
|
86
106
|
type: 'neemata:namespace',
|
|
87
|
-
name: name as
|
|
88
|
-
procedures:
|
|
89
|
-
events:
|
|
107
|
+
name: name as any,
|
|
108
|
+
procedures: procedures as any,
|
|
109
|
+
events: events as any,
|
|
90
110
|
timeout,
|
|
91
111
|
})
|
|
92
112
|
}
|
package/src/schemas/procedure.ts
CHANGED
|
@@ -5,60 +5,66 @@ import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
|
5
5
|
export type TAnyProcedureContract = TProcedureContract<
|
|
6
6
|
BaseType,
|
|
7
7
|
BaseType,
|
|
8
|
-
BaseType,
|
|
8
|
+
BaseType | undefined,
|
|
9
9
|
string | undefined,
|
|
10
10
|
string | undefined
|
|
11
11
|
>
|
|
12
12
|
|
|
13
|
-
export const ProcedureKind = 'NeemataProcedure'
|
|
13
|
+
export const ProcedureKind = Symbol('NeemataProcedure')
|
|
14
14
|
|
|
15
15
|
export interface TProcedureContract<
|
|
16
|
-
Input extends BaseType
|
|
17
|
-
Output extends BaseType
|
|
18
|
-
Stream extends BaseType
|
|
16
|
+
Input extends BaseType,
|
|
17
|
+
Output extends BaseType,
|
|
18
|
+
Stream extends BaseType | undefined,
|
|
19
19
|
Name extends string | undefined = undefined,
|
|
20
20
|
Namespace extends string | undefined = undefined,
|
|
21
21
|
> {
|
|
22
|
-
[Kind]: typeof ProcedureKind
|
|
23
|
-
type: 'neemata:procedure'
|
|
24
|
-
name: Name
|
|
25
|
-
namespace: Namespace
|
|
26
|
-
input: Input
|
|
27
|
-
output: Output
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
readonly [Kind]: typeof ProcedureKind
|
|
23
|
+
readonly type: 'neemata:procedure'
|
|
24
|
+
readonly name: Name
|
|
25
|
+
readonly namespace: Namespace
|
|
26
|
+
readonly input: Input
|
|
27
|
+
readonly output: Output
|
|
28
|
+
readonly stream: Stream
|
|
29
|
+
readonly timeout?: number
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export const ProcedureContract = <
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}) => {
|
|
33
|
+
const Options extends {
|
|
34
|
+
input?: BaseType
|
|
35
|
+
output?: BaseType
|
|
36
|
+
stream?: BaseType | undefined
|
|
37
|
+
timeout?: number
|
|
38
|
+
schemaOptions?: ContractSchemaOptions
|
|
39
|
+
name?: string
|
|
40
|
+
},
|
|
41
|
+
>(
|
|
42
|
+
options: Options,
|
|
43
|
+
) => {
|
|
45
44
|
const {
|
|
46
|
-
input = t.never() as
|
|
47
|
-
output = t.never() as
|
|
48
|
-
stream =
|
|
45
|
+
input = t.never() as any,
|
|
46
|
+
output = t.never() as any,
|
|
47
|
+
stream = undefined as any,
|
|
48
|
+
name = undefined as any,
|
|
49
49
|
timeout,
|
|
50
50
|
schemaOptions = {},
|
|
51
|
-
name,
|
|
52
51
|
} = options
|
|
53
|
-
return createSchema<
|
|
52
|
+
return createSchema<
|
|
53
|
+
TProcedureContract<
|
|
54
|
+
Options['input'] extends BaseType ? Options['input'] : t.NeverType,
|
|
55
|
+
Options['output'] extends BaseType ? Options['output'] : t.NeverType,
|
|
56
|
+
Options['stream'] extends BaseType ? Options['stream'] : undefined,
|
|
57
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
58
|
+
>
|
|
59
|
+
>({
|
|
54
60
|
...schemaOptions,
|
|
55
61
|
[Kind]: ProcedureKind,
|
|
56
62
|
type: 'neemata:procedure',
|
|
57
63
|
input,
|
|
58
64
|
output,
|
|
59
65
|
stream,
|
|
66
|
+
name,
|
|
60
67
|
timeout,
|
|
61
|
-
name: name as Name,
|
|
62
68
|
namespace: undefined,
|
|
63
69
|
})
|
|
64
70
|
}
|
|
@@ -68,3 +74,9 @@ export function IsProcedureContract(
|
|
|
68
74
|
): contract is TAnyProcedureContract {
|
|
69
75
|
return Kind in contract && contract[Kind] === ProcedureKind
|
|
70
76
|
}
|
|
77
|
+
|
|
78
|
+
export function IsStreamProcedureContract(
|
|
79
|
+
contract: any,
|
|
80
|
+
): contract is TAnyProcedureContract {
|
|
81
|
+
return IsProcedureContract(contract) && typeof contract.stream !== 'undefined'
|
|
82
|
+
}
|
|
@@ -2,9 +2,12 @@ import { Kind } from '../constants.ts'
|
|
|
2
2
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
3
|
import type { TAnyEventContract, TEventContract } from './event.ts'
|
|
4
4
|
|
|
5
|
-
export const SubscriptionKind = 'NeemataSubscription'
|
|
5
|
+
export const SubscriptionKind = Symbol('NeemataSubscription')
|
|
6
6
|
|
|
7
|
-
export type SubcriptionOptions = Record<
|
|
7
|
+
export type SubcriptionOptions = Record<
|
|
8
|
+
string,
|
|
9
|
+
string | number | boolean
|
|
10
|
+
> | null
|
|
8
11
|
|
|
9
12
|
export type TAnySubscriptionContract = TSubscriptionContract<
|
|
10
13
|
SubcriptionOptions,
|
|
@@ -13,48 +16,79 @@ export type TAnySubscriptionContract = TSubscriptionContract<
|
|
|
13
16
|
>
|
|
14
17
|
|
|
15
18
|
export interface TSubscriptionContract<
|
|
16
|
-
Options extends SubcriptionOptions =
|
|
19
|
+
Options extends SubcriptionOptions = null,
|
|
17
20
|
Events extends Record<string, unknown> = {},
|
|
18
21
|
Name extends string | undefined = undefined,
|
|
19
22
|
> {
|
|
20
|
-
[Kind]: typeof SubscriptionKind
|
|
21
|
-
type: 'neemata:subscription'
|
|
22
|
-
name: Name
|
|
23
|
-
options: Options
|
|
24
|
-
events: {
|
|
23
|
+
readonly [Kind]: typeof SubscriptionKind
|
|
24
|
+
readonly type: 'neemata:subscription'
|
|
25
|
+
readonly name: Name
|
|
26
|
+
readonly options: Options
|
|
27
|
+
readonly events: {
|
|
25
28
|
[K in keyof Events]: Events[K] extends TAnyEventContract
|
|
26
29
|
? TEventContract<Events[K]['payload'], Extract<K, string>, Name>
|
|
27
30
|
: never
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const _SubscriptionContract = <
|
|
35
|
+
const Options extends {
|
|
36
|
+
events: Record<string, TAnyEventContract>
|
|
37
|
+
name?: string
|
|
38
|
+
schemaOptions?: ContractSchemaOptions
|
|
39
|
+
},
|
|
40
|
+
SubOpt extends SubcriptionOptions = null,
|
|
41
|
+
>(
|
|
42
|
+
options: Options,
|
|
43
|
+
) => {
|
|
44
|
+
const { schemaOptions = {}, name } = options
|
|
40
45
|
const _events = {} as any
|
|
41
|
-
for (const key in events) {
|
|
42
|
-
const event = events[key]
|
|
43
|
-
_events[key] =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
for (const key in options.events) {
|
|
47
|
+
const event = options.events[key]
|
|
48
|
+
_events[key] = createSchema<
|
|
49
|
+
TEventContract<
|
|
50
|
+
(typeof event)['payload'],
|
|
51
|
+
Extract<typeof key, string>,
|
|
52
|
+
undefined,
|
|
53
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
54
|
+
>
|
|
55
|
+
>({
|
|
56
|
+
...event,
|
|
57
|
+
name: key,
|
|
58
|
+
namespace: undefined,
|
|
59
|
+
subscription: name as any,
|
|
60
|
+
})
|
|
55
61
|
}
|
|
62
|
+
return createSchema<
|
|
63
|
+
TSubscriptionContract<
|
|
64
|
+
SubOpt,
|
|
65
|
+
Options['events'],
|
|
66
|
+
Options['name'] extends string ? Options['name'] : undefined
|
|
67
|
+
>
|
|
68
|
+
>({
|
|
69
|
+
...schemaOptions,
|
|
70
|
+
[Kind]: SubscriptionKind,
|
|
71
|
+
type: 'neemata:subscription',
|
|
72
|
+
events: _events,
|
|
73
|
+
name: name as any,
|
|
74
|
+
options: undefined as unknown as SubOpt,
|
|
75
|
+
})
|
|
56
76
|
}
|
|
57
77
|
|
|
78
|
+
export const SubscriptionContract = Object.assign(_SubscriptionContract, {
|
|
79
|
+
withOptions: <Options extends SubcriptionOptions>() => {
|
|
80
|
+
return <
|
|
81
|
+
T extends {
|
|
82
|
+
events: Record<string, TAnyEventContract>
|
|
83
|
+
name?: string
|
|
84
|
+
schemaOptions?: ContractSchemaOptions
|
|
85
|
+
},
|
|
86
|
+
>(
|
|
87
|
+
options: T,
|
|
88
|
+
) => _SubscriptionContract<T, Options>(options)
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
|
|
58
92
|
export function IsSubscriptionContract(
|
|
59
93
|
contract: any,
|
|
60
94
|
): contract is TAnySubscriptionContract {
|
package/src/utils.ts
CHANGED