@atproto/lex-schema 0.0.11 → 0.0.13

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 (261) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/core/$type.d.ts +149 -0
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js +44 -0
  5. package/dist/core/$type.js.map +1 -1
  6. package/dist/core/record-key.d.ts +44 -0
  7. package/dist/core/record-key.d.ts.map +1 -1
  8. package/dist/core/record-key.js +30 -0
  9. package/dist/core/record-key.js.map +1 -1
  10. package/dist/core/result.d.ts +85 -4
  11. package/dist/core/result.d.ts.map +1 -1
  12. package/dist/core/result.js +60 -4
  13. package/dist/core/result.js.map +1 -1
  14. package/dist/core/schema.d.ts +232 -5
  15. package/dist/core/schema.d.ts.map +1 -1
  16. package/dist/core/schema.js +197 -4
  17. package/dist/core/schema.js.map +1 -1
  18. package/dist/core/string-format.d.ts +244 -11
  19. package/dist/core/string-format.d.ts.map +1 -1
  20. package/dist/core/string-format.js +150 -0
  21. package/dist/core/string-format.js.map +1 -1
  22. package/dist/core/types.d.ts +90 -3
  23. package/dist/core/types.d.ts.map +1 -1
  24. package/dist/core/types.js.map +1 -1
  25. package/dist/core/validation-error.d.ts +60 -0
  26. package/dist/core/validation-error.d.ts.map +1 -1
  27. package/dist/core/validation-error.js +60 -0
  28. package/dist/core/validation-error.js.map +1 -1
  29. package/dist/core/validation-issue.d.ts +61 -0
  30. package/dist/core/validation-issue.d.ts.map +1 -1
  31. package/dist/core/validation-issue.js +54 -1
  32. package/dist/core/validation-issue.js.map +1 -1
  33. package/dist/core/validator.d.ts +356 -11
  34. package/dist/core/validator.d.ts.map +1 -1
  35. package/dist/core/validator.js +203 -4
  36. package/dist/core/validator.js.map +1 -1
  37. package/dist/helpers.d.ts +12 -28
  38. package/dist/helpers.d.ts.map +1 -1
  39. package/dist/helpers.js.map +1 -1
  40. package/dist/schema/array.d.ts +46 -0
  41. package/dist/schema/array.d.ts.map +1 -1
  42. package/dist/schema/array.js +16 -1
  43. package/dist/schema/array.js.map +1 -1
  44. package/dist/schema/blob.d.ts +50 -2
  45. package/dist/schema/blob.d.ts.map +1 -1
  46. package/dist/schema/blob.js +44 -2
  47. package/dist/schema/blob.js.map +1 -1
  48. package/dist/schema/boolean.d.ts +29 -0
  49. package/dist/schema/boolean.d.ts.map +1 -1
  50. package/dist/schema/boolean.js +30 -1
  51. package/dist/schema/boolean.js.map +1 -1
  52. package/dist/schema/bytes.d.ts +39 -0
  53. package/dist/schema/bytes.d.ts.map +1 -1
  54. package/dist/schema/bytes.js +34 -1
  55. package/dist/schema/bytes.js.map +1 -1
  56. package/dist/schema/cid.d.ts +39 -0
  57. package/dist/schema/cid.d.ts.map +1 -1
  58. package/dist/schema/cid.js +35 -1
  59. package/dist/schema/cid.js.map +1 -1
  60. package/dist/schema/custom.d.ts +67 -1
  61. package/dist/schema/custom.d.ts.map +1 -1
  62. package/dist/schema/custom.js +55 -0
  63. package/dist/schema/custom.js.map +1 -1
  64. package/dist/schema/dict.d.ts +45 -0
  65. package/dist/schema/dict.d.ts.map +1 -1
  66. package/dist/schema/dict.js +46 -1
  67. package/dist/schema/dict.js.map +1 -1
  68. package/dist/schema/discriminated-union.d.ts +59 -0
  69. package/dist/schema/discriminated-union.d.ts.map +1 -1
  70. package/dist/schema/discriminated-union.js +47 -1
  71. package/dist/schema/discriminated-union.js.map +1 -1
  72. package/dist/schema/enum.d.ts +49 -0
  73. package/dist/schema/enum.d.ts.map +1 -1
  74. package/dist/schema/enum.js +49 -0
  75. package/dist/schema/enum.js.map +1 -1
  76. package/dist/schema/integer.d.ts +43 -0
  77. package/dist/schema/integer.d.ts.map +1 -1
  78. package/dist/schema/integer.js +38 -1
  79. package/dist/schema/integer.js.map +1 -1
  80. package/dist/schema/intersection.d.ts +55 -0
  81. package/dist/schema/intersection.d.ts.map +1 -1
  82. package/dist/schema/intersection.js +50 -0
  83. package/dist/schema/intersection.js.map +1 -1
  84. package/dist/schema/lex-map.d.ts +37 -0
  85. package/dist/schema/lex-map.d.ts.map +1 -0
  86. package/dist/schema/lex-map.js +60 -0
  87. package/dist/schema/lex-map.js.map +1 -0
  88. package/dist/schema/lex-value.d.ts +35 -0
  89. package/dist/schema/lex-value.d.ts.map +1 -0
  90. package/dist/schema/lex-value.js +87 -0
  91. package/dist/schema/lex-value.js.map +1 -0
  92. package/dist/schema/literal.d.ts +45 -0
  93. package/dist/schema/literal.d.ts.map +1 -1
  94. package/dist/schema/literal.js +45 -0
  95. package/dist/schema/literal.js.map +1 -1
  96. package/dist/schema/never.d.ts +43 -0
  97. package/dist/schema/never.d.ts.map +1 -1
  98. package/dist/schema/never.js +44 -1
  99. package/dist/schema/never.js.map +1 -1
  100. package/dist/schema/null.d.ts +30 -0
  101. package/dist/schema/null.d.ts.map +1 -1
  102. package/dist/schema/null.js +31 -1
  103. package/dist/schema/null.js.map +1 -1
  104. package/dist/schema/nullable.d.ts +42 -0
  105. package/dist/schema/nullable.d.ts.map +1 -1
  106. package/dist/schema/nullable.js +42 -0
  107. package/dist/schema/nullable.js.map +1 -1
  108. package/dist/schema/object.d.ts +57 -0
  109. package/dist/schema/object.d.ts.map +1 -1
  110. package/dist/schema/object.js +53 -1
  111. package/dist/schema/object.js.map +1 -1
  112. package/dist/schema/optional.d.ts +43 -0
  113. package/dist/schema/optional.d.ts.map +1 -1
  114. package/dist/schema/optional.js +43 -0
  115. package/dist/schema/optional.js.map +1 -1
  116. package/dist/schema/params.d.ts +96 -12
  117. package/dist/schema/params.d.ts.map +1 -1
  118. package/dist/schema/params.js +155 -21
  119. package/dist/schema/params.js.map +1 -1
  120. package/dist/schema/payload.d.ts +111 -15
  121. package/dist/schema/payload.d.ts.map +1 -1
  122. package/dist/schema/payload.js +73 -3
  123. package/dist/schema/payload.js.map +1 -1
  124. package/dist/schema/permission-set.d.ts +58 -0
  125. package/dist/schema/permission-set.d.ts.map +1 -1
  126. package/dist/schema/permission-set.js +50 -0
  127. package/dist/schema/permission-set.js.map +1 -1
  128. package/dist/schema/permission.d.ts +42 -0
  129. package/dist/schema/permission.d.ts.map +1 -1
  130. package/dist/schema/permission.js +39 -0
  131. package/dist/schema/permission.js.map +1 -1
  132. package/dist/schema/procedure.d.ts +64 -0
  133. package/dist/schema/procedure.d.ts.map +1 -1
  134. package/dist/schema/procedure.js +64 -0
  135. package/dist/schema/procedure.js.map +1 -1
  136. package/dist/schema/query.d.ts +55 -0
  137. package/dist/schema/query.d.ts.map +1 -1
  138. package/dist/schema/query.js +55 -0
  139. package/dist/schema/query.js.map +1 -1
  140. package/dist/schema/record.d.ts +76 -25
  141. package/dist/schema/record.d.ts.map +1 -1
  142. package/dist/schema/record.js +21 -0
  143. package/dist/schema/record.js.map +1 -1
  144. package/dist/schema/ref.d.ts +51 -0
  145. package/dist/schema/ref.d.ts.map +1 -1
  146. package/dist/schema/ref.js +18 -0
  147. package/dist/schema/ref.js.map +1 -1
  148. package/dist/schema/refine.d.ts +58 -9
  149. package/dist/schema/refine.d.ts.map +1 -1
  150. package/dist/schema/refine.js.map +1 -1
  151. package/dist/schema/regexp.d.ts +45 -0
  152. package/dist/schema/regexp.d.ts.map +1 -1
  153. package/dist/schema/regexp.js +46 -1
  154. package/dist/schema/regexp.js.map +1 -1
  155. package/dist/schema/string.d.ts +72 -6
  156. package/dist/schema/string.d.ts.map +1 -1
  157. package/dist/schema/string.js +56 -8
  158. package/dist/schema/string.js.map +1 -1
  159. package/dist/schema/subscription.d.ts +72 -2
  160. package/dist/schema/subscription.d.ts.map +1 -1
  161. package/dist/schema/subscription.js +59 -0
  162. package/dist/schema/subscription.js.map +1 -1
  163. package/dist/schema/token.d.ts +48 -0
  164. package/dist/schema/token.d.ts.map +1 -1
  165. package/dist/schema/token.js +49 -1
  166. package/dist/schema/token.js.map +1 -1
  167. package/dist/schema/typed-object.d.ts +73 -23
  168. package/dist/schema/typed-object.d.ts.map +1 -1
  169. package/dist/schema/typed-object.js +20 -1
  170. package/dist/schema/typed-object.js.map +1 -1
  171. package/dist/schema/typed-ref.d.ts +54 -0
  172. package/dist/schema/typed-ref.d.ts.map +1 -1
  173. package/dist/schema/typed-ref.js +16 -0
  174. package/dist/schema/typed-ref.js.map +1 -1
  175. package/dist/schema/typed-union.d.ts +51 -1
  176. package/dist/schema/typed-union.d.ts.map +1 -1
  177. package/dist/schema/typed-union.js +52 -2
  178. package/dist/schema/typed-union.js.map +1 -1
  179. package/dist/schema/union.d.ts +46 -0
  180. package/dist/schema/union.d.ts.map +1 -1
  181. package/dist/schema/union.js +41 -0
  182. package/dist/schema/union.js.map +1 -1
  183. package/dist/schema/unknown.d.ts +34 -0
  184. package/dist/schema/unknown.d.ts.map +1 -1
  185. package/dist/schema/unknown.js +34 -0
  186. package/dist/schema/unknown.js.map +1 -1
  187. package/dist/schema/with-default.d.ts +45 -0
  188. package/dist/schema/with-default.d.ts.map +1 -1
  189. package/dist/schema/with-default.js +45 -0
  190. package/dist/schema/with-default.js.map +1 -1
  191. package/dist/schema.d.ts +2 -1
  192. package/dist/schema.d.ts.map +1 -1
  193. package/dist/schema.js +2 -1
  194. package/dist/schema.js.map +1 -1
  195. package/dist/util/if-any.d.ts +2 -0
  196. package/dist/util/if-any.d.ts.map +1 -0
  197. package/dist/util/if-any.js +3 -0
  198. package/dist/util/if-any.js.map +1 -0
  199. package/package.json +3 -3
  200. package/src/core/$type.ts +150 -18
  201. package/src/core/record-key.ts +44 -0
  202. package/src/core/result.ts +86 -4
  203. package/src/core/schema.ts +244 -9
  204. package/src/core/string-format.ts +259 -13
  205. package/src/core/types.ts +91 -3
  206. package/src/core/validation-error.ts +60 -0
  207. package/src/core/validation-issue.ts +68 -2
  208. package/src/core/validator.ts +373 -12
  209. package/src/helpers.test.ts +110 -29
  210. package/src/helpers.ts +54 -25
  211. package/src/schema/array.test.ts +94 -79
  212. package/src/schema/array.ts +48 -1
  213. package/src/schema/blob.ts +50 -1
  214. package/src/schema/boolean.ts +31 -1
  215. package/src/schema/bytes.ts +41 -1
  216. package/src/schema/cid.ts +41 -1
  217. package/src/schema/custom.ts +68 -1
  218. package/src/schema/dict.ts +47 -1
  219. package/src/schema/discriminated-union.ts +61 -1
  220. package/src/schema/enum.ts +50 -0
  221. package/src/schema/integer.ts +45 -1
  222. package/src/schema/intersection.ts +56 -0
  223. package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
  224. package/src/schema/lex-map.ts +63 -0
  225. package/src/schema/lex-value.test.ts +81 -0
  226. package/src/schema/lex-value.ts +86 -0
  227. package/src/schema/literal.ts +46 -0
  228. package/src/schema/never.ts +45 -1
  229. package/src/schema/null.ts +32 -1
  230. package/src/schema/nullable.ts +43 -0
  231. package/src/schema/object.ts +59 -1
  232. package/src/schema/optional.ts +44 -0
  233. package/src/schema/params.test.ts +133 -38
  234. package/src/schema/params.ts +237 -37
  235. package/src/schema/payload.test.ts +3 -3
  236. package/src/schema/payload.ts +145 -42
  237. package/src/schema/permission-set.ts +58 -0
  238. package/src/schema/permission.ts +42 -0
  239. package/src/schema/procedure.ts +64 -0
  240. package/src/schema/query.ts +55 -0
  241. package/src/schema/record.ts +82 -16
  242. package/src/schema/ref.ts +52 -0
  243. package/src/schema/refine.ts +58 -9
  244. package/src/schema/regexp.ts +47 -1
  245. package/src/schema/string.test.ts +99 -2
  246. package/src/schema/string.ts +108 -15
  247. package/src/schema/subscription.ts +72 -2
  248. package/src/schema/token.ts +50 -1
  249. package/src/schema/typed-object.ts +81 -16
  250. package/src/schema/typed-ref.ts +55 -0
  251. package/src/schema/typed-union.ts +58 -3
  252. package/src/schema/union.ts +47 -0
  253. package/src/schema/unknown.ts +35 -0
  254. package/src/schema/with-default.ts +46 -0
  255. package/src/schema.ts +2 -1
  256. package/src/util/if-any.ts +3 -0
  257. package/dist/schema/unknown-object.d.ts +0 -8
  258. package/dist/schema/unknown-object.d.ts.map +0 -1
  259. package/dist/schema/unknown-object.js +0 -19
  260. package/dist/schema/unknown-object.js.map +0 -1
  261. package/src/schema/unknown-object.ts +0 -19
@@ -0,0 +1,2 @@
1
+ export type IfAny<T, TrueValue, FalseValue> = 0 extends 1 & T ? TrueValue : FalseValue;
2
+ //# sourceMappingURL=if-any.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"if-any.d.ts","sourceRoot":"","sources":["../../src/util/if-any.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,SAAS,EAAE,UAAU,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GACzD,SAAS,GACT,UAAU,CAAA"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=if-any.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"if-any.js","sourceRoot":"","sources":["../../src/util/if-any.ts"],"names":[],"mappings":"","sourcesContent":["export type IfAny<T, TrueValue, FalseValue> = 0 extends 1 & T\n ? TrueValue\n : FalseValue\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-schema",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon schema system for AT Lexicons",
6
6
  "keywords": [
@@ -31,13 +31,13 @@
31
31
  "types": "./dist/index.d.ts",
32
32
  "browser": "./dist/index.js",
33
33
  "import": "./dist/index.js",
34
- "require": "./dist/index.js"
34
+ "default": "./dist/index.js"
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
38
  "tslib": "^2.8.1",
39
39
  "@atproto/syntax": "^0.4.3",
40
- "@atproto/lex-data": "^0.0.10"
40
+ "@atproto/lex-data": "^0.0.12"
41
41
  },
42
42
  "devDependencies": {
43
43
  "vitest": "^4.0.16"
package/src/core/$type.ts CHANGED
@@ -1,6 +1,25 @@
1
1
  import { NsidString } from './string-format.js'
2
2
  import { OmitKey, Simplify } from './types.js'
3
3
 
4
+ /**
5
+ * Constructs the `$type` string type for a given NSID and hash.
6
+ *
7
+ * The `$type` value identifies a schema definition within a lexicon:
8
+ * - For "main" definitions: just the NSID (e.g., `'app.bsky.feed.post'`)
9
+ * - For named definitions: NSID + hash + name (e.g., `'app.bsky.feed.defs#postView'`)
10
+ *
11
+ * @typeParam N - The NSID string type
12
+ * @typeParam H - The hash/definition name (use `'main'` for the main definition)
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * type MainType = $Type<'app.bsky.feed.post', 'main'>
17
+ * // Result: 'app.bsky.feed.post'
18
+ *
19
+ * type DefType = $Type<'app.bsky.feed.defs', 'postView'>
20
+ * // Result: 'app.bsky.feed.defs#postView'
21
+ * ```
22
+ */
4
23
  export type $Type<
5
24
  N extends NsidString = NsidString,
6
25
  H extends string = string,
@@ -12,8 +31,41 @@ export type $Type<
12
31
  : `${N}#${H}`
13
32
  : never
14
33
 
34
+ /**
35
+ * Extracts the `$type` string type from an object type.
36
+ *
37
+ * @typeParam O - An object type with an optional `$type` property
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * type Post = { $type: 'app.bsky.feed.post'; text: string }
42
+ * type PostType = $TypeOf<Post>
43
+ * // Result: 'app.bsky.feed.post'
44
+ * ```
45
+ */
15
46
  export type $TypeOf<O extends { $type?: string }> = NonNullable<O['$type']>
16
47
 
48
+ /**
49
+ * Constructs a `$type` string value from an NSID and definition name.
50
+ *
51
+ * For the "main" definition, returns just the NSID. For named definitions,
52
+ * returns the NSID followed by `#` and the definition name.
53
+ *
54
+ * @typeParam N - The NSID string type
55
+ * @typeParam H - The definition name type
56
+ * @param nsid - The NSID of the lexicon
57
+ * @param hash - The definition name within the lexicon (use `'main'` for the main definition)
58
+ * @returns The constructed `$type` string
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * $type('app.bsky.feed.post', 'main')
63
+ * // Returns: 'app.bsky.feed.post'
64
+ *
65
+ * $type('app.bsky.feed.defs', 'postView')
66
+ * // Returns: 'app.bsky.feed.defs#postView'
67
+ * ```
68
+ */
17
69
  /*@__NO_SIDE_EFFECTS__*/
18
70
  export function $type<N extends NsidString, H extends string>(
19
71
  nsid: N,
@@ -22,12 +74,50 @@ export function $type<N extends NsidString, H extends string>(
22
74
  return (hash === 'main' ? nsid : `${nsid}#${hash}`) as $Type<N, H>
23
75
  }
24
76
 
77
+ /**
78
+ * Represents an object with a required `$type` property.
79
+ *
80
+ * This type adds a `$type` property to an existing object type, useful for
81
+ * representing typed AT Protocol objects.
82
+ *
83
+ * @typeParam V - The base object type
84
+ * @typeParam T - The `$type` string literal type
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * type Post = $Typed<{ text: string; createdAt: string }, 'app.bsky.feed.post'>
89
+ * // Result: { $type: 'app.bsky.feed.post'; text: string; createdAt: string }
90
+ * ```
91
+ */
25
92
  export type $Typed<V, T extends string = string> = Simplify<
26
93
  V & {
27
94
  $type: T
28
95
  }
29
96
  >
30
97
 
98
+ /**
99
+ * Ensures an object has the specified `$type` property.
100
+ *
101
+ * If the object already has the correct `$type`, returns it unchanged.
102
+ * Otherwise, creates a new object with the `$type` property added.
103
+ *
104
+ * @typeParam V - The object type (may already have `$type`)
105
+ * @typeParam T - The expected `$type` string
106
+ * @param value - The object to add `$type` to
107
+ * @param $type - The `$type` value to ensure
108
+ * @returns The object with the `$type` property
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const post = $typed({ text: 'hello' }, 'app.bsky.feed.post')
113
+ * // Result: { $type: 'app.bsky.feed.post', text: 'hello' }
114
+ *
115
+ * // If already typed, returns same object
116
+ * const typed = { $type: 'app.bsky.feed.post', text: 'hello' }
117
+ * const same = $typed(typed, 'app.bsky.feed.post')
118
+ * console.log(typed === same) // true
119
+ * ```
120
+ */
31
121
  export function $typed<V extends { $type?: unknown }, T extends string>(
32
122
  value: V,
33
123
  $type: T,
@@ -35,33 +125,75 @@ export function $typed<V extends { $type?: unknown }, T extends string>(
35
125
  return value.$type === $type ? (value as $Typed<V, T>) : { ...value, $type }
36
126
  }
37
127
 
128
+ /**
129
+ * Represents an object with an optional `$type` property.
130
+ *
131
+ * This is used for objects that may or may not have type information,
132
+ * such as input parameters that accept both typed and untyped values.
133
+ *
134
+ * @typeParam V - The base object type
135
+ * @typeParam T - The optional `$type` string literal type
136
+ */
38
137
  export type $TypedMaybe<V, T extends string = string> = Simplify<
39
138
  V & {
40
139
  $type?: T
41
140
  }
42
141
  >
43
142
 
143
+ /**
144
+ * Removes the `$type` property from an object type.
145
+ *
146
+ * Useful for extracting the "content" of a typed object without the type marker.
147
+ *
148
+ * @typeParam V - An object type with an optional `$type` property
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * type Post = { $type: 'app.bsky.feed.post'; text: string }
153
+ * type PostContent = Un$Typed<Post>
154
+ * // Result: { text: string }
155
+ * ```
156
+ */
44
157
  export type Un$Typed<V extends { $type?: string }> = OmitKey<V, '$type'>
45
158
 
159
+ /**
160
+ * Unique symbol for branding unknown `$type` strings.
161
+ * @internal
162
+ */
46
163
  declare const unknown$TypeSymbol: unique symbol
164
+
165
+ /**
166
+ * Represents an unknown or unrecognized `$type` string.
167
+ *
168
+ * This branded type is used in union types to distinguish between
169
+ * known typed objects and unknown typed objects (from open unions).
170
+ * The branding prevents accidentally matching known `$type` values.
171
+ */
47
172
  export type Unknown$Type = string & { [unknown$TypeSymbol]: true }
48
173
 
49
- // In order to prevent places that expect a union of known and unknown $typed
50
- // objects (like lexicons schema open unions), from accepting an invalid version
51
- // of the known $typed objects, we need to prevent any other properties from
52
- // being present.
53
- //
54
- // For example, if we expect:
55
- // ```ts
56
- // type MyOpenUnion = { $type: 'A'; a: number } | Unknown$TypedObject
57
- // ```
58
- // we want to make that that the following is rejected:
59
- // ```ts
60
- // { $type: 'A' }
61
- // ```
62
- //
63
- // If we typed `Unknown$TypedObject` as `{ $type: string }`, `{ $type: 'A' }`
64
- // would be a valid `MyOpenUnion` as it would match the `Unknown$TypedObject`.
65
- // By using a $type property that uniquely describes unknown values, we ensure
66
- // that only valid known typed objects, or a type casted value, can be used.
174
+ /**
175
+ * Represents an object with an unknown `$type` value.
176
+ *
177
+ * This type is used in open union schemas to represent typed objects that
178
+ * don't match any of the known types. The {@link Unknown$Type} branding ensures
179
+ * that invalid instances of known types don't accidentally match this type.
180
+ *
181
+ * For example, in an open union like:
182
+ * ```typescript
183
+ * type MyOpenUnion = { $type: 'A'; a: number } | Unknown$TypedObject
184
+ * ```
185
+ *
186
+ * A value `{ $type: 'A' }` (missing the required `a` property) will NOT match
187
+ * `Unknown$TypedObject` because `'A'` is not assignable to `Unknown$Type`.
188
+ * This ensures that malformed instances of known types are properly rejected.
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * // This represents any typed object we don't recognize
193
+ * const unknownTyped: Unknown$TypedObject = {
194
+ * $type: 'some.unknown.type' as Unknown$Type,
195
+ * // ... arbitrary properties
196
+ * }
197
+ * ```
198
+ */
67
199
  export type Unknown$TypedObject = { $type: Unknown$Type }
@@ -1,7 +1,36 @@
1
1
  import { isValidRecordKey } from '@atproto/syntax'
2
2
 
3
+ /**
4
+ * The valid record key constraint types in a lexicon definition.
5
+ *
6
+ * - `'any'` - Accepts any valid record key
7
+ * - `'nsid'` - Record key must be a valid NSID
8
+ * - `'tid'` - Record key must be a valid TID
9
+ * - `'literal:...'` - Record key must be the exact specified value
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const constraint: LexiconRecordKey = 'tid'
14
+ * const literalConstraint: LexiconRecordKey = 'literal:self'
15
+ * ```
16
+ */
3
17
  export type LexiconRecordKey = 'any' | 'nsid' | 'tid' | `literal:${string}`
4
18
 
19
+ /**
20
+ * Type guard that checks if a value is a valid lexicon record key constraint.
21
+ *
22
+ * @typeParam T - The input type
23
+ * @param key - The value to check
24
+ * @returns `true` if the value is a valid record key constraint
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * if (isLexiconRecordKey(value)) {
29
+ * // value is typed as LexiconRecordKey
30
+ * console.log('Valid constraint:', value)
31
+ * }
32
+ * ```
33
+ */
5
34
  /*@__NO_SIDE_EFFECTS__*/
6
35
  export function isLexiconRecordKey<T>(key: T): key is T & LexiconRecordKey {
7
36
  return (
@@ -15,6 +44,21 @@ export function isLexiconRecordKey<T>(key: T): key is T & LexiconRecordKey {
15
44
  )
16
45
  }
17
46
 
47
+ /**
48
+ * Validates and returns a value as a lexicon record key constraint, throwing if invalid.
49
+ *
50
+ * @param key - The value to validate
51
+ * @returns The value typed as {@link LexiconRecordKey}
52
+ * @throws {Error} If the value is not a valid record key constraint
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const constraint = asLexiconRecordKey('tid')
57
+ * // constraint is typed as LexiconRecordKey
58
+ *
59
+ * asLexiconRecordKey('invalid') // throws Error
60
+ * ```
61
+ */
18
62
  /*@__NO_SIDE_EFFECTS__*/
19
63
  export function asLexiconRecordKey(key: unknown): LexiconRecordKey {
20
64
  if (isLexiconRecordKey(key)) return key
@@ -1,23 +1,105 @@
1
1
  export type ResultSuccess<V = any> = { success: true; value: V }
2
+
3
+ /**
4
+ * Represents a failed result containing an error reason.
5
+ *
6
+ * @typeParam E - The type of the error reason
7
+ */
2
8
  export type ResultFailure<E = Error> = { success: false; reason: E }
3
9
 
10
+ /**
11
+ * A discriminated union type representing either a success or failure outcome.
12
+ *
13
+ * Check the `success` property to determine the outcome and access the
14
+ * appropriate property (`value` for success, `reason` for failure).
15
+ *
16
+ * @typeParam V - The type of the success value
17
+ * @typeParam E - The type of the error reason
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * function parseJson(text: string): Result<unknown, SyntaxError> {
22
+ * try {
23
+ * return success(JSON.parse(text))
24
+ * } catch (e) {
25
+ * return failure(e as SyntaxError)
26
+ * }
27
+ * }
28
+ * ```
29
+ */
4
30
  export type Result<V = any, E = Error> = ResultSuccess<V> | ResultFailure<E>
5
31
 
32
+ /**
33
+ * Creates a successful result wrapping the given value.
34
+ *
35
+ * @typeParam V - The type of the value
36
+ * @param value - The success value to wrap
37
+ * @returns {ResultSuccess} A success result containing the value
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const result = success(42)
42
+ * console.log(result.success) // true
43
+ * console.log(result.value) // 42
44
+ * ```
45
+ */
6
46
  /*@__NO_SIDE_EFFECTS__*/
7
47
  export function success<V>(value: V): ResultSuccess<V> {
8
48
  return { success: true, value }
9
49
  }
10
50
 
51
+ /**
52
+ * Creates a failed result wrapping the given error reason.
53
+ *
54
+ * @typeParam E - The type of the error reason
55
+ * @param reason - The error reason to wrap
56
+ * @returns {ResultFailure} A failure result containing the error
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const result = failure(new Error('Something went wrong'))
61
+ * console.log(result.success) // false
62
+ * console.log(result.reason.message) // "Something went wrong"
63
+ * ```
64
+ */
11
65
  /*@__NO_SIDE_EFFECTS__*/
12
66
  export function failure<E>(reason: E): ResultFailure<E> {
13
67
  return { success: false, reason }
14
68
  }
15
69
 
70
+ /**
71
+ * Extracts the error reason from a failure result.
72
+ *
73
+ * @typeParam T - The type of the error reason
74
+ * @param result - A failure result
75
+ * @returns {T} The error reason
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const result = failure(new Error('oops'))
80
+ * const error = failureReason(result)
81
+ * console.log(error.message) // "oops"
82
+ * ```
83
+ */
16
84
  /*@__NO_SIDE_EFFECTS__*/
17
85
  export function failureReason<T>(result: ResultFailure<T>): T {
18
86
  return result.reason
19
87
  }
20
88
 
89
+ /**
90
+ * Extracts the value from a success result.
91
+ *
92
+ * @typeParam T - The type of the success value
93
+ * @param result - A success result
94
+ * @returns {T} The success value
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const result = success(42)
99
+ * const value = successValue(result)
100
+ * console.log(value) // 42
101
+ * ```
102
+ */
21
103
  /*@__NO_SIDE_EFFECTS__*/
22
104
  export function successValue<T>(result: ResultSuccess<T>): T {
23
105
  return result.value
@@ -27,7 +109,7 @@ export function successValue<T>(result: ResultSuccess<T>): T {
27
109
  * Catches any error and wraps it in a {@link ResultFailure<Error>}.
28
110
  *
29
111
  * @param err - The error to catch.
30
- * @returns A {@link ResultFailure<Error>} containing the caught error.
112
+ * @returns {ResultFailure} A failure result containing the error.
31
113
  * @example
32
114
  *
33
115
  * ```ts
@@ -37,8 +119,8 @@ export function successValue<T>(result: ResultSuccess<T>): T {
37
119
  * if (result.success) {
38
120
  * console.log(result.value) // string
39
121
  * } else {
40
- * console.error(result.error instanceof Error) // true
41
- * console.error(result.error.message) // string
122
+ * console.error(result.reason instanceof Error) // true
123
+ * console.error(result.reason.message) // string
42
124
  * }
43
125
  * ```
44
126
  */
@@ -68,7 +150,7 @@ export function catchall(err: unknown): ResultFailure<Error> {
68
150
  * if (result.success) {
69
151
  * console.log(result.value) // string
70
152
  * } else {
71
- * console.error(result.error) // FooError | BarError
153
+ * console.error(result.reason) // FooError | BarError
72
154
  * }
73
155
  */
74
156
  /*@__NO_SIDE_EFFECTS__*/