@nmtjs/contract 0.12.6 → 0.12.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -9,16 +9,15 @@
9
9
  }
10
10
  },
11
11
  "dependencies": {
12
- "@nmtjs/protocol": "0.12.6",
13
- "@nmtjs/type": "0.12.6"
12
+ "@nmtjs/protocol": "0.12.7",
13
+ "@nmtjs/type": "0.12.7"
14
14
  },
15
15
  "files": [
16
- "src",
17
16
  "dist",
18
17
  "LICENSE.md",
19
18
  "README.md"
20
19
  ],
21
- "version": "0.12.6",
20
+ "version": "0.12.7",
22
21
  "scripts": {
23
22
  "build": "tsc",
24
23
  "type-check": "tsc --noEmit"
package/src/constants.ts DELETED
@@ -1 +0,0 @@
1
- export const Kind = Symbol.for('neemata:kind')
package/src/index.ts DELETED
@@ -1,28 +0,0 @@
1
- // biome-ignore lint/correctness/noUnusedImports: TSC wants it
2
- import t from '@nmtjs/type'
3
-
4
- import { APIContract } from './schemas/api.ts'
5
- import { EventContract } from './schemas/event.ts'
6
- import { NamespaceContract } from './schemas/namespace.ts'
7
- import { ProcedureContract } from './schemas/procedure.ts'
8
- import { SubscriptionContract } from './schemas/subscription.ts'
9
- import { BlobType } from './types/blob.ts'
10
-
11
- export * from './schemas/api.ts'
12
- export * from './schemas/event.ts'
13
- export * from './schemas/namespace.ts'
14
- export * from './schemas/procedure.ts'
15
- export * from './schemas/subscription.ts'
16
-
17
- export namespace contract {
18
- export const procedure = ProcedureContract
19
- export const event = EventContract
20
- export const subscription = SubscriptionContract
21
- export const namespace = NamespaceContract
22
- export const api = APIContract
23
- export const blob = BlobType
24
- }
25
-
26
- export { contract as c }
27
-
28
- export default contract
@@ -1,104 +0,0 @@
1
- import { Kind } from '../constants.ts'
2
- import { type ContractSchemaOptions, createSchema } from '../utils.ts'
3
- import type { TEventContract } from './event.ts'
4
- import type { TAnyNamespaceContract, TNamespaceContract } from './namespace.ts'
5
- import type { TProcedureContract } from './procedure.ts'
6
-
7
- export const APIKind = Symbol('NeemataAPI')
8
-
9
- export type TAnyAPIContract = TAPIContract<
10
- Record<string, TAnyNamespaceContract>
11
- >
12
-
13
- export interface TAPIContract<
14
- Namespaces extends Record<string, TAnyNamespaceContract> = {},
15
- > {
16
- readonly [Kind]: typeof APIKind
17
- readonly type: 'neemata:api'
18
- readonly namespaces: {
19
- [K in keyof Namespaces]: TNamespaceContract<
20
- Namespaces[K]['procedures'],
21
- Namespaces[K]['events'],
22
- Extract<K, string>
23
- >
24
- }
25
- readonly timeout?: number
26
- }
27
-
28
- export const APIContract = <
29
- const Options extends {
30
- namespaces: Record<string, TAnyNamespaceContract>
31
- timeout?: number
32
- schemaOptions?: ContractSchemaOptions
33
- },
34
- >(
35
- options: Options,
36
- ) => {
37
- const { timeout, schemaOptions } = options
38
-
39
- const _namespaces = {} as any
40
-
41
- for (const namespaceKey in options.namespaces) {
42
- const namespace = options.namespaces[namespaceKey]
43
- const _procedures = {} as any
44
- for (const procedureKey in namespace.procedures) {
45
- const procedure = namespace.procedures[procedureKey]
46
- _procedures[procedureKey] = createSchema<
47
- TProcedureContract<
48
- (typeof procedure)['input'],
49
- (typeof procedure)['output'],
50
- (typeof procedure)['stream'],
51
- Extract<typeof procedureKey, string>,
52
- Extract<typeof namespaceKey, string>
53
- >
54
- >({
55
- ...procedure,
56
- name: procedureKey,
57
- namespace: namespaceKey,
58
- })
59
- }
60
-
61
- const _events = {} as any
62
- for (const eventKey in namespace.events) {
63
- const event = namespace.events[eventKey]
64
- _events[eventKey] = createSchema<
65
- TEventContract<
66
- (typeof event)['payload'],
67
- Extract<typeof eventKey, string>,
68
- undefined,
69
- Extract<typeof namespaceKey, string>
70
- >
71
- >({
72
- ...event,
73
- subscription: undefined,
74
- name: eventKey,
75
- namespace: namespaceKey,
76
- })
77
- }
78
-
79
- _namespaces[namespaceKey] = createSchema<
80
- TNamespaceContract<
81
- typeof _procedures,
82
- typeof _events,
83
- Extract<typeof namespaceKey, string>
84
- >
85
- >({
86
- ...namespace,
87
- procedures: _procedures,
88
- events: _events,
89
- name: namespaceKey,
90
- })
91
- }
92
-
93
- return createSchema<TAPIContract<Options['namespaces']>>({
94
- ...schemaOptions,
95
- [Kind]: APIKind,
96
- type: 'neemata:api',
97
- namespaces: _namespaces,
98
- timeout,
99
- })
100
- }
101
-
102
- export function IsAPIContract(value: any): value is TAnyAPIContract {
103
- return Kind in value && value[Kind] === APIKind
104
- }
@@ -1,54 +0,0 @@
1
- import { type BaseType, t } from '@nmtjs/type'
2
- import { Kind } from '../constants.ts'
3
- import { type ContractSchemaOptions, createSchema } from '../utils.ts'
4
-
5
- export const EventKind = Symbol('NeemataEvent')
6
-
7
- export type TAnyEventContract = TEventContract<
8
- BaseType,
9
- string | undefined,
10
- string | undefined,
11
- string | undefined
12
- >
13
-
14
- export interface TEventContract<
15
- Payload extends BaseType = t.NeverType,
16
- Name extends string | undefined = undefined,
17
- Subscription extends string | undefined = undefined,
18
- Namespace extends string | undefined = undefined,
19
- > {
20
- readonly [Kind]: typeof EventKind
21
- readonly type: 'neemata:event'
22
- readonly name: Name
23
- readonly subscription: Subscription
24
- readonly namespace: Namespace
25
- readonly payload: Payload
26
- }
27
-
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>>({
42
- ...schemaOptions,
43
- [Kind]: EventKind,
44
- type: 'neemata:event',
45
- payload,
46
- name: name as Name,
47
- subscription: undefined,
48
- namespace: undefined,
49
- })
50
- }
51
-
52
- export function IsEventContract(value: any): value is TAnyEventContract {
53
- return Kind in value && value[Kind] === EventKind
54
- }
@@ -1,118 +0,0 @@
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
-
6
- export const NamespaceKind = Symbol('NeemataNamespace')
7
-
8
- export type TAnyNamespaceContract<
9
- Procedures extends Record<string, TAnyProcedureContract> = Record<
10
- string,
11
- TAnyProcedureContract
12
- >,
13
- > = TNamespaceContract<
14
- Procedures,
15
- Record<string, TAnyEventContract>,
16
- string | undefined
17
- >
18
-
19
- export interface TNamespaceContract<
20
- Procedures extends Record<string, TAnyProcedureContract> = {},
21
- Events extends Record<string, TAnyEventContract> = {},
22
- Name extends string | undefined = undefined,
23
- > {
24
- readonly [Kind]: typeof NamespaceKind
25
- readonly type: 'neemata:namespace'
26
- readonly name: Name
27
- readonly procedures: {
28
- [K in keyof Procedures]: TProcedureContract<
29
- Procedures[K]['input'],
30
- Procedures[K]['output'],
31
- Procedures[K]['stream'],
32
- Extract<K, string>,
33
- Name
34
- >
35
- }
36
- readonly events: {
37
- [K in keyof Events]: TEventContract<
38
- Events[K]['payload'],
39
- Extract<K, string>,
40
- undefined,
41
- Name
42
- >
43
- }
44
- readonly timeout?: number
45
- }
46
-
47
- export const NamespaceContract = <
48
- const Options extends {
49
- procedures: Record<string, TAnyProcedureContract>
50
- events: Record<string, TAnyEventContract>
51
- name?: string
52
- timeout?: number
53
- schemaOptions?: ContractSchemaOptions
54
- },
55
- >(
56
- options: Options,
57
- ) => {
58
- const { name, timeout, schemaOptions = {} as ContractSchemaOptions } = options
59
- const events: Record<string, any> = {}
60
-
61
- for (const name in options.events) {
62
- const event = options.events[name]
63
- events[name] = createSchema<
64
- TEventContract<
65
- (typeof event)['payload'],
66
- Extract<typeof name, string>,
67
- undefined,
68
- Options['name'] extends string ? Options['name'] : undefined
69
- >
70
- >({
71
- ...event,
72
- subscription: undefined,
73
- name: name as any,
74
- namespace: options?.name as any,
75
- })
76
- }
77
-
78
- const procedures: Record<string, any> = {}
79
-
80
- for (const name in options.procedures) {
81
- const procedure: any = options.procedures[name]
82
- procedures[name] = createSchema<
83
- TProcedureContract<
84
- (typeof procedure)['input'],
85
- (typeof procedure)['output'],
86
- (typeof procedure)['stream'],
87
- Extract<typeof name, string>,
88
- Options['name'] extends string ? Options['name'] : undefined
89
- >
90
- >({
91
- ...procedure,
92
- name: name as any,
93
- namespace: options?.name as any,
94
- })
95
- }
96
-
97
- return createSchema<
98
- TNamespaceContract<
99
- Options['procedures'],
100
- Options['events'],
101
- Options['name'] extends string ? Options['name'] : undefined
102
- >
103
- >({
104
- ...schemaOptions,
105
- [Kind]: NamespaceKind,
106
- type: 'neemata:namespace',
107
- name: name as any,
108
- procedures: procedures as any,
109
- events: events as any,
110
- timeout,
111
- })
112
- }
113
-
114
- export function IsNamespaceContract(
115
- value: any,
116
- ): value is TAnyNamespaceContract {
117
- return Kind in value && value[Kind] === NamespaceKind
118
- }
@@ -1,82 +0,0 @@
1
- import { type BaseType, t } from '@nmtjs/type'
2
- import { Kind } from '../constants.ts'
3
- import { type ContractSchemaOptions, createSchema } from '../utils.ts'
4
-
5
- export type TAnyProcedureContract = TProcedureContract<
6
- BaseType,
7
- BaseType,
8
- BaseType | undefined,
9
- string | undefined,
10
- string | undefined
11
- >
12
-
13
- export const ProcedureKind = Symbol('NeemataProcedure')
14
-
15
- export interface TProcedureContract<
16
- Input extends BaseType,
17
- Output extends BaseType,
18
- Stream extends BaseType | undefined,
19
- Name extends string | undefined = undefined,
20
- Namespace extends string | undefined = undefined,
21
- > {
22
- readonly [Kind]: typeof ProcedureKind
23
- readonly type: 'neemata:procedure'
24
- readonly name: Name
25
- readonly namespace: Namespace
26
- readonly input: Input
27
- readonly output: Output
28
- readonly stream: Stream
29
- readonly timeout?: number
30
- }
31
-
32
- export const ProcedureContract = <
33
- const Options extends {
34
- input?: BaseType
35
- output?: BaseType
36
- stream?: BaseType | undefined
37
- timeout?: number
38
- schemaOptions?: ContractSchemaOptions
39
- name?: string
40
- },
41
- >(
42
- options: Options,
43
- ) => {
44
- const {
45
- input = t.never() as any,
46
- output = t.never() as any,
47
- stream = undefined as any,
48
- name = undefined as any,
49
- timeout,
50
- schemaOptions = {},
51
- } = options
52
- return createSchema<
53
- TProcedureContract<
54
- Options['input'] extends BaseType ? Options['input'] : t.NeverType,
55
- Options['output'] extends BaseType ? Options['output'] : t.NeverType,
56
- Options['stream'] extends BaseType ? Options['stream'] : undefined,
57
- Options['name'] extends string ? Options['name'] : undefined
58
- >
59
- >({
60
- ...schemaOptions,
61
- [Kind]: ProcedureKind,
62
- type: 'neemata:procedure',
63
- input,
64
- output,
65
- stream,
66
- name,
67
- timeout,
68
- namespace: undefined,
69
- })
70
- }
71
-
72
- export function IsProcedureContract(
73
- contract: any,
74
- ): contract is TAnyProcedureContract {
75
- return Kind in contract && contract[Kind] === ProcedureKind
76
- }
77
-
78
- export function IsStreamProcedureContract(
79
- contract: any,
80
- ): contract is TAnyProcedureContract {
81
- return IsProcedureContract(contract) && typeof contract.stream !== 'undefined'
82
- }
@@ -1,96 +0,0 @@
1
- import { Kind } from '../constants.ts'
2
- import { type ContractSchemaOptions, createSchema } from '../utils.ts'
3
- import type { TAnyEventContract, TEventContract } from './event.ts'
4
-
5
- export const SubscriptionKind = Symbol('NeemataSubscription')
6
-
7
- export type SubcriptionOptions = Record<
8
- string,
9
- string | number | boolean
10
- > | null
11
-
12
- export type TAnySubscriptionContract = TSubscriptionContract<
13
- SubcriptionOptions,
14
- Record<string, TAnyEventContract>,
15
- string | undefined
16
- >
17
-
18
- export interface TSubscriptionContract<
19
- Options extends SubcriptionOptions = null,
20
- Events extends Record<string, unknown> = {},
21
- Name extends string | undefined = undefined,
22
- > {
23
- readonly [Kind]: typeof SubscriptionKind
24
- readonly type: 'neemata:subscription'
25
- readonly name: Name
26
- readonly options: Options
27
- readonly events: {
28
- [K in keyof Events]: Events[K] extends TAnyEventContract
29
- ? TEventContract<Events[K]['payload'], Extract<K, string>, Name>
30
- : never
31
- }
32
- }
33
-
34
- const _SubscriptionContract = <
35
- const Options extends {
36
- events: Record<string, TAnyEventContract>
37
- name?: string
38
- schemaOptions?: ContractSchemaOptions
39
- },
40
- SubOpt extends SubcriptionOptions = null,
41
- >(
42
- options: Options,
43
- ) => {
44
- const { schemaOptions = {}, name } = options
45
- const _events = {} as any
46
- for (const key in options.events) {
47
- const event = options.events[key]
48
- _events[key] = createSchema<
49
- TEventContract<
50
- (typeof event)['payload'],
51
- Extract<typeof key, string>,
52
- undefined,
53
- Options['name'] extends string ? Options['name'] : undefined
54
- >
55
- >({
56
- ...event,
57
- name: key,
58
- namespace: undefined,
59
- subscription: name as any,
60
- })
61
- }
62
- return createSchema<
63
- TSubscriptionContract<
64
- SubOpt,
65
- Options['events'],
66
- Options['name'] extends string ? Options['name'] : undefined
67
- >
68
- >({
69
- ...schemaOptions,
70
- [Kind]: SubscriptionKind,
71
- type: 'neemata:subscription',
72
- events: _events,
73
- name: name as any,
74
- options: undefined as unknown as SubOpt,
75
- })
76
- }
77
-
78
- export const SubscriptionContract = Object.assign(_SubscriptionContract, {
79
- withOptions: <Options extends SubcriptionOptions>() => {
80
- return <
81
- T extends {
82
- events: Record<string, TAnyEventContract>
83
- name?: string
84
- schemaOptions?: ContractSchemaOptions
85
- },
86
- >(
87
- options: T,
88
- ) => _SubscriptionContract<T, Options>(options)
89
- },
90
- })
91
-
92
- export function IsSubscriptionContract(
93
- contract: any,
94
- ): contract is TAnySubscriptionContract {
95
- return Kind in contract && contract[Kind] === SubscriptionKind
96
- }
package/src/types/blob.ts DELETED
@@ -1,26 +0,0 @@
1
- import type { ProtocolBlobInterface } from '@nmtjs/protocol'
2
- import { t } from '@nmtjs/type'
3
-
4
- export interface BlobOptions {
5
- maxSize?: number
6
- contentType?: string
7
- }
8
-
9
- export const BlobType = (
10
- options: BlobOptions = {},
11
- ): t.CustomType<ProtocolBlobInterface> =>
12
- t.custom<ProtocolBlobInterface>({
13
- decode: (value) => {
14
- // TODO: here should be some validation logic to check if the value is an actual blob
15
- if ('metadata' in value) {
16
- if (options.maxSize) {
17
- const size = (value as ProtocolBlobInterface).metadata.size
18
- if (typeof size !== 'undefined' && size > options.maxSize) {
19
- throw new Error('Blob size unknown or exceeds maximum allowed size')
20
- }
21
- }
22
- }
23
- return value
24
- },
25
- encode: (value) => value,
26
- })
package/src/utils.ts DELETED
@@ -1,15 +0,0 @@
1
- export type ContractSchemaOptions = {
2
- title?: string
3
- description?: string
4
- }
5
-
6
- export const applyNames = <T extends Record<string, { serviceName?: string }>>(
7
- params: T,
8
- opts: { serviceName?: string; subscriptionName?: string },
9
- ) => {
10
- return Object.fromEntries(
11
- Object.entries(params).map(([k, v]) => [k, { ...v, name: k, ...opts }]),
12
- )
13
- }
14
-
15
- export const createSchema = <T>(schema: T) => Object.freeze(schema) as T