@atproto/lex-schema 0.0.10 → 0.0.12

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 (239) hide show
  1. package/CHANGELOG.md +26 -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 +229 -2
  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 +61 -1
  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 +51 -0
  32. package/dist/core/validation-issue.js.map +1 -1
  33. package/dist/core/validator.d.ts +347 -10
  34. package/dist/core/validator.d.ts.map +1 -1
  35. package/dist/core/validator.js +184 -3
  36. package/dist/core/validator.js.map +1 -1
  37. package/dist/helpers.d.ts +13 -25
  38. package/dist/helpers.d.ts.map +1 -1
  39. package/dist/helpers.js +2 -2
  40. package/dist/helpers.js.map +1 -1
  41. package/dist/schema/array.d.ts +45 -0
  42. package/dist/schema/array.d.ts.map +1 -1
  43. package/dist/schema/array.js +14 -0
  44. package/dist/schema/array.js.map +1 -1
  45. package/dist/schema/blob.d.ts +46 -0
  46. package/dist/schema/blob.d.ts.map +1 -1
  47. package/dist/schema/blob.js +39 -0
  48. package/dist/schema/blob.js.map +1 -1
  49. package/dist/schema/boolean.d.ts +28 -0
  50. package/dist/schema/boolean.d.ts.map +1 -1
  51. package/dist/schema/boolean.js +28 -0
  52. package/dist/schema/boolean.js.map +1 -1
  53. package/dist/schema/bytes.d.ts +38 -0
  54. package/dist/schema/bytes.d.ts.map +1 -1
  55. package/dist/schema/bytes.js +32 -0
  56. package/dist/schema/bytes.js.map +1 -1
  57. package/dist/schema/cid.d.ts +38 -0
  58. package/dist/schema/cid.d.ts.map +1 -1
  59. package/dist/schema/cid.js +33 -0
  60. package/dist/schema/cid.js.map +1 -1
  61. package/dist/schema/custom.d.ts +66 -1
  62. package/dist/schema/custom.d.ts.map +1 -1
  63. package/dist/schema/custom.js +54 -0
  64. package/dist/schema/custom.js.map +1 -1
  65. package/dist/schema/dict.d.ts +44 -0
  66. package/dist/schema/dict.d.ts.map +1 -1
  67. package/dist/schema/dict.js +44 -0
  68. package/dist/schema/dict.js.map +1 -1
  69. package/dist/schema/discriminated-union.d.ts +58 -0
  70. package/dist/schema/discriminated-union.d.ts.map +1 -1
  71. package/dist/schema/discriminated-union.js +45 -0
  72. package/dist/schema/discriminated-union.js.map +1 -1
  73. package/dist/schema/enum.d.ts +48 -0
  74. package/dist/schema/enum.d.ts.map +1 -1
  75. package/dist/schema/enum.js +48 -0
  76. package/dist/schema/enum.js.map +1 -1
  77. package/dist/schema/integer.d.ts +42 -0
  78. package/dist/schema/integer.d.ts.map +1 -1
  79. package/dist/schema/integer.js +36 -0
  80. package/dist/schema/integer.js.map +1 -1
  81. package/dist/schema/intersection.d.ts +54 -0
  82. package/dist/schema/intersection.d.ts.map +1 -1
  83. package/dist/schema/intersection.js +49 -0
  84. package/dist/schema/intersection.js.map +1 -1
  85. package/dist/schema/literal.d.ts +44 -0
  86. package/dist/schema/literal.d.ts.map +1 -1
  87. package/dist/schema/literal.js +44 -0
  88. package/dist/schema/literal.js.map +1 -1
  89. package/dist/schema/never.d.ts +42 -0
  90. package/dist/schema/never.d.ts.map +1 -1
  91. package/dist/schema/never.js +42 -0
  92. package/dist/schema/never.js.map +1 -1
  93. package/dist/schema/null.d.ts +29 -0
  94. package/dist/schema/null.d.ts.map +1 -1
  95. package/dist/schema/null.js +29 -0
  96. package/dist/schema/null.js.map +1 -1
  97. package/dist/schema/nullable.d.ts +41 -0
  98. package/dist/schema/nullable.d.ts.map +1 -1
  99. package/dist/schema/nullable.js +41 -0
  100. package/dist/schema/nullable.js.map +1 -1
  101. package/dist/schema/object.d.ts +56 -0
  102. package/dist/schema/object.d.ts.map +1 -1
  103. package/dist/schema/object.js +51 -0
  104. package/dist/schema/object.js.map +1 -1
  105. package/dist/schema/optional.d.ts +42 -0
  106. package/dist/schema/optional.d.ts.map +1 -1
  107. package/dist/schema/optional.js +42 -0
  108. package/dist/schema/optional.js.map +1 -1
  109. package/dist/schema/params.d.ts +89 -7
  110. package/dist/schema/params.d.ts.map +1 -1
  111. package/dist/schema/params.js +84 -10
  112. package/dist/schema/params.js.map +1 -1
  113. package/dist/schema/payload.d.ts +111 -15
  114. package/dist/schema/payload.d.ts.map +1 -1
  115. package/dist/schema/payload.js +70 -0
  116. package/dist/schema/payload.js.map +1 -1
  117. package/dist/schema/permission-set.d.ts +58 -0
  118. package/dist/schema/permission-set.d.ts.map +1 -1
  119. package/dist/schema/permission-set.js +50 -0
  120. package/dist/schema/permission-set.js.map +1 -1
  121. package/dist/schema/permission.d.ts +42 -0
  122. package/dist/schema/permission.d.ts.map +1 -1
  123. package/dist/schema/permission.js +39 -0
  124. package/dist/schema/permission.js.map +1 -1
  125. package/dist/schema/procedure.d.ts +64 -0
  126. package/dist/schema/procedure.d.ts.map +1 -1
  127. package/dist/schema/procedure.js +64 -0
  128. package/dist/schema/procedure.js.map +1 -1
  129. package/dist/schema/query.d.ts +55 -0
  130. package/dist/schema/query.d.ts.map +1 -1
  131. package/dist/schema/query.js +55 -0
  132. package/dist/schema/query.js.map +1 -1
  133. package/dist/schema/record.d.ts +63 -8
  134. package/dist/schema/record.d.ts.map +1 -1
  135. package/dist/schema/record.js +20 -0
  136. package/dist/schema/record.js.map +1 -1
  137. package/dist/schema/ref.d.ts +50 -0
  138. package/dist/schema/ref.d.ts.map +1 -1
  139. package/dist/schema/ref.js +17 -0
  140. package/dist/schema/ref.js.map +1 -1
  141. package/dist/schema/refine.d.ts +58 -9
  142. package/dist/schema/refine.d.ts.map +1 -1
  143. package/dist/schema/refine.js.map +1 -1
  144. package/dist/schema/regexp.d.ts +44 -0
  145. package/dist/schema/regexp.d.ts.map +1 -1
  146. package/dist/schema/regexp.js +44 -0
  147. package/dist/schema/regexp.js.map +1 -1
  148. package/dist/schema/string.d.ts +50 -0
  149. package/dist/schema/string.d.ts.map +1 -1
  150. package/dist/schema/string.js +41 -0
  151. package/dist/schema/string.js.map +1 -1
  152. package/dist/schema/subscription.d.ts +72 -2
  153. package/dist/schema/subscription.d.ts.map +1 -1
  154. package/dist/schema/subscription.js +59 -0
  155. package/dist/schema/subscription.js.map +1 -1
  156. package/dist/schema/token.d.ts +47 -0
  157. package/dist/schema/token.d.ts.map +1 -1
  158. package/dist/schema/token.js +47 -0
  159. package/dist/schema/token.js.map +1 -1
  160. package/dist/schema/typed-object.d.ts +62 -8
  161. package/dist/schema/typed-object.d.ts.map +1 -1
  162. package/dist/schema/typed-object.js +18 -0
  163. package/dist/schema/typed-object.js.map +1 -1
  164. package/dist/schema/typed-ref.d.ts +53 -0
  165. package/dist/schema/typed-ref.d.ts.map +1 -1
  166. package/dist/schema/typed-ref.js +15 -0
  167. package/dist/schema/typed-ref.js.map +1 -1
  168. package/dist/schema/typed-union.d.ts +50 -1
  169. package/dist/schema/typed-union.d.ts.map +1 -1
  170. package/dist/schema/typed-union.js +50 -1
  171. package/dist/schema/typed-union.js.map +1 -1
  172. package/dist/schema/union.d.ts +45 -0
  173. package/dist/schema/union.d.ts.map +1 -1
  174. package/dist/schema/union.js +40 -0
  175. package/dist/schema/union.js.map +1 -1
  176. package/dist/schema/unknown-object.d.ts +34 -0
  177. package/dist/schema/unknown-object.d.ts.map +1 -1
  178. package/dist/schema/unknown-object.js +31 -0
  179. package/dist/schema/unknown-object.js.map +1 -1
  180. package/dist/schema/unknown.d.ts +33 -0
  181. package/dist/schema/unknown.d.ts.map +1 -1
  182. package/dist/schema/unknown.js +33 -0
  183. package/dist/schema/unknown.js.map +1 -1
  184. package/dist/schema/with-default.d.ts +44 -0
  185. package/dist/schema/with-default.d.ts.map +1 -1
  186. package/dist/schema/with-default.js +44 -0
  187. package/dist/schema/with-default.js.map +1 -1
  188. package/package.json +4 -4
  189. package/src/core/$type.ts +150 -18
  190. package/src/core/record-key.ts +44 -0
  191. package/src/core/result.ts +86 -4
  192. package/src/core/schema.ts +236 -7
  193. package/src/core/string-format.ts +259 -13
  194. package/src/core/types.ts +91 -3
  195. package/src/core/validation-error.ts +60 -0
  196. package/src/core/validation-issue.ts +65 -0
  197. package/src/core/validator.ts +351 -10
  198. package/src/helpers.test.ts +110 -29
  199. package/src/helpers.ts +14 -14
  200. package/src/schema/array.test.ts +94 -79
  201. package/src/schema/array.ts +45 -0
  202. package/src/schema/blob.ts +46 -0
  203. package/src/schema/boolean.ts +28 -0
  204. package/src/schema/bytes.ts +38 -0
  205. package/src/schema/cid.ts +38 -0
  206. package/src/schema/custom.ts +66 -1
  207. package/src/schema/dict.ts +44 -0
  208. package/src/schema/discriminated-union.ts +58 -0
  209. package/src/schema/enum.ts +48 -0
  210. package/src/schema/integer.ts +42 -0
  211. package/src/schema/intersection.ts +54 -0
  212. package/src/schema/literal.ts +44 -0
  213. package/src/schema/never.ts +42 -0
  214. package/src/schema/null.ts +29 -0
  215. package/src/schema/nullable.ts +41 -0
  216. package/src/schema/object.ts +56 -0
  217. package/src/schema/optional.ts +42 -0
  218. package/src/schema/params.test.ts +58 -2
  219. package/src/schema/params.ts +124 -16
  220. package/src/schema/payload.test.ts +3 -3
  221. package/src/schema/payload.ts +142 -38
  222. package/src/schema/permission-set.ts +58 -0
  223. package/src/schema/permission.ts +42 -0
  224. package/src/schema/procedure.ts +64 -0
  225. package/src/schema/query.ts +55 -0
  226. package/src/schema/record.ts +63 -8
  227. package/src/schema/ref.ts +50 -0
  228. package/src/schema/refine.ts +58 -9
  229. package/src/schema/regexp.ts +44 -0
  230. package/src/schema/string.ts +50 -0
  231. package/src/schema/subscription.ts +72 -2
  232. package/src/schema/token.ts +47 -0
  233. package/src/schema/typed-object.ts +62 -8
  234. package/src/schema/typed-ref.ts +53 -0
  235. package/src/schema/typed-union.ts +55 -2
  236. package/src/schema/union.ts +45 -0
  237. package/src/schema/unknown-object.ts +34 -0
  238. package/src/schema/unknown.ts +33 -0
  239. package/src/schema/with-default.ts +44 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @atproto/lex-schema
2
2
 
3
+ ## 0.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4603](https://github.com/bluesky-social/atproto/pull/4603) [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Constrain XRPC `Payload` to be `LexValue` instead of `unknown` (better reflecting reality)
8
+
9
+ - [#4603](https://github.com/bluesky-social/atproto/pull/4603) [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Properly infer type of generic `Payload` body
10
+
11
+ - [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix `exports` field in package.json
12
+
13
+ - [#4603](https://github.com/bluesky-social/atproto/pull/4603) [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Properly coerce params into arrays when defined as such
14
+
15
+ - [#4603](https://github.com/bluesky-social/atproto/pull/4603) [`7b9a98a`](https://github.com/bluesky-social/atproto/commit/7b9a98a763636c5f66a06da11fe6013f29dd9157) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Constrain subscription `message` schema to validate `LexValue` only
16
+
17
+ - [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add JSDoc
18
+
19
+ - Updated dependencies [[`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015)]:
20
+ - @atproto/lex-data@0.0.11
21
+
22
+ ## 0.0.11
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies [[`369bb02`](https://github.com/bluesky-social/atproto/commit/369bb02b9f80f0e15e5242e54f09bd4e01117f3a)]:
27
+ - @atproto/lex-data@0.0.10
28
+
3
29
  ## 0.0.10
4
30
 
5
31
  ### Patch Changes
@@ -1,26 +1,175 @@
1
1
  import { NsidString } from './string-format.js';
2
2
  import { OmitKey, Simplify } from './types.js';
3
+ /**
4
+ * Constructs the `$type` string type for a given NSID and hash.
5
+ *
6
+ * The `$type` value identifies a schema definition within a lexicon:
7
+ * - For "main" definitions: just the NSID (e.g., `'app.bsky.feed.post'`)
8
+ * - For named definitions: NSID + hash + name (e.g., `'app.bsky.feed.defs#postView'`)
9
+ *
10
+ * @typeParam N - The NSID string type
11
+ * @typeParam H - The hash/definition name (use `'main'` for the main definition)
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * type MainType = $Type<'app.bsky.feed.post', 'main'>
16
+ * // Result: 'app.bsky.feed.post'
17
+ *
18
+ * type DefType = $Type<'app.bsky.feed.defs', 'postView'>
19
+ * // Result: 'app.bsky.feed.defs#postView'
20
+ * ```
21
+ */
3
22
  export type $Type<N extends NsidString = NsidString, H extends string = string> = N extends NsidString ? string extends H ? N | `${N}#${string}` : H extends 'main' ? N : `${N}#${H}` : never;
23
+ /**
24
+ * Extracts the `$type` string type from an object type.
25
+ *
26
+ * @typeParam O - An object type with an optional `$type` property
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * type Post = { $type: 'app.bsky.feed.post'; text: string }
31
+ * type PostType = $TypeOf<Post>
32
+ * // Result: 'app.bsky.feed.post'
33
+ * ```
34
+ */
4
35
  export type $TypeOf<O extends {
5
36
  $type?: string;
6
37
  }> = NonNullable<O['$type']>;
38
+ /**
39
+ * Constructs a `$type` string value from an NSID and definition name.
40
+ *
41
+ * For the "main" definition, returns just the NSID. For named definitions,
42
+ * returns the NSID followed by `#` and the definition name.
43
+ *
44
+ * @typeParam N - The NSID string type
45
+ * @typeParam H - The definition name type
46
+ * @param nsid - The NSID of the lexicon
47
+ * @param hash - The definition name within the lexicon (use `'main'` for the main definition)
48
+ * @returns The constructed `$type` string
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * $type('app.bsky.feed.post', 'main')
53
+ * // Returns: 'app.bsky.feed.post'
54
+ *
55
+ * $type('app.bsky.feed.defs', 'postView')
56
+ * // Returns: 'app.bsky.feed.defs#postView'
57
+ * ```
58
+ */
7
59
  export declare function $type<N extends NsidString, H extends string>(nsid: N, hash: H): $Type<N, H>;
60
+ /**
61
+ * Represents an object with a required `$type` property.
62
+ *
63
+ * This type adds a `$type` property to an existing object type, useful for
64
+ * representing typed AT Protocol objects.
65
+ *
66
+ * @typeParam V - The base object type
67
+ * @typeParam T - The `$type` string literal type
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * type Post = $Typed<{ text: string; createdAt: string }, 'app.bsky.feed.post'>
72
+ * // Result: { $type: 'app.bsky.feed.post'; text: string; createdAt: string }
73
+ * ```
74
+ */
8
75
  export type $Typed<V, T extends string = string> = Simplify<V & {
9
76
  $type: T;
10
77
  }>;
78
+ /**
79
+ * Ensures an object has the specified `$type` property.
80
+ *
81
+ * If the object already has the correct `$type`, returns it unchanged.
82
+ * Otherwise, creates a new object with the `$type` property added.
83
+ *
84
+ * @typeParam V - The object type (may already have `$type`)
85
+ * @typeParam T - The expected `$type` string
86
+ * @param value - The object to add `$type` to
87
+ * @param $type - The `$type` value to ensure
88
+ * @returns The object with the `$type` property
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const post = $typed({ text: 'hello' }, 'app.bsky.feed.post')
93
+ * // Result: { $type: 'app.bsky.feed.post', text: 'hello' }
94
+ *
95
+ * // If already typed, returns same object
96
+ * const typed = { $type: 'app.bsky.feed.post', text: 'hello' }
97
+ * const same = $typed(typed, 'app.bsky.feed.post')
98
+ * console.log(typed === same) // true
99
+ * ```
100
+ */
11
101
  export declare function $typed<V extends {
12
102
  $type?: unknown;
13
103
  }, T extends string>(value: V, $type: T): $Typed<V, T>;
104
+ /**
105
+ * Represents an object with an optional `$type` property.
106
+ *
107
+ * This is used for objects that may or may not have type information,
108
+ * such as input parameters that accept both typed and untyped values.
109
+ *
110
+ * @typeParam V - The base object type
111
+ * @typeParam T - The optional `$type` string literal type
112
+ */
14
113
  export type $TypedMaybe<V, T extends string = string> = Simplify<V & {
15
114
  $type?: T;
16
115
  }>;
116
+ /**
117
+ * Removes the `$type` property from an object type.
118
+ *
119
+ * Useful for extracting the "content" of a typed object without the type marker.
120
+ *
121
+ * @typeParam V - An object type with an optional `$type` property
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * type Post = { $type: 'app.bsky.feed.post'; text: string }
126
+ * type PostContent = Un$Typed<Post>
127
+ * // Result: { text: string }
128
+ * ```
129
+ */
17
130
  export type Un$Typed<V extends {
18
131
  $type?: string;
19
132
  }> = OmitKey<V, '$type'>;
133
+ /**
134
+ * Unique symbol for branding unknown `$type` strings.
135
+ * @internal
136
+ */
20
137
  declare const unknown$TypeSymbol: unique symbol;
138
+ /**
139
+ * Represents an unknown or unrecognized `$type` string.
140
+ *
141
+ * This branded type is used in union types to distinguish between
142
+ * known typed objects and unknown typed objects (from open unions).
143
+ * The branding prevents accidentally matching known `$type` values.
144
+ */
21
145
  export type Unknown$Type = string & {
22
146
  [unknown$TypeSymbol]: true;
23
147
  };
148
+ /**
149
+ * Represents an object with an unknown `$type` value.
150
+ *
151
+ * This type is used in open union schemas to represent typed objects that
152
+ * don't match any of the known types. The {@link Unknown$Type} branding ensures
153
+ * that invalid instances of known types don't accidentally match this type.
154
+ *
155
+ * For example, in an open union like:
156
+ * ```typescript
157
+ * type MyOpenUnion = { $type: 'A'; a: number } | Unknown$TypedObject
158
+ * ```
159
+ *
160
+ * A value `{ $type: 'A' }` (missing the required `a` property) will NOT match
161
+ * `Unknown$TypedObject` because `'A'` is not assignable to `Unknown$Type`.
162
+ * This ensures that malformed instances of known types are properly rejected.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * // This represents any typed object we don't recognize
167
+ * const unknownTyped: Unknown$TypedObject = {
168
+ * $type: 'some.unknown.type' as Unknown$Type,
169
+ * // ... arbitrary properties
170
+ * }
171
+ * ```
172
+ */
24
173
  export type Unknown$TypedObject = {
25
174
  $type: Unknown$Type;
26
175
  };
@@ -1 +1 @@
1
- {"version":3,"file":"$type.d.ts","sourceRoot":"","sources":["../../src/core/$type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAE9C,MAAM,MAAM,KAAK,CACf,CAAC,SAAS,UAAU,GAAG,UAAU,EACjC,CAAC,SAAS,MAAM,GAAG,MAAM,IACvB,CAAC,SAAS,U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CAAA;CAAE,EAAE,CAAC,SAAS,MAAM,EACpE,KAAK,EAAE,CAAC,EACR,KAAK,EAAE,CAAC,GACP,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAEd;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,QAAQ,CAC9D,CAAC,GAAG;IACF,KAAK,CAAC,EAAE,CAAC,CAAA;CACV,CACF,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,IAAI,OAAO,CAAC,CAAC,EAAE,OAAO,CAAC,CAAA;AAExE;;;GAGG;AACH,OAAO,CAAC,MAAM,kBAAkB,EAAE,OAAO,MAAM,CAAA;AAE/C;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,CAAC,kBAAkB,CAAC,EAAE,IAAI,CAAA;CAAE,CAAA;AAElE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAAE,KAAK,EAAE,YAAY,CAAA;CAAE,CAAA"}
@@ -2,10 +2,54 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.$type = $type;
4
4
  exports.$typed = $typed;
5
+ /**
6
+ * Constructs a `$type` string value from an NSID and definition name.
7
+ *
8
+ * For the "main" definition, returns just the NSID. For named definitions,
9
+ * returns the NSID followed by `#` and the definition name.
10
+ *
11
+ * @typeParam N - The NSID string type
12
+ * @typeParam H - The definition name type
13
+ * @param nsid - The NSID of the lexicon
14
+ * @param hash - The definition name within the lexicon (use `'main'` for the main definition)
15
+ * @returns The constructed `$type` string
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * $type('app.bsky.feed.post', 'main')
20
+ * // Returns: 'app.bsky.feed.post'
21
+ *
22
+ * $type('app.bsky.feed.defs', 'postView')
23
+ * // Returns: 'app.bsky.feed.defs#postView'
24
+ * ```
25
+ */
5
26
  /*@__NO_SIDE_EFFECTS__*/
6
27
  function $type(nsid, hash) {
7
28
  return (hash === 'main' ? nsid : `${nsid}#${hash}`);
8
29
  }
30
+ /**
31
+ * Ensures an object has the specified `$type` property.
32
+ *
33
+ * If the object already has the correct `$type`, returns it unchanged.
34
+ * Otherwise, creates a new object with the `$type` property added.
35
+ *
36
+ * @typeParam V - The object type (may already have `$type`)
37
+ * @typeParam T - The expected `$type` string
38
+ * @param value - The object to add `$type` to
39
+ * @param $type - The `$type` value to ensure
40
+ * @returns The object with the `$type` property
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const post = $typed({ text: 'hello' }, 'app.bsky.feed.post')
45
+ * // Result: { $type: 'app.bsky.feed.post', text: 'hello' }
46
+ *
47
+ * // If already typed, returns same object
48
+ * const typed = { $type: 'app.bsky.feed.post', text: 'hello' }
49
+ * const same = $typed(typed, 'app.bsky.feed.post')
50
+ * console.log(typed === same) // true
51
+ * ```
52
+ */
9
53
  function $typed(value, $type) {
10
54
  return value.$type === $type ? value : { ...value, $type };
11
55
  }
@@ -1 +1 @@
1
- {"version":3,"file":"$type.js","sourceRoot":"","sources":["../../src/core/$type.ts"],"names":[],"mappings":";;AAiBA,sBAKC;AAQD,wBAKC;AAnBD,wBAAwB;AACxB,SAAgB,KAAK,CACnB,IAAO,EACP,IAAO;IAEP,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAgB,CAAA;AACpE,CAAC;AAQD,SAAgB,MAAM,CACpB,KAAQ,EACR,KAAQ;IAER,OAAO,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAE,KAAsB,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,CAAA;AAC9E,CAAC","sourcesContent":["import { NsidString } from './string-format.js'\nimport { OmitKey, Simplify } from './types.js'\n\nexport type $Type<\n N extends NsidString = NsidString,\n H extends string = string,\n> = N extends NsidString\n ? string extends H\n ? N | `${N}#${string}`\n : H extends 'main'\n ? N\n : `${N}#${H}`\n : never\n\nexport type $TypeOf<O extends { $type?: string }> = NonNullable<O['$type']>\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function $type<N extends NsidString, H extends string>(\n nsid: N,\n hash: H,\n): $Type<N, H> {\n return (hash === 'main' ? nsid : `${nsid}#${hash}`) as $Type<N, H>\n}\n\nexport type $Typed<V, T extends string = string> = Simplify<\n V & {\n $type: T\n }\n>\n\nexport function $typed<V extends { $type?: unknown }, T extends string>(\n value: V,\n $type: T,\n): $Typed<V, T> {\n return value.$type === $type ? (value as $Typed<V, T>) : { ...value, $type }\n}\n\nexport type $TypedMaybe<V, T extends string = string> = Simplify<\n V & {\n $type?: T\n }\n>\n\nexport type Un$Typed<V extends { $type?: string }> = OmitKey<V, '$type'>\n\ndeclare const unknown$TypeSymbol: unique symbol\nexport type Unknown$Type = string & { [unknown$TypeSymbol]: true }\n\n// In order to prevent places that expect a union of known and unknown $typed\n// objects (like lexicons schema open unions), from accepting an invalid version\n// of the known $typed objects, we need to prevent any other properties from\n// being present.\n//\n// For example, if we expect:\n// ```ts\n// type MyOpenUnion = { $type: 'A'; a: number } | Unknown$TypedObject\n// ```\n// we want to make that that the following is rejected:\n// ```ts\n// { $type: 'A' }\n// ```\n//\n// If we typed `Unknown$TypedObject` as `{ $type: string }`, `{ $type: 'A' }`\n// would be a valid `MyOpenUnion` as it would match the `Unknown$TypedObject`.\n// By using a $type property that uniquely describes unknown values, we ensure\n// that only valid known typed objects, or a type casted value, can be used.\nexport type Unknown$TypedObject = { $type: Unknown$Type }\n"]}
1
+ {"version":3,"file":"$type.js","sourceRoot":"","sources":["../../src/core/$type.ts"],"names":[],"mappings":";;AAqEA,sBAKC;AA8CD,wBAKC;AA9ED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAwB;AACxB,SAAgB,KAAK,CACnB,IAAO,EACP,IAAO;IAEP,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAgB,CAAA;AACpE,CAAC;AAuBD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,MAAM,CACpB,KAAQ,EACR,KAAQ;IAER,OAAO,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAE,KAAsB,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,CAAA;AAC9E,CAAC","sourcesContent":["import { NsidString } from './string-format.js'\nimport { OmitKey, Simplify } from './types.js'\n\n/**\n * Constructs the `$type` string type for a given NSID and hash.\n *\n * The `$type` value identifies a schema definition within a lexicon:\n * - For \"main\" definitions: just the NSID (e.g., `'app.bsky.feed.post'`)\n * - For named definitions: NSID + hash + name (e.g., `'app.bsky.feed.defs#postView'`)\n *\n * @typeParam N - The NSID string type\n * @typeParam H - The hash/definition name (use `'main'` for the main definition)\n *\n * @example\n * ```typescript\n * type MainType = $Type<'app.bsky.feed.post', 'main'>\n * // Result: 'app.bsky.feed.post'\n *\n * type DefType = $Type<'app.bsky.feed.defs', 'postView'>\n * // Result: 'app.bsky.feed.defs#postView'\n * ```\n */\nexport type $Type<\n N extends NsidString = NsidString,\n H extends string = string,\n> = N extends NsidString\n ? string extends H\n ? N | `${N}#${string}`\n : H extends 'main'\n ? N\n : `${N}#${H}`\n : never\n\n/**\n * Extracts the `$type` string type from an object type.\n *\n * @typeParam O - An object type with an optional `$type` property\n *\n * @example\n * ```typescript\n * type Post = { $type: 'app.bsky.feed.post'; text: string }\n * type PostType = $TypeOf<Post>\n * // Result: 'app.bsky.feed.post'\n * ```\n */\nexport type $TypeOf<O extends { $type?: string }> = NonNullable<O['$type']>\n\n/**\n * Constructs a `$type` string value from an NSID and definition name.\n *\n * For the \"main\" definition, returns just the NSID. For named definitions,\n * returns the NSID followed by `#` and the definition name.\n *\n * @typeParam N - The NSID string type\n * @typeParam H - The definition name type\n * @param nsid - The NSID of the lexicon\n * @param hash - The definition name within the lexicon (use `'main'` for the main definition)\n * @returns The constructed `$type` string\n *\n * @example\n * ```typescript\n * $type('app.bsky.feed.post', 'main')\n * // Returns: 'app.bsky.feed.post'\n *\n * $type('app.bsky.feed.defs', 'postView')\n * // Returns: 'app.bsky.feed.defs#postView'\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function $type<N extends NsidString, H extends string>(\n nsid: N,\n hash: H,\n): $Type<N, H> {\n return (hash === 'main' ? nsid : `${nsid}#${hash}`) as $Type<N, H>\n}\n\n/**\n * Represents an object with a required `$type` property.\n *\n * This type adds a `$type` property to an existing object type, useful for\n * representing typed AT Protocol objects.\n *\n * @typeParam V - The base object type\n * @typeParam T - The `$type` string literal type\n *\n * @example\n * ```typescript\n * type Post = $Typed<{ text: string; createdAt: string }, 'app.bsky.feed.post'>\n * // Result: { $type: 'app.bsky.feed.post'; text: string; createdAt: string }\n * ```\n */\nexport type $Typed<V, T extends string = string> = Simplify<\n V & {\n $type: T\n }\n>\n\n/**\n * Ensures an object has the specified `$type` property.\n *\n * If the object already has the correct `$type`, returns it unchanged.\n * Otherwise, creates a new object with the `$type` property added.\n *\n * @typeParam V - The object type (may already have `$type`)\n * @typeParam T - The expected `$type` string\n * @param value - The object to add `$type` to\n * @param $type - The `$type` value to ensure\n * @returns The object with the `$type` property\n *\n * @example\n * ```typescript\n * const post = $typed({ text: 'hello' }, 'app.bsky.feed.post')\n * // Result: { $type: 'app.bsky.feed.post', text: 'hello' }\n *\n * // If already typed, returns same object\n * const typed = { $type: 'app.bsky.feed.post', text: 'hello' }\n * const same = $typed(typed, 'app.bsky.feed.post')\n * console.log(typed === same) // true\n * ```\n */\nexport function $typed<V extends { $type?: unknown }, T extends string>(\n value: V,\n $type: T,\n): $Typed<V, T> {\n return value.$type === $type ? (value as $Typed<V, T>) : { ...value, $type }\n}\n\n/**\n * Represents an object with an optional `$type` property.\n *\n * This is used for objects that may or may not have type information,\n * such as input parameters that accept both typed and untyped values.\n *\n * @typeParam V - The base object type\n * @typeParam T - The optional `$type` string literal type\n */\nexport type $TypedMaybe<V, T extends string = string> = Simplify<\n V & {\n $type?: T\n }\n>\n\n/**\n * Removes the `$type` property from an object type.\n *\n * Useful for extracting the \"content\" of a typed object without the type marker.\n *\n * @typeParam V - An object type with an optional `$type` property\n *\n * @example\n * ```typescript\n * type Post = { $type: 'app.bsky.feed.post'; text: string }\n * type PostContent = Un$Typed<Post>\n * // Result: { text: string }\n * ```\n */\nexport type Un$Typed<V extends { $type?: string }> = OmitKey<V, '$type'>\n\n/**\n * Unique symbol for branding unknown `$type` strings.\n * @internal\n */\ndeclare const unknown$TypeSymbol: unique symbol\n\n/**\n * Represents an unknown or unrecognized `$type` string.\n *\n * This branded type is used in union types to distinguish between\n * known typed objects and unknown typed objects (from open unions).\n * The branding prevents accidentally matching known `$type` values.\n */\nexport type Unknown$Type = string & { [unknown$TypeSymbol]: true }\n\n/**\n * Represents an object with an unknown `$type` value.\n *\n * This type is used in open union schemas to represent typed objects that\n * don't match any of the known types. The {@link Unknown$Type} branding ensures\n * that invalid instances of known types don't accidentally match this type.\n *\n * For example, in an open union like:\n * ```typescript\n * type MyOpenUnion = { $type: 'A'; a: number } | Unknown$TypedObject\n * ```\n *\n * A value `{ $type: 'A' }` (missing the required `a` property) will NOT match\n * `Unknown$TypedObject` because `'A'` is not assignable to `Unknown$Type`.\n * This ensures that malformed instances of known types are properly rejected.\n *\n * @example\n * ```typescript\n * // This represents any typed object we don't recognize\n * const unknownTyped: Unknown$TypedObject = {\n * $type: 'some.unknown.type' as Unknown$Type,\n * // ... arbitrary properties\n * }\n * ```\n */\nexport type Unknown$TypedObject = { $type: Unknown$Type }\n"]}
@@ -1,4 +1,48 @@
1
+ /**
2
+ * The valid record key constraint types in a lexicon definition.
3
+ *
4
+ * - `'any'` - Accepts any valid record key
5
+ * - `'nsid'` - Record key must be a valid NSID
6
+ * - `'tid'` - Record key must be a valid TID
7
+ * - `'literal:...'` - Record key must be the exact specified value
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const constraint: LexiconRecordKey = 'tid'
12
+ * const literalConstraint: LexiconRecordKey = 'literal:self'
13
+ * ```
14
+ */
1
15
  export type LexiconRecordKey = 'any' | 'nsid' | 'tid' | `literal:${string}`;
16
+ /**
17
+ * Type guard that checks if a value is a valid lexicon record key constraint.
18
+ *
19
+ * @typeParam T - The input type
20
+ * @param key - The value to check
21
+ * @returns `true` if the value is a valid record key constraint
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * if (isLexiconRecordKey(value)) {
26
+ * // value is typed as LexiconRecordKey
27
+ * console.log('Valid constraint:', value)
28
+ * }
29
+ * ```
30
+ */
2
31
  export declare function isLexiconRecordKey<T>(key: T): key is T & LexiconRecordKey;
32
+ /**
33
+ * Validates and returns a value as a lexicon record key constraint, throwing if invalid.
34
+ *
35
+ * @param key - The value to validate
36
+ * @returns The value typed as {@link LexiconRecordKey}
37
+ * @throws {Error} If the value is not a valid record key constraint
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const constraint = asLexiconRecordKey('tid')
42
+ * // constraint is typed as LexiconRecordKey
43
+ *
44
+ * asLexiconRecordKey('invalid') // throws Error
45
+ * ```
46
+ */
3
47
  export declare function asLexiconRecordKey(key: unknown): LexiconRecordKey;
4
48
  //# sourceMappingURL=record-key.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"record-key.d.ts","sourceRoot":"","sources":["../../src/core/record-key.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,WAAW,MAAM,EAAE,CAAA;AAG3E,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,gBAAgB,CAUzE;AAGD,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,CAGjE"}
1
+ {"version":3,"file":"record-key.d.ts","sourceRoot":"","sources":["../../src/core/record-key.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,WAAW,MAAM,EAAE,CAAA;AAE3E;;;;;;;;;;;;;;GAcG;AAEH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,gBAAgB,CAUzE;AAED;;;;;;;;;;;;;;GAcG;AAEH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,CAGjE"}
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isLexiconRecordKey = isLexiconRecordKey;
4
4
  exports.asLexiconRecordKey = asLexiconRecordKey;
5
5
  const syntax_1 = require("@atproto/syntax");
6
+ /**
7
+ * Type guard that checks if a value is a valid lexicon record key constraint.
8
+ *
9
+ * @typeParam T - The input type
10
+ * @param key - The value to check
11
+ * @returns `true` if the value is a valid record key constraint
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * if (isLexiconRecordKey(value)) {
16
+ * // value is typed as LexiconRecordKey
17
+ * console.log('Valid constraint:', value)
18
+ * }
19
+ * ```
20
+ */
6
21
  /*@__NO_SIDE_EFFECTS__*/
7
22
  function isLexiconRecordKey(key) {
8
23
  return (key === 'any' ||
@@ -13,6 +28,21 @@ function isLexiconRecordKey(key) {
13
28
  key.length > 8 &&
14
29
  (0, syntax_1.isValidRecordKey)(key.slice(8))));
15
30
  }
31
+ /**
32
+ * Validates and returns a value as a lexicon record key constraint, throwing if invalid.
33
+ *
34
+ * @param key - The value to validate
35
+ * @returns The value typed as {@link LexiconRecordKey}
36
+ * @throws {Error} If the value is not a valid record key constraint
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * const constraint = asLexiconRecordKey('tid')
41
+ * // constraint is typed as LexiconRecordKey
42
+ *
43
+ * asLexiconRecordKey('invalid') // throws Error
44
+ * ```
45
+ */
16
46
  /*@__NO_SIDE_EFFECTS__*/
17
47
  function asLexiconRecordKey(key) {
18
48
  if (isLexiconRecordKey(key))
@@ -1 +1 @@
1
- {"version":3,"file":"record-key.js","sourceRoot":"","sources":["../../src/core/record-key.ts"],"names":[],"mappings":";;AAKA,gDAUC;AAGD,gDAGC;AArBD,4CAAkD;AAIlD,wBAAwB;AACxB,SAAgB,kBAAkB,CAAI,GAAM;IAC1C,OAAO,CACL,GAAG,KAAK,KAAK;QACb,GAAG,KAAK,MAAM;QACd,GAAG,KAAK,KAAK;QACb,CAAC,OAAO,GAAG,KAAK,QAAQ;YACtB,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC;YAC1B,GAAG,CAAC,MAAM,GAAG,CAAC;YACd,IAAA,yBAAgB,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAClC,CAAA;AACH,CAAC;AAED,wBAAwB;AACxB,SAAgB,kBAAkB,CAAC,GAAY;IAC7C,IAAI,kBAAkB,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAA;IACvC,MAAM,IAAI,KAAK,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;AACvD,CAAC","sourcesContent":["import { isValidRecordKey } from '@atproto/syntax'\n\nexport type LexiconRecordKey = 'any' | 'nsid' | 'tid' | `literal:${string}`\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function isLexiconRecordKey<T>(key: T): key is T & LexiconRecordKey {\n return (\n key === 'any' ||\n key === 'nsid' ||\n key === 'tid' ||\n (typeof key === 'string' &&\n key.startsWith('literal:') &&\n key.length > 8 &&\n isValidRecordKey(key.slice(8)))\n )\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function asLexiconRecordKey(key: unknown): LexiconRecordKey {\n if (isLexiconRecordKey(key)) return key\n throw new Error(`Invalid record key: ${String(key)}`)\n}\n"]}
1
+ {"version":3,"file":"record-key.js","sourceRoot":"","sources":["../../src/core/record-key.ts"],"names":[],"mappings":";;AAkCA,gDAUC;AAkBD,gDAGC;AAjED,4CAAkD;AAkBlD;;;;;;;;;;;;;;GAcG;AACH,wBAAwB;AACxB,SAAgB,kBAAkB,CAAI,GAAM;IAC1C,OAAO,CACL,GAAG,KAAK,KAAK;QACb,GAAG,KAAK,MAAM;QACd,GAAG,KAAK,KAAK;QACb,CAAC,OAAO,GAAG,KAAK,QAAQ;YACtB,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC;YAC1B,GAAG,CAAC,MAAM,GAAG,CAAC;YACd,IAAA,yBAAgB,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAClC,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAwB;AACxB,SAAgB,kBAAkB,CAAC,GAAY;IAC7C,IAAI,kBAAkB,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAA;IACvC,MAAM,IAAI,KAAK,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;AACvD,CAAC","sourcesContent":["import { isValidRecordKey } from '@atproto/syntax'\n\n/**\n * The valid record key constraint types in a lexicon definition.\n *\n * - `'any'` - Accepts any valid record key\n * - `'nsid'` - Record key must be a valid NSID\n * - `'tid'` - Record key must be a valid TID\n * - `'literal:...'` - Record key must be the exact specified value\n *\n * @example\n * ```typescript\n * const constraint: LexiconRecordKey = 'tid'\n * const literalConstraint: LexiconRecordKey = 'literal:self'\n * ```\n */\nexport type LexiconRecordKey = 'any' | 'nsid' | 'tid' | `literal:${string}`\n\n/**\n * Type guard that checks if a value is a valid lexicon record key constraint.\n *\n * @typeParam T - The input type\n * @param key - The value to check\n * @returns `true` if the value is a valid record key constraint\n *\n * @example\n * ```typescript\n * if (isLexiconRecordKey(value)) {\n * // value is typed as LexiconRecordKey\n * console.log('Valid constraint:', value)\n * }\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function isLexiconRecordKey<T>(key: T): key is T & LexiconRecordKey {\n return (\n key === 'any' ||\n key === 'nsid' ||\n key === 'tid' ||\n (typeof key === 'string' &&\n key.startsWith('literal:') &&\n key.length > 8 &&\n isValidRecordKey(key.slice(8)))\n )\n}\n\n/**\n * Validates and returns a value as a lexicon record key constraint, throwing if invalid.\n *\n * @param key - The value to validate\n * @returns The value typed as {@link LexiconRecordKey}\n * @throws {Error} If the value is not a valid record key constraint\n *\n * @example\n * ```typescript\n * const constraint = asLexiconRecordKey('tid')\n * // constraint is typed as LexiconRecordKey\n *\n * asLexiconRecordKey('invalid') // throws Error\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function asLexiconRecordKey(key: unknown): LexiconRecordKey {\n if (isLexiconRecordKey(key)) return key\n throw new Error(`Invalid record key: ${String(key)}`)\n}\n"]}
@@ -2,20 +2,101 @@ export type ResultSuccess<V = any> = {
2
2
  success: true;
3
3
  value: V;
4
4
  };
5
+ /**
6
+ * Represents a failed result containing an error reason.
7
+ *
8
+ * @typeParam E - The type of the error reason
9
+ */
5
10
  export type ResultFailure<E = Error> = {
6
11
  success: false;
7
12
  reason: E;
8
13
  };
14
+ /**
15
+ * A discriminated union type representing either a success or failure outcome.
16
+ *
17
+ * Check the `success` property to determine the outcome and access the
18
+ * appropriate property (`value` for success, `reason` for failure).
19
+ *
20
+ * @typeParam V - The type of the success value
21
+ * @typeParam E - The type of the error reason
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * function parseJson(text: string): Result<unknown, SyntaxError> {
26
+ * try {
27
+ * return success(JSON.parse(text))
28
+ * } catch (e) {
29
+ * return failure(e as SyntaxError)
30
+ * }
31
+ * }
32
+ * ```
33
+ */
9
34
  export type Result<V = any, E = Error> = ResultSuccess<V> | ResultFailure<E>;
35
+ /**
36
+ * Creates a successful result wrapping the given value.
37
+ *
38
+ * @typeParam V - The type of the value
39
+ * @param value - The success value to wrap
40
+ * @returns {ResultSuccess} A success result containing the value
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const result = success(42)
45
+ * console.log(result.success) // true
46
+ * console.log(result.value) // 42
47
+ * ```
48
+ */
10
49
  export declare function success<V>(value: V): ResultSuccess<V>;
50
+ /**
51
+ * Creates a failed result wrapping the given error reason.
52
+ *
53
+ * @typeParam E - The type of the error reason
54
+ * @param reason - The error reason to wrap
55
+ * @returns {ResultFailure} A failure result containing the error
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const result = failure(new Error('Something went wrong'))
60
+ * console.log(result.success) // false
61
+ * console.log(result.reason.message) // "Something went wrong"
62
+ * ```
63
+ */
11
64
  export declare function failure<E>(reason: E): ResultFailure<E>;
65
+ /**
66
+ * Extracts the error reason from a failure result.
67
+ *
68
+ * @typeParam T - The type of the error reason
69
+ * @param result - A failure result
70
+ * @returns {T} The error reason
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const result = failure(new Error('oops'))
75
+ * const error = failureReason(result)
76
+ * console.log(error.message) // "oops"
77
+ * ```
78
+ */
12
79
  export declare function failureReason<T>(result: ResultFailure<T>): T;
80
+ /**
81
+ * Extracts the value from a success result.
82
+ *
83
+ * @typeParam T - The type of the success value
84
+ * @param result - A success result
85
+ * @returns {T} The success value
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const result = success(42)
90
+ * const value = successValue(result)
91
+ * console.log(value) // 42
92
+ * ```
93
+ */
13
94
  export declare function successValue<T>(result: ResultSuccess<T>): T;
14
95
  /**
15
96
  * Catches any error and wraps it in a {@link ResultFailure<Error>}.
16
97
  *
17
98
  * @param err - The error to catch.
18
- * @returns A {@link ResultFailure<Error>} containing the caught error.
99
+ * @returns {ResultFailure} A failure result containing the error.
19
100
  * @example
20
101
  *
21
102
  * ```ts
@@ -25,8 +106,8 @@ export declare function successValue<T>(result: ResultSuccess<T>): T;
25
106
  * if (result.success) {
26
107
  * console.log(result.value) // string
27
108
  * } else {
28
- * console.error(result.error instanceof Error) // true
29
- * console.error(result.error.message) // string
109
+ * console.error(result.reason instanceof Error) // true
110
+ * console.error(result.reason.message) // string
30
111
  * }
31
112
  * ```
32
113
  */
@@ -51,7 +132,7 @@ export declare function catchall(err: unknown): ResultFailure<Error>;
51
132
  * if (result.success) {
52
133
  * console.log(result.value) // string
53
134
  * } else {
54
- * console.error(result.error) // FooError | BarError
135
+ * console.error(result.reason) // FooError | BarError
55
136
  * }
56
137
  */
57
138
  export declare function createCatcher<T>(Ctor: new (...args: any[]) => T): (err: unknown) => ResultFailure<T>;
@@ -1 +1 @@
1
- {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,CAAC,CAAC,GAAG,GAAG,IAAI;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAA;AAChE,MAAM,MAAM,aAAa,CAAC,CAAC,GAAG,KAAK,IAAI;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,CAAC,CAAA;CAAE,CAAA;AAEpE,MAAM,MAAM,MAAM,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,KAAK,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;AAG5E,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAErD;AAGD,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAEtD;AAGD,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAE5D;AAGD,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAE3D;AAED;;;;;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MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAE3D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AAEH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,aAAa,CAAC,KAAK,CAAC,CAG3D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,wBAAgB,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,IACtD,KAAK,OAAO,KAAG,aAAa,CAAC,CAAC,CAAC,CAIxC"}
@@ -6,18 +6,74 @@ exports.failureReason = failureReason;
6
6
  exports.successValue = successValue;
7
7
  exports.catchall = catchall;
8
8
  exports.createCatcher = createCatcher;
9
+ /**
10
+ * Creates a successful result wrapping the given value.
11
+ *
12
+ * @typeParam V - The type of the value
13
+ * @param value - The success value to wrap
14
+ * @returns {ResultSuccess} A success result containing the value
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const result = success(42)
19
+ * console.log(result.success) // true
20
+ * console.log(result.value) // 42
21
+ * ```
22
+ */
9
23
  /*@__NO_SIDE_EFFECTS__*/
10
24
  function success(value) {
11
25
  return { success: true, value };
12
26
  }
27
+ /**
28
+ * Creates a failed result wrapping the given error reason.
29
+ *
30
+ * @typeParam E - The type of the error reason
31
+ * @param reason - The error reason to wrap
32
+ * @returns {ResultFailure} A failure result containing the error
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const result = failure(new Error('Something went wrong'))
37
+ * console.log(result.success) // false
38
+ * console.log(result.reason.message) // "Something went wrong"
39
+ * ```
40
+ */
13
41
  /*@__NO_SIDE_EFFECTS__*/
14
42
  function failure(reason) {
15
43
  return { success: false, reason };
16
44
  }
45
+ /**
46
+ * Extracts the error reason from a failure result.
47
+ *
48
+ * @typeParam T - The type of the error reason
49
+ * @param result - A failure result
50
+ * @returns {T} The error reason
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const result = failure(new Error('oops'))
55
+ * const error = failureReason(result)
56
+ * console.log(error.message) // "oops"
57
+ * ```
58
+ */
17
59
  /*@__NO_SIDE_EFFECTS__*/
18
60
  function failureReason(result) {
19
61
  return result.reason;
20
62
  }
63
+ /**
64
+ * Extracts the value from a success result.
65
+ *
66
+ * @typeParam T - The type of the success value
67
+ * @param result - A success result
68
+ * @returns {T} The success value
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const result = success(42)
73
+ * const value = successValue(result)
74
+ * console.log(value) // 42
75
+ * ```
76
+ */
21
77
  /*@__NO_SIDE_EFFECTS__*/
22
78
  function successValue(result) {
23
79
  return result.value;
@@ -26,7 +82,7 @@ function successValue(result) {
26
82
  * Catches any error and wraps it in a {@link ResultFailure<Error>}.
27
83
  *
28
84
  * @param err - The error to catch.
29
- * @returns A {@link ResultFailure<Error>} containing the caught error.
85
+ * @returns {ResultFailure} A failure result containing the error.
30
86
  * @example
31
87
  *
32
88
  * ```ts
@@ -36,8 +92,8 @@ function successValue(result) {
36
92
  * if (result.success) {
37
93
  * console.log(result.value) // string
38
94
  * } else {
39
- * console.error(result.error instanceof Error) // true
40
- * console.error(result.error.message) // string
95
+ * console.error(result.reason instanceof Error) // true
96
+ * console.error(result.reason.message) // string
41
97
  * }
42
98
  * ```
43
99
  */
@@ -67,7 +123,7 @@ function catchall(err) {
67
123
  * if (result.success) {
68
124
  * console.log(result.value) // string
69
125
  * } else {
70
- * console.error(result.error) // FooError | BarError
126
+ * console.error(result.reason) // FooError | BarError
71
127
  * }
72
128
  */
73
129
  /*@__NO_SIDE_EFFECTS__*/