@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
|
@@ -1,8 +1,41 @@
|
|
|
1
|
+
import { LexValue } from '@atproto/lex-data';
|
|
1
2
|
import { Infer, NsidString, Schema } from '../core.js';
|
|
2
3
|
import { ParamsSchema } from './params.js';
|
|
4
|
+
/**
|
|
5
|
+
* Infers the parameters type from a Subscription definition.
|
|
6
|
+
*
|
|
7
|
+
* @template S - The Subscription type
|
|
8
|
+
*/
|
|
3
9
|
export type InferSubscriptionParameters<S extends Subscription> = Infer<S['parameters']>;
|
|
10
|
+
/**
|
|
11
|
+
* Infers the message type from a Subscription definition.
|
|
12
|
+
*
|
|
13
|
+
* @template S - The Subscription type
|
|
14
|
+
*/
|
|
4
15
|
export type InferSubscriptionMessage<S extends Subscription> = Infer<S['message']>;
|
|
5
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Represents a Lexicon subscription (WebSocket) endpoint definition.
|
|
18
|
+
*
|
|
19
|
+
* Subscriptions are real-time event streams delivered over WebSocket.
|
|
20
|
+
* They have parameters for initializing the connection and a message
|
|
21
|
+
* schema for validating incoming events.
|
|
22
|
+
*
|
|
23
|
+
* @template TNsid - The NSID identifying this subscription
|
|
24
|
+
* @template TParameters - The connection parameters schema type
|
|
25
|
+
* @template TMessage - The message schema type
|
|
26
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const firehose = new Subscription(
|
|
31
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
32
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
33
|
+
* repoEventSchema,
|
|
34
|
+
* ['FutureCursor']
|
|
35
|
+
* )
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class Subscription<const TNsid extends NsidString = NsidString, const TParameters extends ParamsSchema = ParamsSchema, const TMessage extends Schema<LexValue> = Schema<LexValue>, const TErrors extends undefined | readonly string[] = undefined | readonly string[]> {
|
|
6
39
|
readonly nsid: TNsid;
|
|
7
40
|
readonly parameters: TParameters;
|
|
8
41
|
readonly message: TMessage;
|
|
@@ -10,5 +43,42 @@ export declare class Subscription<const TNsid extends NsidString = NsidString, c
|
|
|
10
43
|
readonly type: "subscription";
|
|
11
44
|
constructor(nsid: TNsid, parameters: TParameters, message: TMessage, errors: TErrors);
|
|
12
45
|
}
|
|
13
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Creates a subscription definition for a Lexicon WebSocket endpoint.
|
|
48
|
+
*
|
|
49
|
+
* Subscriptions enable real-time event streaming. The connection is
|
|
50
|
+
* initialized with parameters, and the server sends messages matching
|
|
51
|
+
* the message schema.
|
|
52
|
+
*
|
|
53
|
+
* @param nsid - The NSID identifying this subscription endpoint
|
|
54
|
+
* @param parameters - Schema for connection parameters
|
|
55
|
+
* @param message - Schema for validating incoming messages
|
|
56
|
+
* @param errors - Optional array of error type strings
|
|
57
|
+
* @returns A new {@link Subscription} instance
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // Repository event stream
|
|
62
|
+
* const subscribeRepos = l.subscription(
|
|
63
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
64
|
+
* l.params({
|
|
65
|
+
* cursor: l.optional(l.integer()),
|
|
66
|
+
* }),
|
|
67
|
+
* l.typedUnion([
|
|
68
|
+
* l.typedRef(() => commitEventSchema),
|
|
69
|
+
* l.typedRef(() => handleEventSchema),
|
|
70
|
+
* l.typedRef(() => identityEventSchema),
|
|
71
|
+
* ], false),
|
|
72
|
+
* ['FutureCursor', 'ConsumerTooSlow'],
|
|
73
|
+
* )
|
|
74
|
+
*
|
|
75
|
+
* // Label stream
|
|
76
|
+
* const subscribeLabels = l.subscription(
|
|
77
|
+
* 'com.atproto.label.subscribeLabels',
|
|
78
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
79
|
+
* labelEventSchema,
|
|
80
|
+
* )
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function subscription<const N extends NsidString, const P extends ParamsSchema, const M extends Schema<LexValue>, const E extends undefined | readonly string[] = undefined>(nsid: N, parameters: P, message: M, errors?: E): Subscription<N, P, M, E>;
|
|
14
84
|
//# sourceMappingURL=subscription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CACrE,CAAC,CAAC,YAAY,CAAC,CAChB,CAAA;AAED,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAClE,CAAC,CAAC,SAAS,CAAC,CACb,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,UA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E,OAAO;CAE3B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,wBAAgB,YAAY,CAC1B,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,YAAY,EAC5B,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,QAAQ,CAAC,EAChC,KAAK,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,EACzD,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,GAAE,CAAkB,4BAE/D"}
|
|
@@ -2,6 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Subscription = void 0;
|
|
4
4
|
exports.subscription = subscription;
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Lexicon subscription (WebSocket) endpoint definition.
|
|
7
|
+
*
|
|
8
|
+
* Subscriptions are real-time event streams delivered over WebSocket.
|
|
9
|
+
* They have parameters for initializing the connection and a message
|
|
10
|
+
* schema for validating incoming events.
|
|
11
|
+
*
|
|
12
|
+
* @template TNsid - The NSID identifying this subscription
|
|
13
|
+
* @template TParameters - The connection parameters schema type
|
|
14
|
+
* @template TMessage - The message schema type
|
|
15
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const firehose = new Subscription(
|
|
20
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
21
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
22
|
+
* repoEventSchema,
|
|
23
|
+
* ['FutureCursor']
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
5
27
|
class Subscription {
|
|
6
28
|
nsid;
|
|
7
29
|
parameters;
|
|
@@ -16,6 +38,43 @@ class Subscription {
|
|
|
16
38
|
}
|
|
17
39
|
}
|
|
18
40
|
exports.Subscription = Subscription;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a subscription definition for a Lexicon WebSocket endpoint.
|
|
43
|
+
*
|
|
44
|
+
* Subscriptions enable real-time event streaming. The connection is
|
|
45
|
+
* initialized with parameters, and the server sends messages matching
|
|
46
|
+
* the message schema.
|
|
47
|
+
*
|
|
48
|
+
* @param nsid - The NSID identifying this subscription endpoint
|
|
49
|
+
* @param parameters - Schema for connection parameters
|
|
50
|
+
* @param message - Schema for validating incoming messages
|
|
51
|
+
* @param errors - Optional array of error type strings
|
|
52
|
+
* @returns A new {@link Subscription} instance
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* // Repository event stream
|
|
57
|
+
* const subscribeRepos = l.subscription(
|
|
58
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
59
|
+
* l.params({
|
|
60
|
+
* cursor: l.optional(l.integer()),
|
|
61
|
+
* }),
|
|
62
|
+
* l.typedUnion([
|
|
63
|
+
* l.typedRef(() => commitEventSchema),
|
|
64
|
+
* l.typedRef(() => handleEventSchema),
|
|
65
|
+
* l.typedRef(() => identityEventSchema),
|
|
66
|
+
* ], false),
|
|
67
|
+
* ['FutureCursor', 'ConsumerTooSlow'],
|
|
68
|
+
* )
|
|
69
|
+
*
|
|
70
|
+
* // Label stream
|
|
71
|
+
* const subscribeLabels = l.subscription(
|
|
72
|
+
* 'com.atproto.label.subscribeLabels',
|
|
73
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
74
|
+
* labelEventSchema,
|
|
75
|
+
* )
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
19
78
|
/*@__NO_SIDE_EFFECTS__*/
|
|
20
79
|
function subscription(nsid, parameters, message, errors = undefined) {
|
|
21
80
|
return new Subscription(nsid, parameters, message, errors);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;AAoGA,oCAOC;AArFD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,YAAY;IAWZ;IACA;IACA;IACA;IANF,IAAI,GAAG,cAAuB,CAAA;IAEvC,YACW,IAAW,EACX,UAAuB,EACvB,OAAiB,EACjB,MAAe;QAHf,SAAI,GAAJ,IAAI,CAAO;QACX,eAAU,GAAV,UAAU,CAAa;QACvB,YAAO,GAAP,OAAO,CAAU;QACjB,WAAM,GAAN,MAAM,CAAS;IACvB,CAAC;CACL;AAhBD,oCAgBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAwB;AACxB,SAAgB,YAAY,CAK1B,IAAO,EAAE,UAAa,EAAE,OAAU,EAAE,SAAY,SAAc;IAC9D,OAAO,IAAI,YAAY,CAAa,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AACxE,CAAC","sourcesContent":["import { LexValue } from '@atproto/lex-data'\nimport { Infer, NsidString, Schema } from '../core.js'\nimport { ParamsSchema } from './params.js'\n\n/**\n * Infers the parameters type from a Subscription definition.\n *\n * @template S - The Subscription type\n */\nexport type InferSubscriptionParameters<S extends Subscription> = Infer<\n S['parameters']\n>\n\n/**\n * Infers the message type from a Subscription definition.\n *\n * @template S - The Subscription type\n */\nexport type InferSubscriptionMessage<S extends Subscription> = Infer<\n S['message']\n>\n\n/**\n * Represents a Lexicon subscription (WebSocket) endpoint definition.\n *\n * Subscriptions are real-time event streams delivered over WebSocket.\n * They have parameters for initializing the connection and a message\n * schema for validating incoming events.\n *\n * @template TNsid - The NSID identifying this subscription\n * @template TParameters - The connection parameters schema type\n * @template TMessage - The message schema type\n * @template TErrors - Array of error type strings, or undefined\n *\n * @example\n * ```ts\n * const firehose = new Subscription(\n * 'com.atproto.sync.subscribeRepos',\n * l.params({ cursor: l.optional(l.integer()) }),\n * repoEventSchema,\n * ['FutureCursor']\n * )\n * ```\n */\nexport class Subscription<\n const TNsid extends NsidString = NsidString,\n const TParameters extends ParamsSchema = ParamsSchema,\n const TMessage extends Schema<LexValue> = Schema<LexValue>,\n const TErrors extends undefined | readonly string[] =\n | undefined\n | readonly string[],\n> {\n readonly type = 'subscription' as const\n\n constructor(\n readonly nsid: TNsid,\n readonly parameters: TParameters,\n readonly message: TMessage,\n readonly errors: TErrors,\n ) {}\n}\n\n/**\n * Creates a subscription definition for a Lexicon WebSocket endpoint.\n *\n * Subscriptions enable real-time event streaming. The connection is\n * initialized with parameters, and the server sends messages matching\n * the message schema.\n *\n * @param nsid - The NSID identifying this subscription endpoint\n * @param parameters - Schema for connection parameters\n * @param message - Schema for validating incoming messages\n * @param errors - Optional array of error type strings\n * @returns A new {@link Subscription} instance\n *\n * @example\n * ```ts\n * // Repository event stream\n * const subscribeRepos = l.subscription(\n * 'com.atproto.sync.subscribeRepos',\n * l.params({\n * cursor: l.optional(l.integer()),\n * }),\n * l.typedUnion([\n * l.typedRef(() => commitEventSchema),\n * l.typedRef(() => handleEventSchema),\n * l.typedRef(() => identityEventSchema),\n * ], false),\n * ['FutureCursor', 'ConsumerTooSlow'],\n * )\n *\n * // Label stream\n * const subscribeLabels = l.subscription(\n * 'com.atproto.label.subscribeLabels',\n * l.params({ cursor: l.optional(l.integer()) }),\n * labelEventSchema,\n * )\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function subscription<\n const N extends NsidString,\n const P extends ParamsSchema,\n const M extends Schema<LexValue>,\n const E extends undefined | readonly string[] = undefined,\n>(nsid: N, parameters: P, message: M, errors: E = undefined as E) {\n return new Subscription<N, P, M, E>(nsid, parameters, message, errors)\n}\n"]}
|
package/dist/schema/token.d.ts
CHANGED
|
@@ -1,10 +1,58 @@
|
|
|
1
1
|
import { NsidString, Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for Lexicon token values.
|
|
4
|
+
*
|
|
5
|
+
* Tokens are named constants in Lexicon, identified by their NSID and hash.
|
|
6
|
+
* They validate to their string value (e.g., 'app.bsky.feed.defs#requestLess').
|
|
7
|
+
* TokenSchema instances can also be used as values themselves.
|
|
8
|
+
*
|
|
9
|
+
* @template TValue - The token string literal type
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = new TokenSchema('app.bsky.feed.defs#requestLess')
|
|
14
|
+
* schema.validate('app.bsky.feed.defs#requestLess') // success
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class TokenSchema<const TValue extends string = string> extends Schema<TValue> {
|
|
3
18
|
readonly value: TValue;
|
|
19
|
+
readonly type: "token";
|
|
4
20
|
constructor(value: TValue);
|
|
5
21
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
6
22
|
toJSON(): string;
|
|
7
23
|
toString(): string;
|
|
8
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a token schema for Lexicon named constants.
|
|
27
|
+
*
|
|
28
|
+
* Tokens are used in Lexicon as named constants or enum-like values.
|
|
29
|
+
* The token instance can be used both as a schema validator and as
|
|
30
|
+
* the token value itself (it serializes to its string value).
|
|
31
|
+
*
|
|
32
|
+
* @param nsid - The NSID part of the token
|
|
33
|
+
* @param hash - The hash part of the token (defaults to 'main')
|
|
34
|
+
* @returns A new {@link TokenSchema} instance
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* // Define tokens
|
|
39
|
+
* const requestLess = l.token('app.bsky.feed.defs', 'requestLess')
|
|
40
|
+
* const requestMore = l.token('app.bsky.feed.defs', 'requestMore')
|
|
41
|
+
*
|
|
42
|
+
* // Use as a value
|
|
43
|
+
* console.log(requestLess.toString()) // 'app.bsky.feed.defs#requestLess'
|
|
44
|
+
*
|
|
45
|
+
* // Use in union for validation
|
|
46
|
+
* const feedbackSchema = l.union([requestLess, requestMore])
|
|
47
|
+
*
|
|
48
|
+
* // Validate
|
|
49
|
+
* feedbackSchema.parse('app.bsky.feed.defs#requestLess') // success
|
|
50
|
+
*
|
|
51
|
+
* // Token instances can be used as values in other schemas
|
|
52
|
+
* const feedbackRequest = l.object({
|
|
53
|
+
* feedback: requestLess, // Accepts the token value
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
9
57
|
export declare function token<const N extends NsidString, const H extends string = 'main'>(nsid: N, hash?: H): TokenSchema<import("../core.js").$Type<N, H>>;
|
|
10
58
|
//# sourceMappingURL=token.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEzE,qBAAa,WAAW,CACtB,KAAK,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,CACpC,SAAQ,MAAM,CAAC,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEzE;;;;;;;;;;;;;;GAcG;AACH,qBAAa,WAAW,CACtB,KAAK,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,CACpC,SAAQ,MAAM,CAAC,MAAM,CAAC;IAGV,QAAQ,CAAC,KAAK,EAAE,MAAM;IAFlC,QAAQ,CAAC,IAAI,EAAG,OAAO,CAAS;gBAEX,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;IAqBxD,MAAM,IAAI,MAAM;IAIhB,QAAQ,IAAI,MAAM;CAGnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAC/B,IAAI,EAAE,CAAC,EAAE,IAAI,GAAE,CAAe,iDAE/B"}
|
package/dist/schema/token.js
CHANGED
|
@@ -3,8 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TokenSchema = void 0;
|
|
4
4
|
exports.token = token;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for Lexicon token values.
|
|
8
|
+
*
|
|
9
|
+
* Tokens are named constants in Lexicon, identified by their NSID and hash.
|
|
10
|
+
* They validate to their string value (e.g., 'app.bsky.feed.defs#requestLess').
|
|
11
|
+
* TokenSchema instances can also be used as values themselves.
|
|
12
|
+
*
|
|
13
|
+
* @template TValue - The token string literal type
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const schema = new TokenSchema('app.bsky.feed.defs#requestLess')
|
|
18
|
+
* schema.validate('app.bsky.feed.defs#requestLess') // success
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class TokenSchema extends core_js_1.Schema {
|
|
7
22
|
value;
|
|
23
|
+
type = 'token';
|
|
8
24
|
constructor(value) {
|
|
9
25
|
super();
|
|
10
26
|
this.value = value;
|
|
@@ -19,7 +35,7 @@ class TokenSchema extends core_js_1.Schema {
|
|
|
19
35
|
return ctx.success(this.value);
|
|
20
36
|
}
|
|
21
37
|
if (typeof input !== 'string') {
|
|
22
|
-
return ctx.
|
|
38
|
+
return ctx.issueUnexpectedType(input, 'token');
|
|
23
39
|
}
|
|
24
40
|
return ctx.issueInvalidValue(input, [this.value]);
|
|
25
41
|
}
|
|
@@ -33,6 +49,38 @@ class TokenSchema extends core_js_1.Schema {
|
|
|
33
49
|
}
|
|
34
50
|
}
|
|
35
51
|
exports.TokenSchema = TokenSchema;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a token schema for Lexicon named constants.
|
|
54
|
+
*
|
|
55
|
+
* Tokens are used in Lexicon as named constants or enum-like values.
|
|
56
|
+
* The token instance can be used both as a schema validator and as
|
|
57
|
+
* the token value itself (it serializes to its string value).
|
|
58
|
+
*
|
|
59
|
+
* @param nsid - The NSID part of the token
|
|
60
|
+
* @param hash - The hash part of the token (defaults to 'main')
|
|
61
|
+
* @returns A new {@link TokenSchema} instance
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* // Define tokens
|
|
66
|
+
* const requestLess = l.token('app.bsky.feed.defs', 'requestLess')
|
|
67
|
+
* const requestMore = l.token('app.bsky.feed.defs', 'requestMore')
|
|
68
|
+
*
|
|
69
|
+
* // Use as a value
|
|
70
|
+
* console.log(requestLess.toString()) // 'app.bsky.feed.defs#requestLess'
|
|
71
|
+
*
|
|
72
|
+
* // Use in union for validation
|
|
73
|
+
* const feedbackSchema = l.union([requestLess, requestMore])
|
|
74
|
+
*
|
|
75
|
+
* // Validate
|
|
76
|
+
* feedbackSchema.parse('app.bsky.feed.defs#requestLess') // success
|
|
77
|
+
*
|
|
78
|
+
* // Token instances can be used as values in other schemas
|
|
79
|
+
* const feedbackRequest = l.object({
|
|
80
|
+
* feedback: requestLess, // Accepts the token value
|
|
81
|
+
* })
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
36
84
|
/*@__NO_SIDE_EFFECTS__*/
|
|
37
85
|
function token(nsid, hash = 'main') {
|
|
38
86
|
return new TokenSchema((0, core_js_1.$type)(nsid, hash));
|
package/dist/schema/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":";;;AAyFA,sBAKC;AA9FD,wCAAyE;AAEzE;;;;;;;;;;;;;;GAcG;AACH,MAAa,WAEX,SAAQ,gBAAc;IAGD;IAFZ,IAAI,GAAG,OAAgB,CAAA;IAEhC,YAAqB,KAAa;QAChC,KAAK,EAAE,CAAA;QADY,UAAK,GAAL,KAAK,CAAQ;IAElC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACzB,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;QAED,0EAA0E;QAC1E,eAAe;QACf,IAAI,KAAK,YAAY,WAAW,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAC/D,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAChD,CAAC;QAED,OAAO,GAAG,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;IACnD,CAAC;IAED,yEAAyE;IACzE,cAAc;IAEd,MAAM;QACJ,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;CACF;AArCD,kCAqCC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAwB;AACxB,SAAgB,KAAK,CAGnB,IAAO,EAAE,OAAU,MAAW;IAC9B,OAAO,IAAI,WAAW,CAAC,IAAA,eAAK,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AAC3C,CAAC","sourcesContent":["import { $type, NsidString, Schema, ValidationContext } from '../core.js'\n\n/**\n * Schema for Lexicon token values.\n *\n * Tokens are named constants in Lexicon, identified by their NSID and hash.\n * They validate to their string value (e.g., 'app.bsky.feed.defs#requestLess').\n * TokenSchema instances can also be used as values themselves.\n *\n * @template TValue - The token string literal type\n *\n * @example\n * ```ts\n * const schema = new TokenSchema('app.bsky.feed.defs#requestLess')\n * schema.validate('app.bsky.feed.defs#requestLess') // success\n * ```\n */\nexport class TokenSchema<\n const TValue extends string = string,\n> extends Schema<TValue> {\n readonly type = 'token' as const\n\n constructor(readonly value: TValue) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input === this.value) {\n return ctx.success(this.value)\n }\n\n // @NOTE: allow using the token instance itself (but convert to the actual\n // token value)\n if (input instanceof TokenSchema && input.value === this.value) {\n return ctx.success(this.value)\n }\n\n if (typeof input !== 'string') {\n return ctx.issueUnexpectedType(input, 'token')\n }\n\n return ctx.issueInvalidValue(input, [this.value])\n }\n\n // When using the TokenSchema instance as data, let's serialize it to the\n // token value\n\n toJSON(): string {\n return this.value\n }\n\n toString(): string {\n return this.value\n }\n}\n\n/**\n * Creates a token schema for Lexicon named constants.\n *\n * Tokens are used in Lexicon as named constants or enum-like values.\n * The token instance can be used both as a schema validator and as\n * the token value itself (it serializes to its string value).\n *\n * @param nsid - The NSID part of the token\n * @param hash - The hash part of the token (defaults to 'main')\n * @returns A new {@link TokenSchema} instance\n *\n * @example\n * ```ts\n * // Define tokens\n * const requestLess = l.token('app.bsky.feed.defs', 'requestLess')\n * const requestMore = l.token('app.bsky.feed.defs', 'requestMore')\n *\n * // Use as a value\n * console.log(requestLess.toString()) // 'app.bsky.feed.defs#requestLess'\n *\n * // Use in union for validation\n * const feedbackSchema = l.union([requestLess, requestMore])\n *\n * // Validate\n * feedbackSchema.parse('app.bsky.feed.defs#requestLess') // success\n *\n * // Token instances can be used as values in other schemas\n * const feedbackRequest = l.object({\n * feedback: requestLess, // Accepts the token value\n * })\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function token<\n const N extends NsidString,\n const H extends string = 'main',\n>(nsid: N, hash: H = 'main' as H) {\n return new TokenSchema($type(nsid, hash))\n}\n"]}
|
|
@@ -1,38 +1,88 @@
|
|
|
1
1
|
import { $Type, $TypeOf, $Typed, $TypedMaybe, InferInput, InferOutput, NsidString, Schema, Unknown$TypedObject, ValidationContext, Validator } from '../core.js';
|
|
2
|
+
export type MaybeTypedObject<TType extends $Type, TValue extends {
|
|
3
|
+
$type?: unknown;
|
|
4
|
+
} = {
|
|
5
|
+
$type?: unknown;
|
|
6
|
+
}> = TValue extends {
|
|
7
|
+
$type?: TType;
|
|
8
|
+
} ? TValue : $TypedMaybe<Exclude<TValue, Unknown$TypedObject>, TType>;
|
|
9
|
+
/**
|
|
10
|
+
* Schema for typed objects in Lexicon unions.
|
|
11
|
+
*
|
|
12
|
+
* Typed objects have a `$type` field that identifies which variant they are
|
|
13
|
+
* in a union. The `$type` can be omitted in input (it's implicit), but if
|
|
14
|
+
* present, it must match the expected value.
|
|
15
|
+
*
|
|
16
|
+
* @template TType - The $type string literal type
|
|
17
|
+
* @template TShape - The validator type for the object's shape
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const schema = new TypedObjectSchema(
|
|
22
|
+
* 'app.bsky.embed.images#view',
|
|
23
|
+
* l.object({ images: l.array(imageSchema) })
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
2
27
|
export declare class TypedObjectSchema<const TType extends $Type = $Type, const TShape extends Validator<{
|
|
3
28
|
[k: string]: unknown;
|
|
4
29
|
}> = any> extends Schema<$TypedMaybe<InferInput<TShape>, TType>, $TypedMaybe<InferOutput<TShape>, TType>> {
|
|
5
30
|
readonly $type: TType;
|
|
6
31
|
readonly schema: TShape;
|
|
32
|
+
readonly type: "typedObject";
|
|
7
33
|
constructor($type: TType, schema: TShape);
|
|
8
|
-
isTypeOf<
|
|
9
|
-
$type?: TType;
|
|
10
|
-
} ? X : $TypedMaybe<Exclude<X, Unknown$TypedObject>, TType>;
|
|
34
|
+
isTypeOf<TValue extends Record<string, unknown>>(value: TValue): value is MaybeTypedObject<TType, TValue>;
|
|
11
35
|
build(input: Omit<InferInput<this>, '$type'>): $Typed<InferOutput<this>, TType>;
|
|
12
|
-
$isTypeOf<
|
|
13
|
-
|
|
14
|
-
} ? X : Exclude<X, Unknown$TypedObject> & {
|
|
15
|
-
$type?: TType | undefined;
|
|
16
|
-
} extends infer T ? { [K in keyof T]: (Exclude<X, Unknown$TypedObject> & {
|
|
17
|
-
$type?: TType | undefined;
|
|
18
|
-
})[K]; } : never;
|
|
19
|
-
$build(input: Omit<InferInput<this>, '$type'>): InferOutput<this> & {
|
|
20
|
-
$type: TType;
|
|
21
|
-
} extends infer T ? { [K in keyof T]: (InferOutput<this> & {
|
|
22
|
-
$type: TType;
|
|
23
|
-
})[K]; } : never;
|
|
36
|
+
$isTypeOf<TValue extends Record<string, unknown>>(value: TValue): value is MaybeTypedObject<TType, TValue>;
|
|
37
|
+
$build(input: Omit<InferInput<this>, '$type'>): $Typed<InferOutput<this>, TType>;
|
|
24
38
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<InferInput<TShape>>;
|
|
25
39
|
}
|
|
26
40
|
/**
|
|
41
|
+
* Creates a typed object schema for use in Lexicon unions.
|
|
42
|
+
*
|
|
43
|
+
* Typed objects are identified by their `$type` field, which combines an NSID
|
|
44
|
+
* and a hash (e.g., 'app.bsky.embed.images#view'). Used for union variants.
|
|
45
|
+
*
|
|
27
46
|
* This function offers two overloads:
|
|
28
|
-
* - One that
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
47
|
+
* - One that infers the type from arguments (no circular reference support)
|
|
48
|
+
* - One with explicit interface for codegen with circular references
|
|
49
|
+
*
|
|
50
|
+
* @param nsid - The NSID part of the type (e.g., 'app.bsky.embed.images')
|
|
51
|
+
* @param hash - The hash part of the type (e.g., 'view'), defaults to 'main'
|
|
52
|
+
* @param validator - Schema for validating the object properties
|
|
53
|
+
* @returns A new {@link TypedObjectSchema} instance
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* // Image embed view
|
|
58
|
+
* const imageViewSchema = l.typedObject(
|
|
59
|
+
* 'app.bsky.embed.images',
|
|
60
|
+
* 'view',
|
|
61
|
+
* l.object({
|
|
62
|
+
* images: l.array(l.object({
|
|
63
|
+
* thumb: l.string(),
|
|
64
|
+
* fullsize: l.string(),
|
|
65
|
+
* alt: l.string(),
|
|
66
|
+
* })),
|
|
67
|
+
* })
|
|
68
|
+
* )
|
|
69
|
+
*
|
|
70
|
+
* // Main type (hash defaults to 'main')
|
|
71
|
+
* const postViewSchema = l.typedObject(
|
|
72
|
+
* 'app.bsky.feed.defs',
|
|
73
|
+
* 'postView',
|
|
74
|
+
* l.object({ uri: l.string(), cid: l.string(), author: authorSchema })
|
|
75
|
+
* )
|
|
76
|
+
*
|
|
77
|
+
* // Use $isTypeOf to narrow union types
|
|
78
|
+
* if (imageViewSchema.$isTypeOf(embed)) {
|
|
79
|
+
* // embed is narrowed to image view type
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* // Use $build to construct typed objects
|
|
83
|
+
* const view = imageViewSchema.$build({ images: [...] })
|
|
84
|
+
* // view.$type === 'app.bsky.embed.images#view'
|
|
85
|
+
* ```
|
|
36
86
|
*/
|
|
37
87
|
export declare function typedObject<const N extends NsidString, const H extends string, const S extends Validator<{
|
|
38
88
|
[k: string]: unknown;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-object.d.ts","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,OAAO,EACP,MAAM,EACN,WAAW,EAGX,UAAU,EACV,WAAW,EACX,UAAU,EACV,MAAM,EACN,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,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AAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AACpE,wBAAgB,WAAW,CAAC,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,EACrD,IAAI,EAAE,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC,SAAS,MAAM,CAAA;CAAE,GAC9C,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,GAC9B,CAAC,GACD,CAAC,GACH,KAAK,EACT,IAAI,EAAE,CAAC,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC,SAAS,MAAM,CAAA;CAAE,GAC9C,CAAC,SAAS,GAAG,MAAM,IAAI,MAAM,CAAC,EAAE,GAC9B,CAAC,GACD,MAAM,GACR,KAAK,EACT,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GACrC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -4,9 +4,28 @@ exports.TypedObjectSchema = void 0;
|
|
|
4
4
|
exports.typedObject = typedObject;
|
|
5
5
|
const lex_data_1 = require("@atproto/lex-data");
|
|
6
6
|
const core_js_1 = require("../core.js");
|
|
7
|
+
/**
|
|
8
|
+
* Schema for typed objects in Lexicon unions.
|
|
9
|
+
*
|
|
10
|
+
* Typed objects have a `$type` field that identifies which variant they are
|
|
11
|
+
* in a union. The `$type` can be omitted in input (it's implicit), but if
|
|
12
|
+
* present, it must match the expected value.
|
|
13
|
+
*
|
|
14
|
+
* @template TType - The $type string literal type
|
|
15
|
+
* @template TShape - The validator type for the object's shape
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const schema = new TypedObjectSchema(
|
|
20
|
+
* 'app.bsky.embed.images#view',
|
|
21
|
+
* l.object({ images: l.array(imageSchema) })
|
|
22
|
+
* )
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
7
25
|
class TypedObjectSchema extends core_js_1.Schema {
|
|
8
26
|
$type;
|
|
9
27
|
schema;
|
|
28
|
+
type = 'typedObject';
|
|
10
29
|
constructor($type, schema) {
|
|
11
30
|
super();
|
|
12
31
|
this.$type = $type;
|
|
@@ -26,7 +45,7 @@ class TypedObjectSchema extends core_js_1.Schema {
|
|
|
26
45
|
}
|
|
27
46
|
validateInContext(input, ctx) {
|
|
28
47
|
if (!(0, lex_data_1.isPlainObject)(input)) {
|
|
29
|
-
return ctx.
|
|
48
|
+
return ctx.issueUnexpectedType(input, 'object');
|
|
30
49
|
}
|
|
31
50
|
if ('$type' in input &&
|
|
32
51
|
input.$type !== undefined &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":";;;AAwKA,kCAMC;AA9KD,gDAAiD;AACjD,wCAcmB;AASnB;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,iBAGX,SAAQ,gBAGT;IAIY;IACA;IAJF,IAAI,GAAG,aAAsB,CAAA;IAEtC,YACW,KAAY,EACZ,MAAc;QAEvB,KAAK,EAAE,CAAA;QAHE,UAAK,GAAL,KAAK,CAAO;QACZ,WAAM,GAAN,MAAM,CAAQ;IAGzB,CAAC;IAED,QAAQ,CACN,KAAa;QAEb,OAAO,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAA;IAChE,CAAC;IAED,KAAK,CACH,KAAsC;QAEtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,gBAAM,EAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAG1C,CAAA;IACH,CAAC;IAED,SAAS,CACP,KAAa;QAEb,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;IAED,MAAM,CACJ,KAAsC;QAEtC,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjD,CAAC;QAED,IACE,OAAO,IAAI,KAAK;YAChB,KAAK,CAAC,KAAK,KAAK,SAAS;YACzB,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAC1B,CAAC;YACD,OAAO,GAAG,CAAC,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QACpE,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IACzC,CAAC;CACF;AA1DD,8CA0DC;AAmED,wBAAwB;AACxB,SAAgB,WAAW,CAIzB,IAAO,EAAE,IAAO,EAAE,SAAY;IAC9B,OAAO,IAAI,iBAAiB,CAAiB,IAAA,eAAK,EAAC,IAAI,EAAE,IAAI,CAAC,EAAE,SAAS,CAAC,CAAA;AAC5E,CAAC","sourcesContent":["import { isPlainObject } from '@atproto/lex-data'\nimport {\n $Type,\n $TypeOf,\n $Typed,\n $TypedMaybe,\n $type,\n $typed,\n InferInput,\n InferOutput,\n NsidString,\n Schema,\n Unknown$TypedObject,\n ValidationContext,\n Validator,\n} from '../core.js'\n\nexport type MaybeTypedObject<\n TType extends $Type,\n TValue extends { $type?: unknown } = { $type?: unknown },\n> = TValue extends { $type?: TType }\n ? TValue\n : $TypedMaybe<Exclude<TValue, Unknown$TypedObject>, TType>\n\n/**\n * Schema for typed objects in Lexicon unions.\n *\n * Typed objects have a `$type` field that identifies which variant they are\n * in a union. The `$type` can be omitted in input (it's implicit), but if\n * present, it must match the expected value.\n *\n * @template TType - The $type string literal type\n * @template TShape - The validator type for the object's shape\n *\n * @example\n * ```ts\n * const schema = new TypedObjectSchema(\n * 'app.bsky.embed.images#view',\n * l.object({ images: l.array(imageSchema) })\n * )\n * ```\n */\nexport class TypedObjectSchema<\n const TType extends $Type = $Type,\n const TShape extends Validator<{ [k: string]: unknown }> = any,\n> extends Schema<\n $TypedMaybe<InferInput<TShape>, TType>,\n $TypedMaybe<InferOutput<TShape>, TType>\n> {\n readonly type = 'typedObject' as const\n\n constructor(\n readonly $type: TType,\n readonly schema: TShape,\n ) {\n super()\n }\n\n isTypeOf<TValue extends Record<string, unknown>>(\n value: TValue,\n ): value is MaybeTypedObject<TType, TValue> {\n return value.$type === undefined || value.$type === this.$type\n }\n\n build(\n input: Omit<InferInput<this>, '$type'>,\n ): $Typed<InferOutput<this>, TType> {\n return this.parse($typed(input, this.$type)) as $Typed<\n InferOutput<this>,\n TType\n >\n }\n\n $isTypeOf<TValue extends Record<string, unknown>>(\n value: TValue,\n ): value is MaybeTypedObject<TType, TValue> {\n return this.isTypeOf(value)\n }\n\n $build(\n input: Omit<InferInput<this>, '$type'>,\n ): $Typed<InferOutput<this>, TType> {\n return this.build(input)\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isPlainObject(input)) {\n return ctx.issueUnexpectedType(input, 'object')\n }\n\n if (\n '$type' in input &&\n input.$type !== undefined &&\n input.$type !== this.$type\n ) {\n return ctx.issueInvalidPropertyValue(input, '$type', [this.$type])\n }\n\n return ctx.validate(input, this.schema)\n }\n}\n\n/**\n * Creates a typed object schema for use in Lexicon unions.\n *\n * Typed objects are identified by their `$type` field, which combines an NSID\n * and a hash (e.g., 'app.bsky.embed.images#view'). Used for union variants.\n *\n * This function offers two overloads:\n * - One that infers the type from arguments (no circular reference support)\n * - One with explicit interface for codegen with circular references\n *\n * @param nsid - The NSID part of the type (e.g., 'app.bsky.embed.images')\n * @param hash - The hash part of the type (e.g., 'view'), defaults to 'main'\n * @param validator - Schema for validating the object properties\n * @returns A new {@link TypedObjectSchema} instance\n *\n * @example\n * ```ts\n * // Image embed view\n * const imageViewSchema = l.typedObject(\n * 'app.bsky.embed.images',\n * 'view',\n * l.object({\n * images: l.array(l.object({\n * thumb: l.string(),\n * fullsize: l.string(),\n * alt: l.string(),\n * })),\n * })\n * )\n *\n * // Main type (hash defaults to 'main')\n * const postViewSchema = l.typedObject(\n * 'app.bsky.feed.defs',\n * 'postView',\n * l.object({ uri: l.string(), cid: l.string(), author: authorSchema })\n * )\n *\n * // Use $isTypeOf to narrow union types\n * if (imageViewSchema.$isTypeOf(embed)) {\n * // embed is narrowed to image view type\n * }\n *\n * // Use $build to construct typed objects\n * const view = imageViewSchema.$build({ images: [...] })\n * // view.$type === 'app.bsky.embed.images#view'\n * ```\n */\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<{ [k: string]: unknown }>,\n>(nsid: N, hash: H, validator: S): TypedObjectSchema<$Type<N, H>, S>\nexport function typedObject<V extends { $type?: $Type }>(\n nsid: V extends { $type?: infer T extends string }\n ? T extends `${infer N}#${string}`\n ? N\n : T // (T is a \"main\" type, so already an NSID)\n : never,\n hash: V extends { $type?: infer T extends string }\n ? T extends `${string}#${infer H}`\n ? H\n : 'main'\n : never,\n validator: Validator<Omit<V, '$type'>>,\n): TypedObjectSchema<$TypeOf<V>, Validator<V>>\n/*@__NO_SIDE_EFFECTS__*/\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<{ [k: string]: unknown }>,\n>(nsid: N, hash: H, validator: S) {\n return new TypedObjectSchema<$Type<N, H>, S>($type(nsid, hash), validator)\n}\n"]}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { $Typed, InferInput, InferOutput, Schema, ValidationContext, Validator } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for validators that have a $type property.
|
|
4
|
+
*
|
|
5
|
+
* Used by typed objects and records to identify their type in unions.
|
|
6
|
+
*
|
|
7
|
+
* @template TInput - The input type (with optional $type)
|
|
8
|
+
* @template TOutput - The output type (with non-optional $type)
|
|
9
|
+
*/
|
|
2
10
|
export interface TypedObjectValidator<TInput extends {
|
|
3
11
|
$type?: string;
|
|
4
12
|
} = {
|
|
@@ -6,14 +14,60 @@ export interface TypedObjectValidator<TInput extends {
|
|
|
6
14
|
}, TOutput extends TInput = TInput> extends Validator<TInput, TOutput> {
|
|
7
15
|
$type: NonNullable<TOutput['$type']>;
|
|
8
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Function type that returns a typed object validator, used for lazy resolution.
|
|
19
|
+
*
|
|
20
|
+
* @template TValidator - The typed object validator type
|
|
21
|
+
*/
|
|
9
22
|
export type TypedRefGetter<out TValidator extends TypedObjectValidator> = () => TValidator;
|
|
23
|
+
/**
|
|
24
|
+
* Schema for referencing typed objects with lazy resolution.
|
|
25
|
+
*
|
|
26
|
+
* Used in typed unions to reference typed object schemas. Requires the
|
|
27
|
+
* `$type` field to be present and match the referenced schema's type.
|
|
28
|
+
* The referenced schema is resolved lazily to support circular references.
|
|
29
|
+
*
|
|
30
|
+
* @template TValidator - The referenced typed object validator type
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const ref = new TypedRefSchema(() => imageViewSchema)
|
|
35
|
+
* // ref.$type === 'app.bsky.embed.images#view'
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
10
38
|
export declare class TypedRefSchema<const TValidator extends TypedObjectValidator = TypedObjectValidator> extends Schema<$Typed<InferInput<TValidator>>, $Typed<InferOutput<TValidator>>> {
|
|
11
39
|
#private;
|
|
40
|
+
readonly type: "typedRef";
|
|
12
41
|
constructor(getter: TypedRefGetter<TValidator>);
|
|
13
42
|
get validator(): TValidator;
|
|
14
43
|
get $type(): TValidator['$type'];
|
|
15
44
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationFailure | import("../core.js").ValidationSuccess<InferInput<TValidator>>;
|
|
16
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates a reference to a typed object schema for use in typed unions.
|
|
48
|
+
*
|
|
49
|
+
* Unlike regular `ref()`, this requires the referenced schema to have a
|
|
50
|
+
* `$type` property, and validates that the input's `$type` matches.
|
|
51
|
+
*
|
|
52
|
+
* @param get - Function that returns the typed object validator
|
|
53
|
+
* @returns A new {@link TypedRefSchema} instance
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* // Reference to image embed view
|
|
58
|
+
* const imageRef = l.typedRef(() => imageViewSchema)
|
|
59
|
+
*
|
|
60
|
+
* // Use in a typed union
|
|
61
|
+
* const embedUnion = l.typedUnion([
|
|
62
|
+
* l.typedRef(() => imageViewSchema),
|
|
63
|
+
* l.typedRef(() => videoViewSchema),
|
|
64
|
+
* l.typedRef(() => externalViewSchema),
|
|
65
|
+
* ], true) // closed union
|
|
66
|
+
*
|
|
67
|
+
* // The $type is accessible on the ref
|
|
68
|
+
* console.log(imageRef.$type) // 'app.bsky.embed.images#view'
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
17
71
|
export declare function typedRef<const TValidator extends TypedObjectValidator>(get: TypedRefGetter<TValidator>): TypedRefSchema<TValidator>;
|
|
18
72
|
export declare function typedRef<TInput extends {
|
|
19
73
|
$type?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-ref.d.ts","sourceRoot":"","sources":["../../src/schema/typed-ref.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAEnB,MAAM,WAAW,oBAAoB,CACnC,MAAM,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,EACtD,OAAO,SAAS,MAAM,GAAG,MAAM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C,QAAQ,CAAC,IAAI,EAAG,UAAU,CAAS;gBAIvB,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC;IAS9C,IAAI,SAAS,IAAI,UAAU,CAE1B;IAED,IAAI,KAAK,IAAI,UAAU,CAAC,OAAO,CAAC,CAE/B;IAED,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAUzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,UAAU,SAAS,oBAAoB,EACpE,GAAG,EAAE,cAAc,CAAC,UAAU,CAAC,GAC9B,cAAc,CAAC,UAAU,CAAC,CAAA;AAC7B,wBAAgB,QAAQ,CACtB,MAAM,SAAS;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,EACjC,OAAO,SAAS,MAAM,GAAG,MAAM,EAE/B,GAAG,EAAE,cAAc,CAAC,oBAAoB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GACzD,cAAc,CAAC,oBAAoB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA"}
|
package/dist/schema/typed-ref.js
CHANGED
|
@@ -3,7 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TypedRefSchema = void 0;
|
|
4
4
|
exports.typedRef = typedRef;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for referencing typed objects with lazy resolution.
|
|
8
|
+
*
|
|
9
|
+
* Used in typed unions to reference typed object schemas. Requires the
|
|
10
|
+
* `$type` field to be present and match the referenced schema's type.
|
|
11
|
+
* The referenced schema is resolved lazily to support circular references.
|
|
12
|
+
*
|
|
13
|
+
* @template TValidator - The referenced typed object validator type
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const ref = new TypedRefSchema(() => imageViewSchema)
|
|
18
|
+
* // ref.$type === 'app.bsky.embed.images#view'
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class TypedRefSchema extends core_js_1.Schema {
|
|
22
|
+
type = 'typedRef';
|
|
7
23
|
#getter;
|
|
8
24
|
constructor(getter) {
|
|
9
25
|
// @NOTE In order to avoid circular dependency issues, we don't resolve
|