@atproto/lex-schema 0.0.8 → 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 +41 -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 +10 -8
  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 +15 -13
  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,225 +1,221 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { asNsidString } from '../core.js'
3
- import { IntegerSchema } from './integer.js'
4
- import { ObjectSchema } from './object.js'
5
- import { OptionalSchema } from './optional.js'
6
- import { ParamsSchema } from './params.js'
7
- import { Payload } from './payload.js'
8
- import { Query } from './query.js'
9
- import { StringSchema } from './string.js'
2
+ import { integer } from './integer.js'
3
+ import { ObjectSchema, object } from './object.js'
4
+ import { optional } from './optional.js'
5
+ import { ParamsSchema, params } from './params.js'
6
+ import { Payload, payload } from './payload.js'
7
+ import { Query, query } from './query.js'
8
+ import { string } from './string.js'
9
+ import { withDefault } from './with-default.js'
10
10
 
11
11
  describe('Query', () => {
12
12
  describe('constructor', () => {
13
13
  it('creates a Query instance with all parameters', () => {
14
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
15
- const parameters = new ParamsSchema({
16
- feed: new StringSchema({ format: 'at-uri' }),
17
- limit: new OptionalSchema(
18
- new IntegerSchema({ minimum: 1, maximum: 100 }),
19
- ),
14
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
15
+ const parameters = params({
16
+ feed: string({ format: 'at-uri' }),
17
+ limit: optional(integer({ minimum: 1, maximum: 100 })),
20
18
  })
21
- const output = new Payload(
19
+ const output = payload(
22
20
  'application/json',
23
- new ObjectSchema({
24
- feed: new StringSchema({ format: 'at-uri' }),
21
+ object({
22
+ feed: string({ format: 'at-uri' }),
25
23
  }),
26
24
  )
27
25
  const errors = ['NotFound', 'RateLimitExceeded'] as const
28
26
 
29
- const query = new Query(nsid, parameters, output, errors)
27
+ const myQuery = query(nsid, parameters, output, errors)
30
28
 
31
- expect(query).toBeInstanceOf(Query)
32
- expect(query.nsid).toBe(nsid)
33
- expect(query.parameters).toBe(parameters)
34
- expect(query.output).toBe(output)
35
- expect(query.errors).toBe(errors)
29
+ expect(myQuery).toBeInstanceOf(Query)
30
+ expect(myQuery.nsid).toBe(nsid)
31
+ expect(myQuery.parameters).toBe(parameters)
32
+ expect(myQuery.output).toBe(output)
33
+ expect(myQuery.errors).toBe(errors)
36
34
  })
37
35
 
38
36
  it('creates a Query instance without errors', () => {
39
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
40
- const parameters = new ParamsSchema({
41
- feed: new StringSchema({ format: 'at-uri' }),
37
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
38
+ const parameters = params({
39
+ feed: string({ format: 'at-uri' }),
42
40
  })
43
- const output = new Payload(
41
+ const output = payload(
44
42
  'application/json',
45
- new ObjectSchema({
46
- feed: new StringSchema({ format: 'at-uri' }),
43
+ object({
44
+ feed: string({ format: 'at-uri' }),
47
45
  }),
48
46
  )
49
47
 
50
- const query = new Query(nsid, parameters, output, undefined)
48
+ const myQuery = query(nsid, parameters, output, undefined)
51
49
 
52
- expect(query).toBeInstanceOf(Query)
53
- expect(query.nsid).toBe(nsid)
54
- expect(query.parameters).toBe(parameters)
55
- expect(query.output).toBe(output)
56
- expect(query.errors).toBeUndefined()
50
+ expect(myQuery).toBeInstanceOf(Query)
51
+ expect(myQuery.nsid).toBe(nsid)
52
+ expect(myQuery.parameters).toBe(parameters)
53
+ expect(myQuery.output).toBe(output)
54
+ expect(myQuery.errors).toBeUndefined()
57
55
  })
58
56
 
59
57
  it('creates a Query instance with empty parameters', () => {
60
- const nsid = asNsidString('app.bsky.actor.getProfile')
61
- const parameters = new ParamsSchema({})
62
- const output = new Payload(
58
+ const nsid = 'app.bsky.actor.getProfile'
59
+ const parameters = params()
60
+ const output = payload(
63
61
  'application/json',
64
- new ObjectSchema({
65
- did: new StringSchema({ format: 'did' }),
62
+ object({
63
+ did: string({ format: 'did' }),
66
64
  }),
67
65
  )
68
66
 
69
- const query = new Query(nsid, parameters, output, undefined)
67
+ const myQuery = query(nsid, parameters, output, undefined)
70
68
 
71
- expect(query).toBeInstanceOf(Query)
72
- expect(query.parameters).toBe(parameters)
69
+ expect(myQuery).toBeInstanceOf(Query)
70
+ expect(myQuery.parameters).toBe(parameters)
73
71
  })
74
72
  })
75
73
 
76
74
  describe('properties', () => {
77
75
  it('has nsid property', () => {
78
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
79
- const parameters = new ParamsSchema({})
80
- const output = new Payload('application/json', undefined)
76
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
77
+ const parameters = params()
78
+ const output = payload('application/json', undefined)
81
79
 
82
- const query = new Query(nsid, parameters, output, undefined)
80
+ const myQuery = query(nsid, parameters, output, undefined)
83
81
 
84
- expect(query.nsid).toBe(nsid)
85
- expect(query.nsid).toBe('app.bsky.feed.getFeedSkeleton')
82
+ expect(myQuery.nsid).toBe(nsid)
83
+ expect(myQuery.nsid).toBe('app.bsky.feed.getFeedSkeleton')
86
84
  })
87
85
 
88
86
  it('has parameters property', () => {
89
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
90
- const parameters = new ParamsSchema({
91
- feed: new StringSchema({ format: 'at-uri' }),
87
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
88
+ const parameters = params({
89
+ feed: string({ format: 'at-uri' }),
92
90
  })
93
- const output = new Payload('application/json', undefined)
91
+ const output = payload('application/json', undefined)
94
92
 
95
- const query = new Query(nsid, parameters, output, undefined)
93
+ const myQuery = query(nsid, parameters, output, undefined)
96
94
 
97
- expect(query.parameters).toBe(parameters)
98
- expect(query.parameters).toBeInstanceOf(ParamsSchema)
95
+ expect(myQuery.parameters).toBe(parameters)
96
+ expect(myQuery.parameters).toBeInstanceOf(ParamsSchema)
99
97
  })
100
98
 
101
99
  it('has output property', () => {
102
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
103
- const parameters = new ParamsSchema({})
104
- const output = new Payload('application/json', undefined)
100
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
101
+ const parameters = params()
102
+ const output = payload('application/json', undefined)
105
103
 
106
- const query = new Query(nsid, parameters, output, undefined)
104
+ const myQuery = query(nsid, parameters, output, undefined)
107
105
 
108
- expect(query.output).toBe(output)
109
- expect(query.output).toBeInstanceOf(Payload)
106
+ expect(myQuery.output).toBe(output)
107
+ expect(myQuery.output).toBeInstanceOf(Payload)
110
108
  })
111
109
 
112
110
  it('has errors property', () => {
113
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
114
- const parameters = new ParamsSchema({})
115
- const output = new Payload('application/json', undefined)
111
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
112
+ const parameters = params()
113
+ const output = payload('application/json', undefined)
116
114
  const errors = ['NotFound', 'RateLimitExceeded'] as const
117
115
 
118
- const query = new Query(nsid, parameters, output, errors)
116
+ const myQuery = query(nsid, parameters, output, errors)
119
117
 
120
- expect(query.errors).toBe(errors)
121
- expect(query.errors).toEqual(['NotFound', 'RateLimitExceeded'])
118
+ expect(myQuery.errors).toBe(errors)
119
+ expect(myQuery.errors).toEqual(['NotFound', 'RateLimitExceeded'])
122
120
  })
123
121
  })
124
122
 
125
123
  describe('with complex parameters', () => {
126
124
  it('creates a Query with multiple parameter types', () => {
127
- const nsid = asNsidString('app.bsky.feed.searchPosts')
128
- const parameters = new ParamsSchema({
129
- q: new StringSchema({ minLength: 1 }),
130
- limit: new OptionalSchema(
131
- new IntegerSchema({ minimum: 1, maximum: 100 }),
132
- ),
133
- cursor: new OptionalSchema(new StringSchema({})),
134
- author: new OptionalSchema(new StringSchema({ format: 'did' })),
125
+ const nsid = 'app.bsky.feed.searchPosts'
126
+ const parameters = params({
127
+ q: string({ minLength: 1 }),
128
+ limit: optional(integer({ minimum: 1, maximum: 100 })),
129
+ cursor: optional(string()),
130
+ author: optional(string({ format: 'did' })),
135
131
  })
136
- const output = new Payload(
132
+ const output = payload(
137
133
  'application/json',
138
- new ObjectSchema({
139
- cursor: new OptionalSchema(new StringSchema({})),
140
- posts: new ObjectSchema({}),
134
+ object({
135
+ cursor: optional(string()),
136
+ posts: object({}),
141
137
  }),
142
138
  )
143
139
  const errors = ['BadRequest', 'RateLimitExceeded'] as const
144
140
 
145
- const query = new Query(nsid, parameters, output, errors)
141
+ const myQuery = query(nsid, parameters, output, errors)
146
142
 
147
- expect(query).toBeInstanceOf(Query)
148
- expect(query.parameters).toBe(parameters)
149
- expect(query.errors).toEqual(['BadRequest', 'RateLimitExceeded'])
143
+ expect(myQuery).toBeInstanceOf(Query)
144
+ expect(myQuery.parameters).toBe(parameters)
145
+ expect(myQuery.errors).toEqual(['BadRequest', 'RateLimitExceeded'])
150
146
  })
151
147
  })
152
148
 
153
149
  describe('with various output payloads', () => {
154
150
  it('creates a Query with undefined output payload', () => {
155
- const nsid = asNsidString('app.bsky.actor.getProfile')
156
- const parameters = new ParamsSchema({
157
- actor: new StringSchema({ format: 'at-identifier' }),
151
+ const nsid = 'app.bsky.actor.getProfile'
152
+ const parameters = params({
153
+ actor: string({ format: 'at-identifier' }),
158
154
  })
159
- const output = new Payload(undefined, undefined)
155
+ const output = payload(undefined, undefined)
160
156
 
161
- const query = new Query(nsid, parameters, output, undefined)
157
+ const myQuery = query(nsid, parameters, output, undefined)
162
158
 
163
- expect(query).toBeInstanceOf(Query)
164
- expect(query.output.encoding).toBeUndefined()
165
- expect(query.output.schema).toBeUndefined()
159
+ expect(myQuery).toBeInstanceOf(Query)
160
+ expect(myQuery.output.encoding).toBeUndefined()
161
+ expect(myQuery.output.schema).toBeUndefined()
166
162
  })
167
163
 
168
164
  it('creates a Query with JSON payload', () => {
169
- const nsid = asNsidString('app.bsky.actor.getProfile')
170
- const parameters = new ParamsSchema({
171
- actor: new StringSchema({ format: 'at-identifier' }),
165
+ const nsid = 'app.bsky.actor.getProfile'
166
+ const parameters = params({
167
+ actor: string({ format: 'at-identifier' }),
172
168
  })
173
- const output = new Payload(
169
+ const output = payload(
174
170
  'application/json',
175
- new ObjectSchema({
176
- did: new StringSchema({ format: 'did' }),
177
- handle: new StringSchema({ format: 'handle' }),
171
+ object({
172
+ did: string({ format: 'did' }),
173
+ handle: string({ format: 'handle' }),
178
174
  }),
179
175
  )
180
176
 
181
- const query = new Query(nsid, parameters, output, undefined)
177
+ const myQuery = query(nsid, parameters, output, undefined)
182
178
 
183
- expect(query).toBeInstanceOf(Query)
184
- expect(query.output.encoding).toBe('application/json')
185
- expect(query.output.schema).toBeInstanceOf(ObjectSchema)
179
+ expect(myQuery).toBeInstanceOf(Query)
180
+ expect(myQuery.output.encoding).toBe('application/json')
181
+ expect(myQuery.output.schema).toBeInstanceOf(ObjectSchema)
186
182
  })
187
183
 
188
184
  it('creates a Query with text payload', () => {
189
- const nsid = asNsidString('app.bsky.feed.getPost')
190
- const parameters = new ParamsSchema({
191
- uri: new StringSchema({ format: 'at-uri' }),
185
+ const nsid = 'app.bsky.feed.getPost'
186
+ const parameters = params({
187
+ uri: string({ format: 'at-uri' }),
192
188
  })
193
- const output = new Payload('text/plain', undefined)
189
+ const output = payload('text/plain', undefined)
194
190
 
195
- const query = new Query(nsid, parameters, output, undefined)
191
+ const myQuery = query(nsid, parameters, output, undefined)
196
192
 
197
- expect(query).toBeInstanceOf(Query)
198
- expect(query.output.encoding).toBe('text/plain')
193
+ expect(myQuery).toBeInstanceOf(Query)
194
+ expect(myQuery.output.encoding).toBe('text/plain')
199
195
  })
200
196
  })
201
197
 
202
198
  describe('with different error configurations', () => {
203
199
  it('creates a Query with a single error', () => {
204
- const nsid = asNsidString('app.bsky.feed.getPost')
205
- const parameters = new ParamsSchema({})
206
- const output = new Payload('application/json', undefined)
200
+ const nsid = 'app.bsky.feed.getPost'
201
+ const parameters = params()
202
+ const output = payload('application/json', undefined)
207
203
  const errors = ['NotFound'] as const
208
204
 
209
- const query = new Query(nsid, parameters, output, errors)
205
+ const myQuery = query(nsid, parameters, output, errors)
210
206
 
211
- expect(query.errors).toEqual(['NotFound'])
207
+ expect(myQuery.errors).toEqual(['NotFound'])
212
208
  })
213
209
 
214
210
  it('creates a Query with multiple errors', () => {
215
- const nsid = asNsidString('app.bsky.feed.getPost')
216
- const parameters = new ParamsSchema({})
217
- const output = new Payload('application/json', undefined)
211
+ const nsid = 'app.bsky.feed.getPost'
212
+ const parameters = params()
213
+ const output = payload('application/json', undefined)
218
214
  const errors = ['NotFound', 'Unauthorized', 'RateLimitExceeded'] as const
219
215
 
220
- const query = new Query(nsid, parameters, output, errors)
216
+ const myQuery = query(nsid, parameters, output, errors)
221
217
 
222
- expect(query.errors).toEqual([
218
+ expect(myQuery.errors).toEqual([
223
219
  'NotFound',
224
220
  'Unauthorized',
225
221
  'RateLimitExceeded',
@@ -227,136 +223,126 @@ describe('Query', () => {
227
223
  })
228
224
 
229
225
  it('creates a Query with empty errors array', () => {
230
- const nsid = asNsidString('app.bsky.feed.getPost')
231
- const parameters = new ParamsSchema({})
232
- const output = new Payload('application/json', undefined)
226
+ const nsid = 'app.bsky.feed.getPost'
227
+ const parameters = params()
228
+ const output = payload('application/json', undefined)
233
229
  const errors = [] as const
234
230
 
235
- const query = new Query(nsid, parameters, output, errors)
231
+ const myQuery = query(nsid, parameters, output, errors)
236
232
 
237
- expect(query.errors).toEqual([])
233
+ expect(myQuery.errors).toEqual([])
238
234
  })
239
235
  })
240
236
 
241
237
  describe('edge cases', () => {
242
238
  it('handles very long NSID', () => {
243
- const nsid = asNsidString(
244
- 'com.example.very.long.namespace.identifier.method.name',
245
- )
246
- const parameters = new ParamsSchema({})
247
- const output = new Payload('application/json', undefined)
239
+ const nsid = 'com.example.very.long.namespace.identifier.method.name'
240
+ const parameters = params()
241
+ const output = payload('application/json', undefined)
248
242
 
249
- const query = new Query(nsid, parameters, output, undefined)
243
+ const myQuery = query(nsid, parameters, output, undefined)
250
244
 
251
- expect(query.nsid).toBe(nsid)
245
+ expect(myQuery.nsid).toBe(nsid)
252
246
  })
253
247
 
254
- it('handles query with all optional parameters', () => {
255
- const nsid = asNsidString('app.bsky.feed.search')
256
- const parameters = new ParamsSchema({
257
- q: new OptionalSchema(new StringSchema({})),
258
- limit: new OptionalSchema(new IntegerSchema({})),
259
- cursor: new OptionalSchema(new StringSchema({})),
248
+ it('handles myQuery with all optional parameters', () => {
249
+ const nsid = 'app.bsky.feed.search'
250
+ const parameters = params({
251
+ q: optional(string()),
252
+ limit: optional(integer()),
253
+ cursor: optional(string()),
260
254
  })
261
- const output = new Payload('application/json', undefined)
255
+ const output = payload('application/json', undefined)
262
256
 
263
- const query = new Query(nsid, parameters, output, undefined)
257
+ const myQuery = query(nsid, parameters, output, undefined)
264
258
 
265
- expect(query.parameters).toBe(parameters)
259
+ expect(myQuery.parameters).toBe(parameters)
266
260
  })
267
261
 
268
- it('handles query with complex nested output schema', () => {
269
- const nsid = asNsidString('app.bsky.feed.getTimeline')
270
- const parameters = new ParamsSchema({})
271
- const output = new Payload(
262
+ it('handles myQuery with complex nested output schema', () => {
263
+ const nsid = 'app.bsky.feed.getTimeline'
264
+ const parameters = params()
265
+ const output = payload(
272
266
  'application/json',
273
- new ObjectSchema({
274
- cursor: new OptionalSchema(new StringSchema({})),
275
- feed: new ObjectSchema({
276
- post: new ObjectSchema({
277
- uri: new StringSchema({ format: 'at-uri' }),
278
- cid: new StringSchema({ format: 'cid' }),
267
+ object({
268
+ cursor: optional(string()),
269
+ feed: object({
270
+ post: object({
271
+ uri: string({ format: 'at-uri' }),
272
+ cid: string({ format: 'cid' }),
279
273
  }),
280
274
  }),
281
275
  }),
282
276
  )
283
277
 
284
- const query = new Query(nsid, parameters, output, undefined)
278
+ const myQuery = query(nsid, parameters, output, undefined)
285
279
 
286
- expect(query.output.schema).toBeInstanceOf(ObjectSchema)
280
+ expect(myQuery.output.schema).toBeInstanceOf(ObjectSchema)
287
281
  })
288
282
  })
289
283
 
290
- describe('real-world query examples', () => {
291
- it('creates a getFeedSkeleton query', () => {
292
- const nsid = asNsidString('app.bsky.feed.getFeedSkeleton')
293
- const parameters = new ParamsSchema({
294
- feed: new StringSchema({ format: 'at-uri' }),
295
- limit: new OptionalSchema(
296
- new IntegerSchema({ minimum: 1, maximum: 100, default: 50 }),
297
- ),
298
- cursor: new OptionalSchema(new StringSchema({})),
284
+ describe('real-world myQuery examples', () => {
285
+ it('creates a getFeedSkeleton myQuery', () => {
286
+ const nsid = 'app.bsky.feed.getFeedSkeleton'
287
+ const parameters = params({
288
+ feed: string({ format: 'at-uri' }),
289
+ limit: optional(withDefault(integer({ minimum: 1, maximum: 100 }), 50)),
290
+ cursor: optional(string()),
299
291
  })
300
- const output = new Payload(
292
+ const output = payload(
301
293
  'application/json',
302
- new ObjectSchema({
303
- cursor: new OptionalSchema(new StringSchema({})),
304
- feed: new ObjectSchema({}),
294
+ object({
295
+ cursor: optional(string()),
296
+ feed: object({}),
305
297
  }),
306
298
  )
307
299
 
308
- const query = new Query(nsid, parameters, output, undefined)
300
+ const myQuery = query(nsid, parameters, output, undefined)
309
301
 
310
- expect(query.nsid).toBe('app.bsky.feed.getFeedSkeleton')
302
+ expect(myQuery.nsid).toBe('app.bsky.feed.getFeedSkeleton')
311
303
  })
312
304
 
313
- it('creates a searchPosts query', () => {
314
- const nsid = asNsidString('app.bsky.feed.searchPosts')
315
- const parameters = new ParamsSchema({
316
- q: new StringSchema({ minLength: 1, maxLength: 300 }),
317
- limit: new OptionalSchema(
318
- new IntegerSchema({ minimum: 1, maximum: 100, default: 25 }),
319
- ),
320
- cursor: new OptionalSchema(new StringSchema({})),
305
+ it('creates a searchPosts myQuery', () => {
306
+ const nsid = 'app.bsky.feed.searchPosts'
307
+ const parameters = params({
308
+ q: string({ minLength: 1, maxLength: 300 }),
309
+ limit: optional(withDefault(integer({ minimum: 1, maximum: 100 }), 25)),
310
+ cursor: optional(string()),
321
311
  })
322
- const output = new Payload(
312
+ const output = payload(
323
313
  'application/json',
324
- new ObjectSchema({
325
- cursor: new OptionalSchema(new StringSchema({})),
326
- hitsTotal: new OptionalSchema(new IntegerSchema({})),
327
- posts: new ObjectSchema({}),
314
+ object({
315
+ cursor: optional(string()),
316
+ hitsTotal: optional(integer()),
317
+ posts: object({}),
328
318
  }),
329
319
  )
330
320
  const errors = ['BadRequest'] as const
331
321
 
332
- const query = new Query(nsid, parameters, output, errors)
322
+ const myQuery = query(nsid, parameters, output, errors)
333
323
 
334
- expect(query.nsid).toBe('app.bsky.feed.searchPosts')
335
- expect(query.errors).toEqual(['BadRequest'])
324
+ expect(myQuery.nsid).toBe('app.bsky.feed.searchPosts')
325
+ expect(myQuery.errors).toEqual(['BadRequest'])
336
326
  })
337
327
 
338
- it('creates a getProfile query', () => {
339
- const nsid = asNsidString('app.bsky.actor.getProfile')
340
- const parameters = new ParamsSchema({
341
- actor: new StringSchema({ format: 'at-identifier' }),
328
+ it('creates a getProfile myQuery', () => {
329
+ const nsid = 'app.bsky.actor.getProfile'
330
+ const parameters = params({
331
+ actor: string({ format: 'at-identifier' }),
342
332
  })
343
- const output = new Payload(
333
+ const output = payload(
344
334
  'application/json',
345
- new ObjectSchema({
346
- did: new StringSchema({ format: 'did' }),
347
- handle: new StringSchema({ format: 'handle' }),
348
- displayName: new OptionalSchema(
349
- new StringSchema({ maxGraphemes: 64 }),
350
- ),
351
- description: new OptionalSchema(
352
- new StringSchema({ maxGraphemes: 256 }),
353
- ),
335
+ object({
336
+ did: string({ format: 'did' }),
337
+ handle: string({ format: 'handle' }),
338
+ displayName: optional(string({ maxGraphemes: 64 })),
339
+ description: optional(string({ maxGraphemes: 256 })),
354
340
  }),
355
341
  )
356
342
 
357
- const query = new Query(nsid, parameters, output, undefined)
343
+ const myQuery = query(nsid, parameters, output, undefined)
358
344
 
359
- expect(query.nsid).toBe('app.bsky.actor.getProfile')
345
+ expect(myQuery.nsid).toBe('app.bsky.actor.getProfile')
360
346
  })
361
347
  })
362
348
  })
@@ -3,10 +3,12 @@ import { ParamsSchema } from './params.js'
3
3
  import { Payload } from './payload.js'
4
4
 
5
5
  export class Query<
6
- TNsid extends NsidString = NsidString,
7
- TParameters extends ParamsSchema = ParamsSchema,
8
- TOutputPayload extends Payload = Payload,
9
- TErrors extends undefined | readonly string[] = undefined | readonly string[],
6
+ const TNsid extends NsidString = NsidString,
7
+ const TParameters extends ParamsSchema = ParamsSchema,
8
+ const TOutputPayload extends Payload = Payload,
9
+ const TErrors extends undefined | readonly string[] =
10
+ | undefined
11
+ | readonly string[],
10
12
  > {
11
13
  readonly type = 'query' as const
12
14
 
@@ -17,3 +19,13 @@ export class Query<
17
19
  readonly errors: TErrors,
18
20
  ) {}
19
21
  }
22
+
23
+ /*@__NO_SIDE_EFFECTS__*/
24
+ export function query<
25
+ const N extends NsidString,
26
+ const P extends ParamsSchema,
27
+ const O extends Payload,
28
+ const E extends undefined | readonly string[] = undefined,
29
+ >(nsid: N, parameters: P, output: O, errors: E = undefined as E) {
30
+ return new Query<N, P, O, E>(nsid, parameters, output, errors)
31
+ }