@nmtjs/contract 0.14.4 → 0.15.0-beta.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/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2024 Denis Ilchyshyn
1
+ Copyright (c) 2025 Denys Ilchyshyn
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -6,4 +6,4 @@
6
6
  - binary data streaming and event subscriptions
7
7
  - contract-based API
8
8
  - end-to-end type safety
9
- - CPU-intensive task execution on separate workers
9
+ - CPU-intensive task execution on separate workers
package/package.json CHANGED
@@ -2,24 +2,25 @@
2
2
  "name": "@nmtjs/contract",
3
3
  "type": "module",
4
4
  "exports": {
5
- ".": {
6
- "types": "./dist/index.d.ts",
7
- "import": "./dist/index.js",
8
- "module-sync": "./dist/index.js"
9
- }
5
+ ".": "./dist/index.js"
10
6
  },
11
7
  "dependencies": {
12
- "@nmtjs/type": "0.14.4",
13
- "@nmtjs/protocol": "0.14.4"
8
+ "@nmtjs/protocol": "0.15.0-beta.1",
9
+ "@nmtjs/type": "0.15.0-beta.1"
10
+ },
11
+ "devDependencies": {
12
+ "zod": "^4.1.0"
14
13
  },
15
14
  "files": [
16
15
  "dist",
17
16
  "LICENSE.md",
18
17
  "README.md"
19
18
  ],
20
- "version": "0.14.4",
19
+ "version": "0.15.0-beta.1",
21
20
  "scripts": {
21
+ "clean-build": "rm -rf ./dist",
22
22
  "build": "tsc",
23
+ "dev": "tsc --watch",
23
24
  "type-check": "tsc --noEmit"
24
25
  }
25
26
  }
@@ -1 +0,0 @@
1
- export declare const Kind: unique symbol;
package/dist/constants.js DELETED
@@ -1 +0,0 @@
1
- export const Kind = Symbol.for('neemata:kind');
package/dist/index.d.ts DELETED
@@ -1,45 +0,0 @@
1
- export * from './schemas/api.ts';
2
- export * from './schemas/event.ts';
3
- export * from './schemas/procedure.ts';
4
- export * from './schemas/router.ts';
5
- export * from './schemas/subscription.ts';
6
- export declare namespace contract {
7
- const procedure: <const Options extends {
8
- input?: import("@nmtjs/type").BaseType;
9
- output?: import("@nmtjs/type").BaseType;
10
- stream?: import("@nmtjs/type").BaseType | undefined;
11
- timeout?: number;
12
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
13
- name?: string;
14
- }>(options: Options) => import("./schemas/procedure.ts").TProcedureContract<Options["input"] extends import("@nmtjs/type").BaseType ? Options["input"] : import("@nmtjs/type/never").NeverType, Options["output"] extends import("@nmtjs/type").BaseType ? Options["output"] : import("@nmtjs/type/never").NeverType, Options["stream"] extends import("@nmtjs/type").BaseType ? Options["stream"] : undefined, Options["name"] extends string ? Options["name"] : undefined>;
15
- const event: <Payload extends import("@nmtjs/type").BaseType, Name extends string | undefined = undefined, Options extends import("./schemas/subscription.ts").SubcriptionOptions | undefined = undefined>(options?: {
16
- payload?: Payload;
17
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
18
- name?: Name;
19
- }) => import("./schemas/event.ts").TEventContract<Payload, Name, Options>;
20
- const subscription: (<const Options extends {
21
- events: Record<string, import("./schemas/event.ts").TAnyEventContract>;
22
- name?: string;
23
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
24
- }, SubOpt extends import("./schemas/subscription.ts").SubcriptionOptions = null>(options: Options) => import("./schemas/subscription.ts").TSubscriptionContract<SubOpt, Options["events"], Options["name"]>) & {
25
- withOptions: <Options extends import("./schemas/subscription.ts").SubcriptionOptions>() => <T extends {
26
- events: Record<string, import("./schemas/event.ts").TAnyEventContract>;
27
- name?: string;
28
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
29
- }>(options: T) => import("./schemas/subscription.ts").TSubscriptionContract<Options, T["events"], T["name"]>;
30
- };
31
- const router: <const Options extends {
32
- routes: Record<string, import("./schemas/router.ts").TRouteContract>;
33
- name?: string;
34
- timeout?: number;
35
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
36
- }>(options: Options) => import("./schemas/router.ts").TRouterContract<Options["routes"], Options["name"] extends string ? Options["name"] : undefined>;
37
- const api: <const Options extends {
38
- router: import("./schemas/router.ts").TAnyRouterContract;
39
- timeout?: number;
40
- schemaOptions?: import("./utils.ts").ContractSchemaOptions;
41
- }>(options: Options) => import("./schemas/api.ts").TAPIContract<Options["router"]>;
42
- const blob: (options?: import("./types/blob.ts").BlobOptions) => import("@nmtjs/type/custom").CustomType<import("@nmtjs/protocol").ProtocolBlobInterface>;
43
- }
44
- export { contract as c };
45
- export default contract;
package/dist/index.js DELETED
@@ -1,22 +0,0 @@
1
- import { APIContract } from "./schemas/api.js";
2
- import { EventContract } from "./schemas/event.js";
3
- import { ProcedureContract } from "./schemas/procedure.js";
4
- import { RouterContract } from "./schemas/router.js";
5
- import { SubscriptionContract } from "./schemas/subscription.js";
6
- import { BlobType } from "./types/blob.js";
7
- export * from "./schemas/api.js";
8
- export * from "./schemas/event.js";
9
- export * from "./schemas/procedure.js";
10
- export * from "./schemas/router.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.router = RouterContract;
18
- contract.api = APIContract;
19
- contract.blob = BlobType;
20
- })(contract || (contract = {}));
21
- export { contract as c };
22
- export default contract;
@@ -1,17 +0,0 @@
1
- import type { ContractSchemaOptions } from '../utils.ts';
2
- import type { TAnyRouterContract } from './router.ts';
3
- import { Kind } from '../constants.ts';
4
- export declare const APIKind: unique symbol;
5
- export type TAnyAPIContract = TAPIContract<TAnyRouterContract>;
6
- export interface TAPIContract<Router extends TAnyRouterContract = TAnyRouterContract> {
7
- readonly [Kind]: typeof APIKind;
8
- readonly type: 'neemata:api';
9
- readonly router: Router;
10
- readonly timeout?: number;
11
- }
12
- export declare const APIContract: <const Options extends {
13
- router: TAnyRouterContract;
14
- timeout?: number;
15
- schemaOptions?: ContractSchemaOptions;
16
- }>(options: Options) => TAPIContract<Options["router"]>;
17
- export declare function IsAPIContract(value: any): value is TAnyAPIContract;
@@ -1,16 +0,0 @@
1
- import { Kind } from "../constants.js";
2
- import { createSchema } from "../utils.js";
3
- export const APIKind = Symbol('NeemataAPI');
4
- export const APIContract = (options) => {
5
- const { router, timeout, schemaOptions } = options;
6
- return createSchema({
7
- ...schemaOptions,
8
- [Kind]: APIKind,
9
- type: 'neemata:api',
10
- router,
11
- timeout,
12
- });
13
- };
14
- export function IsAPIContract(value) {
15
- return Kind in value && value[Kind] === APIKind;
16
- }
@@ -1,20 +0,0 @@
1
- import type { BaseType } from '@nmtjs/type';
2
- import { t } from '@nmtjs/type';
3
- import type { ContractSchemaOptions } from '../utils.ts';
4
- import type { SubcriptionOptions } from './subscription.ts';
5
- import { Kind } from '../constants.ts';
6
- export declare const EventKind: unique symbol;
7
- export type TAnyEventContract = TEventContract<BaseType, string | undefined, SubcriptionOptions | undefined>;
8
- export interface TEventContract<Payload extends BaseType = t.NeverType, Name extends string | undefined = undefined, Options extends SubcriptionOptions | undefined = undefined> {
9
- readonly [Kind]: typeof EventKind;
10
- readonly type: 'neemata:event';
11
- readonly name: Name;
12
- readonly payload: Payload;
13
- readonly options: Options;
14
- }
15
- export declare const EventContract: <Payload extends BaseType, Name extends string | undefined = undefined, Options extends SubcriptionOptions | undefined = undefined>(options?: {
16
- payload?: Payload;
17
- schemaOptions?: ContractSchemaOptions;
18
- name?: Name;
19
- }) => TEventContract<Payload, Name, Options>;
20
- export declare function IsEventContract(value: any): value is TAnyEventContract;
@@ -1,18 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
1
- import type { BaseType } from '@nmtjs/type';
2
- import type { NeverType } from '@nmtjs/type/never';
3
- import type { ContractSchemaOptions } from '../utils.ts';
4
- import { Kind } from '../constants.ts';
5
- export type TAnyProcedureContract = TProcedureContract<BaseType, BaseType, BaseType | undefined, string | undefined>;
6
- export declare const ProcedureKind: unique symbol;
7
- export interface TProcedureContract<Input extends BaseType, Output extends BaseType, Stream extends BaseType | undefined, Name extends string | undefined = undefined> {
8
- readonly [Kind]: typeof ProcedureKind;
9
- readonly type: 'neemata:procedure';
10
- readonly name: Name;
11
- readonly input: Input;
12
- readonly output: Output;
13
- readonly stream: Stream;
14
- readonly timeout?: number;
15
- }
16
- export declare const ProcedureContract: <const Options extends {
17
- input?: BaseType;
18
- output?: BaseType;
19
- stream?: BaseType | undefined;
20
- timeout?: number;
21
- schemaOptions?: ContractSchemaOptions;
22
- name?: string;
23
- }>(options: Options) => TProcedureContract<Options["input"] extends BaseType ? Options["input"] : NeverType, Options["output"] extends BaseType ? Options["output"] : NeverType, Options["stream"] extends BaseType ? Options["stream"] : undefined, Options["name"] extends string ? Options["name"] : undefined>;
24
- export declare function IsProcedureContract(contract: any): contract is TAnyProcedureContract;
25
- export declare function IsStreamProcedureContract(contract: any): contract is TAnyProcedureContract;
@@ -1,23 +0,0 @@
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
- }
@@ -1,22 +0,0 @@
1
- import type { ContractSchemaOptions } from '../utils.ts';
2
- import type { TAnyProcedureContract, TProcedureContract } from './procedure.ts';
3
- import { Kind } from '../constants.ts';
4
- export declare const RouterKind: unique symbol;
5
- export type TAnyRouterContract<RouteContracts extends Record<string, TRouteContract> = Record<string, TRouteContract>> = TRouterContract<RouteContracts, string | undefined>;
6
- export type TRouteContract = TAnyProcedureContract | TRouterContract<Record<string, TRouteContract>, string | undefined>;
7
- export interface TRouterContract<Routes extends Record<string, TRouteContract> = {}, Name extends string | undefined = undefined> {
8
- readonly [Kind]: typeof RouterKind;
9
- readonly type: 'neemata:router';
10
- readonly name: Name;
11
- readonly routes: {
12
- [K in keyof Routes]: Routes[K] extends TAnyRouterContract ? TRouterContract<Routes[K]['routes'], Name extends string ? `${Name}/${Extract<K, string>}` : Extract<K, string>> : Routes[K] extends TAnyProcedureContract ? TProcedureContract<Routes[K]['input'], Routes[K]['output'], Routes[K]['stream'], Name extends string ? `${Name}/${Extract<K, string>}` : Extract<K, string>> : never;
13
- };
14
- readonly timeout?: number;
15
- }
16
- export declare const RouterContract: <const Options extends {
17
- routes: Record<string, TRouteContract>;
18
- name?: string;
19
- timeout?: number;
20
- schemaOptions?: ContractSchemaOptions;
21
- }>(options: Options) => TRouterContract<Options["routes"], Options["name"] extends string ? Options["name"] : undefined>;
22
- export declare function IsRouterContract(value: any): value is TAnyRouterContract;
@@ -1,41 +0,0 @@
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
- }
@@ -1,27 +0,0 @@
1
- import type { ContractSchemaOptions } from '../utils.ts';
2
- import type { TAnyEventContract, TEventContract } from './event.ts';
3
- import { Kind } from '../constants.ts';
4
- export declare const SubscriptionKind: unique symbol;
5
- export type SubcriptionOptions = Record<string, string | number | boolean> | null;
6
- export type TAnySubscriptionContract = TSubscriptionContract<SubcriptionOptions, Record<string, TAnyEventContract>, string | undefined>;
7
- export interface TSubscriptionContract<Options extends SubcriptionOptions = null, Events extends Record<string, unknown> = {}, Name extends string | undefined = undefined> {
8
- readonly [Kind]: typeof SubscriptionKind;
9
- readonly type: 'neemata:subscription';
10
- readonly name: Name;
11
- readonly options: Options;
12
- readonly events: {
13
- [K in keyof Events]: Events[K] extends TAnyEventContract ? TEventContract<Events[K]['payload'], Name extends string ? `${Name}/${Extract<K, string>}` : Extract<K, string>, Options> : never;
14
- };
15
- }
16
- export declare const SubscriptionContract: (<const Options extends {
17
- events: Record<string, TAnyEventContract>;
18
- name?: string;
19
- schemaOptions?: ContractSchemaOptions;
20
- }, SubOpt extends SubcriptionOptions = null>(options: Options) => TSubscriptionContract<SubOpt, Options["events"], Options["name"]>) & {
21
- withOptions: <Options extends SubcriptionOptions>() => <T extends {
22
- events: Record<string, TAnyEventContract>;
23
- name?: string;
24
- schemaOptions?: ContractSchemaOptions;
25
- }>(options: T) => TSubscriptionContract<Options, T["events"], T["name"]>;
26
- };
27
- export declare function IsSubscriptionContract(contract: any): contract is TAnySubscriptionContract;
@@ -1,28 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
1
- import type { ProtocolBlobInterface } from '@nmtjs/protocol';
2
- import { t } from '@nmtjs/type';
3
- export interface BlobOptions {
4
- maxSize?: number;
5
- contentType?: string;
6
- }
7
- export declare const BlobType: (options?: BlobOptions) => t.CustomType<ProtocolBlobInterface>;
@@ -1,16 +0,0 @@
1
- import { t } from '@nmtjs/type';
2
- export const BlobType = (options = {}) => t.custom({
3
- decode: (value) => {
4
- // TODO: here should be some validation logic to check if the value is an actual blob
5
- if ('metadata' in value) {
6
- if (options.maxSize) {
7
- const size = value.metadata.size;
8
- if (typeof size !== 'undefined' && size > options.maxSize) {
9
- throw new Error('Blob size unknown or exceeds maximum allowed size');
10
- }
11
- }
12
- }
13
- return value;
14
- },
15
- encode: (value) => value,
16
- });
package/dist/utils.d.ts DELETED
@@ -1,18 +0,0 @@
1
- export type ContractSchemaOptions = {
2
- title?: string;
3
- description?: string;
4
- };
5
- export declare const applyNames: <T extends Record<string, {
6
- serviceName?: string;
7
- }>>(params: T, opts: {
8
- serviceName?: string;
9
- subscriptionName?: string;
10
- }) => {
11
- [k: string]: {
12
- serviceName?: string;
13
- subscriptionName?: string;
14
- name: string;
15
- };
16
- };
17
- export declare const createSchema: <T>(schema: T) => T;
18
- export declare const concatFullName: (parent: string | undefined, name: string) => string;
package/dist/utils.js DELETED
@@ -1,7 +0,0 @@
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
- };