@atproto/lex-schema 0.1.5 → 0.1.6

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 (263) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/core/$type.d.ts +2 -2
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js.map +1 -1
  5. package/dist/core/record-key.d.ts +1 -1
  6. package/dist/core/record-key.d.ts.map +1 -1
  7. package/dist/core/record-key.js.map +1 -1
  8. package/dist/core/schema.d.ts +3 -2
  9. package/dist/core/schema.d.ts.map +1 -1
  10. package/dist/core/schema.js +1 -1
  11. package/dist/core/schema.js.map +1 -1
  12. package/dist/core/standard-schema.d.ts +2 -2
  13. package/dist/core/standard-schema.d.ts.map +1 -1
  14. package/dist/core/standard-schema.js.map +1 -1
  15. package/dist/core/string-format.d.ts +2 -2
  16. package/dist/core/string-format.d.ts.map +1 -1
  17. package/dist/core/string-format.js.map +1 -1
  18. package/dist/core/validation-error.d.ts +1 -1
  19. package/dist/core/validation-error.d.ts.map +1 -1
  20. package/dist/core/validation-error.js +1 -1
  21. package/dist/core/validation-error.js.map +1 -1
  22. package/dist/core/validator.d.ts +1 -1
  23. package/dist/core/validator.d.ts.map +1 -1
  24. package/dist/core/validator.js +1 -1
  25. package/dist/core/validator.js.map +1 -1
  26. package/dist/helpers.d.ts +2 -2
  27. package/dist/helpers.d.ts.map +1 -1
  28. package/dist/helpers.js +2 -2
  29. package/dist/helpers.js.map +1 -1
  30. package/dist/schema/array.d.ts +1 -1
  31. package/dist/schema/array.d.ts.map +1 -1
  32. package/dist/schema/array.js +1 -1
  33. package/dist/schema/array.js.map +1 -1
  34. package/dist/schema/blob.d.ts +1 -1
  35. package/dist/schema/blob.d.ts.map +1 -1
  36. package/dist/schema/blob.js +2 -2
  37. package/dist/schema/blob.js.map +1 -1
  38. package/dist/schema/boolean.js +1 -1
  39. package/dist/schema/boolean.js.map +1 -1
  40. package/dist/schema/bytes.js +1 -1
  41. package/dist/schema/bytes.js.map +1 -1
  42. package/dist/schema/cid.d.ts +1 -1
  43. package/dist/schema/cid.d.ts.map +1 -1
  44. package/dist/schema/cid.js +3 -3
  45. package/dist/schema/cid.js.map +1 -1
  46. package/dist/schema/custom.js +1 -1
  47. package/dist/schema/custom.js.map +1 -1
  48. package/dist/schema/dict.d.ts +1 -1
  49. package/dist/schema/dict.d.ts.map +1 -1
  50. package/dist/schema/dict.js +1 -1
  51. package/dist/schema/dict.js.map +1 -1
  52. package/dist/schema/discriminated-union.d.ts +1 -1
  53. package/dist/schema/discriminated-union.d.ts.map +1 -1
  54. package/dist/schema/discriminated-union.js +2 -1
  55. package/dist/schema/discriminated-union.js.map +1 -1
  56. package/dist/schema/enum.js +1 -1
  57. package/dist/schema/enum.js.map +1 -1
  58. package/dist/schema/integer.js +1 -1
  59. package/dist/schema/integer.js.map +1 -1
  60. package/dist/schema/intersection.d.ts +1 -1
  61. package/dist/schema/intersection.d.ts.map +1 -1
  62. package/dist/schema/intersection.js +3 -1
  63. package/dist/schema/intersection.js.map +1 -1
  64. package/dist/schema/lex-map.d.ts +1 -1
  65. package/dist/schema/lex-map.d.ts.map +1 -1
  66. package/dist/schema/lex-map.js +1 -1
  67. package/dist/schema/lex-map.js.map +1 -1
  68. package/dist/schema/lex-value.d.ts +1 -1
  69. package/dist/schema/lex-value.d.ts.map +1 -1
  70. package/dist/schema/lex-value.js +1 -1
  71. package/dist/schema/lex-value.js.map +1 -1
  72. package/dist/schema/literal.js +1 -1
  73. package/dist/schema/literal.js.map +1 -1
  74. package/dist/schema/never.js +1 -1
  75. package/dist/schema/never.js.map +1 -1
  76. package/dist/schema/null.js +1 -1
  77. package/dist/schema/null.js.map +1 -1
  78. package/dist/schema/nullable.d.ts +1 -1
  79. package/dist/schema/nullable.d.ts.map +1 -1
  80. package/dist/schema/nullable.js +1 -1
  81. package/dist/schema/nullable.js.map +1 -1
  82. package/dist/schema/object.d.ts +2 -1
  83. package/dist/schema/object.d.ts.map +1 -1
  84. package/dist/schema/object.js +1 -1
  85. package/dist/schema/object.js.map +1 -1
  86. package/dist/schema/optional.d.ts +2 -1
  87. package/dist/schema/optional.d.ts.map +1 -1
  88. package/dist/schema/optional.js +2 -1
  89. package/dist/schema/optional.js.map +1 -1
  90. package/dist/schema/params.d.ts +1 -1
  91. package/dist/schema/params.d.ts.map +1 -1
  92. package/dist/schema/params.js +1 -1
  93. package/dist/schema/params.js.map +1 -1
  94. package/dist/schema/payload.d.ts +3 -2
  95. package/dist/schema/payload.d.ts.map +1 -1
  96. package/dist/schema/payload.js +2 -1
  97. package/dist/schema/payload.js.map +1 -1
  98. package/dist/schema/permission-set.d.ts +1 -1
  99. package/dist/schema/permission-set.d.ts.map +1 -1
  100. package/dist/schema/permission-set.js +1 -0
  101. package/dist/schema/permission-set.js.map +1 -1
  102. package/dist/schema/permission.d.ts +1 -1
  103. package/dist/schema/permission.d.ts.map +1 -1
  104. package/dist/schema/permission.js.map +1 -1
  105. package/dist/schema/procedure.d.ts +1 -1
  106. package/dist/schema/procedure.d.ts.map +1 -1
  107. package/dist/schema/procedure.js +2 -0
  108. package/dist/schema/procedure.js.map +1 -1
  109. package/dist/schema/query.d.ts +1 -1
  110. package/dist/schema/query.d.ts.map +1 -1
  111. package/dist/schema/query.js +2 -0
  112. package/dist/schema/query.js.map +1 -1
  113. package/dist/schema/record.d.ts +2 -2
  114. package/dist/schema/record.d.ts.map +1 -1
  115. package/dist/schema/record.js +1 -1
  116. package/dist/schema/record.js.map +1 -1
  117. package/dist/schema/ref.d.ts +1 -1
  118. package/dist/schema/ref.d.ts.map +1 -1
  119. package/dist/schema/ref.js +1 -1
  120. package/dist/schema/ref.js.map +1 -1
  121. package/dist/schema/refine.d.ts +2 -2
  122. package/dist/schema/refine.d.ts.map +1 -1
  123. package/dist/schema/refine.js +1 -1
  124. package/dist/schema/refine.js.map +1 -1
  125. package/dist/schema/regexp.js +1 -1
  126. package/dist/schema/regexp.js.map +1 -1
  127. package/dist/schema/string.d.ts +2 -2
  128. package/dist/schema/string.d.ts.map +1 -1
  129. package/dist/schema/string.js +1 -1
  130. package/dist/schema/string.js.map +1 -1
  131. package/dist/schema/subscription.d.ts +3 -2
  132. package/dist/schema/subscription.d.ts.map +1 -1
  133. package/dist/schema/subscription.js +2 -0
  134. package/dist/schema/subscription.js.map +1 -1
  135. package/dist/schema/token.d.ts +1 -1
  136. package/dist/schema/token.d.ts.map +1 -1
  137. package/dist/schema/token.js +1 -1
  138. package/dist/schema/token.js.map +1 -1
  139. package/dist/schema/typed-object.d.ts +2 -2
  140. package/dist/schema/typed-object.d.ts.map +1 -1
  141. package/dist/schema/typed-object.js +1 -1
  142. package/dist/schema/typed-object.js.map +1 -1
  143. package/dist/schema/typed-ref.d.ts +1 -1
  144. package/dist/schema/typed-ref.d.ts.map +1 -1
  145. package/dist/schema/typed-ref.js +1 -1
  146. package/dist/schema/typed-ref.js.map +1 -1
  147. package/dist/schema/typed-union.d.ts +1 -1
  148. package/dist/schema/typed-union.d.ts.map +1 -1
  149. package/dist/schema/typed-union.js +3 -1
  150. package/dist/schema/typed-union.js.map +1 -1
  151. package/dist/schema/union.d.ts +1 -1
  152. package/dist/schema/union.d.ts.map +1 -1
  153. package/dist/schema/union.js +1 -1
  154. package/dist/schema/union.js.map +1 -1
  155. package/dist/schema/unknown.js +1 -1
  156. package/dist/schema/unknown.js.map +1 -1
  157. package/dist/schema/with-default.d.ts +1 -1
  158. package/dist/schema/with-default.d.ts.map +1 -1
  159. package/dist/schema/with-default.js +1 -1
  160. package/dist/schema/with-default.js.map +1 -1
  161. package/package.json +6 -10
  162. package/src/core/$type.test.ts +0 -24
  163. package/src/core/$type.ts +0 -199
  164. package/src/core/record-key.ts +0 -85
  165. package/src/core/result.ts +0 -15
  166. package/src/core/schema.ts +0 -412
  167. package/src/core/standard-schema.test.ts +0 -124
  168. package/src/core/standard-schema.ts +0 -31
  169. package/src/core/string-format.ts +0 -411
  170. package/src/core/types.ts +0 -120
  171. package/src/core/validation-error.ts +0 -134
  172. package/src/core/validation-issue.ts +0 -340
  173. package/src/core/validator.ts +0 -636
  174. package/src/core.ts +0 -9
  175. package/src/external.ts +0 -3
  176. package/src/helpers.test.ts +0 -694
  177. package/src/helpers.ts +0 -222
  178. package/src/index.ts +0 -3
  179. package/src/schema/array.test.ts +0 -251
  180. package/src/schema/array.ts +0 -126
  181. package/src/schema/blob.test.ts +0 -733
  182. package/src/schema/blob.ts +0 -150
  183. package/src/schema/boolean.test.ts +0 -118
  184. package/src/schema/boolean.ts +0 -46
  185. package/src/schema/bytes.test.ts +0 -227
  186. package/src/schema/bytes.ts +0 -81
  187. package/src/schema/cid.test.ts +0 -125
  188. package/src/schema/cid.ts +0 -69
  189. package/src/schema/custom.test.ts +0 -414
  190. package/src/schema/custom.ts +0 -106
  191. package/src/schema/dict.test.ts +0 -181
  192. package/src/schema/dict.ts +0 -122
  193. package/src/schema/discriminated-union.test.ts +0 -676
  194. package/src/schema/discriminated-union.ts +0 -196
  195. package/src/schema/enum.test.ts +0 -398
  196. package/src/schema/enum.ts +0 -77
  197. package/src/schema/integer.test.ts +0 -314
  198. package/src/schema/integer.ts +0 -86
  199. package/src/schema/intersection.test.ts +0 -33
  200. package/src/schema/intersection.ts +0 -113
  201. package/src/schema/lex-map.test.ts +0 -593
  202. package/src/schema/lex-map.ts +0 -63
  203. package/src/schema/lex-value.test.ts +0 -81
  204. package/src/schema/lex-value.ts +0 -86
  205. package/src/schema/literal.test.ts +0 -533
  206. package/src/schema/literal.ts +0 -70
  207. package/src/schema/never.test.ts +0 -175
  208. package/src/schema/never.ts +0 -56
  209. package/src/schema/null.test.ts +0 -80
  210. package/src/schema/null.ts +0 -49
  211. package/src/schema/nullable.test.ts +0 -470
  212. package/src/schema/nullable.ts +0 -74
  213. package/src/schema/object.test.ts +0 -69
  214. package/src/schema/object.ts +0 -136
  215. package/src/schema/optional.test.ts +0 -479
  216. package/src/schema/optional.ts +0 -92
  217. package/src/schema/params.test.ts +0 -1118
  218. package/src/schema/params.ts +0 -371
  219. package/src/schema/payload.test.ts +0 -340
  220. package/src/schema/payload.ts +0 -204
  221. package/src/schema/permission-set.test.ts +0 -613
  222. package/src/schema/permission-set.ts +0 -86
  223. package/src/schema/permission.test.ts +0 -537
  224. package/src/schema/permission.ts +0 -63
  225. package/src/schema/procedure.test.ts +0 -324
  226. package/src/schema/procedure.ts +0 -98
  227. package/src/schema/query.test.ts +0 -348
  228. package/src/schema/query.ts +0 -86
  229. package/src/schema/record.test.ts +0 -812
  230. package/src/schema/record.ts +0 -217
  231. package/src/schema/ref.test.ts +0 -349
  232. package/src/schema/ref.ts +0 -103
  233. package/src/schema/refine.test.ts +0 -579
  234. package/src/schema/refine.ts +0 -153
  235. package/src/schema/regexp.test.ts +0 -577
  236. package/src/schema/regexp.ts +0 -82
  237. package/src/schema/string.test.ts +0 -773
  238. package/src/schema/string.ts +0 -229
  239. package/src/schema/subscription.test.ts +0 -499
  240. package/src/schema/subscription.ts +0 -108
  241. package/src/schema/token.test.ts +0 -152
  242. package/src/schema/token.ts +0 -103
  243. package/src/schema/typed-object.test.ts +0 -745
  244. package/src/schema/typed-object.ts +0 -181
  245. package/src/schema/typed-ref.test.ts +0 -796
  246. package/src/schema/typed-ref.ts +0 -126
  247. package/src/schema/typed-union.test.ts +0 -355
  248. package/src/schema/typed-union.ts +0 -130
  249. package/src/schema/union.test.ts +0 -191
  250. package/src/schema/union.ts +0 -89
  251. package/src/schema/unknown.test.ts +0 -313
  252. package/src/schema/unknown.ts +0 -47
  253. package/src/schema/with-default.ts +0 -81
  254. package/src/schema.ts +0 -43
  255. package/src/util/array-agg.test.ts +0 -42
  256. package/src/util/array-agg.ts +0 -44
  257. package/src/util/assertion-util.ts +0 -1
  258. package/src/util/if-any.ts +0 -3
  259. package/src/util/lazy-property.ts +0 -14
  260. package/src/util/memoize.ts +0 -37
  261. package/tsconfig.build.json +0 -12
  262. package/tsconfig.json +0 -7
  263. package/tsconfig.tests.json +0 -8
@@ -1,694 +0,0 @@
1
- import { describe, expect, expectTypeOf, it, test } from 'vitest'
2
- import * as l from './external.js'
3
-
4
- class BinaryValue {
5
- declare readonly ['__binaryValue']: BinaryValue
6
- }
7
-
8
- const expectType = <T>(_: T) => {}
9
- const binaryValue = new BinaryValue()
10
-
11
- describe('InferMethodParams', () => {
12
- describe('query', () => {
13
- test('infers parameter types', () => {
14
- const _query = l.query(
15
- 'com.example.query',
16
- l.params({
17
- feed: l.string({ format: 'at-uri' }),
18
- limit: l.optional(l.integer()),
19
- }),
20
- l.payload('application/json', undefined),
21
- )
22
-
23
- type Params = l.InferMethodParams<typeof _query>
24
-
25
- expectType<Params>({
26
- feed: 'at://did:plc:abc123/app.bsky.feed.post/xyz',
27
- limit: 50,
28
- })
29
- // @ts-expect-error
30
- expectType<Params>({ feed: 123, limit: 50 })
31
- // @ts-expect-error
32
- expectType<Params>({ limit: 50 })
33
- })
34
- })
35
-
36
- describe('procedure', () => {
37
- test('infers parameter types', () => {
38
- const _procedure = l.procedure(
39
- 'com.example.list',
40
- l.params({ limit: l.string() }),
41
- l.payload(),
42
- l.payload(),
43
- )
44
-
45
- type Params = l.InferMethodParams<typeof _procedure>
46
-
47
- expectType<Params>({ limit: '10' })
48
- // @ts-expect-error
49
- expectType<Params>({ limit: 10 })
50
- // @ts-expect-error
51
- expectType<Params>({ limit: binaryValue })
52
- // @ts-expect-error
53
- expectType<Params>(binaryValue)
54
- })
55
- })
56
-
57
- describe('subscription', () => {
58
- test('infers parameter types', () => {
59
- const _subscription = l.subscription(
60
- 'com.example.subscribe',
61
- l.params({
62
- cursor: l.optional(l.integer()),
63
- }),
64
- l.lexMap(),
65
- )
66
-
67
- type Params = l.InferMethodParams<typeof _subscription>
68
-
69
- expectType<Params>({ cursor: 100 })
70
- // @ts-expect-error
71
- expectType<Params>({ cursor: '100' })
72
- // @ts-expect-error
73
- expectType<Params>({ cursor: binaryValue })
74
- })
75
- })
76
- })
77
-
78
- describe('InferMethodInput', () => {
79
- describe('procedure', () => {
80
- test('with payload schema', () => {
81
- const _procedure = l.procedure(
82
- 'com.example.create',
83
- l.params(),
84
- l.jsonPayload({ text: l.string() }),
85
- l.payload(),
86
- )
87
-
88
- type Input = l.InferMethodInput<typeof _procedure, BinaryValue>
89
-
90
- expectType<Input>({ encoding: 'application/json', body: { text: 'hi' } })
91
- // @ts-expect-error
92
- expectType<Input>({ encoding: 'application/json', body: { text: 123 } })
93
- // @ts-expect-error
94
- expectType<Input>({ encoding: 'text/plain', body: { text: 'hi' } })
95
- // @ts-expect-error
96
- expectType<Input>({ encoding: 'text/plain', body: 'hello' })
97
- // @ts-expect-error
98
- expectType<Input>({ encoding: 'text/plain', body: new Uint8Array() })
99
- // @ts-expect-error
100
- expectType<Input>({ encoding: 'text/plain', body: binaryValue })
101
- })
102
-
103
- test('without payload schema', () => {
104
- const _procedure = l.procedure(
105
- 'com.example.create',
106
- l.params(),
107
- l.payload('*/*', undefined),
108
- l.payload(),
109
- )
110
-
111
- type Input = l.InferMethodInput<typeof _procedure, BinaryValue>
112
-
113
- expectType<Input>({ encoding: 'image/png', body: binaryValue })
114
- // @ts-expect-error
115
- expectType<Input>({ encoding: 'image/png', body: new Uint8Array() })
116
- // @ts-expect-error
117
- expectType<Input>({ encoding: 'image/png', body: [] })
118
- // @ts-expect-error
119
- expectType<Input>({ encoding: 'image/png', body: 'foo' })
120
- })
121
- })
122
- })
123
-
124
- describe('InferMethodInputBody', () => {
125
- describe('procedure', () => {
126
- test('with payload schema', () => {
127
- const _procedure = l.procedure(
128
- 'com.example.create',
129
- l.params(),
130
- l.jsonPayload({ text: l.string() }),
131
- l.payload(),
132
- )
133
-
134
- type InputBody = l.InferMethodInputBody<typeof _procedure, BinaryValue>
135
-
136
- expectType<InputBody>({ text: 'hello' })
137
- // @ts-expect-error
138
- expectType<InputBody>({ text: 123 })
139
- // @ts-expect-error
140
- expectType<InputBody>(binaryValue)
141
- })
142
-
143
- test('without payload schema', () => {
144
- const _procedure = l.procedure(
145
- 'com.example.upload',
146
- l.params(),
147
- l.payload('*/*', undefined),
148
- l.payload(),
149
- )
150
-
151
- type InputBody = l.InferMethodInputBody<typeof _procedure, BinaryValue>
152
-
153
- expectType<InputBody>(binaryValue)
154
- // @ts-expect-error
155
- expectType<InputBody>(new Uint8Array())
156
- // @ts-expect-error
157
- expectType<InputBody>({ text: 'hello' })
158
- })
159
- })
160
- })
161
-
162
- describe('InferMethodInputEncoding', () => {
163
- describe('procedure', () => {
164
- test('with payload schema', () => {
165
- const _procedure = l.procedure(
166
- 'com.example.create',
167
- l.params(),
168
- l.jsonPayload({ text: l.string() }),
169
- l.payload(),
170
- )
171
-
172
- type InputEncoding = l.InferMethodInputEncoding<typeof _procedure>
173
-
174
- expectType<InputEncoding>('application/json')
175
- // @ts-expect-error
176
- expectType<InputEncoding>('text/plain')
177
- // @ts-expect-error
178
- expectType<InputEncoding>(123)
179
- })
180
-
181
- test('without payload schema', () => {
182
- const _procedure = l.procedure(
183
- 'com.example.upload',
184
- l.params(),
185
- l.payload('*/*', undefined),
186
- l.payload(),
187
- )
188
-
189
- type InputEncoding = l.InferMethodInputEncoding<typeof _procedure>
190
-
191
- expectType<InputEncoding>('image/png')
192
- expectType<InputEncoding>('application/octet-stream')
193
- // @ts-expect-error
194
- expectType<InputEncoding>(123)
195
- })
196
- })
197
- })
198
-
199
- describe('InferMethodOutput', () => {
200
- describe('query', () => {
201
- test('generic output', () => {
202
- const _query = l.query(
203
- 'com.example.query',
204
- l.params(),
205
- l.payload(),
206
- ) as unknown as l.Query
207
- const unknownValue = {} as unknown
208
- const lexValue = {} as l.LexValue
209
-
210
- type Output = l.InferMethodOutput<typeof _query, BinaryValue>
211
-
212
- expectType<Output>(undefined)
213
- expectType<Output>({ body: binaryValue, encoding: 'text/plain' })
214
- expectType<Output>({ body: lexValue, encoding: 'application/json' })
215
-
216
- expectType<Output>({
217
- // @ts-expect-error
218
- body: unknownValue,
219
- encoding: 'application/octet-stream',
220
- })
221
-
222
- class Foo {
223
- constructor(readonly field: number = 3) {}
224
- }
225
- expectType<Output>({
226
- // @ts-expect-error
227
- body: new Foo(),
228
- encoding: 'application/octet-stream',
229
- })
230
- })
231
-
232
- test('with payload schema', () => {
233
- const _query = l.query(
234
- 'com.example.query',
235
- l.params(),
236
- l.jsonPayload({ items: l.array(l.string()) }),
237
- )
238
-
239
- type Output = l.InferMethodOutput<typeof _query, BinaryValue>
240
-
241
- expectType<Output>({ encoding: 'application/json', body: { items: [] } })
242
- // @ts-expect-error
243
- expectType<Output>({ encoding: 'application/json', body: { items: [1] } })
244
- // @ts-expect-error
245
- expectType<Output>({ encoding: 'text/plain', body: { items: [] } })
246
- })
247
-
248
- test('without payload schema', () => {
249
- const _query = l.query(
250
- 'com.example.query',
251
- l.params(),
252
- l.payload('*/*', undefined),
253
- )
254
-
255
- type Output = l.InferMethodOutput<typeof _query, BinaryValue>
256
-
257
- expectType<Output>({ encoding: 'image/png', body: binaryValue })
258
- // @ts-expect-error
259
- expectType<Output>({ encoding: 'image/png', body: new Uint8Array() })
260
- // @ts-expect-error
261
- expectType<Output>({ encoding: 'image/png', body: 'string' })
262
- })
263
- })
264
-
265
- describe('procedure', () => {
266
- test('generic output', () => {
267
- const _procedure = l.procedure(
268
- 'com.example.procedure',
269
- l.params(),
270
- l.payload(),
271
- l.payload(),
272
- ) as unknown as l.Procedure
273
- const unknownValue = {} as unknown
274
- const lexValue = {} as l.LexValue
275
-
276
- type Output = l.InferMethodOutput<typeof _procedure, BinaryValue>
277
-
278
- expectType<Output>(undefined)
279
- expectType<Output>({ body: binaryValue, encoding: 'text/plain' })
280
- expectType<Output>({ body: lexValue, encoding: 'application/json' })
281
- expectType<Output>({ body: { foo: 'bar' }, encoding: 'application/json' })
282
-
283
- expectType<Output>({
284
- // @ts-expect-error
285
- body: unknownValue,
286
- encoding: 'application/octet-stream',
287
- })
288
-
289
- class Foo {
290
- constructor(readonly field: number = 3) {}
291
- }
292
- expectType<Output>({
293
- // @ts-expect-error
294
- body: new Foo(),
295
- encoding: 'application/octet-stream',
296
- })
297
- })
298
-
299
- test('with payload schema', () => {
300
- const _procedure = l.procedure(
301
- 'com.example.create',
302
- l.params(),
303
- l.payload(),
304
- l.payload(
305
- 'application/json',
306
- l.object({ uri: l.string({ format: 'at-uri' }) }),
307
- ),
308
- undefined,
309
- )
310
-
311
- type Output = l.InferMethodOutput<typeof _procedure, BinaryValue>
312
-
313
- expectType<Output>({
314
- encoding: 'application/json',
315
- body: { uri: 'at://did:plc:abc/post/123' },
316
- })
317
- // @ts-expect-error
318
- expectType<Output>({ encoding: 'application/json', body: { uri: 123 } })
319
- // @ts-expect-error
320
- expectType<Output>({ encoding: 'text/plain', body: { uri: 'at://...' } })
321
- // @ts-expect-error
322
- expectType<Output>({ encoding: 'text/plain', body: 'Hello' })
323
- })
324
-
325
- test('without payload schema', () => {
326
- const _procedure = l.procedure(
327
- 'com.example.export',
328
- l.params(),
329
- l.payload(),
330
- l.payload('*/*', undefined),
331
- undefined,
332
- )
333
-
334
- type Output = l.InferMethodOutput<typeof _procedure, BinaryValue>
335
-
336
- expectType<Output>({
337
- encoding: 'application/zip',
338
- body: binaryValue,
339
- })
340
- expectType<Output>({
341
- encoding: 'application/zip',
342
- // @ts-expect-error
343
- body: new Uint8Array(),
344
- })
345
- // @ts-expect-error
346
- expectType<Output>({ encoding: 'application/zip', body: 'string' })
347
- })
348
- })
349
- })
350
-
351
- describe('InferMethodOutputBody', () => {
352
- describe('query', () => {
353
- test('generic output', () => {
354
- const _query = l.query(
355
- 'com.example.query',
356
- l.params(),
357
- l.payload(),
358
- ) as unknown as l.Query
359
- const lexValue = {} as l.LexValue
360
-
361
- type OutputBody = l.InferMethodOutputBody<typeof _query, BinaryValue>
362
-
363
- expectType<OutputBody>(undefined)
364
- expectType<OutputBody>(binaryValue)
365
- expectType<OutputBody>(lexValue)
366
-
367
- class Foo {
368
- constructor(readonly field: number = 3) {}
369
- }
370
- // @ts-expect-error
371
- expectType<OutputBody>(new Foo())
372
- })
373
-
374
- test('with payload schema', () => {
375
- const _query = l.query(
376
- 'com.example.query',
377
- l.params(),
378
- l.payload(
379
- 'application/json',
380
- l.object({
381
- cursor: l.optional(l.string()),
382
- feed: l.string({ format: 'at-uri' }),
383
- }),
384
- ),
385
- )
386
-
387
- type OutputBody = l.InferMethodOutputBody<typeof _query, BinaryValue>
388
-
389
- expectType<OutputBody>({
390
- cursor: 'abc123',
391
- feed: 'at://did:plc:abc123/app.bsky.feed.post/xyz',
392
- })
393
- // @ts-expect-error
394
- expectType<OutputBody>({ cursor: 123, feed: 'at://...' })
395
- // @ts-expect-error
396
- expectType<OutputBody>({ feed: 123 })
397
- })
398
-
399
- test('without payload schema', () => {
400
- const _query = l.query(
401
- 'com.example.query',
402
- l.params(),
403
- l.payload('*/*', undefined),
404
- )
405
-
406
- type OutputBody = l.InferMethodOutputBody<typeof _query, BinaryValue>
407
-
408
- expectType<OutputBody>(binaryValue)
409
- // @ts-expect-error
410
- expectType<OutputBody>(new Uint8Array())
411
- // @ts-expect-error
412
- expectType<OutputBody>({ data: 'foo' })
413
- })
414
- })
415
-
416
- describe('procedure', () => {
417
- test('with payload schema', () => {
418
- const _procedure = l.procedure(
419
- 'com.example.get',
420
- l.params(),
421
- l.payload(),
422
- l.payload(
423
- 'application/json',
424
- l.object({ uri: l.string({ format: 'at-uri' }) }),
425
- ),
426
- undefined,
427
- )
428
-
429
- type OutputBody = l.InferMethodOutputBody<typeof _procedure, BinaryValue>
430
-
431
- expectType<OutputBody>({ uri: 'at://did:plc:abc/post/123' })
432
- // @ts-expect-error
433
- expectType<OutputBody>({ uri: 123 })
434
- // @ts-expect-error
435
- expectType<OutputBody>(binaryValue)
436
- })
437
-
438
- test('without payload schema', () => {
439
- const _procedure = l.procedure(
440
- 'com.example.export',
441
- l.params(),
442
- l.payload(),
443
- l.payload('*/*', undefined),
444
- undefined,
445
- )
446
-
447
- type OutputBody = l.InferMethodOutputBody<typeof _procedure, BinaryValue>
448
-
449
- expectType<OutputBody>(binaryValue)
450
- // @ts-expect-error
451
- expectType<OutputBody>(new Uint8Array())
452
- // @ts-expect-error
453
- expectType<OutputBody>({ data: 'foo' })
454
- })
455
- })
456
- })
457
-
458
- describe('InferMethodOutputEncoding', () => {
459
- describe('query', () => {
460
- test('with payload schema', () => {
461
- const _query = l.query(
462
- 'com.example.query',
463
- l.params(),
464
- l.jsonPayload({ data: l.string() }),
465
- )
466
-
467
- type OutputEncoding = l.InferMethodOutputEncoding<typeof _query>
468
-
469
- expectType<OutputEncoding>('application/json')
470
- // @ts-expect-error
471
- expectType<OutputEncoding>('text/plain')
472
- // @ts-expect-error
473
- expectType<OutputEncoding>(123)
474
- })
475
-
476
- test('without payload schema', () => {
477
- const _query = l.query(
478
- 'com.example.query',
479
- l.params(),
480
- l.payload('*/*', undefined),
481
- )
482
-
483
- type OutputEncoding = l.InferMethodOutputEncoding<typeof _query>
484
-
485
- expectType<OutputEncoding>('image/png')
486
- expectType<OutputEncoding>('application/octet-stream')
487
- // @ts-expect-error
488
- expectType<OutputEncoding>(123)
489
- })
490
- })
491
-
492
- describe('procedure', () => {
493
- test('with payload schema', () => {
494
- const _procedure = l.procedure(
495
- 'com.example.create',
496
- l.params(),
497
- l.payload(),
498
- l.jsonPayload({ id: l.string() }),
499
- undefined,
500
- )
501
-
502
- type OutputEncoding = l.InferMethodOutputEncoding<typeof _procedure>
503
-
504
- expectType<OutputEncoding>('application/json')
505
- // @ts-expect-error
506
- expectType<OutputEncoding>('text/plain')
507
- // @ts-expect-error
508
- expectType<OutputEncoding>(123)
509
- })
510
-
511
- test('without payload schema', () => {
512
- const _procedure = l.procedure(
513
- 'com.example.export',
514
- l.params(),
515
- l.payload(),
516
- l.payload('*/*', undefined),
517
- undefined,
518
- )
519
-
520
- type OutputEncoding = l.InferMethodOutputEncoding<typeof _procedure>
521
-
522
- expectType<OutputEncoding>('application/zip')
523
- expectType<OutputEncoding>('application/octet-stream')
524
- // @ts-expect-error
525
- expectType<OutputEncoding>(123)
526
- })
527
- })
528
- })
529
-
530
- describe('InferMethodMessage', () => {
531
- describe('subscription', () => {
532
- test('with message schema', () => {
533
- const _subscription = l.subscription(
534
- 'com.example.subscribe',
535
- l.params(),
536
- l.object({
537
- seq: l.integer(),
538
- event: l.string(),
539
- }),
540
- )
541
-
542
- type Message = l.InferMethodMessage<typeof _subscription>
543
-
544
- expectType<Message>({ seq: 1, event: 'create' })
545
- // @ts-expect-error
546
- expectType<Message>({ seq: '1', event: 'create' })
547
- // @ts-expect-error
548
- expectType<Message>({ seq: 1 })
549
- // @ts-expect-error
550
- expectType<Message>(binaryValue)
551
- })
552
-
553
- test('without message schema', () => {
554
- const _subscription = l.subscription(
555
- 'com.example.subscribe',
556
- l.params(),
557
- l.integer(),
558
- )
559
-
560
- type Message = l.InferMethodMessage<typeof _subscription>
561
-
562
- // @ts-expect-error "unknown" is turned into LexValue
563
- expectType<Message>(undefined)
564
- // @ts-expect-error Not a LexValue
565
- expectType<Message>({ any: 'value' })
566
- expectType<Message>(123)
567
- })
568
- })
569
- })
570
-
571
- describe(l.atUri, () => {
572
- describe('string collection', () => {
573
- const did = 'did:example:alice'
574
-
575
- it('builds a valid record URI', () => {
576
- const uri = l.atUri(did, 'com.ex.foo', 'bar')
577
- expect(uri).toBe('at://did:example:alice/com.ex.foo/bar')
578
- expectTypeOf(uri).toEqualTypeOf<`at://did:example:alice/com.ex.foo/bar`>()
579
- })
580
-
581
- it('validates record key values', () => {
582
- expect(() => {
583
- // @ts-expect-error
584
- l.atUri(did, 'com.ex.foo', '.')
585
- }).toThrow()
586
- expect(() => {
587
- // @ts-expect-error
588
- l.atUri(did, 'com.ex.foo', '..')
589
- }).toThrow()
590
- })
591
-
592
- it('expects a second argument', () => {
593
- expect(() => {
594
- // @ts-expect-error
595
- l.atUri(did, 'com.ex.foo')
596
- }).toThrow()
597
- })
598
- })
599
-
600
- describe('literal', () => {
601
- const schema = l.record('literal:bar', 'com.ex.foo', l.object({}))
602
- const did = 'did:example:alice'
603
-
604
- it('allows omitting the record key for literal keys', () => {
605
- const uri = l.atUri(did, schema)
606
- expect(uri).toBe('at://did:example:alice/com.ex.foo/bar')
607
- expectTypeOf(uri).toEqualTypeOf<`at://did:example:alice/com.ex.foo/bar`>()
608
- })
609
-
610
- it('still allows providing the record key for literal keys', () => {
611
- expectTypeOf(
612
- l.atUri(did, schema, 'bar'),
613
- ).toEqualTypeOf<`at://did:example:alice/com.ex.foo/bar`>()
614
- })
615
-
616
- it('validates invalid record keys', () => {
617
- expect(() => {
618
- // @ts-expect-error
619
- l.atUri(did, schema, 'wrong')
620
- }).toThrow()
621
- })
622
- })
623
-
624
- describe('tid', () => {
625
- const schema = l.record('tid', 'com.ex.foo', l.object({}))
626
- const did = 'did:example:alice'
627
-
628
- it('builds a valid record URI', () => {
629
- const uri = l.atUri(did, schema, '3jzfcijpj2z2a')
630
- expectTypeOf(
631
- uri,
632
- ).toEqualTypeOf<`at://did:example:alice/com.ex.foo/3jzfcijpj2z2a`>()
633
- expect(uri).toBe('at://did:example:alice/com.ex.foo/3jzfcijpj2z2a')
634
- })
635
-
636
- it('expects a second argument for non-literal keys', () => {
637
- expect(() => {
638
- // @ts-expect-error
639
- l.atUri(did, schema)
640
- }).toThrow()
641
- })
642
-
643
- it('validates the record key value', () => {
644
- expect(() => {
645
- l.atUri(did, schema, 'invalid-tid')
646
- }).toThrow()
647
- })
648
- })
649
-
650
- describe('any', () => {
651
- const schema = l.record('any', 'com.ex.foo', l.object({}))
652
- const did = 'did:example:alice'
653
-
654
- it('builds a valid record URI valid keys', () => {
655
- const uri = l.atUri(did, schema, 'customKey')
656
- expect(uri).toBe('at://did:example:alice/com.ex.foo/customKey')
657
- expectTypeOf(
658
- uri,
659
- ).toEqualTypeOf<`at://did:example:alice/com.ex.foo/customKey`>()
660
- })
661
-
662
- it('rejects invalid record key values', () => {
663
- expect(() => {
664
- // @ts-expect-error
665
- l.atUri(did, schema, '.')
666
- }).toThrow()
667
- expect(() => {
668
- // @ts-expect-error
669
- l.atUri(did, schema, '..')
670
- }).toThrow()
671
- })
672
-
673
- it('expects a second argument', () => {
674
- expect(() => {
675
- // @ts-expect-error
676
- l.atUri(did, schema)
677
- }).toThrow()
678
- })
679
-
680
- describe('edge cases', () => {
681
- it('limits the record key to 512 characters', () => {
682
- const uri = l.atUri(did, schema, 'a'.repeat(512))
683
-
684
- expect(uri).toBe(`at://did:example:alice/com.ex.foo/${'a'.repeat(512)}`)
685
- expectTypeOf(
686
- uri,
687
- ).toEqualTypeOf<`at://did:example:alice/com.ex.foo/${string}`>()
688
- expect(() => {
689
- l.atUri(did, schema, 'a'.repeat(513))
690
- }).toThrow()
691
- })
692
- })
693
- })
694
- })