@nmtjs/contract 0.15.0-beta.1 → 0.15.0-beta.2
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/index.js +19 -0
- package/dist/schemas/event.js +18 -0
- package/dist/schemas/procedure.js +23 -0
- package/dist/schemas/router.js +41 -0
- package/dist/schemas/subscription.js +28 -0
- package/dist/types/blob.js +35 -0
- package/dist/utils.js +7 -0
- package/package.json +3 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const Kind = Symbol.for('neemata:kind');
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EventContract } from "./schemas/event.js";
|
|
2
|
+
import { ProcedureContract } from "./schemas/procedure.js";
|
|
3
|
+
import { RouterContract } from "./schemas/router.js";
|
|
4
|
+
import { SubscriptionContract } from "./schemas/subscription.js";
|
|
5
|
+
import { BlobType } from "./types/blob.js";
|
|
6
|
+
export * from "./schemas/event.js";
|
|
7
|
+
export * from "./schemas/procedure.js";
|
|
8
|
+
export * from "./schemas/router.js";
|
|
9
|
+
export * from "./schemas/subscription.js";
|
|
10
|
+
export var contract;
|
|
11
|
+
(function (contract) {
|
|
12
|
+
contract.procedure = ProcedureContract;
|
|
13
|
+
contract.event = EventContract;
|
|
14
|
+
contract.subscription = SubscriptionContract;
|
|
15
|
+
contract.router = RouterContract;
|
|
16
|
+
contract.blob = BlobType;
|
|
17
|
+
})(contract || (contract = {}));
|
|
18
|
+
export { contract as c };
|
|
19
|
+
export default contract;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { t } from '@nmtjs/type';
|
|
2
|
+
import { Kind } from "../constants.js";
|
|
3
|
+
import { createSchema } from "../utils.js";
|
|
4
|
+
export const EventKind = Symbol('NeemataEvent');
|
|
5
|
+
export const EventContract = (options) => {
|
|
6
|
+
const { payload = t.never(), schemaOptions = {}, name = undefined, } = options ?? {};
|
|
7
|
+
return createSchema({
|
|
8
|
+
...schemaOptions,
|
|
9
|
+
[Kind]: EventKind,
|
|
10
|
+
type: 'neemata:event',
|
|
11
|
+
payload,
|
|
12
|
+
name,
|
|
13
|
+
options: undefined,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
export function IsEventContract(value) {
|
|
17
|
+
return Kind in value && value[Kind] === EventKind;
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { t } from '@nmtjs/type';
|
|
2
|
+
import { Kind } from "../constants.js";
|
|
3
|
+
import { createSchema } from "../utils.js";
|
|
4
|
+
export const ProcedureKind = Symbol('NeemataProcedure');
|
|
5
|
+
export const ProcedureContract = (options) => {
|
|
6
|
+
const { input = t.never(), output = t.never(), stream = undefined, name = undefined, timeout, schemaOptions = {}, } = options;
|
|
7
|
+
return createSchema({
|
|
8
|
+
...schemaOptions,
|
|
9
|
+
[Kind]: ProcedureKind,
|
|
10
|
+
type: 'neemata:procedure',
|
|
11
|
+
input,
|
|
12
|
+
output,
|
|
13
|
+
stream,
|
|
14
|
+
name,
|
|
15
|
+
timeout,
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
export function IsProcedureContract(contract) {
|
|
19
|
+
return Kind in contract && contract[Kind] === ProcedureKind;
|
|
20
|
+
}
|
|
21
|
+
export function IsStreamProcedureContract(contract) {
|
|
22
|
+
return IsProcedureContract(contract) && typeof contract.stream !== 'undefined';
|
|
23
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Kind } from "../constants.js";
|
|
2
|
+
import { concatFullName, createSchema } from "../utils.js";
|
|
3
|
+
import { IsProcedureContract } from "./procedure.js";
|
|
4
|
+
export const RouterKind = Symbol('NeemataRouter');
|
|
5
|
+
export const RouterContract = (options) => {
|
|
6
|
+
const { name = undefined, timeout, schemaOptions = {}, } = options;
|
|
7
|
+
const routes = processNestedRoutes(options.routes, name);
|
|
8
|
+
return createSchema({
|
|
9
|
+
...schemaOptions,
|
|
10
|
+
[Kind]: RouterKind,
|
|
11
|
+
type: 'neemata:router',
|
|
12
|
+
name,
|
|
13
|
+
routes,
|
|
14
|
+
timeout,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
function processNestedRoutes(routes, parentName) {
|
|
18
|
+
const processed = {};
|
|
19
|
+
for (const routeName in routes) {
|
|
20
|
+
const route = routes[routeName];
|
|
21
|
+
if (IsRouterContract(route)) {
|
|
22
|
+
const nestedName = concatFullName(parentName, routeName);
|
|
23
|
+
processed[routeName] = createSchema({
|
|
24
|
+
...route,
|
|
25
|
+
name: nestedName,
|
|
26
|
+
routes: processNestedRoutes(route.routes, nestedName),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else if (IsProcedureContract(route)) {
|
|
30
|
+
const fullName = concatFullName(parentName, routeName);
|
|
31
|
+
processed[routeName] = createSchema({ ...route, name: fullName });
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
throw new Error(`Invalid route type for ${routeName}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return processed;
|
|
38
|
+
}
|
|
39
|
+
export function IsRouterContract(value) {
|
|
40
|
+
return Kind in value && value[Kind] === RouterKind;
|
|
41
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Kind } from "../constants.js";
|
|
2
|
+
import { concatFullName, createSchema } from "../utils.js";
|
|
3
|
+
export const SubscriptionKind = Symbol('NeemataSubscription');
|
|
4
|
+
const _SubscriptionContract = (options) => {
|
|
5
|
+
const { schemaOptions = {}, name } = options;
|
|
6
|
+
const _events = {};
|
|
7
|
+
for (const key in options.events) {
|
|
8
|
+
const event = options.events[key];
|
|
9
|
+
const fullName = concatFullName(name, key);
|
|
10
|
+
_events[key] = createSchema({ ...event, name: fullName });
|
|
11
|
+
}
|
|
12
|
+
return createSchema({
|
|
13
|
+
...schemaOptions,
|
|
14
|
+
[Kind]: SubscriptionKind,
|
|
15
|
+
type: 'neemata:subscription',
|
|
16
|
+
events: _events,
|
|
17
|
+
name: name,
|
|
18
|
+
options: undefined,
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export const SubscriptionContract = Object.assign(_SubscriptionContract, {
|
|
22
|
+
withOptions: () => {
|
|
23
|
+
return (options) => _SubscriptionContract(options);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
export function IsSubscriptionContract(contract) {
|
|
27
|
+
return Kind in contract && contract[Kind] === SubscriptionKind;
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isBlobInterface } from '@nmtjs/protocol';
|
|
2
|
+
import { CustomType } from '@nmtjs/type/custom';
|
|
3
|
+
export const BlobType = (options = {}) => CustomType.factory({
|
|
4
|
+
decode: (value) => value,
|
|
5
|
+
encode: (value) => value,
|
|
6
|
+
validation: {
|
|
7
|
+
decode(value, { addIssue }) {
|
|
8
|
+
if (isBlobInterface(value)) {
|
|
9
|
+
if (options.maxSize) {
|
|
10
|
+
const size = value.metadata.size;
|
|
11
|
+
if (typeof size !== 'undefined' && size > options.maxSize) {
|
|
12
|
+
addIssue({
|
|
13
|
+
code: 'custom',
|
|
14
|
+
message: `Blob size unknown or exceeds maximum allowed size of ${options.maxSize} bytes`,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
addIssue({
|
|
21
|
+
code: 'custom',
|
|
22
|
+
message: 'Value is not a Neemata Blob. Make sure to use transport that supports encoded streams.',
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
encode(value, { addIssue }) {
|
|
27
|
+
if (!isBlobInterface(value)) {
|
|
28
|
+
addIssue({
|
|
29
|
+
code: 'custom',
|
|
30
|
+
message: 'Value is not a Neemata Blob. Make sure to use transport that supports encoded streams.',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const applyNames = (params, opts) => {
|
|
2
|
+
return Object.fromEntries(Object.entries(params).map(([k, v]) => [k, { ...v, name: k, ...opts }]));
|
|
3
|
+
};
|
|
4
|
+
export const createSchema = (schema) => Object.freeze(schema);
|
|
5
|
+
export const concatFullName = (parent, name) => {
|
|
6
|
+
return parent ? `${parent}/${name}` : name;
|
|
7
|
+
};
|
package/package.json
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
".": "./dist/index.js"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@nmtjs/protocol": "0.15.0-beta.
|
|
9
|
-
"@nmtjs/type": "0.15.0-beta.
|
|
8
|
+
"@nmtjs/protocol": "0.15.0-beta.2",
|
|
9
|
+
"@nmtjs/type": "0.15.0-beta.2"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"zod": "^4.1.0"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"LICENSE.md",
|
|
17
17
|
"README.md"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.15.0-beta.
|
|
19
|
+
"version": "0.15.0-beta.2",
|
|
20
20
|
"scripts": {
|
|
21
21
|
"clean-build": "rm -rf ./dist",
|
|
22
22
|
"build": "tsc",
|