nmtjs 0.16.0-beta.8 → 0.16.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.
Files changed (150) hide show
  1. package/dist/cli.js +2 -1
  2. package/dist/cli.js.map +1 -1
  3. package/dist/entrypoints/worker.js +9 -9
  4. package/dist/entrypoints/worker.js.map +1 -1
  5. package/dist/index.d.ts +14 -11
  6. package/dist/index.js +5 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/runtime/application/index.d.ts +1 -5
  9. package/dist/runtime/application/index.js +1 -5
  10. package/dist/runtime/application/index.js.map +1 -1
  11. package/dist/runtime/enums.d.ts +1 -12
  12. package/dist/runtime/enums.js +1 -14
  13. package/dist/runtime/enums.js.map +1 -1
  14. package/dist/runtime/hooks.d.ts +1 -4
  15. package/dist/runtime/hooks.js +1 -3
  16. package/dist/runtime/hooks.js.map +1 -1
  17. package/dist/runtime/index.d.ts +1 -3
  18. package/dist/runtime/index.js +1 -3
  19. package/dist/runtime/index.js.map +1 -1
  20. package/dist/runtime/injectables.d.ts +3 -3
  21. package/dist/runtime/jobs/job.d.ts +1 -2
  22. package/dist/runtime/jobs/job.js.map +1 -1
  23. package/dist/runtime/jobs/router.d.ts +2 -5
  24. package/dist/runtime/jobs/router.js +2 -3
  25. package/dist/runtime/jobs/router.js.map +1 -1
  26. package/dist/runtime/jobs/runner.d.ts +1 -1
  27. package/dist/runtime/jobs/runner.js +1 -1
  28. package/dist/runtime/jobs/runner.js.map +1 -1
  29. package/dist/runtime/jobs/types.d.ts +1 -1
  30. package/dist/runtime/metrics/metric.d.ts +2 -2
  31. package/dist/runtime/metrics/metric.js +1 -1
  32. package/dist/runtime/metrics/metric.js.map +1 -1
  33. package/dist/runtime/metrics/registry.d.ts +3 -3
  34. package/dist/runtime/metrics/registry.js +1 -1
  35. package/dist/runtime/metrics/registry.js.map +1 -1
  36. package/dist/runtime/metrics/server.js +1 -1
  37. package/dist/runtime/metrics/server.js.map +1 -1
  38. package/dist/runtime/plugin.d.ts +2 -8
  39. package/dist/runtime/plugin.js +1 -3
  40. package/dist/runtime/plugin.js.map +1 -1
  41. package/dist/runtime/runtime.d.ts +2 -2
  42. package/dist/runtime/runtime.js +1 -2
  43. package/dist/runtime/runtime.js.map +1 -1
  44. package/dist/runtime/server/applications.js +3 -0
  45. package/dist/runtime/server/applications.js.map +1 -1
  46. package/dist/runtime/server/config.d.ts +15 -12
  47. package/dist/runtime/server/config.js.map +1 -1
  48. package/dist/runtime/server/jobs.d.ts +6 -7
  49. package/dist/runtime/server/jobs.js +44 -30
  50. package/dist/runtime/server/jobs.js.map +1 -1
  51. package/dist/runtime/subscription/redis.d.ts +1 -1
  52. package/dist/runtime/subscription/redis.js.map +1 -1
  53. package/dist/runtime/types.d.ts +3 -11
  54. package/dist/runtime/workers/application.d.ts +15 -33
  55. package/dist/runtime/workers/application.js +46 -139
  56. package/dist/runtime/workers/application.js.map +1 -1
  57. package/dist/runtime/workers/job.js +2 -1
  58. package/dist/runtime/workers/job.js.map +1 -1
  59. package/dist/vite/server.js +1 -1
  60. package/dist/vite/server.js.map +1 -1
  61. package/package.json +19 -14
  62. package/src/cli.ts +2 -3
  63. package/src/entrypoints/worker.ts +11 -9
  64. package/src/index.ts +12 -5
  65. package/src/runtime/application/index.ts +1 -5
  66. package/src/runtime/enums.ts +2 -14
  67. package/src/runtime/hooks.ts +1 -5
  68. package/src/runtime/index.ts +2 -3
  69. package/src/runtime/injectables.ts +2 -2
  70. package/src/runtime/jobs/job.ts +1 -2
  71. package/src/runtime/jobs/router.ts +12 -15
  72. package/src/runtime/jobs/runner.ts +2 -2
  73. package/src/runtime/jobs/types.ts +1 -1
  74. package/src/runtime/metrics/metric.ts +2 -2
  75. package/src/runtime/metrics/registry.ts +5 -1
  76. package/src/runtime/metrics/server.ts +1 -1
  77. package/src/runtime/plugin.ts +2 -13
  78. package/src/runtime/runtime.ts +2 -4
  79. package/src/runtime/server/applications.ts +6 -0
  80. package/src/runtime/server/config.ts +27 -21
  81. package/src/runtime/server/jobs.ts +58 -34
  82. package/src/runtime/subscription/redis.ts +1 -1
  83. package/src/runtime/types.ts +3 -12
  84. package/src/runtime/workers/application.ts +76 -160
  85. package/src/runtime/workers/job.ts +3 -6
  86. package/src/vite/server.ts +1 -1
  87. package/dist/runtime/application/api/api.d.ts +0 -55
  88. package/dist/runtime/application/api/api.js +0 -252
  89. package/dist/runtime/application/api/api.js.map +0 -1
  90. package/dist/runtime/application/api/config.d.ts +0 -14
  91. package/dist/runtime/application/api/config.js +0 -6
  92. package/dist/runtime/application/api/config.js.map +0 -1
  93. package/dist/runtime/application/api/constants.d.ts +0 -14
  94. package/dist/runtime/application/api/constants.js +0 -8
  95. package/dist/runtime/application/api/constants.js.map +0 -1
  96. package/dist/runtime/application/api/filters.d.ts +0 -14
  97. package/dist/runtime/application/api/filters.js +0 -11
  98. package/dist/runtime/application/api/filters.js.map +0 -1
  99. package/dist/runtime/application/api/guards.d.ts +0 -15
  100. package/dist/runtime/application/api/guards.js +0 -8
  101. package/dist/runtime/application/api/guards.js.map +0 -1
  102. package/dist/runtime/application/api/index.d.ts +0 -23
  103. package/dist/runtime/application/api/index.js +0 -17
  104. package/dist/runtime/application/api/index.js.map +0 -1
  105. package/dist/runtime/application/api/logging.d.ts +0 -14
  106. package/dist/runtime/application/api/logging.js +0 -77
  107. package/dist/runtime/application/api/logging.js.map +0 -1
  108. package/dist/runtime/application/api/meta.d.ts +0 -21
  109. package/dist/runtime/application/api/meta.js +0 -2
  110. package/dist/runtime/application/api/meta.js.map +0 -1
  111. package/dist/runtime/application/api/middlewares.d.ts +0 -14
  112. package/dist/runtime/application/api/middlewares.js +0 -12
  113. package/dist/runtime/application/api/middlewares.js.map +0 -1
  114. package/dist/runtime/application/api/procedure.d.ts +0 -71
  115. package/dist/runtime/application/api/procedure.js +0 -41
  116. package/dist/runtime/application/api/procedure.js.map +0 -1
  117. package/dist/runtime/application/api/router.d.ts +0 -93
  118. package/dist/runtime/application/api/router.js +0 -55
  119. package/dist/runtime/application/api/router.js.map +0 -1
  120. package/dist/runtime/application/api/types.d.ts +0 -38
  121. package/dist/runtime/application/api/types.js +0 -2
  122. package/dist/runtime/application/api/types.js.map +0 -1
  123. package/dist/runtime/application/config.d.ts +0 -28
  124. package/dist/runtime/application/config.js +0 -25
  125. package/dist/runtime/application/config.js.map +0 -1
  126. package/dist/runtime/application/constants.d.ts +0 -2
  127. package/dist/runtime/application/constants.js +0 -2
  128. package/dist/runtime/application/constants.js.map +0 -1
  129. package/dist/runtime/application/hook.d.ts +0 -19
  130. package/dist/runtime/application/hook.js +0 -11
  131. package/dist/runtime/application/hook.js.map +0 -1
  132. package/dist/runtime/application/hooks.d.ts +0 -3
  133. package/dist/runtime/application/hooks.js +0 -4
  134. package/dist/runtime/application/hooks.js.map +0 -1
  135. package/src/runtime/application/api/api.ts +0 -406
  136. package/src/runtime/application/api/config.ts +0 -18
  137. package/src/runtime/application/api/constants.ts +0 -22
  138. package/src/runtime/application/api/filters.ts +0 -39
  139. package/src/runtime/application/api/guards.ts +0 -33
  140. package/src/runtime/application/api/index.ts +0 -69
  141. package/src/runtime/application/api/logging.ts +0 -110
  142. package/src/runtime/application/api/meta.ts +0 -37
  143. package/src/runtime/application/api/middlewares.ts +0 -37
  144. package/src/runtime/application/api/procedure.ts +0 -224
  145. package/src/runtime/application/api/router.ts +0 -247
  146. package/src/runtime/application/api/types.ts +0 -138
  147. package/src/runtime/application/config.ts +0 -82
  148. package/src/runtime/application/constants.ts +0 -4
  149. package/src/runtime/application/hook.ts +0 -51
  150. package/src/runtime/application/hooks.ts +0 -3
@@ -1,33 +0,0 @@
1
- import type { MaybePromise } from '@nmtjs/common'
2
- import type { Dependant, Dependencies, DependencyContext } from '@nmtjs/core'
3
-
4
- import type { ApiGuardContext } from './types.ts'
5
- import { kGuard } from './constants.ts'
6
-
7
- export type GuardCanFn<Deps extends Dependencies> = (
8
- ctx: DependencyContext<Deps>,
9
- call: ApiGuardContext,
10
- ) => MaybePromise<boolean>
11
-
12
- export type GuardParams<Deps extends Dependencies> =
13
- | { dependencies?: Deps; can: GuardCanFn<Deps> }
14
- | GuardCanFn<Deps>
15
-
16
- export interface Guard<Deps extends Dependencies = Dependencies>
17
- extends Dependant<Deps> {
18
- [kGuard]: true
19
- can: GuardCanFn<Deps>
20
- }
21
-
22
- export type AnyGuard = Guard<any>
23
-
24
- export function createGuard<Deps extends Dependencies = {}>(
25
- paramsOrHandler: GuardParams<Deps>,
26
- ): Guard<Deps> {
27
- const { dependencies = {} as Deps, can } =
28
- typeof paramsOrHandler === 'function'
29
- ? { can: paramsOrHandler }
30
- : paramsOrHandler
31
-
32
- return Object.freeze({ dependencies, can, [kGuard]: true }) as Guard<Deps>
33
- }
@@ -1,69 +0,0 @@
1
- import type {
2
- AnyMeta,
3
- AfterDecodeMetaBinding as CoreAfterDecodeMetaBinding,
4
- AnyFactoryMetaBinding as CoreAnyFactoryMetaBinding,
5
- BeforeDecodeMetaBinding as CoreBeforeDecodeMetaBinding,
6
- Meta as CoreMeta,
7
- MetaFactoryBinding as CoreMetaFactoryBinding,
8
- Dependencies,
9
- MetaPhase,
10
- StaticMetaBinding,
11
- } from '@nmtjs/core'
12
- import { createMeta as createCoreMeta, MetadataKind } from '@nmtjs/core'
13
-
14
- import type { ApiMetaContext } from './meta.ts'
15
-
16
- export type { AnyMeta, MetaPhase, StaticMetaBinding }
17
- export { MetadataKind }
18
-
19
- export type Meta<Value, Kind extends MetadataKind = MetadataKind> = CoreMeta<
20
- Value,
21
- Kind,
22
- ApiMetaContext
23
- >
24
-
25
- export type BeforeDecodeMetaBinding<
26
- T extends AnyMeta = AnyMeta,
27
- Deps extends Dependencies = {},
28
- > = CoreBeforeDecodeMetaBinding<T, Deps, ApiMetaContext>
29
-
30
- export type AfterDecodeMetaBinding<
31
- T extends AnyMeta = AnyMeta,
32
- Deps extends Dependencies = {},
33
- Input = unknown,
34
- > = CoreAfterDecodeMetaBinding<T, Deps, ApiMetaContext, Input>
35
-
36
- export type MetaFactoryBinding<
37
- T extends AnyMeta = AnyMeta,
38
- Deps extends Dependencies = {},
39
- Phase extends MetaPhase = MetaPhase,
40
- Input = unknown,
41
- > = CoreMetaFactoryBinding<T, Deps, Phase, ApiMetaContext, Input>
42
-
43
- export type AnyFactoryMetaBinding<
44
- T extends AnyMeta = AnyMeta,
45
- Deps extends Dependencies = Dependencies,
46
- Phase extends MetaPhase = MetaPhase,
47
- Input = unknown,
48
- > = CoreAnyFactoryMetaBinding<T, Deps, Phase, ApiMetaContext, Input>
49
-
50
- export type AnyMetaBinding = StaticMetaBinding | AnyFactoryMetaBinding
51
-
52
- export function createMeta<
53
- Value,
54
- Kind extends MetadataKind = MetadataKind,
55
- >(): Meta<Value, Kind> {
56
- return createCoreMeta<Value, Kind, ApiMetaContext>() as Meta<Value, Kind>
57
- }
58
-
59
- export * from './api.ts'
60
- export * from './config.ts'
61
- export * from './constants.ts'
62
- export * from './filters.ts'
63
- export * from './guards.ts'
64
- export * from './logging.ts'
65
- export * from './meta.ts'
66
- export * from './middlewares.ts'
67
- export * from './procedure.ts'
68
- export * from './router.ts'
69
- export * from './types.ts'
@@ -1,110 +0,0 @@
1
- import type { MaybePromise } from '@nmtjs/common'
2
- import { IsStreamProcedureContract } from '@nmtjs/contract'
3
- import { CoreInjectables, loggerLocalStorage } from '@nmtjs/core'
4
-
5
- import type { ApiCallContext } from './types.ts'
6
- import { createMiddleware } from './middlewares.ts'
7
-
8
- const defaultContext = (options: ApiCallContext, payload: unknown) => {
9
- return {
10
- callId: options.callId,
11
- connection: {
12
- id: options.connection.id,
13
- type: options.connection.type,
14
- transport: options.connection.transport,
15
- protocol: options.connection.protocol,
16
- identity: options.connection.identity,
17
- },
18
- }
19
- }
20
-
21
- export const LoggingCallContextMiddleware = (
22
- cb: (
23
- options: ApiCallContext,
24
- payload: unknown,
25
- ) => MaybePromise<object> = defaultContext,
26
- ) =>
27
- createMiddleware({
28
- handle: async (_, call, next, payload) => {
29
- const loggingContext = await cb(call, payload)
30
- return loggerLocalStorage.run(loggingContext, async () => {
31
- return next()
32
- })
33
- },
34
- })
35
-
36
- export const LoggingCallMiddleware = (
37
- options: {
38
- level?: 'info' | 'debug' | 'trace'
39
- errorLevel?: 'warn' | 'error' | 'fatal'
40
- includePayload?: boolean
41
- includeResponse?: boolean
42
- includeStreamChunks?: boolean
43
- } = {},
44
- ) =>
45
- createMiddleware({
46
- dependencies: { logger: CoreInjectables.logger('RPC') },
47
- handle: async ({ logger }, call, next, payload) => {
48
- const {
49
- includePayload,
50
- includeResponse,
51
- includeStreamChunks,
52
- level,
53
- errorLevel,
54
- } = {
55
- level: 'info' as const,
56
- errorLevel: 'error' as const,
57
- includePayload: true,
58
- includeResponse: true,
59
- includeStreamChunks: true,
60
- ...options,
61
- }
62
-
63
- const logFn = logger[level].bind(logger)
64
- const errorLogFn = logger[errorLevel].bind(logger)
65
-
66
- logFn(
67
- includePayload
68
- ? { procedure: call.procedure.contract.name, payload: payload }
69
- : { procedure: call.procedure.contract.name },
70
- 'RPC call',
71
- )
72
-
73
- const isIterableProcedure = IsStreamProcedureContract(
74
- call.procedure.contract,
75
- )
76
-
77
- try {
78
- const response = await next()
79
- if (includeResponse) {
80
- if (isIterableProcedure) {
81
- logFn({ result: 'success', response: 'Stream' }, 'RPC response')
82
- } else {
83
- logFn({ result: 'success', response }, 'RPC response')
84
- }
85
- } else {
86
- logFn({ result: 'success' }, 'RPC response')
87
- }
88
-
89
- if (isIterableProcedure && includeStreamChunks) {
90
- return async function* (...args: any[]) {
91
- try {
92
- for await (const chunk of response(...args)) {
93
- logFn({ callId: call.callId, chunk }, 'RPC stream chunk')
94
- yield chunk
95
- }
96
- logFn({ callId: call.callId }, 'RPC stream end')
97
- } catch (error) {
98
- errorLogFn({ callId: call.callId, error }, 'RPC stream error')
99
- throw error
100
- }
101
- }
102
- }
103
-
104
- return response
105
- } catch (error) {
106
- errorLogFn({ error }, 'RPC error')
107
- throw error
108
- }
109
- },
110
- })
@@ -1,37 +0,0 @@
1
- import type { TAnyProcedureContract, TAnyRouterContract } from '@nmtjs/contract'
2
- import type {
3
- AfterDecodeMetaBinding,
4
- AnyMeta,
5
- BeforeDecodeMetaBinding,
6
- Container,
7
- StaticMetaBinding,
8
- } from '@nmtjs/core'
9
- import type { GatewayConnection } from '@nmtjs/gateway'
10
-
11
- export type ApiMetaRouteContext = Readonly<{
12
- contract: TAnyRouterContract
13
- timeout?: number
14
- }>
15
-
16
- export type ApiMetaProcedureContext = Readonly<{
17
- contract: TAnyProcedureContract
18
- streamTimeout?: number
19
- }>
20
-
21
- export type ApiMetaContext = Readonly<{
22
- callId: string
23
- connection: GatewayConnection
24
- container: Container
25
- path: readonly ApiMetaRouteContext[]
26
- procedure: ApiMetaProcedureContext
27
- }>
28
-
29
- export type StaticOrBeforeDecodeMetaBinding =
30
- | StaticMetaBinding
31
- | BeforeDecodeMetaBinding<AnyMeta, any, ApiMetaContext>
32
-
33
- export type CompatibleMetaBinding<Input> =
34
- | StaticOrBeforeDecodeMetaBinding
35
- | AfterDecodeMetaBinding<AnyMeta, any, ApiMetaContext, Input>
36
-
37
- export type AnyCompatibleMetaBinding = CompatibleMetaBinding<any>
@@ -1,37 +0,0 @@
1
- import type { MaybePromise } from '@nmtjs/common'
2
- import type { Dependant, Dependencies, DependencyContext } from '@nmtjs/core'
3
-
4
- import type { ApiCallContext } from './types.ts'
5
- import { kMiddleware } from './constants.ts'
6
-
7
- export type MiddlewareNext = (payload?: any) => any
8
-
9
- export interface Middleware<Deps extends Dependencies = Dependencies>
10
- extends Dependant<Deps> {
11
- [kMiddleware]: true
12
- handle: (
13
- ctx: DependencyContext<Deps>,
14
- call: ApiCallContext,
15
- next: MiddlewareNext,
16
- payload: any,
17
- ) => MaybePromise<any>
18
- }
19
-
20
- export type AnyMiddleware = Middleware<any>
21
-
22
- export function createMiddleware<Deps extends Dependencies = {}>(
23
- paramsOrHandler:
24
- | { dependencies?: Deps; handle: Middleware<Deps>['handle'] }
25
- | Middleware<Deps>['handle'],
26
- ): Middleware<Deps> {
27
- const { dependencies = {} as Deps, handle } =
28
- typeof paramsOrHandler === 'function'
29
- ? { handle: paramsOrHandler }
30
- : paramsOrHandler
31
-
32
- return Object.freeze({
33
- dependencies,
34
- handle,
35
- [kMiddleware]: true,
36
- }) as Middleware<Deps>
37
- }
@@ -1,224 +0,0 @@
1
- import type { MaybePromise } from '@nmtjs/common'
2
- import type { TAnyProcedureContract, TProcedureContract } from '@nmtjs/contract'
3
- import type { Dependant, Dependencies, DependencyContext } from '@nmtjs/core'
4
- import type { BaseType } from '@nmtjs/type'
5
- import type * as zod from 'zod/mini'
6
- import { c } from '@nmtjs/contract'
7
- import { assertUniqueMetaBindings } from '@nmtjs/core'
8
- import { t } from '@nmtjs/type'
9
-
10
- import type { AnyGuard } from './guards.ts'
11
- import type { AnyCompatibleMetaBinding, CompatibleMetaBinding } from './meta.ts'
12
- import type { AnyMiddleware } from './middlewares.ts'
13
- import type { JsonPrimitive } from './types.ts'
14
- import { kProcedure } from './constants.ts'
15
-
16
- export type {
17
- AnyCompatibleMetaBinding,
18
- CompatibleMetaBinding,
19
- StaticOrBeforeDecodeMetaBinding,
20
- } from './meta.ts'
21
- export type ProcedureMetaBinding<Input> = CompatibleMetaBinding<Input>
22
- export type AnyProcedureMetaBinding = AnyCompatibleMetaBinding
23
-
24
- export type ProcedureDecodedInput<Input extends BaseType | undefined> =
25
- Input extends BaseType ? t.infer.decode.output<Input> : never
26
-
27
- export type ProcedureContractDecodedInput<
28
- ProcedureContract extends TAnyProcedureContract,
29
- > = t.infer.decode.output<ProcedureContract['input']>
30
-
31
- export interface BaseProcedure<
32
- ProcedureContract extends TAnyProcedureContract,
33
- ProcedureDeps extends Dependencies,
34
- > extends Dependant<ProcedureDeps> {
35
- contract: ProcedureContract
36
- handler: (...args: any[]) => any
37
- meta: readonly AnyProcedureMetaBinding[]
38
- dependencies: ProcedureDeps
39
- guards: Set<AnyGuard>
40
- middlewares: Set<AnyMiddleware>
41
- streamTimeout?: number
42
- [kProcedure]: any
43
- }
44
-
45
- export type ProcedureHandlerType<Input, Output, Deps extends Dependencies> = (
46
- ctx: DependencyContext<Deps>,
47
- data: Input,
48
- ) => MaybePromise<Output>
49
-
50
- export interface Procedure<
51
- ProcedureContract extends TAnyProcedureContract,
52
- ProcedureDeps extends Dependencies,
53
- > extends BaseProcedure<ProcedureContract, ProcedureDeps> {
54
- handler: ProcedureHandlerType<
55
- ProcedureContractDecodedInput<ProcedureContract>,
56
- ProcedureContract['stream'] extends true
57
- ? AsyncIterable<t.infer.encode.input<ProcedureContract['output']>>
58
- : t.infer.encode.input<ProcedureContract['output']>,
59
- ProcedureDeps
60
- >
61
- }
62
-
63
- export type AnyProcedure<
64
- Contract extends TAnyProcedureContract = TAnyProcedureContract,
65
- > = BaseProcedure<Contract, Dependencies>
66
-
67
- export type CreateProcedureParams<
68
- ProcedureContract extends TAnyProcedureContract,
69
- ProcedureDeps extends Dependencies,
70
- > =
71
- | {
72
- dependencies?: ProcedureDeps
73
- guards?: AnyGuard[]
74
- middlewares?: AnyMiddleware[]
75
- meta?: ProcedureMetaBinding<
76
- ProcedureContractDecodedInput<ProcedureContract>
77
- >[]
78
- streamTimeout?: number
79
- handler: ProcedureHandlerType<
80
- ProcedureContractDecodedInput<ProcedureContract>,
81
- ProcedureContract['stream'] extends undefined
82
- ? t.infer.encode.input<ProcedureContract['output']>
83
- : AsyncIterable<
84
- Exclude<ProcedureContract['stream'], undefined | boolean>
85
- >,
86
- ProcedureDeps
87
- >
88
- }
89
- | ProcedureHandlerType<
90
- ProcedureContractDecodedInput<ProcedureContract>,
91
- ProcedureContract['stream'] extends undefined
92
- ? t.infer.decode.input<ProcedureContract['output']>
93
- : AsyncIterable<
94
- Exclude<ProcedureContract['stream'], undefined | boolean>
95
- >,
96
- ProcedureDeps
97
- >
98
-
99
- export function _createBaseProcedure<
100
- ProcedureContract extends TAnyProcedureContract,
101
- ProcedureDeps extends Dependencies,
102
- >(
103
- contract: ProcedureContract,
104
- params: {
105
- dependencies?: ProcedureDeps
106
- middlewares?: AnyMiddleware[]
107
- guards?: AnyGuard[]
108
- meta?: AnyProcedureMetaBinding[]
109
- streamTimeout?: number
110
- },
111
- ) {
112
- const dependencies = params.dependencies ?? ({} as ProcedureDeps)
113
- const middlewares = new Set(params.middlewares ?? [])
114
- const guards = new Set(params.guards ?? [])
115
- const meta = Object.freeze([...(params.meta ?? [])])
116
- const streamTimeout = params.streamTimeout
117
-
118
- if (typeof streamTimeout !== 'undefined' && streamTimeout <= 0) {
119
- throw new Error('Stream timeout must be a positive integer')
120
- }
121
-
122
- assertUniqueMetaBindings(meta, 'procedure config')
123
-
124
- return { contract, dependencies, middlewares, guards, meta, streamTimeout }
125
- }
126
-
127
- export function createContractProcedure<
128
- ProcedureContract extends TAnyProcedureContract,
129
- ProcedureDeps extends Dependencies,
130
- >(
131
- contract: ProcedureContract,
132
- paramsOrHandler: CreateProcedureParams<ProcedureContract, ProcedureDeps>,
133
- ): Procedure<ProcedureContract, ProcedureDeps> {
134
- const { handler, ...params } =
135
- typeof paramsOrHandler === 'function'
136
- ? { handler: paramsOrHandler }
137
- : paramsOrHandler
138
-
139
- return Object.assign(_createBaseProcedure(contract, params), {
140
- handler,
141
- [kProcedure]: true,
142
- }) as any
143
- }
144
-
145
- export function createProcedure<
146
- Return,
147
- TInput extends BaseType | undefined = undefined,
148
- TOutput extends BaseType | undefined = undefined,
149
- TStream extends true | number | undefined = undefined,
150
- Deps extends Dependencies = {},
151
- >(
152
- paramsOrHandler:
153
- | {
154
- input?: TInput
155
- output?: TOutput
156
- /**
157
- * Whether the procedure is a stream procedure.
158
- * If set to `true`, the procedure handler should return an `AsyncIterable` of output items.
159
- * If set to a number, it specifies an explicit stream timeout in milliseconds.
160
- */
161
- stream?: TStream
162
- dependencies?: Deps
163
- guards?: AnyGuard[]
164
- middlewares?: AnyMiddleware[]
165
- meta?: ProcedureMetaBinding<ProcedureDecodedInput<TInput>>[]
166
- timeout?: number
167
- handler: ProcedureHandlerType<
168
- ProcedureDecodedInput<TInput>,
169
- TStream extends true | number
170
- ? AsyncIterable<
171
- TOutput extends BaseType
172
- ? t.infer.encode.input<TOutput>
173
- : Return
174
- >
175
- : TOutput extends BaseType
176
- ? t.infer.encode.input<TOutput>
177
- : Return,
178
- Deps
179
- >
180
- }
181
- | ProcedureHandlerType<ProcedureDecodedInput<TInput>, Return, Deps>,
182
- ): Procedure<
183
- TProcedureContract<
184
- TInput extends undefined ? t.NeverType : TInput,
185
- TOutput extends undefined
186
- ? t.CustomType<
187
- JsonPrimitive<Return>,
188
- zod.ZodMiniCustom<JsonPrimitive<Return>, JsonPrimitive<Return>>
189
- >
190
- : TOutput,
191
- TStream extends true | number ? true : undefined
192
- >,
193
- Deps
194
- > {
195
- const {
196
- input = t.never() as any,
197
- output = t.any() as any,
198
- stream = undefined as any,
199
- dependencies = {} as Deps,
200
- guards = [],
201
- middlewares = [],
202
- meta = [],
203
- handler,
204
- timeout,
205
- } = typeof paramsOrHandler === 'function'
206
- ? { handler: paramsOrHandler }
207
- : paramsOrHandler
208
-
209
- // @ts-expect-error
210
- return createContractProcedure(
211
- c.procedure({ input, output, stream, timeout }),
212
- {
213
- dependencies,
214
- handler: handler as any,
215
- guards,
216
- middlewares,
217
- meta,
218
- streamTimeout: typeof stream === 'number' ? stream : undefined,
219
- },
220
- )
221
- }
222
-
223
- export const isProcedure = (value: any): value is AnyProcedure =>
224
- Boolean(value?.[kProcedure])