@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.
- package/CHANGELOG.md +54 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +232 -5
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +60 -0
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +54 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +356 -11
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +203 -4
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +12 -28
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +46 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +16 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +50 -2
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +44 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +29 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +30 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +39 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +34 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +39 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +35 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +67 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +55 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +45 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +46 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +59 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +47 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +49 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +49 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +43 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +38 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +55 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +50 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +37 -0
- package/dist/schema/lex-map.d.ts.map +1 -0
- package/dist/schema/lex-map.js +60 -0
- package/dist/schema/lex-map.js.map +1 -0
- package/dist/schema/lex-value.d.ts +35 -0
- package/dist/schema/lex-value.d.ts.map +1 -0
- package/dist/schema/lex-value.js +87 -0
- package/dist/schema/lex-value.js.map +1 -0
- package/dist/schema/literal.d.ts +45 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +45 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +43 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +44 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +30 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +31 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +42 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +42 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +57 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +53 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +43 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +43 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +96 -12
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +155 -21
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +73 -3
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +76 -25
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +21 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +51 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +18 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +45 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +46 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +72 -6
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +56 -8
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +48 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +49 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +73 -23
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +20 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +54 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +16 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +51 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +52 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +46 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +41 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.d.ts +34 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +34 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +45 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +45 -0
- package/dist/schema/with-default.js.map +1 -1
- package/dist/schema.d.ts +2 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -1
- package/dist/schema.js.map +1 -1
- package/dist/util/if-any.d.ts +2 -0
- package/dist/util/if-any.d.ts.map +1 -0
- package/dist/util/if-any.js +3 -0
- package/dist/util/if-any.js.map +1 -0
- package/package.json +3 -3
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +244 -9
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +68 -2
- package/src/core/validator.ts +373 -12
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +54 -25
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +48 -1
- package/src/schema/blob.ts +50 -1
- package/src/schema/boolean.ts +31 -1
- package/src/schema/bytes.ts +41 -1
- package/src/schema/cid.ts +41 -1
- package/src/schema/custom.ts +68 -1
- package/src/schema/dict.ts +47 -1
- package/src/schema/discriminated-union.ts +61 -1
- package/src/schema/enum.ts +50 -0
- package/src/schema/integer.ts +45 -1
- package/src/schema/intersection.ts +56 -0
- package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
- package/src/schema/lex-map.ts +63 -0
- package/src/schema/lex-value.test.ts +81 -0
- package/src/schema/lex-value.ts +86 -0
- package/src/schema/literal.ts +46 -0
- package/src/schema/never.ts +45 -1
- package/src/schema/null.ts +32 -1
- package/src/schema/nullable.ts +43 -0
- package/src/schema/object.ts +59 -1
- package/src/schema/optional.ts +44 -0
- package/src/schema/params.test.ts +133 -38
- package/src/schema/params.ts +237 -37
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +145 -42
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +82 -16
- package/src/schema/ref.ts +52 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +47 -1
- package/src/schema/string.test.ts +99 -2
- package/src/schema/string.ts +108 -15
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +50 -1
- package/src/schema/typed-object.ts +81 -16
- package/src/schema/typed-ref.ts +55 -0
- package/src/schema/typed-union.ts +58 -3
- package/src/schema/union.ts +47 -0
- package/src/schema/unknown.ts +35 -0
- package/src/schema/with-default.ts +46 -0
- package/src/schema.ts +2 -1
- package/src/util/if-any.ts +3 -0
- package/dist/schema/unknown-object.d.ts +0 -8
- package/dist/schema/unknown-object.d.ts.map +0 -1
- package/dist/schema/unknown-object.js +0 -19
- package/dist/schema/unknown-object.js.map +0 -1
- package/src/schema/unknown-object.ts +0 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,59 @@
|
|
|
1
1
|
# @atproto/lex-schema
|
|
2
2
|
|
|
3
|
+
## 0.0.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Params cannot be arrays of mixed string, number and booleans (items can only be of one type)
|
|
8
|
+
|
|
9
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve typeing of `string()` schemas
|
|
10
|
+
|
|
11
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix import statement
|
|
12
|
+
|
|
13
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Schemas now define a `type` property that allows to better discriminate them
|
|
14
|
+
|
|
15
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add support for `knownValues` in string schemas
|
|
16
|
+
|
|
17
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `MaybeTypedObject` type utility (retrun type of `TypedObjectSchema.isTypeOf` method)
|
|
18
|
+
|
|
19
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Properly support "enum" and "const" schemas in params
|
|
20
|
+
|
|
21
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Allow using `InferMethod*` type helpers without any argument
|
|
22
|
+
|
|
23
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Minor optimization when checking payload encoding
|
|
24
|
+
|
|
25
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve error messages when encountering an unexpected legacy blob
|
|
26
|
+
|
|
27
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add new `issueUnexpectedType` method on `ValidationContext` to better allow creation issues with multiple allowed types
|
|
28
|
+
|
|
29
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update validation message for $typed object to match working of `@atproto/lexicon`
|
|
30
|
+
|
|
31
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Replace `UnknownObject` with `LexMap` to improve consistency with underlying data structure being validated
|
|
32
|
+
|
|
33
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `TypedRecord` type utility (result of `RecordSchema.isTypeOf` methid
|
|
34
|
+
|
|
35
|
+
- Updated dependencies [[`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df)]:
|
|
36
|
+
- @atproto/lex-data@0.0.12
|
|
37
|
+
|
|
38
|
+
## 0.0.12
|
|
39
|
+
|
|
40
|
+
### Patch Changes
|
|
41
|
+
|
|
42
|
+
- [#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)
|
|
43
|
+
|
|
44
|
+
- [#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
|
|
45
|
+
|
|
46
|
+
- [#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
|
|
47
|
+
|
|
48
|
+
- [#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
|
|
49
|
+
|
|
50
|
+
- [#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
|
|
51
|
+
|
|
52
|
+
- [#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
|
|
53
|
+
|
|
54
|
+
- Updated dependencies [[`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015)]:
|
|
55
|
+
- @atproto/lex-data@0.0.11
|
|
56
|
+
|
|
3
57
|
## 0.0.11
|
|
4
58
|
|
|
5
59
|
### Patch Changes
|
package/dist/core/$type.d.ts
CHANGED
|
@@ -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
|
};
|
package/dist/core/$type.d.ts.map
CHANGED
|
@@ -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,UAAU,GACpB,MAAM,SAAS,CAAC,GACd,CAAC,GAAG,GAAG,CAAC,IAAI,MAAM,EAAE,GACpB,CAAC,SAAS,MAAM,GACd,CAAC,GACD,GAAG,CAAC,IAAI,CAAC,EAAE,GACf,KAAK,CAAA;AAET,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,IAAI,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;
|
|
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;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,KAAK,CACf,CAAC,SAAS,UAAU,GAAG,UAAU,EACjC,CAAC,SAAS,MAAM,GAAG,MAAM,IACvB,CAAC,SAAS,UAAU,GACpB,MAAM,SAAS,CAAC,GACd,CAAC,GAAG,GAAG,CAAC,IAAI,MAAM,EAAE,GACpB,CAAC,SAAS,MAAM,GACd,CAAC,GACD,GAAG,CAAC,IAAI,CAAC,EAAE,GACf,KAAK,CAAA;AAET;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,IAAI,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;AAE3E;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,wBAAgB,KAAK,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,MAAM,EAC1D,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,CAAC,GACN,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAEb;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,QAAQ,CACzD,CAAC,GAAG;IACF,KAAK,EAAE,CAAC,CAAA;CACT,CACF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,OAAO,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"}
|
package/dist/core/$type.js
CHANGED
|
@@ -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
|
}
|
package/dist/core/$type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"$type.js","sourceRoot":"","sources":["../../src/core/$type.ts"],"names":[],"mappings":";;
|
|
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;
|
|
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"}
|
package/dist/core/record-key.js
CHANGED
|
@@ -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":";;
|
|
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"]}
|
package/dist/core/result.d.ts
CHANGED
|
@@ -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
|
|
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.
|
|
29
|
-
* console.error(result.
|
|
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.
|
|
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;
|
|
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;AAEhE;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,GAAG,KAAK,IAAI;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,CAAC,CAAA;CAAE,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,KAAK,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;AAE5E;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAErD;AAED;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAEtD;AAED;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAE5D;AAED;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,YAAY,CAAC,CAAC,EAAE,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"}
|