@nmtjs/contract 0.5.3 → 0.6.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.
- package/dist/constants.js +1 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.js +18 -10
- package/dist/index.js.map +1 -1
- package/dist/schemas/api.js +60 -0
- package/dist/schemas/api.js.map +1 -0
- package/dist/schemas/event.js +11 -4
- package/dist/schemas/event.js.map +1 -1
- package/dist/schemas/namespace.js +53 -0
- package/dist/schemas/namespace.js.map +1 -0
- package/dist/schemas/procedure.js +15 -6
- package/dist/schemas/procedure.js.map +1 -1
- package/dist/schemas/subscription.js +20 -6
- package/dist/schemas/subscription.js.map +1 -1
- package/dist/types/blob.js +2 -2
- package/dist/types/blob.js.map +1 -1
- package/package.json +3 -3
- package/src/constants.ts +1 -0
- package/src/index.ts +17 -25
- package/src/schemas/api.ts +94 -0
- package/src/schemas/event.ts +38 -15
- package/src/schemas/namespace.ts +152 -0
- package/src/schemas/procedure.ts +69 -34
- package/src/schemas/subscription.ts +79 -43
- package/src/types/blob.ts +5 -5
- package/dist/schemas/service.js +0 -30
- package/dist/schemas/service.js.map +0 -1
- package/src/schemas/service.ts +0 -119
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const Kind = Symbol.for('neemata:kind');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/constants.ts"],"sourcesContent":["export const Kind = Symbol.for('neemata:kind')\n"],"names":["Kind","Symbol","for"],"mappings":"AAAA,OAAO,MAAMA,OAAOC,OAAOC,GAAG,CAAC,gBAAe"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
|
+
import { APIContract } from "./schemas/api.js";
|
|
1
2
|
import { EventContract } from "./schemas/event.js";
|
|
3
|
+
import { NamespaceContract } from "./schemas/namespace.js";
|
|
2
4
|
import { ProcedureContract } from "./schemas/procedure.js";
|
|
3
|
-
import { ServiceContract } from "./schemas/service.js";
|
|
4
5
|
import { SubscriptionContract } from "./schemas/subscription.js";
|
|
5
|
-
import {
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
import { BlobType } from "./types/blob.js";
|
|
7
|
+
export * from "./schemas/api.js";
|
|
8
|
+
export * from "./schemas/event.js";
|
|
9
|
+
export * from "./schemas/namespace.js";
|
|
10
|
+
export * from "./schemas/procedure.js";
|
|
11
|
+
export * from "./schemas/subscription.js";
|
|
12
|
+
export var contract;
|
|
13
|
+
(function(contract) {
|
|
14
|
+
contract.procedure = ProcedureContract;
|
|
15
|
+
contract.event = EventContract;
|
|
16
|
+
contract.subscription = SubscriptionContract;
|
|
17
|
+
contract.namespace = NamespaceContract;
|
|
18
|
+
contract.api = APIContract;
|
|
19
|
+
contract.blob = BlobType;
|
|
20
|
+
})(contract || (contract = {}));
|
|
21
|
+
export { contract as c };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { APIContract } from './schemas/api.ts'\nimport { EventContract } from './schemas/event.ts'\nimport { NamespaceContract } from './schemas/namespace.ts'\nimport { ProcedureContract } from './schemas/procedure.ts'\nimport { SubscriptionContract } from './schemas/subscription.ts'\nimport { BlobType } from './types/blob.ts'\n\nexport * from './schemas/api.ts'\nexport * from './schemas/event.ts'\nexport * from './schemas/namespace.ts'\nexport * from './schemas/procedure.ts'\nexport * from './schemas/subscription.ts'\n\nexport namespace contract {\n export const procedure = ProcedureContract\n export const event = EventContract\n export const subscription = SubscriptionContract\n export const namespace = NamespaceContract\n export const api = APIContract\n export const blob = BlobType\n}\n\nexport { contract as c }\n"],"names":["APIContract","EventContract","NamespaceContract","ProcedureContract","SubscriptionContract","BlobType","contract","procedure","event","subscription","namespace","api","blob","c"],"mappings":"AAAA,SAASA,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,aAAa,QAAQ,qBAAoB;AAClD,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,oBAAoB,QAAQ,4BAA2B;AAChE,SAASC,QAAQ,QAAQ,kBAAiB;AAE1C,cAAc,mBAAkB;AAChC,cAAc,qBAAoB;AAClC,cAAc,yBAAwB;AACtC,cAAc,yBAAwB;AACtC,cAAc,4BAA2B;;UAExBC;aACFC,YAAYJ;aACZK,QAAQP;aACRQ,eAAeL;aACfM,YAAYR;aACZS,MAAMX;aACNY,OAAOP;AACtB,GAPiBC,aAAAA;AASjB,SAASA,YAAYO,CAAC,GAAE"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Kind } from "../constants.js";
|
|
2
|
+
import { createSchema } from "../utils.js";
|
|
3
|
+
export const APIKind = 'NeemataAPI';
|
|
4
|
+
export const APIContract = (options)=>{
|
|
5
|
+
const { namespaces = {}, timeout = 1000, schemaOptions = {} } = options || {};
|
|
6
|
+
const _namespaces = {};
|
|
7
|
+
for(const namespaceKey in namespaces){
|
|
8
|
+
const namespace = namespaces[namespaceKey];
|
|
9
|
+
const _procedures = {};
|
|
10
|
+
for(const procedureKey in namespace.procedures){
|
|
11
|
+
const procedure = namespace.procedures[procedureKey];
|
|
12
|
+
_procedures[procedureKey] = Object.assign({}, procedure, {
|
|
13
|
+
name: procedureKey,
|
|
14
|
+
namespace: namespaceKey
|
|
15
|
+
});
|
|
16
|
+
}
|
|
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
|
+
const _events = {};
|
|
36
|
+
for(const eventKey in namespace.events){
|
|
37
|
+
const event = namespace.events[eventKey];
|
|
38
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
39
|
+
name: eventKey,
|
|
40
|
+
namespace: namespaceKey
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
_namespaces[namespaceKey] = Object.assign({}, namespace, {
|
|
44
|
+
name: namespaceKey,
|
|
45
|
+
procedures: _procedures,
|
|
46
|
+
subscriptions: _subscriptions,
|
|
47
|
+
events: _events
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return createSchema({
|
|
51
|
+
...schemaOptions,
|
|
52
|
+
[Kind]: APIKind,
|
|
53
|
+
type: 'neemata:api',
|
|
54
|
+
namespaces: _namespaces,
|
|
55
|
+
timeout
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
export function IsAPIContract(value) {
|
|
59
|
+
return Kind in value && value[Kind] === APIKind;
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/api.ts"],"sourcesContent":["import { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\nimport type { TAnyNamespaceContract, TNamespaceContract } from './namespace.ts'\n\nexport const APIKind = 'NeemataAPI'\n\nexport type TAnyAPIContract = TAPIContract<Record<string, any>>\n\nexport interface TAPIContract<Namespaces extends Record<string, unknown> = {}> {\n [Kind]: typeof APIKind\n type: 'neemata:api'\n namespaces: {\n [K in keyof Namespaces]: Namespaces[K] extends TAnyNamespaceContract\n ? TNamespaceContract<\n Namespaces[K]['procedures'],\n Namespaces[K]['subscriptions'],\n Namespaces[K]['events'],\n Extract<K, string>\n >\n : never\n }\n timeout?: number\n}\n\nexport const APIContract = <\n Namespaces extends Record<string, unknown> = {},\n>(options?: {\n namespaces?: Namespaces\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n}) => {\n const { namespaces = {}, timeout = 1000, schemaOptions = {} } = options || {}\n\n const _namespaces = {} as any\n\n for (const namespaceKey in namespaces) {\n const namespace = namespaces[namespaceKey]\n const _procedures = {} as any\n for (const procedureKey in namespace.procedures) {\n const procedure = namespace.procedures[procedureKey]\n _procedures[procedureKey] = Object.assign({}, procedure, {\n name: procedureKey,\n namespace: namespaceKey,\n })\n }\n\n const _subscriptions = {} as any\n for (const subscriptionKey in namespace.subscriptions) {\n const subscription = namespace.subscriptions[subscriptionKey]\n const _events = {} as any\n for (const eventKey in subscription.events) {\n const event = subscription.events[eventKey]\n _events[eventKey] = Object.assign({}, event, {\n name: eventKey,\n namespace: namespaceKey,\n subscription: subscriptionKey,\n })\n }\n _subscriptions[subscriptionKey] = Object.assign({}, subscription, {\n name: subscriptionKey,\n namespace: namespaceKey,\n events: _events,\n })\n }\n\n const _events = {} as any\n for (const eventKey in namespace.events) {\n const event = namespace.events[eventKey]\n _events[eventKey] = Object.assign({}, event, {\n name: eventKey,\n namespace: namespaceKey,\n })\n }\n\n _namespaces[namespaceKey] = Object.assign({}, namespace, {\n name: namespaceKey,\n procedures: _procedures,\n subscriptions: _subscriptions,\n events: _events,\n })\n }\n\n return createSchema<TAPIContract<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"],"names":["Kind","createSchema","APIKind","APIContract","options","namespaces","timeout","schemaOptions","_namespaces","namespaceKey","namespace","_procedures","procedureKey","procedures","procedure","Object","assign","name","_subscriptions","subscriptionKey","subscriptions","subscription","_events","eventKey","events","event","type","IsAPIContract","value"],"mappings":"AAAA,SAASA,IAAI,QAAQ,kBAAiB;AACtC,SAAqCC,YAAY,QAAQ,cAAa;AAGtE,OAAO,MAAMC,UAAU,aAAY;AAoBnC,OAAO,MAAMC,cAAc,CAEzBC;IAKA,MAAM,EAAEC,aAAa,CAAC,CAAC,EAAEC,UAAU,IAAI,EAAEC,gBAAgB,CAAC,CAAC,EAAE,GAAGH,WAAW,CAAC;IAE5E,MAAMI,cAAc,CAAC;IAErB,IAAK,MAAMC,gBAAgBJ,WAAY;QACrC,MAAMK,YAAYL,UAAU,CAACI,aAAa;QAC1C,MAAME,cAAc,CAAC;QACrB,IAAK,MAAMC,gBAAgBF,UAAUG,UAAU,CAAE;YAC/C,MAAMC,YAAYJ,UAAUG,UAAU,CAACD,aAAa;YACpDD,WAAW,CAACC,aAAa,GAAGG,OAAOC,MAAM,CAAC,CAAC,GAAGF,WAAW;gBACvDG,MAAML;gBACNF,WAAWD;YACb;QACF;QAEA,MAAMS,iBAAiB,CAAC;QACxB,IAAK,MAAMC,mBAAmBT,UAAUU,aAAa,CAAE;YACrD,MAAMC,eAAeX,UAAUU,aAAa,CAACD,gBAAgB;YAC7D,MAAMG,UAAU,CAAC;YACjB,IAAK,MAAMC,YAAYF,aAAaG,MAAM,CAAE;gBAC1C,MAAMC,QAAQJ,aAAaG,MAAM,CAACD,SAAS;gBAC3CD,OAAO,CAACC,SAAS,GAAGR,OAAOC,MAAM,CAAC,CAAC,GAAGS,OAAO;oBAC3CR,MAAMM;oBACNb,WAAWD;oBACXY,cAAcF;gBAChB;YACF;YACAD,cAAc,CAACC,gBAAgB,GAAGJ,OAAOC,MAAM,CAAC,CAAC,GAAGK,cAAc;gBAChEJ,MAAME;gBACNT,WAAWD;gBACXe,QAAQF;YACV;QACF;QAEA,MAAMA,UAAU,CAAC;QACjB,IAAK,MAAMC,YAAYb,UAAUc,MAAM,CAAE;YACvC,MAAMC,QAAQf,UAAUc,MAAM,CAACD,SAAS;YACxCD,OAAO,CAACC,SAAS,GAAGR,OAAOC,MAAM,CAAC,CAAC,GAAGS,OAAO;gBAC3CR,MAAMM;gBACNb,WAAWD;YACb;QACF;QAEAD,WAAW,CAACC,aAAa,GAAGM,OAAOC,MAAM,CAAC,CAAC,GAAGN,WAAW;YACvDO,MAAMR;YACNI,YAAYF;YACZS,eAAeF;YACfM,QAAQF;QACV;IACF;IAEA,OAAOrB,aAAuC;QAC5C,GAAGM,aAAa;QAChB,CAACP,KAAK,EAAEE;QACRwB,MAAM;QACNrB,YAAYG;QACZF;IACF;AACF,EAAC;AAED,OAAO,SAASqB,cAAcC,KAAU;IACtC,OAAO5B,QAAQ4B,SAASA,KAAK,CAAC5B,KAAK,KAAKE;AAC1C"}
|
package/dist/schemas/event.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
import { t } from '@nmtjs/type';
|
|
2
|
+
import { Kind } from "../constants.js";
|
|
1
3
|
import { createSchema } from "../utils.js";
|
|
2
4
|
export const EventKind = 'NeemataEvent';
|
|
3
|
-
export const EventContract = (
|
|
5
|
+
export const EventContract = (options)=>{
|
|
6
|
+
const { payload = t.never(), schemaOptions = {}, name } = options ?? {};
|
|
4
7
|
return createSchema({
|
|
5
8
|
...schemaOptions,
|
|
9
|
+
[Kind]: EventKind,
|
|
6
10
|
type: 'neemata:event',
|
|
7
11
|
payload,
|
|
8
|
-
name:
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
name: name,
|
|
13
|
+
subscription: undefined,
|
|
14
|
+
namespace: undefined
|
|
11
15
|
});
|
|
12
16
|
};
|
|
17
|
+
export function IsEventContract(value) {
|
|
18
|
+
return Kind in value && value[Kind] === EventKind;
|
|
19
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/event.ts"],"sourcesContent":["import type
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/event.ts"],"sourcesContent":["import { type BaseType, type NeverType, t } from '@nmtjs/type'\nimport { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\n\nexport const EventKind = '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 = NeverType,\n Name extends string | undefined = undefined,\n Subscription extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> {\n [Kind]: typeof EventKind\n type: 'neemata:event'\n name: Name\n subscription: Subscription\n namespace: Namespace\n 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"],"names":["t","Kind","createSchema","EventKind","EventContract","options","payload","never","schemaOptions","name","type","subscription","undefined","namespace","IsEventContract","value"],"mappings":"AAAA,SAAwCA,CAAC,QAAQ,cAAa;AAC9D,SAASC,IAAI,QAAQ,kBAAiB;AACtC,SAAqCC,YAAY,QAAQ,cAAa;AAEtE,OAAO,MAAMC,YAAY,eAAc;AAuBvC,OAAO,MAAMC,gBAAgB,CAG3BC;IAKA,MAAM,EACJC,UAAUN,EAAEO,KAAK,EAAwB,EACzCC,gBAAgB,CAAC,CAAC,EAClBC,IAAI,EACL,GAAGJ,WAAW,CAAC;IAChB,OAAOH,aAA4C;QACjD,GAAGM,aAAa;QAChB,CAACP,KAAK,EAAEE;QACRO,MAAM;QACNJ;QACAG,MAAMA;QACNE,cAAcC;QACdC,WAAWD;IACb;AACF,EAAC;AAED,OAAO,SAASE,gBAAgBC,KAAU;IACxC,OAAOd,QAAQc,SAASA,KAAK,CAACd,KAAK,KAAKE;AAC1C"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Kind } from "../constants.js";
|
|
2
|
+
import { createSchema } from "../utils.js";
|
|
3
|
+
export const NamespaceKind = 'NeemataNamespace';
|
|
4
|
+
export const NamespaceContract = (options)=>{
|
|
5
|
+
const { procedures = {}, subscriptions = {}, events = {}, name, timeout, schemaOptions = {} } = options ?? {};
|
|
6
|
+
const _events = {};
|
|
7
|
+
for(const eventKey in events){
|
|
8
|
+
const event = events[eventKey];
|
|
9
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
10
|
+
name: eventKey,
|
|
11
|
+
namespace: options?.name
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const _procedures = {};
|
|
15
|
+
for(const procedureKey in procedures){
|
|
16
|
+
const procedure = procedures[procedureKey];
|
|
17
|
+
_procedures[procedureKey] = Object.assign({}, procedure, {
|
|
18
|
+
name: procedureKey,
|
|
19
|
+
namespace: options?.name
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
const _subscriptions = {};
|
|
23
|
+
for(const subscriptionKey in subscriptions){
|
|
24
|
+
const subscription = subscriptions[subscriptionKey];
|
|
25
|
+
const _events = {};
|
|
26
|
+
for(const eventKey in subscription.events){
|
|
27
|
+
const event = subscription.events[eventKey];
|
|
28
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
29
|
+
name: eventKey,
|
|
30
|
+
subscription: subscriptionKey,
|
|
31
|
+
namespace: options?.name
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
_subscriptions[subscriptionKey] = Object.assign({}, subscription, {
|
|
35
|
+
name: subscriptionKey,
|
|
36
|
+
namespace: options?.name,
|
|
37
|
+
events: _events
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return createSchema({
|
|
41
|
+
...schemaOptions,
|
|
42
|
+
[Kind]: NamespaceKind,
|
|
43
|
+
type: 'neemata:namespace',
|
|
44
|
+
name: name,
|
|
45
|
+
procedures: _procedures,
|
|
46
|
+
subscriptions: _subscriptions,
|
|
47
|
+
events: _events,
|
|
48
|
+
timeout
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
export function IsNamespaceContract(value) {
|
|
52
|
+
return Kind in value && value[Kind] === NamespaceKind;
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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'\nimport type {\n TAnySubscriptionContract,\n TSubscriptionContract,\n} from './subscription.ts'\n\nexport const NamespaceKind = 'NeemataNamespace'\n\nexport type TAnyNamespaceContract = TNamespaceContract<\n Record<string, any>,\n Record<string, any>,\n Record<string, any>,\n string | undefined\n>\n\nexport interface TNamespaceContract<\n Procedures extends Record<string, unknown> = {},\n Subscriptions extends Record<string, unknown> = {},\n Events extends Record<string, unknown> = {},\n Name extends string | undefined = undefined,\n> {\n [Kind]: typeof NamespaceKind\n type: 'neemata:namespace'\n name: Name\n procedures: {\n [K in keyof Procedures]: Procedures[K] extends TAnyProcedureContract\n ? TProcedureContract<\n Procedures[K]['input'],\n Procedures[K]['output'],\n Procedures[K]['stream'],\n Extract<K, string>,\n Name\n >\n : never\n }\n subscriptions: {\n [K in keyof Subscriptions]: Subscriptions[K] extends TAnySubscriptionContract\n ? TSubscriptionContract<\n Subscriptions[K]['input'],\n Subscriptions[K]['output'],\n Subscriptions[K]['options'],\n {\n [E in keyof Subscriptions[K]['events']]: Subscriptions[K]['events'][E] extends TAnyEventContract\n ? TEventContract<\n Subscriptions[K]['events'][E]['payload'],\n Extract<E, string>,\n Extract<K, string>,\n Name\n >\n : never\n },\n Extract<K, string>,\n Name\n >\n : never\n }\n events: {\n [K in keyof Events]: Events[K] extends TAnyEventContract\n ? TEventContract<\n Events[K]['payload'],\n Extract<K, string>,\n undefined,\n Name\n >\n : never\n }\n timeout?: number\n}\n\nexport const NamespaceContract = <\n Procedures extends Record<string, unknown> = {},\n Subscriptions extends Record<string, unknown> = {},\n Events extends Record<string, unknown> = {},\n Name extends string | undefined = undefined,\n>(options?: {\n procedures?: Procedures\n subscriptions?: Subscriptions\n events?: Events\n name?: Name\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n}) => {\n const {\n procedures = {} as Procedures,\n subscriptions = {} as Subscriptions,\n events = {} as Events,\n name,\n timeout,\n schemaOptions = {} as ContractSchemaOptions,\n } = options ?? {}\n const _events = {} as any\n\n for (const eventKey in events) {\n const event = events[eventKey]\n _events[eventKey] = Object.assign({}, event, {\n name: eventKey,\n namespace: options?.name,\n })\n }\n\n const _procedures = {} as any\n for (const procedureKey in procedures) {\n const procedure: any = procedures[procedureKey]\n _procedures[procedureKey] = Object.assign({}, procedure, {\n name: procedureKey,\n namespace: options?.name,\n })\n }\n\n const _subscriptions = {} as any\n for (const subscriptionKey in subscriptions) {\n const subscription: any = subscriptions[subscriptionKey]\n const _events = {} as any\n\n for (const eventKey in subscription.events) {\n const event = subscription.events[eventKey]\n _events[eventKey] = Object.assign({}, event, {\n name: eventKey,\n subscription: subscriptionKey,\n namespace: options?.name,\n })\n }\n\n _subscriptions[subscriptionKey] = Object.assign({}, subscription, {\n name: subscriptionKey,\n namespace: options?.name,\n events: _events,\n })\n }\n\n return createSchema<\n TNamespaceContract<Procedures, Subscriptions, Events, Name>\n >({\n ...schemaOptions,\n [Kind]: NamespaceKind,\n type: 'neemata:namespace',\n name: name as Name,\n procedures: _procedures,\n subscriptions: _subscriptions,\n events: _events,\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"],"names":["Kind","createSchema","NamespaceKind","NamespaceContract","options","procedures","subscriptions","events","name","timeout","schemaOptions","_events","eventKey","event","Object","assign","namespace","_procedures","procedureKey","procedure","_subscriptions","subscriptionKey","subscription","type","IsNamespaceContract","value"],"mappings":"AAAA,SAASA,IAAI,QAAQ,kBAAiB;AACtC,SAAqCC,YAAY,QAAQ,cAAa;AAQtE,OAAO,MAAMC,gBAAgB,mBAAkB;AA+D/C,OAAO,MAAMC,oBAAoB,CAK/BC;IAQA,MAAM,EACJC,aAAa,CAAC,CAAe,EAC7BC,gBAAgB,CAAC,CAAkB,EACnCC,SAAS,CAAC,CAAW,EACrBC,IAAI,EACJC,OAAO,EACPC,gBAAgB,CAAC,CAA0B,EAC5C,GAAGN,WAAW,CAAC;IAChB,MAAMO,UAAU,CAAC;IAEjB,IAAK,MAAMC,YAAYL,OAAQ;QAC7B,MAAMM,QAAQN,MAAM,CAACK,SAAS;QAC9BD,OAAO,CAACC,SAAS,GAAGE,OAAOC,MAAM,CAAC,CAAC,GAAGF,OAAO;YAC3CL,MAAMI;YACNI,WAAWZ,SAASI;QACtB;IACF;IAEA,MAAMS,cAAc,CAAC;IACrB,IAAK,MAAMC,gBAAgBb,WAAY;QACrC,MAAMc,YAAiBd,UAAU,CAACa,aAAa;QAC/CD,WAAW,CAACC,aAAa,GAAGJ,OAAOC,MAAM,CAAC,CAAC,GAAGI,WAAW;YACvDX,MAAMU;YACNF,WAAWZ,SAASI;QACtB;IACF;IAEA,MAAMY,iBAAiB,CAAC;IACxB,IAAK,MAAMC,mBAAmBf,cAAe;QAC3C,MAAMgB,eAAoBhB,aAAa,CAACe,gBAAgB;QACxD,MAAMV,UAAU,CAAC;QAEjB,IAAK,MAAMC,YAAYU,aAAaf,MAAM,CAAE;YAC1C,MAAMM,QAAQS,aAAaf,MAAM,CAACK,SAAS;YAC3CD,OAAO,CAACC,SAAS,GAAGE,OAAOC,MAAM,CAAC,CAAC,GAAGF,OAAO;gBAC3CL,MAAMI;gBACNU,cAAcD;gBACdL,WAAWZ,SAASI;YACtB;QACF;QAEAY,cAAc,CAACC,gBAAgB,GAAGP,OAAOC,MAAM,CAAC,CAAC,GAAGO,cAAc;YAChEd,MAAMa;YACNL,WAAWZ,SAASI;YACpBD,QAAQI;QACV;IACF;IAEA,OAAOV,aAEL;QACA,GAAGS,aAAa;QAChB,CAACV,KAAK,EAAEE;QACRqB,MAAM;QACNf,MAAMA;QACNH,YAAYY;QACZX,eAAec;QACfb,QAAQI;QACRF;IACF;AACF,EAAC;AAED,OAAO,SAASe,oBACdC,KAAU;IAEV,OAAOzB,QAAQyB,SAASA,KAAK,CAACzB,KAAK,KAAKE;AAC1C"}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { t } from '@nmtjs/type';
|
|
2
|
+
import { Kind } from "../constants.js";
|
|
3
|
+
import { createSchema } from "../utils.js";
|
|
4
|
+
export const ProcedureKind = 'NeemataProcedure';
|
|
5
|
+
export const ProcedureContract = (options)=>{
|
|
6
|
+
const { input = t.never(), output = t.never(), stream = t.never(), timeout, schemaOptions = {}, name } = options;
|
|
7
|
+
return createSchema({
|
|
3
8
|
...schemaOptions,
|
|
9
|
+
[Kind]: ProcedureKind,
|
|
4
10
|
type: 'neemata:procedure',
|
|
5
11
|
input,
|
|
6
12
|
output,
|
|
13
|
+
stream,
|
|
7
14
|
timeout,
|
|
8
|
-
name:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
15
|
+
name: name,
|
|
16
|
+
namespace: undefined
|
|
17
|
+
});
|
|
12
18
|
};
|
|
19
|
+
export function IsProcedureContract(contract) {
|
|
20
|
+
return Kind in contract && contract[Kind] === ProcedureKind;
|
|
21
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/procedure.ts"],"sourcesContent":["import type
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/procedure.ts"],"sourcesContent":["import { type BaseType, type NeverType, t } from '@nmtjs/type'\nimport { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\n\nexport type TAnyBaseProcedureContract = TBaseProcedureContract<\n string,\n BaseType,\n BaseType,\n string | undefined,\n string | undefined\n>\n\nexport interface TBaseProcedureContract<\n Type extends string,\n Input extends BaseType,\n Output extends BaseType,\n Name extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> {\n [Kind]: string\n type: Type\n name: Name\n namespace: Namespace\n input: Input\n output: Output\n timeout?: number\n}\n\nexport const ProcedureKind = 'NeemataProcedure'\n\nexport type TAnyProcedureContract = TProcedureContract<\n BaseType,\n BaseType,\n BaseType,\n string | undefined,\n string | undefined\n>\n\nexport interface TProcedureContract<\n Input extends BaseType = NeverType,\n Output extends BaseType = NeverType,\n Stream extends BaseType = NeverType,\n Name extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> extends TBaseProcedureContract<\n 'neemata:procedure',\n Input,\n Output,\n Name,\n Namespace\n > {\n [Kind]: typeof ProcedureKind\n stream: Stream\n}\n\nexport const ProcedureContract = <\n Input extends BaseType = NeverType,\n Output extends BaseType = NeverType,\n Stream extends BaseType = NeverType,\n Name extends string | undefined = undefined,\n>(options: {\n input?: Input\n output?: Output\n stream?: Stream\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n name?: Name\n}) => {\n const {\n input = t.never() as unknown as Input,\n output = t.never() as unknown as Output,\n stream = t.never() as unknown as Stream,\n timeout,\n schemaOptions = {},\n name,\n } = options\n return createSchema<TProcedureContract<Input, Output, Stream, Name>>({\n ...schemaOptions,\n [Kind]: ProcedureKind,\n type: 'neemata:procedure',\n input,\n output,\n stream,\n timeout,\n name: name as Name,\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"],"names":["t","Kind","createSchema","ProcedureKind","ProcedureContract","options","input","never","output","stream","timeout","schemaOptions","name","type","namespace","undefined","IsProcedureContract","contract"],"mappings":"AAAA,SAAwCA,CAAC,QAAQ,cAAa;AAC9D,SAASC,IAAI,QAAQ,kBAAiB;AACtC,SAAqCC,YAAY,QAAQ,cAAa;AA0BtE,OAAO,MAAMC,gBAAgB,mBAAkB;AA2B/C,OAAO,MAAMC,oBAAoB,CAK/BC;IAQA,MAAM,EACJC,QAAQN,EAAEO,KAAK,EAAsB,EACrCC,SAASR,EAAEO,KAAK,EAAuB,EACvCE,SAAST,EAAEO,KAAK,EAAuB,EACvCG,OAAO,EACPC,gBAAgB,CAAC,CAAC,EAClBC,IAAI,EACL,GAAGP;IACJ,OAAOH,aAA8D;QACnE,GAAGS,aAAa;QAChB,CAACV,KAAK,EAAEE;QACRU,MAAM;QACNP;QACAE;QACAC;QACAC;QACAE,MAAMA;QACNE,WAAWC;IACb;AACF,EAAC;AAED,OAAO,SAASC,oBACdC,QAAa;IAEb,OAAOhB,QAAQgB,YAAYA,QAAQ,CAAChB,KAAK,KAAKE;AAChD"}
|
|
@@ -1,17 +1,31 @@
|
|
|
1
|
+
import { t } from '@nmtjs/type';
|
|
2
|
+
import { Kind } from "../constants.js";
|
|
1
3
|
import { createSchema } from "../utils.js";
|
|
2
|
-
export const
|
|
4
|
+
export const SubscriptionKind = 'NeemataSubscription';
|
|
5
|
+
export const SubscriptionContract = (options)=>{
|
|
6
|
+
const { input = t.never(), output = t.never(), events = {}, timeout, schemaOptions = {}, name } = options ?? {};
|
|
7
|
+
const _events = {};
|
|
8
|
+
for(const key in events){
|
|
9
|
+
const event = events[key];
|
|
10
|
+
_events[key] = Object.assign({}, event, {
|
|
11
|
+
subscription: name
|
|
12
|
+
});
|
|
13
|
+
}
|
|
3
14
|
return {
|
|
4
15
|
$withOptions: ()=>createSchema({
|
|
5
16
|
...schemaOptions,
|
|
17
|
+
[Kind]: SubscriptionKind,
|
|
6
18
|
type: 'neemata:subscription',
|
|
7
19
|
input,
|
|
8
20
|
output,
|
|
9
|
-
events,
|
|
21
|
+
events: _events,
|
|
10
22
|
timeout,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
transports: undefined
|
|
23
|
+
name: name,
|
|
24
|
+
namespace: undefined,
|
|
25
|
+
options: undefined
|
|
15
26
|
})
|
|
16
27
|
};
|
|
17
28
|
};
|
|
29
|
+
export function IsSubscriptionContract(contract) {
|
|
30
|
+
return Kind in contract && contract[Kind] === SubscriptionKind;
|
|
31
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/subscription.ts"],"sourcesContent":["import type
|
|
1
|
+
{"version":3,"sources":["../../../src/schemas/subscription.ts"],"sourcesContent":["import { type BaseType, type NeverType, t } from '@nmtjs/type'\nimport { Kind } from '../constants.ts'\nimport { type ContractSchemaOptions, createSchema } from '../utils.ts'\nimport type { TAnyEventContract, TEventContract } from './event.ts'\nimport type { TBaseProcedureContract } from './procedure.ts'\n\nexport const SubscriptionKind = 'NeemataSubscription'\n\nexport type SubcriptionOptions = Record<string, string | number | boolean>\n\nexport type TAnySubscriptionContract = TSubscriptionContract<\n BaseType,\n BaseType,\n SubcriptionOptions,\n Record<string, unknown>,\n string | undefined,\n string | undefined\n>\n\nexport interface TSubscriptionContract<\n Input extends BaseType = NeverType,\n Output extends BaseType = NeverType,\n Options extends SubcriptionOptions = {},\n Events extends Record<string, unknown> = {},\n Name extends string | undefined = undefined,\n Namespace extends string | undefined = undefined,\n> extends TBaseProcedureContract<\n 'neemata:subscription',\n Input,\n Output,\n Name,\n Namespace\n > {\n [Kind]: typeof SubscriptionKind\n options: Options\n events: {\n [K in keyof Events]: Events[K] extends TAnyEventContract\n ? TEventContract<\n Events[K]['payload'],\n Extract<K, string>,\n Name,\n Namespace\n >\n : never\n }\n}\n\nexport const SubscriptionContract = <\n Input extends BaseType = NeverType,\n Output extends BaseType = NeverType,\n Events extends Record<string, unknown> = {},\n Name extends string | undefined = undefined,\n>(options?: {\n input?: Input\n output?: Output\n events?: Events\n timeout?: number\n schemaOptions?: ContractSchemaOptions\n name?: Name\n}) => {\n const {\n input = t.never() as unknown as Input,\n output = t.never() as unknown as Output,\n events = {} as Events,\n timeout,\n schemaOptions = {},\n name,\n } = options ?? {}\n\n const _events = {} as any\n for (const key in events) {\n const event = events[key]\n _events[key] = Object.assign({}, event, { subscription: name })\n }\n return {\n $withOptions: <Options extends SubcriptionOptions>() =>\n createSchema<TSubscriptionContract<Input, Output, Options, Events, Name>>(\n {\n ...schemaOptions,\n [Kind]: SubscriptionKind,\n type: 'neemata:subscription',\n input,\n output,\n events: _events,\n timeout,\n name: name as Name,\n namespace: undefined,\n options: undefined as unknown as Options,\n },\n ),\n }\n}\n\nexport function IsSubscriptionContract(\n contract: any,\n): contract is TAnySubscriptionContract {\n return Kind in contract && contract[Kind] === SubscriptionKind\n}\n"],"names":["t","Kind","createSchema","SubscriptionKind","SubscriptionContract","options","input","never","output","events","timeout","schemaOptions","name","_events","key","event","Object","assign","subscription","$withOptions","type","namespace","undefined","IsSubscriptionContract","contract"],"mappings":"AAAA,SAAwCA,CAAC,QAAQ,cAAa;AAC9D,SAASC,IAAI,QAAQ,kBAAiB;AACtC,SAAqCC,YAAY,QAAQ,cAAa;AAItE,OAAO,MAAMC,mBAAmB,sBAAqB;AAyCrD,OAAO,MAAMC,uBAAuB,CAKlCC;IAQA,MAAM,EACJC,QAAQN,EAAEO,KAAK,EAAsB,EACrCC,SAASR,EAAEO,KAAK,EAAuB,EACvCE,SAAS,CAAC,CAAW,EACrBC,OAAO,EACPC,gBAAgB,CAAC,CAAC,EAClBC,IAAI,EACL,GAAGP,WAAW,CAAC;IAEhB,MAAMQ,UAAU,CAAC;IACjB,IAAK,MAAMC,OAAOL,OAAQ;QACxB,MAAMM,QAAQN,MAAM,CAACK,IAAI;QACzBD,OAAO,CAACC,IAAI,GAAGE,OAAOC,MAAM,CAAC,CAAC,GAAGF,OAAO;YAAEG,cAAcN;QAAK;IAC/D;IACA,OAAO;QACLO,cAAc,IACZjB,aACE;gBACE,GAAGS,aAAa;gBAChB,CAACV,KAAK,EAAEE;gBACRiB,MAAM;gBACNd;gBACAE;gBACAC,QAAQI;gBACRH;gBACAE,MAAMA;gBACNS,WAAWC;gBACXjB,SAASiB;YACX;IAEN;AACF,EAAC;AAED,OAAO,SAASC,uBACdC,QAAa;IAEb,OAAOvB,QAAQuB,YAAYA,QAAQ,CAACvB,KAAK,KAAKE;AAChD"}
|
package/dist/types/blob.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t } from '@nmtjs/type';
|
|
2
|
-
export const BlobKind = '
|
|
3
|
-
export const
|
|
2
|
+
export const BlobKind = 'ProtocolBlob';
|
|
3
|
+
export const BlobType = (options = {})=>t.custom((value)=>{
|
|
4
4
|
if ('metadata' in value) {
|
|
5
5
|
if (options.maxSize) {
|
|
6
6
|
const size = value.metadata.size;
|
package/dist/types/blob.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/blob.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/blob.ts"],"sourcesContent":["import type { ProtocolBlobInterface } from '@nmtjs/protocol/common'\nimport { t } from '@nmtjs/type'\n\nexport const BlobKind = 'ProtocolBlob'\n\nexport interface BlobOptions {\n maxSize?: number\n contentType?: string\n}\n\nexport const BlobType = (options: BlobOptions = {}) =>\n t.custom<ProtocolBlobInterface>(\n (value) => {\n // TODO: this should be registered separately for server and client\n // ref: https://github.com/sinclairzx81/typebox/issues/977\n if ('metadata' in value) {\n if (options.maxSize) {\n const size = (value as ProtocolBlobInterface).metadata.size\n if (size === -1 || size > options.maxSize) {\n throw new Error('Blob size unknown or exceeds maximum allowed size')\n }\n }\n }\n return value\n },\n (value) => value,\n )\n"],"names":["t","BlobKind","BlobType","options","custom","value","maxSize","size","metadata","Error"],"mappings":"AACA,SAASA,CAAC,QAAQ,cAAa;AAE/B,OAAO,MAAMC,WAAW,eAAc;AAOtC,OAAO,MAAMC,WAAW,CAACC,UAAuB,CAAC,CAAC,GAChDH,EAAEI,MAAM,CACN,CAACC;QAGC,IAAI,cAAcA,OAAO;YACvB,IAAIF,QAAQG,OAAO,EAAE;gBACnB,MAAMC,OAAO,AAACF,MAAgCG,QAAQ,CAACD,IAAI;gBAC3D,IAAIA,SAAS,CAAC,KAAKA,OAAOJ,QAAQG,OAAO,EAAE;oBACzC,MAAM,IAAIG,MAAM;gBAClB;YACF;QACF;QACA,OAAOJ;IACT,GACA,CAACA,QAAUA,OACZ"}
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@nmtjs/type": "0.
|
|
13
|
-
"@nmtjs/
|
|
12
|
+
"@nmtjs/type": "0.6.0",
|
|
13
|
+
"@nmtjs/protocol": "0.6.0"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"src",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"LICENSE.md",
|
|
20
20
|
"README.md"
|
|
21
21
|
],
|
|
22
|
-
"version": "0.
|
|
22
|
+
"version": "0.6.0",
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
25
25
|
"type-check": "tsc --noEmit"
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const Kind = Symbol.for('neemata:kind')
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { APIContract } from './schemas/api.ts'
|
|
2
|
+
import { EventContract } from './schemas/event.ts'
|
|
3
|
+
import { NamespaceContract } from './schemas/namespace.ts'
|
|
4
|
+
import { ProcedureContract } from './schemas/procedure.ts'
|
|
5
|
+
import { SubscriptionContract } from './schemas/subscription.ts'
|
|
6
|
+
import { BlobType } from './types/blob.ts'
|
|
2
7
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import { ServiceContract, type TServiceContract } from './schemas/service.ts'
|
|
9
|
-
import {
|
|
10
|
-
type SubcriptionOptions,
|
|
11
|
-
SubscriptionContract,
|
|
12
|
-
type TSubscriptionContract,
|
|
13
|
-
} from './schemas/subscription.ts'
|
|
14
|
-
import { blob as blobType } from './types/blob.ts'
|
|
8
|
+
export * from './schemas/api.ts'
|
|
9
|
+
export * from './schemas/event.ts'
|
|
10
|
+
export * from './schemas/namespace.ts'
|
|
11
|
+
export * from './schemas/procedure.ts'
|
|
12
|
+
export * from './schemas/subscription.ts'
|
|
15
13
|
|
|
16
|
-
export
|
|
17
|
-
TEventContract,
|
|
18
|
-
TProcedureContract,
|
|
19
|
-
TBaseProcedureContract,
|
|
20
|
-
TServiceContract,
|
|
21
|
-
TSubscriptionContract,
|
|
22
|
-
SubcriptionOptions,
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export namespace c {
|
|
14
|
+
export namespace contract {
|
|
26
15
|
export const procedure = ProcedureContract
|
|
27
16
|
export const event = EventContract
|
|
28
17
|
export const subscription = SubscriptionContract
|
|
29
|
-
export const
|
|
30
|
-
export const
|
|
18
|
+
export const namespace = NamespaceContract
|
|
19
|
+
export const api = APIContract
|
|
20
|
+
export const blob = BlobType
|
|
31
21
|
}
|
|
22
|
+
|
|
23
|
+
export { contract as c }
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Kind } from '../constants.ts'
|
|
2
|
+
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
|
+
import type { TAnyNamespaceContract, TNamespaceContract } from './namespace.ts'
|
|
4
|
+
|
|
5
|
+
export const APIKind = 'NeemataAPI'
|
|
6
|
+
|
|
7
|
+
export type TAnyAPIContract = TAPIContract<Record<string, any>>
|
|
8
|
+
|
|
9
|
+
export interface TAPIContract<Namespaces extends Record<string, unknown> = {}> {
|
|
10
|
+
[Kind]: typeof APIKind
|
|
11
|
+
type: 'neemata:api'
|
|
12
|
+
namespaces: {
|
|
13
|
+
[K in keyof Namespaces]: Namespaces[K] extends TAnyNamespaceContract
|
|
14
|
+
? TNamespaceContract<
|
|
15
|
+
Namespaces[K]['procedures'],
|
|
16
|
+
Namespaces[K]['subscriptions'],
|
|
17
|
+
Namespaces[K]['events'],
|
|
18
|
+
Extract<K, string>
|
|
19
|
+
>
|
|
20
|
+
: never
|
|
21
|
+
}
|
|
22
|
+
timeout?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const APIContract = <
|
|
26
|
+
Namespaces extends Record<string, unknown> = {},
|
|
27
|
+
>(options?: {
|
|
28
|
+
namespaces?: Namespaces
|
|
29
|
+
timeout?: number
|
|
30
|
+
schemaOptions?: ContractSchemaOptions
|
|
31
|
+
}) => {
|
|
32
|
+
const { namespaces = {}, timeout = 1000, schemaOptions = {} } = options || {}
|
|
33
|
+
|
|
34
|
+
const _namespaces = {} as any
|
|
35
|
+
|
|
36
|
+
for (const namespaceKey in namespaces) {
|
|
37
|
+
const namespace = namespaces[namespaceKey]
|
|
38
|
+
const _procedures = {} as any
|
|
39
|
+
for (const procedureKey in namespace.procedures) {
|
|
40
|
+
const procedure = namespace.procedures[procedureKey]
|
|
41
|
+
_procedures[procedureKey] = Object.assign({}, procedure, {
|
|
42
|
+
name: procedureKey,
|
|
43
|
+
namespace: namespaceKey,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
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
|
+
const _events = {} as any
|
|
67
|
+
for (const eventKey in namespace.events) {
|
|
68
|
+
const event = namespace.events[eventKey]
|
|
69
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
70
|
+
name: eventKey,
|
|
71
|
+
namespace: namespaceKey,
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_namespaces[namespaceKey] = Object.assign({}, namespace, {
|
|
76
|
+
name: namespaceKey,
|
|
77
|
+
procedures: _procedures,
|
|
78
|
+
subscriptions: _subscriptions,
|
|
79
|
+
events: _events,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return createSchema<TAPIContract<Namespaces>>({
|
|
84
|
+
...schemaOptions,
|
|
85
|
+
[Kind]: APIKind,
|
|
86
|
+
type: 'neemata:api',
|
|
87
|
+
namespaces: _namespaces,
|
|
88
|
+
timeout,
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function IsAPIContract(value: any): value is TAnyAPIContract {
|
|
93
|
+
return Kind in value && value[Kind] === APIKind
|
|
94
|
+
}
|
package/src/schemas/event.ts
CHANGED
|
@@ -1,31 +1,54 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type BaseType, type NeverType, t } from '@nmtjs/type'
|
|
2
|
+
import { Kind } from '../constants.ts'
|
|
2
3
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
4
|
|
|
4
5
|
export const EventKind = 'NeemataEvent'
|
|
5
6
|
|
|
7
|
+
export type TAnyEventContract = TEventContract<
|
|
8
|
+
BaseType,
|
|
9
|
+
string | undefined,
|
|
10
|
+
string | undefined,
|
|
11
|
+
string | undefined
|
|
12
|
+
>
|
|
13
|
+
|
|
6
14
|
export interface TEventContract<
|
|
7
|
-
Payload extends BaseType =
|
|
8
|
-
Name extends string | undefined =
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
Payload extends BaseType = NeverType,
|
|
16
|
+
Name extends string | undefined = undefined,
|
|
17
|
+
Subscription extends string | undefined = undefined,
|
|
18
|
+
Namespace extends string | undefined = undefined,
|
|
11
19
|
> {
|
|
20
|
+
[Kind]: typeof EventKind
|
|
12
21
|
type: 'neemata:event'
|
|
13
22
|
name: Name
|
|
14
|
-
|
|
15
|
-
|
|
23
|
+
subscription: Subscription
|
|
24
|
+
namespace: Namespace
|
|
16
25
|
payload: Payload
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
export const EventContract = <
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
export const EventContract = <
|
|
29
|
+
Payload extends BaseType,
|
|
30
|
+
Name extends string | undefined = undefined,
|
|
31
|
+
>(options?: {
|
|
32
|
+
payload?: Payload
|
|
33
|
+
schemaOptions?: ContractSchemaOptions
|
|
34
|
+
name?: Name
|
|
35
|
+
}) => {
|
|
36
|
+
const {
|
|
37
|
+
payload = t.never() as unknown as Payload,
|
|
38
|
+
schemaOptions = {},
|
|
39
|
+
name,
|
|
40
|
+
} = options ?? {}
|
|
41
|
+
return createSchema<TEventContract<Payload, Name>>({
|
|
24
42
|
...schemaOptions,
|
|
43
|
+
[Kind]: EventKind,
|
|
25
44
|
type: 'neemata:event',
|
|
26
45
|
payload,
|
|
27
|
-
name:
|
|
28
|
-
|
|
29
|
-
|
|
46
|
+
name: name as Name,
|
|
47
|
+
subscription: undefined,
|
|
48
|
+
namespace: undefined,
|
|
30
49
|
})
|
|
31
50
|
}
|
|
51
|
+
|
|
52
|
+
export function IsEventContract(value: any): value is TAnyEventContract {
|
|
53
|
+
return Kind in value && value[Kind] === EventKind
|
|
54
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Kind } from '../constants.ts'
|
|
2
|
+
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
|
+
import type { TAnyEventContract, TEventContract } from './event.ts'
|
|
4
|
+
import type { TAnyProcedureContract, TProcedureContract } from './procedure.ts'
|
|
5
|
+
import type {
|
|
6
|
+
TAnySubscriptionContract,
|
|
7
|
+
TSubscriptionContract,
|
|
8
|
+
} from './subscription.ts'
|
|
9
|
+
|
|
10
|
+
export const NamespaceKind = 'NeemataNamespace'
|
|
11
|
+
|
|
12
|
+
export type TAnyNamespaceContract = TNamespaceContract<
|
|
13
|
+
Record<string, any>,
|
|
14
|
+
Record<string, any>,
|
|
15
|
+
Record<string, any>,
|
|
16
|
+
string | undefined
|
|
17
|
+
>
|
|
18
|
+
|
|
19
|
+
export interface TNamespaceContract<
|
|
20
|
+
Procedures extends Record<string, unknown> = {},
|
|
21
|
+
Subscriptions extends Record<string, unknown> = {},
|
|
22
|
+
Events extends Record<string, unknown> = {},
|
|
23
|
+
Name extends string | undefined = undefined,
|
|
24
|
+
> {
|
|
25
|
+
[Kind]: typeof NamespaceKind
|
|
26
|
+
type: 'neemata:namespace'
|
|
27
|
+
name: Name
|
|
28
|
+
procedures: {
|
|
29
|
+
[K in keyof Procedures]: Procedures[K] extends TAnyProcedureContract
|
|
30
|
+
? TProcedureContract<
|
|
31
|
+
Procedures[K]['input'],
|
|
32
|
+
Procedures[K]['output'],
|
|
33
|
+
Procedures[K]['stream'],
|
|
34
|
+
Extract<K, string>,
|
|
35
|
+
Name
|
|
36
|
+
>
|
|
37
|
+
: never
|
|
38
|
+
}
|
|
39
|
+
subscriptions: {
|
|
40
|
+
[K in keyof Subscriptions]: Subscriptions[K] extends TAnySubscriptionContract
|
|
41
|
+
? TSubscriptionContract<
|
|
42
|
+
Subscriptions[K]['input'],
|
|
43
|
+
Subscriptions[K]['output'],
|
|
44
|
+
Subscriptions[K]['options'],
|
|
45
|
+
{
|
|
46
|
+
[E in keyof Subscriptions[K]['events']]: Subscriptions[K]['events'][E] extends TAnyEventContract
|
|
47
|
+
? TEventContract<
|
|
48
|
+
Subscriptions[K]['events'][E]['payload'],
|
|
49
|
+
Extract<E, string>,
|
|
50
|
+
Extract<K, string>,
|
|
51
|
+
Name
|
|
52
|
+
>
|
|
53
|
+
: never
|
|
54
|
+
},
|
|
55
|
+
Extract<K, string>,
|
|
56
|
+
Name
|
|
57
|
+
>
|
|
58
|
+
: never
|
|
59
|
+
}
|
|
60
|
+
events: {
|
|
61
|
+
[K in keyof Events]: Events[K] extends TAnyEventContract
|
|
62
|
+
? TEventContract<
|
|
63
|
+
Events[K]['payload'],
|
|
64
|
+
Extract<K, string>,
|
|
65
|
+
undefined,
|
|
66
|
+
Name
|
|
67
|
+
>
|
|
68
|
+
: never
|
|
69
|
+
}
|
|
70
|
+
timeout?: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const NamespaceContract = <
|
|
74
|
+
Procedures extends Record<string, unknown> = {},
|
|
75
|
+
Subscriptions extends Record<string, unknown> = {},
|
|
76
|
+
Events extends Record<string, unknown> = {},
|
|
77
|
+
Name extends string | undefined = undefined,
|
|
78
|
+
>(options?: {
|
|
79
|
+
procedures?: Procedures
|
|
80
|
+
subscriptions?: Subscriptions
|
|
81
|
+
events?: Events
|
|
82
|
+
name?: Name
|
|
83
|
+
timeout?: number
|
|
84
|
+
schemaOptions?: ContractSchemaOptions
|
|
85
|
+
}) => {
|
|
86
|
+
const {
|
|
87
|
+
procedures = {} as Procedures,
|
|
88
|
+
subscriptions = {} as Subscriptions,
|
|
89
|
+
events = {} as Events,
|
|
90
|
+
name,
|
|
91
|
+
timeout,
|
|
92
|
+
schemaOptions = {} as ContractSchemaOptions,
|
|
93
|
+
} = options ?? {}
|
|
94
|
+
const _events = {} as any
|
|
95
|
+
|
|
96
|
+
for (const eventKey in events) {
|
|
97
|
+
const event = events[eventKey]
|
|
98
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
99
|
+
name: eventKey,
|
|
100
|
+
namespace: options?.name,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const _procedures = {} as any
|
|
105
|
+
for (const procedureKey in procedures) {
|
|
106
|
+
const procedure: any = procedures[procedureKey]
|
|
107
|
+
_procedures[procedureKey] = Object.assign({}, procedure, {
|
|
108
|
+
name: procedureKey,
|
|
109
|
+
namespace: options?.name,
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const _subscriptions = {} as any
|
|
114
|
+
for (const subscriptionKey in subscriptions) {
|
|
115
|
+
const subscription: any = subscriptions[subscriptionKey]
|
|
116
|
+
const _events = {} as any
|
|
117
|
+
|
|
118
|
+
for (const eventKey in subscription.events) {
|
|
119
|
+
const event = subscription.events[eventKey]
|
|
120
|
+
_events[eventKey] = Object.assign({}, event, {
|
|
121
|
+
name: eventKey,
|
|
122
|
+
subscription: subscriptionKey,
|
|
123
|
+
namespace: options?.name,
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
_subscriptions[subscriptionKey] = Object.assign({}, subscription, {
|
|
128
|
+
name: subscriptionKey,
|
|
129
|
+
namespace: options?.name,
|
|
130
|
+
events: _events,
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return createSchema<
|
|
135
|
+
TNamespaceContract<Procedures, Subscriptions, Events, Name>
|
|
136
|
+
>({
|
|
137
|
+
...schemaOptions,
|
|
138
|
+
[Kind]: NamespaceKind,
|
|
139
|
+
type: 'neemata:namespace',
|
|
140
|
+
name: name as Name,
|
|
141
|
+
procedures: _procedures,
|
|
142
|
+
subscriptions: _subscriptions,
|
|
143
|
+
events: _events,
|
|
144
|
+
timeout,
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function IsNamespaceContract(
|
|
149
|
+
value: any,
|
|
150
|
+
): value is TAnyNamespaceContract {
|
|
151
|
+
return Kind in value && value[Kind] === NamespaceKind
|
|
152
|
+
}
|
package/src/schemas/procedure.ts
CHANGED
|
@@ -1,59 +1,94 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type BaseType, type NeverType, t } from '@nmtjs/type'
|
|
2
|
+
import { Kind } from '../constants.ts'
|
|
2
3
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
4
|
|
|
5
|
+
export type TAnyBaseProcedureContract = TBaseProcedureContract<
|
|
6
|
+
string,
|
|
7
|
+
BaseType,
|
|
8
|
+
BaseType,
|
|
9
|
+
string | undefined,
|
|
10
|
+
string | undefined
|
|
11
|
+
>
|
|
12
|
+
|
|
4
13
|
export interface TBaseProcedureContract<
|
|
5
|
-
Type extends string
|
|
6
|
-
Input extends BaseType
|
|
7
|
-
Output extends BaseType
|
|
8
|
-
Name extends string | undefined =
|
|
9
|
-
|
|
10
|
-
Transports extends { [K in string]?: true } | undefined =
|
|
11
|
-
| { [K in string]?: true }
|
|
12
|
-
| undefined,
|
|
14
|
+
Type extends string,
|
|
15
|
+
Input extends BaseType,
|
|
16
|
+
Output extends BaseType,
|
|
17
|
+
Name extends string | undefined = undefined,
|
|
18
|
+
Namespace extends string | undefined = undefined,
|
|
13
19
|
> {
|
|
20
|
+
[Kind]: string
|
|
14
21
|
type: Type
|
|
15
22
|
name: Name
|
|
16
|
-
|
|
17
|
-
transports: Transports
|
|
23
|
+
namespace: Namespace
|
|
18
24
|
input: Input
|
|
19
25
|
output: Output
|
|
20
26
|
timeout?: number
|
|
21
27
|
}
|
|
22
28
|
|
|
29
|
+
export const ProcedureKind = 'NeemataProcedure'
|
|
30
|
+
|
|
31
|
+
export type TAnyProcedureContract = TProcedureContract<
|
|
32
|
+
BaseType,
|
|
33
|
+
BaseType,
|
|
34
|
+
BaseType,
|
|
35
|
+
string | undefined,
|
|
36
|
+
string | undefined
|
|
37
|
+
>
|
|
38
|
+
|
|
23
39
|
export interface TProcedureContract<
|
|
24
|
-
Input extends BaseType =
|
|
25
|
-
Output extends BaseType =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
| { [K in string]?: true }
|
|
30
|
-
| undefined,
|
|
40
|
+
Input extends BaseType = NeverType,
|
|
41
|
+
Output extends BaseType = NeverType,
|
|
42
|
+
Stream extends BaseType = NeverType,
|
|
43
|
+
Name extends string | undefined = undefined,
|
|
44
|
+
Namespace extends string | undefined = undefined,
|
|
31
45
|
> extends TBaseProcedureContract<
|
|
32
46
|
'neemata:procedure',
|
|
33
47
|
Input,
|
|
34
48
|
Output,
|
|
35
49
|
Name,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
50
|
+
Namespace
|
|
51
|
+
> {
|
|
52
|
+
[Kind]: typeof ProcedureKind
|
|
53
|
+
stream: Stream
|
|
54
|
+
}
|
|
39
55
|
|
|
40
56
|
export const ProcedureContract = <
|
|
41
|
-
Input extends BaseType,
|
|
42
|
-
Output extends BaseType,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
Input extends BaseType = NeverType,
|
|
58
|
+
Output extends BaseType = NeverType,
|
|
59
|
+
Stream extends BaseType = NeverType,
|
|
60
|
+
Name extends string | undefined = undefined,
|
|
61
|
+
>(options: {
|
|
62
|
+
input?: Input
|
|
63
|
+
output?: Output
|
|
64
|
+
stream?: Stream
|
|
65
|
+
timeout?: number
|
|
66
|
+
schemaOptions?: ContractSchemaOptions
|
|
67
|
+
name?: Name
|
|
68
|
+
}) => {
|
|
69
|
+
const {
|
|
70
|
+
input = t.never() as unknown as Input,
|
|
71
|
+
output = t.never() as unknown as Output,
|
|
72
|
+
stream = t.never() as unknown as Stream,
|
|
73
|
+
timeout,
|
|
74
|
+
schemaOptions = {},
|
|
75
|
+
name,
|
|
76
|
+
} = options
|
|
77
|
+
return createSchema<TProcedureContract<Input, Output, Stream, Name>>({
|
|
50
78
|
...schemaOptions,
|
|
79
|
+
[Kind]: ProcedureKind,
|
|
51
80
|
type: 'neemata:procedure',
|
|
52
81
|
input,
|
|
53
82
|
output,
|
|
83
|
+
stream,
|
|
54
84
|
timeout,
|
|
55
|
-
name:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
85
|
+
name: name as Name,
|
|
86
|
+
namespace: undefined,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function IsProcedureContract(
|
|
91
|
+
contract: any,
|
|
92
|
+
): contract is TAnyProcedureContract {
|
|
93
|
+
return Kind in contract && contract[Kind] === ProcedureKind
|
|
59
94
|
}
|
|
@@ -1,62 +1,98 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type BaseType, type NeverType, t } from '@nmtjs/type'
|
|
2
|
+
import { Kind } from '../constants.ts'
|
|
2
3
|
import { type ContractSchemaOptions, createSchema } from '../utils.ts'
|
|
3
|
-
import type { TEventContract } from './event.ts'
|
|
4
|
+
import type { TAnyEventContract, TEventContract } from './event.ts'
|
|
4
5
|
import type { TBaseProcedureContract } from './procedure.ts'
|
|
5
6
|
|
|
6
|
-
export
|
|
7
|
+
export const SubscriptionKind = 'NeemataSubscription'
|
|
8
|
+
|
|
9
|
+
export type SubcriptionOptions = Record<string, string | number | boolean>
|
|
10
|
+
|
|
11
|
+
export type TAnySubscriptionContract = TSubscriptionContract<
|
|
12
|
+
BaseType,
|
|
13
|
+
BaseType,
|
|
14
|
+
SubcriptionOptions,
|
|
15
|
+
Record<string, unknown>,
|
|
16
|
+
string | undefined,
|
|
17
|
+
string | undefined
|
|
18
|
+
>
|
|
7
19
|
|
|
8
20
|
export interface TSubscriptionContract<
|
|
9
|
-
Input extends BaseType =
|
|
10
|
-
Output extends BaseType =
|
|
11
|
-
Options extends SubcriptionOptions =
|
|
12
|
-
Events extends Record<string,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
>,
|
|
16
|
-
Name extends string | undefined = string | undefined,
|
|
17
|
-
ServiceName extends string | undefined = string | undefined,
|
|
18
|
-
Transports extends { [K in string]?: true } | undefined =
|
|
19
|
-
| { [K in string]?: true }
|
|
20
|
-
| undefined,
|
|
21
|
+
Input extends BaseType = NeverType,
|
|
22
|
+
Output extends BaseType = NeverType,
|
|
23
|
+
Options extends SubcriptionOptions = {},
|
|
24
|
+
Events extends Record<string, unknown> = {},
|
|
25
|
+
Name extends string | undefined = undefined,
|
|
26
|
+
Namespace extends string | undefined = undefined,
|
|
21
27
|
> extends TBaseProcedureContract<
|
|
22
28
|
'neemata:subscription',
|
|
23
29
|
Input,
|
|
24
30
|
Output,
|
|
25
31
|
Name,
|
|
26
|
-
|
|
27
|
-
Transports
|
|
32
|
+
Namespace
|
|
28
33
|
> {
|
|
34
|
+
[Kind]: typeof SubscriptionKind
|
|
29
35
|
options: Options
|
|
30
|
-
events:
|
|
36
|
+
events: {
|
|
37
|
+
[K in keyof Events]: Events[K] extends TAnyEventContract
|
|
38
|
+
? TEventContract<
|
|
39
|
+
Events[K]['payload'],
|
|
40
|
+
Extract<K, string>,
|
|
41
|
+
Name,
|
|
42
|
+
Namespace
|
|
43
|
+
>
|
|
44
|
+
: never
|
|
45
|
+
}
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
export const SubscriptionContract = <
|
|
34
|
-
Input extends BaseType =
|
|
35
|
-
Output extends BaseType =
|
|
36
|
-
Events extends Record<string,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
Input extends BaseType = NeverType,
|
|
50
|
+
Output extends BaseType = NeverType,
|
|
51
|
+
Events extends Record<string, unknown> = {},
|
|
52
|
+
Name extends string | undefined = undefined,
|
|
53
|
+
>(options?: {
|
|
54
|
+
input?: Input
|
|
55
|
+
output?: Output
|
|
56
|
+
events?: Events
|
|
57
|
+
timeout?: number
|
|
58
|
+
schemaOptions?: ContractSchemaOptions
|
|
59
|
+
name?: Name
|
|
60
|
+
}) => {
|
|
61
|
+
const {
|
|
62
|
+
input = t.never() as unknown as Input,
|
|
63
|
+
output = t.never() as unknown as Output,
|
|
64
|
+
events = {} as Events,
|
|
65
|
+
timeout,
|
|
66
|
+
schemaOptions = {},
|
|
67
|
+
name,
|
|
68
|
+
} = options ?? {}
|
|
69
|
+
|
|
70
|
+
const _events = {} as any
|
|
71
|
+
for (const key in events) {
|
|
72
|
+
const event = events[key]
|
|
73
|
+
_events[key] = Object.assign({}, event, { subscription: name })
|
|
74
|
+
}
|
|
47
75
|
return {
|
|
48
76
|
$withOptions: <Options extends SubcriptionOptions>() =>
|
|
49
|
-
createSchema<TSubscriptionContract<Input, Output, Options, Events>>(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
createSchema<TSubscriptionContract<Input, Output, Options, Events, Name>>(
|
|
78
|
+
{
|
|
79
|
+
...schemaOptions,
|
|
80
|
+
[Kind]: SubscriptionKind,
|
|
81
|
+
type: 'neemata:subscription',
|
|
82
|
+
input,
|
|
83
|
+
output,
|
|
84
|
+
events: _events,
|
|
85
|
+
timeout,
|
|
86
|
+
name: name as Name,
|
|
87
|
+
namespace: undefined,
|
|
88
|
+
options: undefined as unknown as Options,
|
|
89
|
+
},
|
|
90
|
+
),
|
|
61
91
|
}
|
|
62
92
|
}
|
|
93
|
+
|
|
94
|
+
export function IsSubscriptionContract(
|
|
95
|
+
contract: any,
|
|
96
|
+
): contract is TAnySubscriptionContract {
|
|
97
|
+
return Kind in contract && contract[Kind] === SubscriptionKind
|
|
98
|
+
}
|
package/src/types/blob.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ProtocolBlobInterface } from '@nmtjs/protocol/common'
|
|
2
2
|
import { t } from '@nmtjs/type'
|
|
3
3
|
|
|
4
|
-
export const BlobKind = '
|
|
4
|
+
export const BlobKind = 'ProtocolBlob'
|
|
5
5
|
|
|
6
6
|
export interface BlobOptions {
|
|
7
7
|
maxSize?: number
|
|
8
8
|
contentType?: string
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export const
|
|
12
|
-
t.custom<
|
|
11
|
+
export const BlobType = (options: BlobOptions = {}) =>
|
|
12
|
+
t.custom<ProtocolBlobInterface>(
|
|
13
13
|
(value) => {
|
|
14
14
|
// TODO: this should be registered separately for server and client
|
|
15
15
|
// ref: https://github.com/sinclairzx81/typebox/issues/977
|
|
16
16
|
if ('metadata' in value) {
|
|
17
17
|
if (options.maxSize) {
|
|
18
|
-
const size = (value as
|
|
18
|
+
const size = (value as ProtocolBlobInterface).metadata.size
|
|
19
19
|
if (size === -1 || size > options.maxSize) {
|
|
20
20
|
throw new Error('Blob size unknown or exceeds maximum allowed size')
|
|
21
21
|
}
|
package/dist/schemas/service.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { applyNames, createSchema } from "../utils.js";
|
|
2
|
-
export const ServiceContract = (name, transports, procedures = {}, events = {}, timeout, schemaOptions = {})=>{
|
|
3
|
-
const serviceProcedures = {};
|
|
4
|
-
for (const [procedureName, procedure] of Object.entries(procedures)){
|
|
5
|
-
if (procedure.type === 'neemata:subscription') {
|
|
6
|
-
serviceProcedures[procedureName] = {
|
|
7
|
-
...procedure,
|
|
8
|
-
events: applyNames(procedure.events, {
|
|
9
|
-
serviceName: name,
|
|
10
|
-
subscriptionName: procedureName
|
|
11
|
-
})
|
|
12
|
-
};
|
|
13
|
-
} else {
|
|
14
|
-
serviceProcedures[procedureName] = procedure;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return createSchema({
|
|
18
|
-
...schemaOptions,
|
|
19
|
-
name: name,
|
|
20
|
-
type: 'neemata:service',
|
|
21
|
-
procedures: applyNames(serviceProcedures, {
|
|
22
|
-
serviceName: name
|
|
23
|
-
}),
|
|
24
|
-
events: applyNames(events, {
|
|
25
|
-
serviceName: name
|
|
26
|
-
}),
|
|
27
|
-
transports,
|
|
28
|
-
timeout
|
|
29
|
-
});
|
|
30
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/schemas/service.ts"],"sourcesContent":["import {\n type ContractSchemaOptions,\n applyNames,\n createSchema,\n} from '../utils.ts'\nimport type { TEventContract } from './event.ts'\nimport type { TBaseProcedureContract, TProcedureContract } from './procedure.ts'\nimport type { TSubscriptionContract } from './subscription.ts'\n\nexport interface TServiceContract<\n Name extends string = string,\n Transports extends { [K in string]?: true } = { [K in string]?: true },\n Procedures extends Record<string, TBaseProcedureContract> = Record<\n string,\n TBaseProcedureContract\n >,\n Events extends Record<string, TEventContract> = Record<\n string,\n TEventContract\n >,\n> {\n type: 'neemata:service'\n name: Name\n transports: Transports\n procedures: {\n [K in keyof Procedures]: Procedures[K] extends TProcedureContract<\n infer Input,\n infer Output,\n any,\n any,\n any\n >\n ? // ? true\n TProcedureContract<Input, Output, Extract<K, string>, Name, Transports>\n : Procedures[K] extends TSubscriptionContract<\n infer Input,\n infer Output,\n infer Options,\n infer Events,\n any,\n any,\n any\n >\n ? TSubscriptionContract<\n Input,\n Output,\n Options,\n {\n [EK in keyof Events]: Events[EK] extends TEventContract<\n infer Payload\n >\n ? TEventContract<\n Payload,\n Extract<EK, string>,\n Name,\n Extract<K, string>\n >\n : never\n },\n Extract<K, string>,\n Name,\n Transports\n >\n : Procedures[K]\n }\n events: {\n [K in Extract<keyof Events, string>]: Events[K] extends TEventContract<\n infer Payload\n >\n ? TEventContract<Payload, K, Name>\n : Events[K]\n }\n timeout?: number\n}\n\nexport const ServiceContract = <\n Name extends string,\n Transports extends { [key: string]: true },\n Procedures extends Record<\n string,\n TBaseProcedureContract | TProcedureContract | TSubscriptionContract\n >,\n Events extends Record<string, TEventContract>,\n>(\n name: Name,\n transports: Transports,\n procedures: Procedures = {} as Procedures,\n events: Events = {} as Events,\n timeout?: number,\n schemaOptions: ContractSchemaOptions = {} as ContractSchemaOptions,\n) => {\n const serviceProcedures = {}\n\n for (const [procedureName, procedure] of Object.entries(procedures)) {\n if (procedure.type === 'neemata:subscription') {\n serviceProcedures[procedureName] = {\n ...procedure,\n events: applyNames((procedure as TSubscriptionContract).events, {\n serviceName: name,\n subscriptionName: procedureName,\n }),\n }\n } else {\n serviceProcedures[procedureName] = procedure\n }\n }\n\n return createSchema<TServiceContract<Name, Transports, Procedures, Events>>({\n ...schemaOptions,\n name: name,\n type: 'neemata:service',\n // @ts-expect-error\n procedures: applyNames(serviceProcedures, { serviceName: name }),\n // @ts-expect-error\n events: applyNames(events, { serviceName: name }),\n transports,\n timeout,\n })\n}\n"],"names":["applyNames","createSchema","ServiceContract","name","transports","procedures","events","timeout","schemaOptions","serviceProcedures","procedureName","procedure","Object","entries","type","serviceName","subscriptionName"],"mappings":"AAAA,SAEEA,UAAU,EACVC,YAAY,QACP,cAAa;AAuEpB,OAAO,MAAMC,kBAAkB,CAS7BC,MACAC,YACAC,aAAyB,CAAC,CAAe,EACzCC,SAAiB,CAAC,CAAW,EAC7BC,SACAC,gBAAuC,CAAC,CAA0B;IAElE,MAAMC,oBAAoB,CAAC;IAE3B,KAAK,MAAM,CAACC,eAAeC,UAAU,IAAIC,OAAOC,OAAO,CAACR,YAAa;QACnE,IAAIM,UAAUG,IAAI,KAAK,wBAAwB;YAC7CL,iBAAiB,CAACC,cAAc,GAAG;gBACjC,GAAGC,SAAS;gBACZL,QAAQN,WAAW,AAACW,UAAoCL,MAAM,EAAE;oBAC9DS,aAAaZ;oBACba,kBAAkBN;gBACpB;YACF;QACF,OAAO;YACLD,iBAAiB,CAACC,cAAc,GAAGC;QACrC;IACF;IAEA,OAAOV,aAAqE;QAC1E,GAAGO,aAAa;QAChBL,MAAMA;QACNW,MAAM;QAENT,YAAYL,WAAWS,mBAAmB;YAAEM,aAAaZ;QAAK;QAE9DG,QAAQN,WAAWM,QAAQ;YAAES,aAAaZ;QAAK;QAC/CC;QACAG;IACF;AACF,EAAC"}
|
package/src/schemas/service.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ContractSchemaOptions,
|
|
3
|
-
applyNames,
|
|
4
|
-
createSchema,
|
|
5
|
-
} from '../utils.ts'
|
|
6
|
-
import type { TEventContract } from './event.ts'
|
|
7
|
-
import type { TBaseProcedureContract, TProcedureContract } from './procedure.ts'
|
|
8
|
-
import type { TSubscriptionContract } from './subscription.ts'
|
|
9
|
-
|
|
10
|
-
export interface TServiceContract<
|
|
11
|
-
Name extends string = string,
|
|
12
|
-
Transports extends { [K in string]?: true } = { [K in string]?: true },
|
|
13
|
-
Procedures extends Record<string, TBaseProcedureContract> = Record<
|
|
14
|
-
string,
|
|
15
|
-
TBaseProcedureContract
|
|
16
|
-
>,
|
|
17
|
-
Events extends Record<string, TEventContract> = Record<
|
|
18
|
-
string,
|
|
19
|
-
TEventContract
|
|
20
|
-
>,
|
|
21
|
-
> {
|
|
22
|
-
type: 'neemata:service'
|
|
23
|
-
name: Name
|
|
24
|
-
transports: Transports
|
|
25
|
-
procedures: {
|
|
26
|
-
[K in keyof Procedures]: Procedures[K] extends TProcedureContract<
|
|
27
|
-
infer Input,
|
|
28
|
-
infer Output,
|
|
29
|
-
any,
|
|
30
|
-
any,
|
|
31
|
-
any
|
|
32
|
-
>
|
|
33
|
-
? // ? true
|
|
34
|
-
TProcedureContract<Input, Output, Extract<K, string>, Name, Transports>
|
|
35
|
-
: Procedures[K] extends TSubscriptionContract<
|
|
36
|
-
infer Input,
|
|
37
|
-
infer Output,
|
|
38
|
-
infer Options,
|
|
39
|
-
infer Events,
|
|
40
|
-
any,
|
|
41
|
-
any,
|
|
42
|
-
any
|
|
43
|
-
>
|
|
44
|
-
? TSubscriptionContract<
|
|
45
|
-
Input,
|
|
46
|
-
Output,
|
|
47
|
-
Options,
|
|
48
|
-
{
|
|
49
|
-
[EK in keyof Events]: Events[EK] extends TEventContract<
|
|
50
|
-
infer Payload
|
|
51
|
-
>
|
|
52
|
-
? TEventContract<
|
|
53
|
-
Payload,
|
|
54
|
-
Extract<EK, string>,
|
|
55
|
-
Name,
|
|
56
|
-
Extract<K, string>
|
|
57
|
-
>
|
|
58
|
-
: never
|
|
59
|
-
},
|
|
60
|
-
Extract<K, string>,
|
|
61
|
-
Name,
|
|
62
|
-
Transports
|
|
63
|
-
>
|
|
64
|
-
: Procedures[K]
|
|
65
|
-
}
|
|
66
|
-
events: {
|
|
67
|
-
[K in Extract<keyof Events, string>]: Events[K] extends TEventContract<
|
|
68
|
-
infer Payload
|
|
69
|
-
>
|
|
70
|
-
? TEventContract<Payload, K, Name>
|
|
71
|
-
: Events[K]
|
|
72
|
-
}
|
|
73
|
-
timeout?: number
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export const ServiceContract = <
|
|
77
|
-
Name extends string,
|
|
78
|
-
Transports extends { [key: string]: true },
|
|
79
|
-
Procedures extends Record<
|
|
80
|
-
string,
|
|
81
|
-
TBaseProcedureContract | TProcedureContract | TSubscriptionContract
|
|
82
|
-
>,
|
|
83
|
-
Events extends Record<string, TEventContract>,
|
|
84
|
-
>(
|
|
85
|
-
name: Name,
|
|
86
|
-
transports: Transports,
|
|
87
|
-
procedures: Procedures = {} as Procedures,
|
|
88
|
-
events: Events = {} as Events,
|
|
89
|
-
timeout?: number,
|
|
90
|
-
schemaOptions: ContractSchemaOptions = {} as ContractSchemaOptions,
|
|
91
|
-
) => {
|
|
92
|
-
const serviceProcedures = {}
|
|
93
|
-
|
|
94
|
-
for (const [procedureName, procedure] of Object.entries(procedures)) {
|
|
95
|
-
if (procedure.type === 'neemata:subscription') {
|
|
96
|
-
serviceProcedures[procedureName] = {
|
|
97
|
-
...procedure,
|
|
98
|
-
events: applyNames((procedure as TSubscriptionContract).events, {
|
|
99
|
-
serviceName: name,
|
|
100
|
-
subscriptionName: procedureName,
|
|
101
|
-
}),
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
serviceProcedures[procedureName] = procedure
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return createSchema<TServiceContract<Name, Transports, Procedures, Events>>({
|
|
109
|
-
...schemaOptions,
|
|
110
|
-
name: name,
|
|
111
|
-
type: 'neemata:service',
|
|
112
|
-
// @ts-expect-error
|
|
113
|
-
procedures: applyNames(serviceProcedures, { serviceName: name }),
|
|
114
|
-
// @ts-expect-error
|
|
115
|
-
events: applyNames(events, { serviceName: name }),
|
|
116
|
-
transports,
|
|
117
|
-
timeout,
|
|
118
|
-
})
|
|
119
|
-
}
|