@atproto/lex-schema 0.0.9 → 0.0.10

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 (279) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/LICENSE.txt +1 -1
  3. package/dist/core/$type.d.ts +11 -0
  4. package/dist/core/$type.d.ts.map +1 -1
  5. package/dist/core/$type.js +4 -0
  6. package/dist/core/$type.js.map +1 -1
  7. package/dist/core/schema.d.ts +31 -24
  8. package/dist/core/schema.d.ts.map +1 -1
  9. package/dist/core/schema.js +38 -8
  10. package/dist/core/schema.js.map +1 -1
  11. package/dist/core/string-format.d.ts +35 -35
  12. package/dist/core/string-format.d.ts.map +1 -1
  13. package/dist/core/string-format.js +49 -91
  14. package/dist/core/string-format.js.map +1 -1
  15. package/dist/core/validation-issue.js +1 -1
  16. package/dist/core/validation-issue.js.map +1 -1
  17. package/dist/core/validator.d.ts +53 -32
  18. package/dist/core/validator.d.ts.map +1 -1
  19. package/dist/core/validator.js +18 -22
  20. package/dist/core/validator.js.map +1 -1
  21. package/dist/external.d.ts +0 -85
  22. package/dist/external.d.ts.map +1 -1
  23. package/dist/external.js +0 -164
  24. package/dist/external.js.map +1 -1
  25. package/dist/helpers.d.ts +10 -5
  26. package/dist/helpers.d.ts.map +1 -1
  27. package/dist/helpers.js +3 -3
  28. package/dist/helpers.js.map +1 -1
  29. package/dist/schema/array.d.ts +9 -5
  30. package/dist/schema/array.d.ts.map +1 -1
  31. package/dist/schema/array.js +14 -5
  32. package/dist/schema/array.js.map +1 -1
  33. package/dist/schema/blob.d.ts +9 -7
  34. package/dist/schema/blob.d.ts.map +1 -1
  35. package/dist/schema/blob.js +9 -5
  36. package/dist/schema/blob.js.map +1 -1
  37. package/dist/schema/boolean.d.ts +3 -7
  38. package/dist/schema/boolean.d.ts.map +1 -1
  39. package/dist/schema/boolean.js +6 -7
  40. package/dist/schema/boolean.js.map +1 -1
  41. package/dist/schema/bytes.d.ts +3 -2
  42. package/dist/schema/bytes.d.ts.map +1 -1
  43. package/dist/schema/bytes.js +7 -3
  44. package/dist/schema/bytes.js.map +1 -1
  45. package/dist/schema/cid.d.ts +7 -7
  46. package/dist/schema/cid.d.ts.map +1 -1
  47. package/dist/schema/cid.js +5 -1
  48. package/dist/schema/cid.js.map +1 -1
  49. package/dist/schema/custom.d.ts +6 -5
  50. package/dist/schema/custom.d.ts.map +1 -1
  51. package/dist/schema/custom.js +10 -4
  52. package/dist/schema/custom.js.map +1 -1
  53. package/dist/schema/dict.d.ts +8 -8
  54. package/dist/schema/dict.d.ts.map +1 -1
  55. package/dist/schema/dict.js +11 -2
  56. package/dist/schema/dict.js.map +1 -1
  57. package/dist/schema/discriminated-union.d.ts +21 -14
  58. package/dist/schema/discriminated-union.d.ts.map +1 -1
  59. package/dist/schema/discriminated-union.js +7 -0
  60. package/dist/schema/discriminated-union.js.map +1 -1
  61. package/dist/schema/enum.d.ts +7 -9
  62. package/dist/schema/enum.d.ts.map +1 -1
  63. package/dist/schema/enum.js +8 -4
  64. package/dist/schema/enum.js.map +1 -1
  65. package/dist/schema/integer.d.ts +5 -5
  66. package/dist/schema/integer.d.ts.map +1 -1
  67. package/dist/schema/integer.js +9 -5
  68. package/dist/schema/integer.js.map +1 -1
  69. package/dist/schema/intersection.d.ts +4 -4
  70. package/dist/schema/intersection.d.ts.map +1 -1
  71. package/dist/schema/intersection.js +5 -0
  72. package/dist/schema/intersection.js.map +1 -1
  73. package/dist/schema/literal.d.ts +6 -9
  74. package/dist/schema/literal.d.ts.map +1 -1
  75. package/dist/schema/literal.js +7 -4
  76. package/dist/schema/literal.js.map +1 -1
  77. package/dist/schema/never.d.ts +3 -2
  78. package/dist/schema/never.d.ts.map +1 -1
  79. package/dist/schema/never.js +5 -1
  80. package/dist/schema/never.js.map +1 -1
  81. package/dist/schema/null.d.ts +4 -3
  82. package/dist/schema/null.d.ts.map +1 -1
  83. package/dist/schema/null.js +6 -4
  84. package/dist/schema/null.js.map +1 -1
  85. package/dist/schema/nullable.d.ts +6 -5
  86. package/dist/schema/nullable.d.ts.map +1 -1
  87. package/dist/schema/nullable.js +9 -5
  88. package/dist/schema/nullable.js.map +1 -1
  89. package/dist/schema/object.d.ts +10 -8
  90. package/dist/schema/object.d.ts.map +1 -1
  91. package/dist/schema/object.js +11 -3
  92. package/dist/schema/object.js.map +1 -1
  93. package/dist/schema/optional.d.ts +7 -5
  94. package/dist/schema/optional.d.ts.map +1 -1
  95. package/dist/schema/optional.js +14 -6
  96. package/dist/schema/optional.js.map +1 -1
  97. package/dist/schema/params.d.ts +24 -13
  98. package/dist/schema/params.d.ts.map +1 -1
  99. package/dist/schema/params.js +47 -25
  100. package/dist/schema/params.js.map +1 -1
  101. package/dist/schema/payload.d.ts +12 -9
  102. package/dist/schema/payload.d.ts.map +1 -1
  103. package/dist/schema/payload.js +11 -0
  104. package/dist/schema/payload.js.map +1 -1
  105. package/dist/schema/permission-set.d.ts +1 -0
  106. package/dist/schema/permission-set.d.ts.map +1 -1
  107. package/dist/schema/permission-set.js +5 -0
  108. package/dist/schema/permission-set.js.map +1 -1
  109. package/dist/schema/permission.d.ts +6 -5
  110. package/dist/schema/permission.d.ts.map +1 -1
  111. package/dist/schema/permission.js +5 -0
  112. package/dist/schema/permission.js.map +1 -1
  113. package/dist/schema/procedure.d.ts +2 -1
  114. package/dist/schema/procedure.d.ts.map +1 -1
  115. package/dist/schema/procedure.js +5 -0
  116. package/dist/schema/procedure.js.map +1 -1
  117. package/dist/schema/query.d.ts +2 -1
  118. package/dist/schema/query.d.ts.map +1 -1
  119. package/dist/schema/query.js +5 -0
  120. package/dist/schema/query.js.map +1 -1
  121. package/dist/schema/record.d.ts +48 -30
  122. package/dist/schema/record.d.ts.map +1 -1
  123. package/dist/schema/record.js +12 -9
  124. package/dist/schema/record.js.map +1 -1
  125. package/dist/schema/ref.d.ts +9 -6
  126. package/dist/schema/ref.d.ts.map +1 -1
  127. package/dist/schema/ref.js +9 -16
  128. package/dist/schema/ref.js.map +1 -1
  129. package/dist/schema/refine.d.ts +4 -4
  130. package/dist/schema/refine.d.ts.map +1 -1
  131. package/dist/schema/refine.js.map +1 -1
  132. package/dist/schema/regexp.d.ts +4 -3
  133. package/dist/schema/regexp.d.ts.map +1 -1
  134. package/dist/schema/regexp.js +5 -0
  135. package/dist/schema/regexp.js.map +1 -1
  136. package/dist/schema/string.d.ts +7 -8
  137. package/dist/schema/string.d.ts.map +1 -1
  138. package/dist/schema/string.js +13 -19
  139. package/dist/schema/string.js.map +1 -1
  140. package/dist/schema/subscription.d.ts +2 -1
  141. package/dist/schema/subscription.d.ts.map +1 -1
  142. package/dist/schema/subscription.js +5 -0
  143. package/dist/schema/subscription.js.map +1 -1
  144. package/dist/schema/token.d.ts +6 -5
  145. package/dist/schema/token.d.ts.map +1 -1
  146. package/dist/schema/token.js +5 -0
  147. package/dist/schema/token.js.map +1 -1
  148. package/dist/schema/typed-object.d.ts +43 -26
  149. package/dist/schema/typed-object.d.ts.map +1 -1
  150. package/dist/schema/typed-object.js +6 -3
  151. package/dist/schema/typed-object.js.map +1 -1
  152. package/dist/schema/typed-ref.d.ts +16 -25
  153. package/dist/schema/typed-ref.d.ts.map +1 -1
  154. package/dist/schema/typed-ref.js +7 -17
  155. package/dist/schema/typed-ref.js.map +1 -1
  156. package/dist/schema/typed-union.d.ts +9 -21
  157. package/dist/schema/typed-union.d.ts.map +1 -1
  158. package/dist/schema/typed-union.js +15 -11
  159. package/dist/schema/typed-union.js.map +1 -1
  160. package/dist/schema/union.d.ts +6 -6
  161. package/dist/schema/union.d.ts.map +1 -1
  162. package/dist/schema/union.js +7 -5
  163. package/dist/schema/union.js.map +1 -1
  164. package/dist/schema/unknown-object.d.ts +5 -4
  165. package/dist/schema/unknown-object.d.ts.map +1 -1
  166. package/dist/schema/unknown-object.js +5 -1
  167. package/dist/schema/unknown-object.js.map +1 -1
  168. package/dist/schema/unknown.d.ts +3 -2
  169. package/dist/schema/unknown.d.ts.map +1 -1
  170. package/dist/schema/unknown.js +5 -1
  171. package/dist/schema/unknown.js.map +1 -1
  172. package/dist/schema/with-default.d.ts +9 -0
  173. package/dist/schema/with-default.d.ts.map +1 -0
  174. package/dist/schema/with-default.js +27 -0
  175. package/dist/schema/with-default.js.map +1 -0
  176. package/dist/schema.d.ts +2 -2
  177. package/dist/schema.d.ts.map +1 -1
  178. package/dist/schema.js +2 -4
  179. package/dist/schema.js.map +1 -1
  180. package/dist/util/assertion-util.d.ts +0 -6
  181. package/dist/util/assertion-util.d.ts.map +1 -1
  182. package/dist/util/assertion-util.js +0 -28
  183. package/dist/util/assertion-util.js.map +1 -1
  184. package/dist/util/memoize.d.ts +2 -2
  185. package/dist/util/memoize.d.ts.map +1 -1
  186. package/dist/util/memoize.js +23 -39
  187. package/dist/util/memoize.js.map +1 -1
  188. package/package.json +3 -3
  189. package/src/core/$type.test.ts +20 -0
  190. package/src/core/$type.ts +30 -0
  191. package/src/core/schema.ts +86 -38
  192. package/src/core/string-format.ts +119 -158
  193. package/src/core/validation-issue.ts +1 -1
  194. package/src/core/validator.ts +93 -53
  195. package/src/external.ts +0 -404
  196. package/src/helpers.test.ts +22 -21
  197. package/src/helpers.ts +14 -14
  198. package/src/schema/array.test.ts +38 -40
  199. package/src/schema/array.ts +35 -13
  200. package/src/schema/blob.test.ts +21 -21
  201. package/src/schema/blob.ts +19 -17
  202. package/src/schema/boolean.test.ts +9 -8
  203. package/src/schema/boolean.ts +7 -13
  204. package/src/schema/bytes.test.ts +13 -13
  205. package/src/schema/bytes.ts +13 -8
  206. package/src/schema/cid.test.ts +3 -3
  207. package/src/schema/cid.ts +13 -12
  208. package/src/schema/custom.test.ts +26 -26
  209. package/src/schema/custom.ts +20 -13
  210. package/src/schema/dict.test.ts +21 -39
  211. package/src/schema/dict.ts +28 -19
  212. package/src/schema/discriminated-union.test.ts +128 -128
  213. package/src/schema/discriminated-union.ts +45 -26
  214. package/src/schema/enum.test.ts +17 -16
  215. package/src/schema/enum.ts +16 -16
  216. package/src/schema/integer.test.ts +22 -21
  217. package/src/schema/integer.ts +12 -9
  218. package/src/schema/intersection.test.ts +10 -10
  219. package/src/schema/intersection.ts +17 -14
  220. package/src/schema/literal.test.ts +35 -34
  221. package/src/schema/literal.ts +12 -15
  222. package/src/schema/never.test.ts +5 -5
  223. package/src/schema/never.ts +7 -2
  224. package/src/schema/null.test.ts +3 -3
  225. package/src/schema/null.ts +9 -9
  226. package/src/schema/nullable.test.ts +31 -42
  227. package/src/schema/nullable.ts +17 -9
  228. package/src/schema/object.test.ts +10 -12
  229. package/src/schema/object.ts +27 -18
  230. package/src/schema/optional.test.ts +21 -28
  231. package/src/schema/optional.ts +27 -10
  232. package/src/schema/params.test.ts +471 -47
  233. package/src/schema/params.ts +72 -38
  234. package/src/schema/payload.test.ts +150 -156
  235. package/src/schema/payload.ts +35 -19
  236. package/src/schema/permission-set.test.ts +206 -273
  237. package/src/schema/permission-set.ts +8 -0
  238. package/src/schema/permission.test.ts +177 -177
  239. package/src/schema/permission.ts +13 -5
  240. package/src/schema/procedure.test.ts +183 -242
  241. package/src/schema/procedure.ts +18 -5
  242. package/src/schema/query.test.ts +186 -200
  243. package/src/schema/query.ts +16 -4
  244. package/src/schema/record.test.ts +121 -101
  245. package/src/schema/record.ts +74 -40
  246. package/src/schema/ref.test.ts +101 -118
  247. package/src/schema/ref.ts +33 -28
  248. package/src/schema/refine.test.ts +28 -28
  249. package/src/schema/refine.ts +23 -20
  250. package/src/schema/regexp.test.ts +29 -33
  251. package/src/schema/regexp.ts +11 -7
  252. package/src/schema/string.test.ts +35 -35
  253. package/src/schema/string.ts +24 -33
  254. package/src/schema/subscription.test.ts +259 -387
  255. package/src/schema/subscription.ts +16 -4
  256. package/src/schema/token.test.ts +47 -324
  257. package/src/schema/token.ts +14 -7
  258. package/src/schema/typed-object.test.ts +98 -81
  259. package/src/schema/typed-object.ts +68 -33
  260. package/src/schema/typed-ref.test.ts +206 -234
  261. package/src/schema/typed-ref.ts +40 -42
  262. package/src/schema/typed-union.test.ts +40 -64
  263. package/src/schema/typed-union.ts +36 -58
  264. package/src/schema/union.test.ts +17 -27
  265. package/src/schema/union.ts +20 -16
  266. package/src/schema/unknown-object.test.ts +8 -8
  267. package/src/schema/unknown-object.ts +9 -7
  268. package/src/schema/unknown.test.ts +4 -4
  269. package/src/schema/unknown.ts +7 -5
  270. package/src/schema/with-default.ts +35 -0
  271. package/src/schema.ts +2 -6
  272. package/src/util/assertion-util.ts +0 -39
  273. package/src/util/memoize.ts +26 -46
  274. package/dist/schema/_parameters.d.ts +0 -17
  275. package/dist/schema/_parameters.d.ts.map +0 -1
  276. package/dist/schema/_parameters.js +0 -20
  277. package/dist/schema/_parameters.js.map +0 -1
  278. package/src/schema/_parameters.test.ts +0 -417
  279. package/src/schema/_parameters.ts +0 -26
@@ -1,13 +1,21 @@
1
- import { Params } from './_parameters.js'
1
+ import { Params } from './params.js'
2
2
 
3
3
  export type PermissionOptions = Params
4
4
 
5
5
  export class Permission<
6
- const Resource extends string = any,
7
- const Options extends PermissionOptions = any,
6
+ const TResource extends string = any,
7
+ const TOptions extends PermissionOptions = any,
8
8
  > {
9
9
  constructor(
10
- readonly resource: Resource,
11
- readonly options: Options,
10
+ readonly resource: TResource,
11
+ readonly options: TOptions,
12
12
  ) {}
13
13
  }
14
+
15
+ /*@__NO_SIDE_EFFECTS__*/
16
+ export function permission<
17
+ const R extends string,
18
+ const O extends PermissionOptions,
19
+ >(resource: R, options: PermissionOptions & O = {} as O) {
20
+ return new Permission<R, O>(resource, options)
21
+ }
@@ -1,315 +1,259 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { ObjectSchema } from './object.js'
3
- import { ParamsSchema } from './params.js'
4
- import { Payload } from './payload.js'
5
- import { Procedure } from './procedure.js'
6
- import { StringSchema } from './string.js'
2
+ import { object } from './object.js'
3
+ import { params } from './params.js'
4
+ import { payload } from './payload.js'
5
+ import { procedure } from './procedure.js'
6
+ import { string } from './string.js'
7
7
 
8
8
  describe('Procedure', () => {
9
9
  describe('basic construction', () => {
10
10
  it('creates a procedure with all parameters', () => {
11
- const nsid = 'com.example.createPost'
12
- const parameters = new ParamsSchema({})
13
- const input = new Payload('application/json', undefined)
14
- const output = new Payload('application/json', undefined)
15
11
  const errors = ['InvalidRequest', 'Unauthorized'] as const
16
12
 
17
- const procedure = new Procedure(nsid, parameters, input, output, errors)
13
+ const createPost = procedure(
14
+ 'com.example.createPost',
15
+ params(),
16
+ payload('application/json', undefined),
17
+ payload('application/json', undefined),
18
+ errors,
19
+ )
18
20
 
19
- expect(procedure.nsid).toBe(nsid)
20
- expect(procedure.parameters).toBe(parameters)
21
- expect(procedure.input).toBe(input)
22
- expect(procedure.output).toBe(output)
23
- expect(procedure.errors).toBe(errors)
21
+ expect(createPost.nsid).toBe('com.example.createPost')
22
+ expect(createPost.parameters).toEqual(params())
23
+ expect(createPost.input).toEqual(payload('application/json', undefined))
24
+ expect(createPost.output).toEqual(payload('application/json', undefined))
25
+ expect(createPost.errors).toBe(errors)
24
26
  })
25
27
 
26
28
  it('creates a procedure without errors', () => {
27
- const nsid = 'com.example.doSomething'
28
- const parameters = new ParamsSchema({})
29
- const input = new Payload('application/json', undefined)
30
- const output = new Payload('application/json', undefined)
31
-
32
- const procedure = new Procedure(
33
- nsid,
34
- parameters,
35
- input,
36
- output,
29
+ const doSomething = procedure(
30
+ 'com.example.doSomething',
31
+ params(),
32
+ payload('application/json', undefined),
33
+ payload('application/json', undefined),
37
34
  undefined,
38
35
  )
39
36
 
40
- expect(procedure.nsid).toBe(nsid)
41
- expect(procedure.parameters).toBe(parameters)
42
- expect(procedure.input).toBe(input)
43
- expect(procedure.output).toBe(output)
44
- expect(procedure.errors).toBeUndefined()
37
+ expect(doSomething.nsid).toBe('com.example.doSomething')
38
+ expect(doSomething.parameters).toEqual(params())
39
+ expect(doSomething.input).toEqual(payload('application/json', undefined))
40
+ expect(doSomething.output).toEqual(payload('application/json', undefined))
41
+ expect(doSomething.errors).toBeUndefined()
45
42
  })
46
43
 
47
44
  it('creates a procedure with empty errors array', () => {
48
- const nsid = 'com.example.action'
49
- const parameters = new ParamsSchema({})
50
- const input = new Payload('application/json', undefined)
51
- const output = new Payload('application/json', undefined)
52
- const errors = [] as const
53
-
54
- const procedure = new Procedure(nsid, parameters, input, output, errors)
45
+ const performAction = procedure(
46
+ 'com.example.performAction',
47
+ params(),
48
+ payload('application/json', undefined),
49
+ payload('application/json', undefined),
50
+ [] as const,
51
+ )
55
52
 
56
- expect(procedure.errors).toEqual([])
53
+ expect(performAction.errors).toEqual([])
57
54
  })
58
55
  })
59
56
 
60
57
  describe('with parameters schema', () => {
61
58
  it('creates a procedure with query parameters', () => {
62
- const nsid = 'com.example.listPosts'
63
- const parameters = new ParamsSchema({
64
- limit: new StringSchema({}),
65
- cursor: new StringSchema({}),
66
- })
67
- const input = new Payload(undefined, undefined)
68
- const output = new Payload('application/json', undefined)
69
-
70
- const procedure = new Procedure(
71
- nsid,
72
- parameters,
73
- input,
74
- output,
59
+ const listPosts = procedure(
60
+ 'com.example.listPosts',
61
+ params({
62
+ limit: string(),
63
+ cursor: string(),
64
+ }),
65
+ payload(undefined, undefined),
66
+ payload('application/json', undefined),
75
67
  undefined,
76
68
  )
77
69
 
78
- expect(procedure.parameters).toBe(parameters)
79
- expect(procedure.parameters.validators).toHaveProperty('limit')
80
- expect(procedure.parameters.validators).toHaveProperty('cursor')
70
+ expect(listPosts.parameters.shape).toHaveProperty('limit')
71
+ expect(listPosts.parameters.shape).toHaveProperty('cursor')
81
72
  })
82
73
 
83
74
  it('creates a procedure with empty parameters', () => {
84
- const nsid = 'com.example.action'
85
- const parameters = new ParamsSchema({})
86
- const input = new Payload('application/json', undefined)
87
- const output = new Payload('application/json', undefined)
88
-
89
- const procedure = new Procedure(
90
- nsid,
91
- parameters,
92
- input,
93
- output,
75
+ const myProcedure = procedure(
76
+ 'com.example.action',
77
+ params(),
78
+ payload('application/json', undefined),
79
+ payload('application/json', undefined),
94
80
  undefined,
95
81
  )
96
82
 
97
- expect(procedure.parameters).toBe(parameters)
98
- expect(Object.keys(procedure.parameters.validators)).toHaveLength(0)
83
+ expect(Object.keys(myProcedure.parameters.shape)).toHaveLength(0)
99
84
  })
100
85
  })
101
86
 
102
87
  describe('with input payload', () => {
103
88
  it('creates a procedure with JSON input', () => {
104
- const nsid = 'com.example.createPost'
105
- const parameters = new ParamsSchema({})
106
- const inputSchema = new ObjectSchema({
107
- text: new StringSchema({}),
89
+ const inputSchema = object({
90
+ text: string(),
108
91
  })
109
- const input = new Payload('application/json', inputSchema)
110
- const output = new Payload('application/json', undefined)
111
92
 
112
- const procedure = new Procedure(
113
- nsid,
114
- parameters,
115
- input,
116
- output,
93
+ const myProcedure = procedure(
94
+ 'com.example.createPost',
95
+ params(),
96
+ payload('application/json', inputSchema),
97
+ payload('application/json', undefined),
117
98
  undefined,
118
99
  )
119
100
 
120
- expect(procedure.input.encoding).toBe('application/json')
121
- expect(procedure.input.schema).toBe(inputSchema)
101
+ expect(myProcedure.input.encoding).toBe('application/json')
102
+ expect(myProcedure.input.schema).toBe(inputSchema)
122
103
  })
123
104
 
124
105
  it('creates a procedure with text input', () => {
125
- const nsid = 'com.example.uploadText'
126
- const parameters = new ParamsSchema({})
127
- const input = new Payload('text/plain', undefined)
128
- const output = new Payload('application/json', undefined)
129
-
130
- const procedure = new Procedure(
131
- nsid,
132
- parameters,
133
- input,
134
- output,
106
+ const myProcedure = procedure(
107
+ 'com.example.uploadText',
108
+ params(),
109
+ payload('text/plain', undefined),
110
+ payload('application/json', undefined),
135
111
  undefined,
136
112
  )
137
113
 
138
- expect(procedure.input.encoding).toBe('text/plain')
139
- expect(procedure.input.schema).toBeUndefined()
114
+ expect(myProcedure.input.encoding).toBe('text/plain')
115
+ expect(myProcedure.input.schema).toBeUndefined()
140
116
  })
141
117
 
142
118
  it('creates a procedure with binary input', () => {
143
- const nsid = 'com.example.uploadBlob'
144
- const parameters = new ParamsSchema({})
145
- const input = new Payload('application/octet-stream', undefined)
146
- const output = new Payload('application/json', undefined)
147
-
148
- const procedure = new Procedure(
149
- nsid,
150
- parameters,
151
- input,
152
- output,
119
+ const myProcedure = procedure(
120
+ 'com.example.uploadBlob',
121
+ params(),
122
+ payload('application/octet-stream', undefined),
123
+ payload('application/json', undefined),
153
124
  undefined,
154
125
  )
155
126
 
156
- expect(procedure.input.encoding).toBe('application/octet-stream')
127
+ expect(myProcedure.input.encoding).toBe('application/octet-stream')
157
128
  })
158
129
 
159
130
  it('creates a procedure with no input', () => {
160
- const nsid = 'com.example.action'
161
- const parameters = new ParamsSchema({})
162
- const input = new Payload(undefined, undefined)
163
- const output = new Payload('application/json', undefined)
164
-
165
- const procedure = new Procedure(
166
- nsid,
167
- parameters,
168
- input,
169
- output,
131
+ const myProcedure = procedure(
132
+ 'com.example.action',
133
+ params(),
134
+ payload(undefined, undefined),
135
+ payload('application/json', undefined),
170
136
  undefined,
171
137
  )
172
138
 
173
- expect(procedure.input.encoding).toBeUndefined()
174
- expect(procedure.input.schema).toBeUndefined()
139
+ expect(myProcedure.input.encoding).toBeUndefined()
140
+ expect(myProcedure.input.schema).toBeUndefined()
175
141
  })
176
142
  })
177
143
 
178
144
  describe('with output payload', () => {
179
145
  it('creates a procedure with JSON output', () => {
180
- const nsid = 'com.example.getPost'
181
- const parameters = new ParamsSchema({})
182
- const input = new Payload(undefined, undefined)
183
- const outputSchema = new ObjectSchema({
184
- uri: new StringSchema({}),
185
- cid: new StringSchema({}),
146
+ const outputSchema = object({
147
+ uri: string(),
148
+ cid: string(),
186
149
  })
187
- const output = new Payload('application/json', outputSchema)
188
150
 
189
- const procedure = new Procedure(
190
- nsid,
191
- parameters,
192
- input,
193
- output,
151
+ const myProcedure = procedure(
152
+ 'com.example.getPost',
153
+ params(),
154
+ payload(undefined, undefined),
155
+ payload('application/json', outputSchema),
194
156
  undefined,
195
157
  )
196
158
 
197
- expect(procedure.output.encoding).toBe('application/json')
198
- expect(procedure.output.schema).toBe(outputSchema)
159
+ expect(myProcedure.output.encoding).toBe('application/json')
160
+ expect(myProcedure.output.schema).toBe(outputSchema)
199
161
  })
200
162
 
201
163
  it('creates a procedure with text output', () => {
202
- const nsid = 'com.example.export'
203
- const parameters = new ParamsSchema({})
204
- const input = new Payload(undefined, undefined)
205
- const output = new Payload('text/plain', undefined)
206
-
207
- const procedure = new Procedure(
208
- nsid,
209
- parameters,
210
- input,
211
- output,
164
+ const myProcedure = procedure(
165
+ 'com.example.export',
166
+ params(),
167
+ payload(undefined, undefined),
168
+ payload('text/plain', undefined),
212
169
  undefined,
213
170
  )
214
171
 
215
- expect(procedure.output.encoding).toBe('text/plain')
172
+ expect(myProcedure.output.encoding).toBe('text/plain')
216
173
  })
217
174
 
218
175
  it('creates a procedure with binary output', () => {
219
- const nsid = 'com.example.download'
220
- const parameters = new ParamsSchema({})
221
- const input = new Payload(undefined, undefined)
222
- const output = new Payload('application/octet-stream', undefined)
223
-
224
- const procedure = new Procedure(
225
- nsid,
226
- parameters,
227
- input,
228
- output,
176
+ const myProcedure = procedure(
177
+ 'com.example.download',
178
+ params(),
179
+ payload(undefined, undefined),
180
+ payload('application/octet-stream', undefined),
229
181
  undefined,
230
182
  )
231
183
 
232
- expect(procedure.output.encoding).toBe('application/octet-stream')
184
+ expect(myProcedure.output.encoding).toBe('application/octet-stream')
233
185
  })
234
186
 
235
187
  it('creates a procedure with no output', () => {
236
- const nsid = 'com.example.deletePost'
237
- const parameters = new ParamsSchema({})
238
- const input = new Payload('application/json', undefined)
239
- const output = new Payload(undefined, undefined)
240
-
241
- const procedure = new Procedure(
242
- nsid,
243
- parameters,
244
- input,
245
- output,
188
+ const myProcedure = procedure(
189
+ 'com.example.deletePost',
190
+ params(),
191
+ payload('application/json', undefined),
192
+ payload(undefined, undefined),
246
193
  undefined,
247
194
  )
248
195
 
249
- expect(procedure.output.encoding).toBeUndefined()
250
- expect(procedure.output.schema).toBeUndefined()
196
+ expect(myProcedure.output.encoding).toBeUndefined()
197
+ expect(myProcedure.output.schema).toBeUndefined()
251
198
  })
252
199
  })
253
200
 
254
201
  describe('with error definitions', () => {
255
202
  it('creates a procedure with single error', () => {
256
- const nsid = 'com.example.action'
257
- const parameters = new ParamsSchema({})
258
- const input = new Payload('application/json', undefined)
259
- const output = new Payload('application/json', undefined)
260
- const errors = ['InvalidRequest'] as const
261
-
262
- const procedure = new Procedure(nsid, parameters, input, output, errors)
203
+ const myProcedure = procedure(
204
+ 'com.example.action',
205
+ params(),
206
+ payload('application/json', undefined),
207
+ payload('application/json', undefined),
208
+ ['InvalidRequest'] as const,
209
+ )
263
210
 
264
- expect(procedure.errors).toEqual(['InvalidRequest'])
211
+ expect(myProcedure.errors).toEqual(['InvalidRequest'])
265
212
  })
266
213
 
267
214
  it('creates a procedure with multiple errors', () => {
268
- const nsid = 'com.example.createPost'
269
- const parameters = new ParamsSchema({})
270
- const input = new Payload('application/json', undefined)
271
- const output = new Payload('application/json', undefined)
272
- const errors = [
273
- 'InvalidRequest',
274
- 'Unauthorized',
275
- 'RateLimitExceeded',
276
- ] as const
277
-
278
- const procedure = new Procedure(nsid, parameters, input, output, errors)
215
+ const myProcedure = procedure(
216
+ 'com.example.createPost',
217
+ params(),
218
+ payload('application/json', undefined),
219
+ payload('application/json', undefined),
220
+ ['InvalidRequest', 'Unauthorized', 'RateLimitExceeded'] as const,
221
+ )
279
222
 
280
- expect(procedure.errors).toHaveLength(3)
281
- expect(procedure.errors).toContain('InvalidRequest')
282
- expect(procedure.errors).toContain('Unauthorized')
283
- expect(procedure.errors).toContain('RateLimitExceeded')
223
+ expect(myProcedure.errors).toHaveLength(3)
224
+ expect(myProcedure.errors).toContain('InvalidRequest')
225
+ expect(myProcedure.errors).toContain('Unauthorized')
226
+ expect(myProcedure.errors).toContain('RateLimitExceeded')
284
227
  })
285
228
  })
286
229
 
287
230
  describe('property access', () => {
288
231
  it('provides access to all properties', () => {
289
- const nsid = 'com.example.test'
290
- const parameters = new ParamsSchema({})
291
- const input = new Payload('application/json', undefined)
292
- const output = new Payload('application/json', undefined)
293
232
  const errors = ['Error1', 'Error2'] as const
294
233
 
295
- const procedure = new Procedure(nsid, parameters, input, output, errors)
234
+ const myProcedure = procedure(
235
+ 'com.example.test',
236
+ params(),
237
+ payload('application/json', undefined),
238
+ payload('application/json', undefined),
239
+ errors,
240
+ )
296
241
 
297
- // Verify all properties are accessible
298
- expect(procedure.nsid).toBe(nsid)
299
- expect(procedure.parameters).toBe(parameters)
300
- expect(procedure.input).toBe(input)
301
- expect(procedure.output).toBe(output)
302
- expect(procedure.errors).toBe(errors)
242
+ expect(myProcedure.nsid).toBe('com.example.test')
243
+ expect(myProcedure.parameters).toEqual(params())
244
+ expect(myProcedure.input).toEqual(payload('application/json', undefined))
245
+ expect(myProcedure.output).toEqual(payload('application/json', undefined))
246
+ expect(myProcedure.errors).toBe(errors)
303
247
  })
304
248
 
305
249
  it('maintains reference equality for complex properties', () => {
306
- const parameters = new ParamsSchema({ test: new StringSchema({}) })
307
- const inputSchema = new ObjectSchema({ field: new StringSchema({}) })
308
- const outputSchema = new ObjectSchema({ result: new StringSchema({}) })
309
- const input = new Payload('application/json', inputSchema)
310
- const output = new Payload('application/json', outputSchema)
250
+ const parameters = params({ test: string() })
251
+ const inputSchema = object({ field: string() })
252
+ const outputSchema = object({ result: string() })
253
+ const input = payload('application/json', inputSchema)
254
+ const output = payload('application/json', outputSchema)
311
255
 
312
- const procedure = new Procedure(
256
+ const myProcedure = procedure(
313
257
  'com.example.test',
314
258
  parameters,
315
259
  input,
@@ -318,66 +262,63 @@ describe('Procedure', () => {
318
262
  )
319
263
 
320
264
  // Verify references are maintained
321
- expect(procedure.parameters).toBe(parameters)
322
- expect(procedure.input).toBe(input)
323
- expect(procedure.output).toBe(output)
324
- expect(procedure.input.schema).toBe(inputSchema)
325
- expect(procedure.output.schema).toBe(outputSchema)
265
+ expect(myProcedure.parameters).toBe(parameters)
266
+ expect(myProcedure.input).toBe(input)
267
+ expect(myProcedure.output).toBe(output)
268
+ expect(myProcedure.input.schema).toBe(inputSchema)
269
+ expect(myProcedure.output.schema).toBe(outputSchema)
326
270
  })
327
271
  })
328
272
 
329
273
  describe('complex scenarios', () => {
330
274
  it('creates a fully-featured procedure', () => {
331
- const nsid = 'com.example.chat.sendMessage'
332
- const parameters = new ParamsSchema({
333
- conversationId: new StringSchema({}),
334
- })
335
- const inputSchema = new ObjectSchema({
336
- text: new StringSchema({}),
337
- mentions: new StringSchema({}),
275
+ const inputSchema = object({
276
+ text: string(),
277
+ mentions: string(),
338
278
  })
339
- const outputSchema = new ObjectSchema({
340
- messageId: new StringSchema({}),
341
- timestamp: new StringSchema({}),
279
+ const outputSchema = object({
280
+ messageId: string(),
281
+ timestamp: string(),
342
282
  })
343
- const input = new Payload('application/json', inputSchema)
344
- const output = new Payload('application/json', outputSchema)
345
283
  const errors = [
346
284
  'ConversationNotFound',
347
285
  'MessageTooLong',
348
286
  'RateLimitExceeded',
349
287
  ] as const
350
288
 
351
- const procedure = new Procedure(nsid, parameters, input, output, errors)
289
+ const myProcedure = procedure(
290
+ 'com.example.chat.sendMessage',
291
+ params({
292
+ conversationId: string(),
293
+ }),
294
+ payload('application/json', inputSchema),
295
+ payload('application/json', outputSchema),
296
+ errors,
297
+ )
352
298
 
353
- expect(procedure.nsid).toBe(nsid)
354
- expect(procedure.parameters.validators).toHaveProperty('conversationId')
355
- expect(procedure.input.encoding).toBe('application/json')
356
- expect(procedure.input.schema).toBe(inputSchema)
357
- expect(procedure.output.encoding).toBe('application/json')
358
- expect(procedure.output.schema).toBe(outputSchema)
359
- expect(procedure.errors).toEqual(errors)
299
+ expect(myProcedure.nsid).toBe('com.example.chat.sendMessage')
300
+ expect(myProcedure.parameters.shape).toHaveProperty('conversationId')
301
+ expect(myProcedure.input.encoding).toBe('application/json')
302
+ expect(myProcedure.input.schema).toBe(inputSchema)
303
+ expect(myProcedure.output.encoding).toBe('application/json')
304
+ expect(myProcedure.output.schema).toBe(outputSchema)
305
+ expect(myProcedure.errors).toEqual(errors)
360
306
  })
361
307
 
362
308
  it('creates a minimal procedure', () => {
363
- const nsid = 'com.example.ping'
364
- const parameters = new ParamsSchema({})
365
- const input = new Payload(undefined, undefined)
366
- const output = new Payload(undefined, undefined)
367
-
368
- const procedure = new Procedure(
369
- nsid,
370
- parameters,
371
- input,
372
- output,
309
+ const myProcedure = procedure(
310
+ 'com.example.ping',
311
+ params(),
312
+ payload(undefined, undefined),
313
+ payload(undefined, undefined),
373
314
  undefined,
374
315
  )
375
316
 
376
- expect(procedure.nsid).toBe(nsid)
377
- expect(Object.keys(procedure.parameters.validators)).toHaveLength(0)
378
- expect(procedure.input.encoding).toBeUndefined()
379
- expect(procedure.output.encoding).toBeUndefined()
380
- expect(procedure.errors).toBeUndefined()
317
+ expect(myProcedure.nsid).toBe('com.example.ping')
318
+ expect(Object.keys(myProcedure.parameters.shape)).toHaveLength(0)
319
+ expect(myProcedure.input.encoding).toBeUndefined()
320
+ expect(myProcedure.output.encoding).toBeUndefined()
321
+ expect(myProcedure.errors).toBeUndefined()
381
322
  })
382
323
  })
383
324
  })
@@ -3,11 +3,13 @@ import { ParamsSchema } from './params.js'
3
3
  import { Payload } from './payload.js'
4
4
 
5
5
  export class Procedure<
6
- TNsid extends NsidString = NsidString,
7
- TParameters extends ParamsSchema = ParamsSchema,
8
- TInputPayload extends Payload = Payload,
9
- TOutputPayload extends Payload = Payload,
10
- TErrors extends undefined | readonly string[] = undefined | readonly string[],
6
+ const TNsid extends NsidString = NsidString,
7
+ const TParameters extends ParamsSchema = ParamsSchema,
8
+ const TInputPayload extends Payload = Payload,
9
+ const TOutputPayload extends Payload = Payload,
10
+ const TErrors extends undefined | readonly string[] =
11
+ | undefined
12
+ | readonly string[],
11
13
  > {
12
14
  readonly type = 'procedure' as const
13
15
 
@@ -19,3 +21,14 @@ export class Procedure<
19
21
  readonly errors: TErrors,
20
22
  ) {}
21
23
  }
24
+
25
+ /*@__NO_SIDE_EFFECTS__*/
26
+ export function procedure<
27
+ const N extends NsidString,
28
+ const P extends ParamsSchema,
29
+ const I extends Payload,
30
+ const O extends Payload,
31
+ const E extends undefined | readonly string[] = undefined,
32
+ >(nsid: N, parameters: P, input: I, output: O, errors: E = undefined as E) {
33
+ return new Procedure<N, P, I, O, E>(nsid, parameters, input, output, errors)
34
+ }