@atproto/lex-schema 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -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 +229 -2
- 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 +61 -1
- 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 +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +13 -25
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +89 -7
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- 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 +70 -0
- 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 +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -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 +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- 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 +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +4 -4
- 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 +236 -7
- 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 +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +14 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +124 -16
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- 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 +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
package/dist/schema/procedure.js
CHANGED
|
@@ -2,6 +2,30 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Procedure = void 0;
|
|
4
4
|
exports.procedure = procedure;
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Lexicon procedure (HTTP POST) endpoint definition.
|
|
7
|
+
*
|
|
8
|
+
* Procedures are operations that may modify state on the server.
|
|
9
|
+
* They have parameters, an input payload (request body), an output
|
|
10
|
+
* payload (response body), and optional error types.
|
|
11
|
+
*
|
|
12
|
+
* @template TNsid - The NSID identifying this procedure
|
|
13
|
+
* @template TParameters - The parameters schema type
|
|
14
|
+
* @template TInputPayload - The request body payload type
|
|
15
|
+
* @template TOutputPayload - The response body payload type
|
|
16
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const createPost = new Procedure(
|
|
21
|
+
* 'app.bsky.feed.post',
|
|
22
|
+
* l.params({}),
|
|
23
|
+
* l.jsonPayload({ text: l.string() }),
|
|
24
|
+
* l.jsonPayload({ uri: l.string(), cid: l.string() }),
|
|
25
|
+
* ['InvalidRecord']
|
|
26
|
+
* )
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
5
29
|
class Procedure {
|
|
6
30
|
nsid;
|
|
7
31
|
parameters;
|
|
@@ -18,6 +42,46 @@ class Procedure {
|
|
|
18
42
|
}
|
|
19
43
|
}
|
|
20
44
|
exports.Procedure = Procedure;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a procedure definition for a Lexicon POST endpoint.
|
|
47
|
+
*
|
|
48
|
+
* Procedures can modify server state. They accept both URL parameters
|
|
49
|
+
* and a request body (input payload).
|
|
50
|
+
*
|
|
51
|
+
* @param nsid - The NSID identifying this procedure endpoint
|
|
52
|
+
* @param parameters - Schema for URL query parameters
|
|
53
|
+
* @param input - Schema for request body payload
|
|
54
|
+
* @param output - Schema for response body payload
|
|
55
|
+
* @param errors - Optional array of error type strings
|
|
56
|
+
* @returns A new {@link Procedure} instance
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* // Create record procedure
|
|
61
|
+
* const createRecord = l.procedure(
|
|
62
|
+
* 'com.atproto.repo.createRecord',
|
|
63
|
+
* l.params({}),
|
|
64
|
+
* l.jsonPayload({
|
|
65
|
+
* repo: l.string({ format: 'at-identifier' }),
|
|
66
|
+
* collection: l.string({ format: 'nsid' }),
|
|
67
|
+
* record: l.unknown(),
|
|
68
|
+
* }),
|
|
69
|
+
* l.jsonPayload({
|
|
70
|
+
* uri: l.string({ format: 'at-uri' }),
|
|
71
|
+
* cid: l.string({ format: 'cid' }),
|
|
72
|
+
* }),
|
|
73
|
+
* ['InvalidRecord', 'RepoNotFound'],
|
|
74
|
+
* )
|
|
75
|
+
*
|
|
76
|
+
* // Procedure with binary input
|
|
77
|
+
* const uploadBlob = l.procedure(
|
|
78
|
+
* 'com.atproto.repo.uploadBlob',
|
|
79
|
+
* l.params({}),
|
|
80
|
+
* l.payload('*\/*', undefined), // Accept any content type
|
|
81
|
+
* l.jsonPayload({ blob: l.blob() }),
|
|
82
|
+
* )
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
21
85
|
/*@__NO_SIDE_EFFECTS__*/
|
|
22
86
|
function procedure(nsid, parameters, input, output, errors = undefined) {
|
|
23
87
|
return new Procedure(nsid, parameters, input, output, errors);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"procedure.js","sourceRoot":"","sources":["../../src/schema/procedure.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"procedure.js","sourceRoot":"","sources":["../../src/schema/procedure.ts"],"names":[],"mappings":";;;AAyFA,8BAQC;AA7FD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,SAAS;IAYT;IACA;IACA;IACA;IACA;IAPF,IAAI,GAAG,WAAoB,CAAA;IAEpC,YACW,IAAW,EACX,UAAuB,EACvB,KAAoB,EACpB,MAAsB,EACtB,MAAe;QAJf,SAAI,GAAJ,IAAI,CAAO;QACX,eAAU,GAAV,UAAU,CAAa;QACvB,UAAK,GAAL,KAAK,CAAe;QACpB,WAAM,GAAN,MAAM,CAAgB;QACtB,WAAM,GAAN,MAAM,CAAS;IACvB,CAAC;CACL;AAlBD,8BAkBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAwB;AACxB,SAAgB,SAAS,CAMvB,IAAO,EAAE,UAAa,EAAE,KAAQ,EAAE,MAAS,EAAE,SAAY,SAAc;IACvE,OAAO,IAAI,SAAS,CAAgB,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAC9E,CAAC","sourcesContent":["import { NsidString } from '../core.js'\nimport { ParamsSchema } from './params.js'\nimport { Payload } from './payload.js'\n\n/**\n * Represents a Lexicon procedure (HTTP POST) endpoint definition.\n *\n * Procedures are operations that may modify state on the server.\n * They have parameters, an input payload (request body), an output\n * payload (response body), and optional error types.\n *\n * @template TNsid - The NSID identifying this procedure\n * @template TParameters - The parameters schema type\n * @template TInputPayload - The request body payload type\n * @template TOutputPayload - The response body payload type\n * @template TErrors - Array of error type strings, or undefined\n *\n * @example\n * ```ts\n * const createPost = new Procedure(\n * 'app.bsky.feed.post',\n * l.params({}),\n * l.jsonPayload({ text: l.string() }),\n * l.jsonPayload({ uri: l.string(), cid: l.string() }),\n * ['InvalidRecord']\n * )\n * ```\n */\nexport class Procedure<\n const TNsid extends NsidString = NsidString,\n const TParameters extends ParamsSchema = ParamsSchema,\n const TInputPayload extends Payload = Payload,\n const TOutputPayload extends Payload = Payload,\n const TErrors extends undefined | readonly string[] =\n | undefined\n | readonly string[],\n> {\n readonly type = 'procedure' as const\n\n constructor(\n readonly nsid: TNsid,\n readonly parameters: TParameters,\n readonly input: TInputPayload,\n readonly output: TOutputPayload,\n readonly errors: TErrors,\n ) {}\n}\n\n/**\n * Creates a procedure definition for a Lexicon POST endpoint.\n *\n * Procedures can modify server state. They accept both URL parameters\n * and a request body (input payload).\n *\n * @param nsid - The NSID identifying this procedure endpoint\n * @param parameters - Schema for URL query parameters\n * @param input - Schema for request body payload\n * @param output - Schema for response body payload\n * @param errors - Optional array of error type strings\n * @returns A new {@link Procedure} instance\n *\n * @example\n * ```ts\n * // Create record procedure\n * const createRecord = l.procedure(\n * 'com.atproto.repo.createRecord',\n * l.params({}),\n * l.jsonPayload({\n * repo: l.string({ format: 'at-identifier' }),\n * collection: l.string({ format: 'nsid' }),\n * record: l.unknown(),\n * }),\n * l.jsonPayload({\n * uri: l.string({ format: 'at-uri' }),\n * cid: l.string({ format: 'cid' }),\n * }),\n * ['InvalidRecord', 'RepoNotFound'],\n * )\n *\n * // Procedure with binary input\n * const uploadBlob = l.procedure(\n * 'com.atproto.repo.uploadBlob',\n * l.params({}),\n * l.payload('*\\/*', undefined), // Accept any content type\n * l.jsonPayload({ blob: l.blob() }),\n * )\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function procedure<\n const N extends NsidString,\n const P extends ParamsSchema,\n const I extends Payload,\n const O extends Payload,\n const E extends undefined | readonly string[] = undefined,\n>(nsid: N, parameters: P, input: I, output: O, errors: E = undefined as E) {\n return new Procedure<N, P, I, O, E>(nsid, parameters, input, output, errors)\n}\n"]}
|
package/dist/schema/query.d.ts
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { NsidString } from '../core.js';
|
|
2
2
|
import { ParamsSchema } from './params.js';
|
|
3
3
|
import { Payload } from './payload.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Lexicon query (HTTP GET) endpoint definition.
|
|
6
|
+
*
|
|
7
|
+
* Queries are read-only operations that retrieve data from a server.
|
|
8
|
+
* They have parameters (passed as URL query parameters), an output
|
|
9
|
+
* payload, and optional error types.
|
|
10
|
+
*
|
|
11
|
+
* @template TNsid - The NSID identifying this query
|
|
12
|
+
* @template TParameters - The parameters schema type
|
|
13
|
+
* @template TOutputPayload - The output payload type
|
|
14
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const getPostQuery = new Query(
|
|
19
|
+
* 'app.bsky.feed.getPost',
|
|
20
|
+
* l.params({ uri: l.string({ format: 'at-uri' }) }),
|
|
21
|
+
* l.payload('application/json', postSchema),
|
|
22
|
+
* ['NotFound']
|
|
23
|
+
* )
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
4
26
|
export declare class Query<const TNsid extends NsidString = NsidString, const TParameters extends ParamsSchema = ParamsSchema, const TOutputPayload extends Payload = Payload, const TErrors extends undefined | readonly string[] = undefined | readonly string[]> {
|
|
5
27
|
readonly nsid: TNsid;
|
|
6
28
|
readonly parameters: TParameters;
|
|
@@ -9,5 +31,38 @@ export declare class Query<const TNsid extends NsidString = NsidString, const TP
|
|
|
9
31
|
readonly type: "query";
|
|
10
32
|
constructor(nsid: TNsid, parameters: TParameters, output: TOutputPayload, errors: TErrors);
|
|
11
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a query definition for a Lexicon GET endpoint.
|
|
36
|
+
*
|
|
37
|
+
* Queries retrieve data without side effects. Parameters are sent as
|
|
38
|
+
* URL query string parameters.
|
|
39
|
+
*
|
|
40
|
+
* @param nsid - The NSID identifying this query endpoint
|
|
41
|
+
* @param parameters - Schema for URL query parameters
|
|
42
|
+
* @param output - Expected response payload schema
|
|
43
|
+
* @param errors - Optional array of error type strings
|
|
44
|
+
* @returns A new {@link Query} instance
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // Simple query with JSON output
|
|
49
|
+
* const getProfile = l.query(
|
|
50
|
+
* 'app.bsky.actor.getProfile',
|
|
51
|
+
* l.params({ actor: l.string({ format: 'at-identifier' }) }),
|
|
52
|
+
* l.jsonPayload({ displayName: l.string(), handle: l.string() }),
|
|
53
|
+
* )
|
|
54
|
+
*
|
|
55
|
+
* // Query with pagination and errors
|
|
56
|
+
* const getTimeline = l.query(
|
|
57
|
+
* 'app.bsky.feed.getTimeline',
|
|
58
|
+
* l.params({
|
|
59
|
+
* limit: l.optional(l.integer({ minimum: 1, maximum: 100 })),
|
|
60
|
+
* cursor: l.optional(l.string()),
|
|
61
|
+
* }),
|
|
62
|
+
* l.jsonPayload({ feed: l.array(feedItemSchema), cursor: l.optional(l.string()) }),
|
|
63
|
+
* ['BlockedActor', 'BlockedByActor'],
|
|
64
|
+
* )
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
12
67
|
export declare function query<const N extends NsidString, const P extends ParamsSchema, const O extends Payload, const E extends undefined | readonly string[] = undefined>(nsid: N, parameters: P, output: O, errors?: E): Query<N, P, O, E>;
|
|
13
68
|
//# sourceMappingURL=query.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../src/schema/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,qBAAa,KAAK,CAChB,KAAK,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU,EAC3C,KAAK,CAAC,WAAW,SAAS,YAAY,GAAG,YAAY,EACrD,KAAK,CAAC,cAAc,SAAS,OAAO,GAAG,OAAO,EAC9C,KAAK,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAC/C,SAAS,GACT,SAAS,MAAM,EAAE;IAKnB,QAAQ,CAAC,IAAI,EAAE,KAAK;IACpB,QAAQ,CAAC,UAAU,EAAE,WAAW;IAChC,QAAQ,CAAC,MAAM,EAAE,cAAc;IAC/B,QAAQ,CAAC,MAAM,EAAE,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EACzD,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAE,CAAkB,qBAE9D"}
|
package/dist/schema/query.js
CHANGED
|
@@ -2,6 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Query = void 0;
|
|
4
4
|
exports.query = query;
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Lexicon query (HTTP GET) endpoint definition.
|
|
7
|
+
*
|
|
8
|
+
* Queries are read-only operations that retrieve data from a server.
|
|
9
|
+
* They have parameters (passed as URL query parameters), an output
|
|
10
|
+
* payload, and optional error types.
|
|
11
|
+
*
|
|
12
|
+
* @template TNsid - The NSID identifying this query
|
|
13
|
+
* @template TParameters - The parameters schema type
|
|
14
|
+
* @template TOutputPayload - The output payload type
|
|
15
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const getPostQuery = new Query(
|
|
20
|
+
* 'app.bsky.feed.getPost',
|
|
21
|
+
* l.params({ uri: l.string({ format: 'at-uri' }) }),
|
|
22
|
+
* l.payload('application/json', postSchema),
|
|
23
|
+
* ['NotFound']
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
5
27
|
class Query {
|
|
6
28
|
nsid;
|
|
7
29
|
parameters;
|
|
@@ -16,6 +38,39 @@ class Query {
|
|
|
16
38
|
}
|
|
17
39
|
}
|
|
18
40
|
exports.Query = Query;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a query definition for a Lexicon GET endpoint.
|
|
43
|
+
*
|
|
44
|
+
* Queries retrieve data without side effects. Parameters are sent as
|
|
45
|
+
* URL query string parameters.
|
|
46
|
+
*
|
|
47
|
+
* @param nsid - The NSID identifying this query endpoint
|
|
48
|
+
* @param parameters - Schema for URL query parameters
|
|
49
|
+
* @param output - Expected response payload schema
|
|
50
|
+
* @param errors - Optional array of error type strings
|
|
51
|
+
* @returns A new {@link Query} instance
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Simple query with JSON output
|
|
56
|
+
* const getProfile = l.query(
|
|
57
|
+
* 'app.bsky.actor.getProfile',
|
|
58
|
+
* l.params({ actor: l.string({ format: 'at-identifier' }) }),
|
|
59
|
+
* l.jsonPayload({ displayName: l.string(), handle: l.string() }),
|
|
60
|
+
* )
|
|
61
|
+
*
|
|
62
|
+
* // Query with pagination and errors
|
|
63
|
+
* const getTimeline = l.query(
|
|
64
|
+
* 'app.bsky.feed.getTimeline',
|
|
65
|
+
* l.params({
|
|
66
|
+
* limit: l.optional(l.integer({ minimum: 1, maximum: 100 })),
|
|
67
|
+
* cursor: l.optional(l.string()),
|
|
68
|
+
* }),
|
|
69
|
+
* l.jsonPayload({ feed: l.array(feedItemSchema), cursor: l.optional(l.string()) }),
|
|
70
|
+
* ['BlockedActor', 'BlockedByActor'],
|
|
71
|
+
* )
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
19
74
|
/*@__NO_SIDE_EFFECTS__*/
|
|
20
75
|
function query(nsid, parameters, output, errors = undefined) {
|
|
21
76
|
return new Query(nsid, parameters, output, errors);
|
package/dist/schema/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/schema/query.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/schema/query.ts"],"names":[],"mappings":";;;AA8EA,sBAOC;AAjFD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,KAAK;IAWL;IACA;IACA;IACA;IANF,IAAI,GAAG,OAAgB,CAAA;IAEhC,YACW,IAAW,EACX,UAAuB,EACvB,MAAsB,EACtB,MAAe;QAHf,SAAI,GAAJ,IAAI,CAAO;QACX,eAAU,GAAV,UAAU,CAAa;QACvB,WAAM,GAAN,MAAM,CAAgB;QACtB,WAAM,GAAN,MAAM,CAAS;IACvB,CAAC;CACL;AAhBD,sBAgBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAwB;AACxB,SAAgB,KAAK,CAKnB,IAAO,EAAE,UAAa,EAAE,MAAS,EAAE,SAAY,SAAc;IAC7D,OAAO,IAAI,KAAK,CAAa,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAChE,CAAC","sourcesContent":["import { NsidString } from '../core.js'\nimport { ParamsSchema } from './params.js'\nimport { Payload } from './payload.js'\n\n/**\n * Represents a Lexicon query (HTTP GET) endpoint definition.\n *\n * Queries are read-only operations that retrieve data from a server.\n * They have parameters (passed as URL query parameters), an output\n * payload, and optional error types.\n *\n * @template TNsid - The NSID identifying this query\n * @template TParameters - The parameters schema type\n * @template TOutputPayload - The output payload type\n * @template TErrors - Array of error type strings, or undefined\n *\n * @example\n * ```ts\n * const getPostQuery = new Query(\n * 'app.bsky.feed.getPost',\n * l.params({ uri: l.string({ format: 'at-uri' }) }),\n * l.payload('application/json', postSchema),\n * ['NotFound']\n * )\n * ```\n */\nexport class Query<\n const TNsid extends NsidString = NsidString,\n const TParameters extends ParamsSchema = ParamsSchema,\n const TOutputPayload extends Payload = Payload,\n const TErrors extends undefined | readonly string[] =\n | undefined\n | readonly string[],\n> {\n readonly type = 'query' as const\n\n constructor(\n readonly nsid: TNsid,\n readonly parameters: TParameters,\n readonly output: TOutputPayload,\n readonly errors: TErrors,\n ) {}\n}\n\n/**\n * Creates a query definition for a Lexicon GET endpoint.\n *\n * Queries retrieve data without side effects. Parameters are sent as\n * URL query string parameters.\n *\n * @param nsid - The NSID identifying this query endpoint\n * @param parameters - Schema for URL query parameters\n * @param output - Expected response payload schema\n * @param errors - Optional array of error type strings\n * @returns A new {@link Query} instance\n *\n * @example\n * ```ts\n * // Simple query with JSON output\n * const getProfile = l.query(\n * 'app.bsky.actor.getProfile',\n * l.params({ actor: l.string({ format: 'at-identifier' }) }),\n * l.jsonPayload({ displayName: l.string(), handle: l.string() }),\n * )\n *\n * // Query with pagination and errors\n * const getTimeline = l.query(\n * 'app.bsky.feed.getTimeline',\n * l.params({\n * limit: l.optional(l.integer({ minimum: 1, maximum: 100 })),\n * cursor: l.optional(l.string()),\n * }),\n * l.jsonPayload({ feed: l.array(feedItemSchema), cursor: l.optional(l.string()) }),\n * ['BlockedActor', 'BlockedByActor'],\n * )\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function query<\n const N extends NsidString,\n const P extends ParamsSchema,\n const O extends Payload,\n const E extends undefined | readonly string[] = undefined,\n>(nsid: N, parameters: P, output: O, errors: E = undefined as E) {\n return new Query<N, P, O, E>(nsid, parameters, output, errors)\n}\n"]}
|
package/dist/schema/record.d.ts
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
import { $Typed, InferInput, InferOutput, LexiconRecordKey, NsidString, Schema, TidString, Unknown$TypedObject, ValidationContext, Validator } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Infers the record key type from a RecordSchema.
|
|
4
|
+
*
|
|
5
|
+
* @template R - The RecordSchema type
|
|
6
|
+
*/
|
|
2
7
|
export type InferRecordKey<R extends RecordSchema> = R extends RecordSchema<infer TKey> ? RecordKeySchemaOutput<TKey> : never;
|
|
8
|
+
/**
|
|
9
|
+
* Schema for AT Protocol records with a type identifier and key constraints.
|
|
10
|
+
*
|
|
11
|
+
* Records are the primary data unit in AT Protocol. Each record has a `$type`
|
|
12
|
+
* field identifying its Lexicon schema, and is stored at a specific key
|
|
13
|
+
* (TID, NSID, or other format) in a repository.
|
|
14
|
+
*
|
|
15
|
+
* @template TKey - The record key type ('tid', 'nsid', 'any', or 'literal:...')
|
|
16
|
+
* @template TType - The NSID string identifying this record type
|
|
17
|
+
* @template TShape - The validator type for the record's data shape
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const postSchema = new RecordSchema(
|
|
22
|
+
* 'tid',
|
|
23
|
+
* 'app.bsky.feed.post',
|
|
24
|
+
* l.object({ text: l.string(), createdAt: l.string() })
|
|
25
|
+
* )
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
3
28
|
export declare class RecordSchema<const TKey extends LexiconRecordKey = any, const TType extends NsidString = any, const TShape extends Validator<{
|
|
4
29
|
[k: string]: unknown;
|
|
5
30
|
}> = any> extends Schema<$Typed<InferInput<TShape>, TType>, $Typed<InferOutput<TShape>, TType>> {
|
|
@@ -37,15 +62,45 @@ export type RecordKeySchema<Key extends LexiconRecordKey> = Schema<RecordKeySche
|
|
|
37
62
|
*/
|
|
38
63
|
type AsNsid<T> = T extends `${string}#${string}` ? never : T;
|
|
39
64
|
/**
|
|
65
|
+
* Creates a record schema for AT Protocol records.
|
|
66
|
+
*
|
|
67
|
+
* Records are the primary data unit in AT Protocol repositories. They have
|
|
68
|
+
* a `$type` field identifying their Lexicon schema, and are stored at keys
|
|
69
|
+
* following a specific format (TID, NSID, etc.).
|
|
70
|
+
*
|
|
40
71
|
* This function offers two overloads:
|
|
41
|
-
* - One that
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* -
|
|
47
|
-
*
|
|
48
|
-
*
|
|
72
|
+
* - One that infers the output type from the provided arguments (does not
|
|
73
|
+
* support circular references)
|
|
74
|
+
* - One with an explicitly defined interface for use with codegen and
|
|
75
|
+
* circular references
|
|
76
|
+
*
|
|
77
|
+
* @param key - The record key type: 'tid', 'nsid', 'any', or 'literal:value'
|
|
78
|
+
* @param type - The NSID identifying this record type (e.g., 'app.bsky.feed.post')
|
|
79
|
+
* @param validator - Schema validator for the record's properties
|
|
80
|
+
* @returns A new {@link RecordSchema} instance
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* // Post record with TID key
|
|
85
|
+
* const postSchema = l.record('tid', 'app.bsky.feed.post', l.object({
|
|
86
|
+
* text: l.string({ maxGraphemes: 300 }),
|
|
87
|
+
* createdAt: l.string({ format: 'datetime' }),
|
|
88
|
+
* reply: l.optional(l.object({
|
|
89
|
+
* root: l.ref(() => strongRefSchema),
|
|
90
|
+
* parent: l.ref(() => strongRefSchema),
|
|
91
|
+
* })),
|
|
92
|
+
* }))
|
|
93
|
+
*
|
|
94
|
+
* // Profile record with literal 'self' key
|
|
95
|
+
* const profileSchema = l.record('literal:self', 'app.bsky.actor.profile', l.object({
|
|
96
|
+
* displayName: l.optional(l.string({ maxGraphemes: 64 })),
|
|
97
|
+
* description: l.optional(l.string({ maxGraphemes: 256 })),
|
|
98
|
+
* avatar: l.optional(l.blob({ accept: ['image/*'] })),
|
|
99
|
+
* }))
|
|
100
|
+
*
|
|
101
|
+
* // Build a record with automatic $type injection
|
|
102
|
+
* const post = postSchema.build({ text: 'Hello!', createdAt: new Date().toISOString() })
|
|
103
|
+
* ```
|
|
49
104
|
*/
|
|
50
105
|
export declare function record<const K extends LexiconRecordKey, const T extends NsidString, const S extends Validator<{
|
|
51
106
|
[k: string]: unknown;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../../src/schema/record.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EAEN,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,UAAU,EACV,MAAM,EACN,SAAS,EACT,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAInB,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,YAAY,IAC/C,CAAC,SAAS,YAAY,CAAC,MAAM,IAAI,CAAC,GAAG,qBAAqB,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;AAE1E,qBAAa,YAAY,CACvB,KAAK,CAAC,IAAI,SAAS,gBAAgB,GAAG,GAAG,EACzC,KAAK,CAAC,KAAK,SAAS,UAAU,GAAG,GAAG,EACpC,KAAK,CAAC,MAAM,SAAS,SA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K,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,MAAM,CACpB,KAAK,CAAC,CAAC,SAAS,gBAAgB,EAChC,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,SAAS,CAAC;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC,EACnD,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;AAC/D,wBAAgB,MAAM,CACpB,KAAK,CAAC,CAAC,SAAS,gBAAgB,EAChC,KAAK,CAAC,CAAC,SAAS;IAAE,KAAK,EAAE,UAAU,CAAA;CAAE,EAErC,GAAG,EAAE,CAAC,EACN,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EACxB,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GACrC,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA"}
|
package/dist/schema/record.js
CHANGED
|
@@ -5,6 +5,26 @@ exports.record = record;
|
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
6
|
const literal_js_1 = require("./literal.js");
|
|
7
7
|
const string_js_1 = require("./string.js");
|
|
8
|
+
/**
|
|
9
|
+
* Schema for AT Protocol records with a type identifier and key constraints.
|
|
10
|
+
*
|
|
11
|
+
* Records are the primary data unit in AT Protocol. Each record has a `$type`
|
|
12
|
+
* field identifying its Lexicon schema, and is stored at a specific key
|
|
13
|
+
* (TID, NSID, or other format) in a repository.
|
|
14
|
+
*
|
|
15
|
+
* @template TKey - The record key type ('tid', 'nsid', 'any', or 'literal:...')
|
|
16
|
+
* @template TType - The NSID string identifying this record type
|
|
17
|
+
* @template TShape - The validator type for the record's data shape
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const postSchema = new RecordSchema(
|
|
22
|
+
* 'tid',
|
|
23
|
+
* 'app.bsky.feed.post',
|
|
24
|
+
* l.object({ text: l.string(), createdAt: l.string() })
|
|
25
|
+
* )
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
8
28
|
class RecordSchema extends core_js_1.Schema {
|
|
9
29
|
key;
|
|
10
30
|
$type;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../src/schema/record.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../src/schema/record.ts"],"names":[],"mappings":";;;AAoMA,wBAMC;AA1MD,wCAYmB;AACnB,6CAAsC;AACtC,2CAAoC;AAUpC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,YAIX,SAAQ,gBAGT;IAIY;IACA;IACA;IALX,SAAS,CAAuB;IAEhC,YACW,GAAS,EACT,KAAY,EACZ,MAAc;QAEvB,KAAK,EAAE,CAAA;QAJE,QAAG,GAAH,GAAG,CAAM;QACT,UAAK,GAAL,KAAK,CAAO;QACZ,WAAM,GAAN,MAAM,CAAQ;QAGvB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,CAAA;IACjC,CAAC;IAED,QAAQ,CACN,KAAQ;QAIR,OAAO,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAA;IACnC,CAAC;IAED,KAAK,CACH,KAAsC;QAEtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,gBAAM,EAAC,KAAK,EAAE,IAAI,CAAC,KAAK,C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AC5C,IAAI,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAA+B,CAAA;QACxD,IAAI,KAAK,KAAK,MAAM;YAAE,OAAO,iBAAwB,CAAA;QACrD,OAAO,IAAA,oBAAO,EAAC,KAAK,CAAC,CAAA;IACvB,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,gCAAgC,GAAG,EAAE,CAAC,CAAA;AACxD,CAAC;AA6DD,wBAAwB;AACxB,SAAgB,MAAM,CAIpB,GAAM,EAAE,IAAO,EAAE,SAAY;IAC7B,OAAO,IAAI,YAAY,CAAU,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC,CAAA;AACxD,CAAC","sourcesContent":["import {\n $Typed,\n $typed,\n InferInput,\n InferOutput,\n LexiconRecordKey,\n NsidString,\n Schema,\n TidString,\n Unknown$TypedObject,\n ValidationContext,\n Validator,\n} from '../core.js'\nimport { literal } from './literal.js'\nimport { string } from './string.js'\n\n/**\n * Infers the record key type from a RecordSchema.\n *\n * @template R - The RecordSchema type\n */\nexport type InferRecordKey<R extends RecordSchema> =\n R extends RecordSchema<infer TKey> ? RecordKeySchemaOutput<TKey> : never\n\n/**\n * Schema for AT Protocol records with a type identifier and key constraints.\n *\n * Records are the primary data unit in AT Protocol. Each record has a `$type`\n * field identifying its Lexicon schema, and is stored at a specific key\n * (TID, NSID, or other format) in a repository.\n *\n * @template TKey - The record key type ('tid', 'nsid', 'any', or 'literal:...')\n * @template TType - The NSID string identifying this record type\n * @template TShape - The validator type for the record's data shape\n *\n * @example\n * ```ts\n * const postSchema = new RecordSchema(\n * 'tid',\n * 'app.bsky.feed.post',\n * l.object({ text: l.string(), createdAt: l.string() })\n * )\n * ```\n */\nexport class RecordSchema<\n const TKey extends LexiconRecordKey = any,\n const TType extends NsidString = any,\n const TShape extends Validator<{ [k: string]: unknown }> = any,\n> extends Schema<\n $Typed<InferInput<TShape>, TType>,\n $Typed<InferOutput<TShape>, TType>\n> {\n keySchema: RecordKeySchema<TKey>\n\n constructor(\n readonly key: TKey,\n readonly $type: TType,\n readonly schema: TShape,\n ) {\n super()\n this.keySchema = recordKey(key)\n }\n\n isTypeOf<X extends { $type?: unknown }>(\n value: X,\n ): value is X extends { $type: TType }\n ? X\n : $Typed<Exclude<X, Unknown$TypedObject>, TType> {\n return 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))\n }\n\n $isTypeOf<X extends { $type?: unknown }>(value: X) {\n return this.isTypeOf<X>(value)\n }\n\n $build(input: Omit<InferInput<this>, '$type'>) {\n return this.build(input)\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const result = ctx.validate(input, this.schema)\n\n if (!result.success) {\n return result\n }\n\n if (result.value.$type !== this.$type) {\n return ctx.issueInvalidPropertyValue(result.value, '$type', [this.$type])\n }\n\n return result\n }\n}\n\nexport type RecordKeySchemaOutput<Key extends LexiconRecordKey> =\n Key extends 'any'\n ? string\n : Key extends 'tid'\n ? TidString\n : Key extends 'nsid'\n ? NsidString\n : Key extends `literal:${infer L extends string}`\n ? L\n : never\n\nexport type RecordKeySchema<Key extends LexiconRecordKey> = Schema<\n RecordKeySchemaOutput<Key>\n>\n\nconst keySchema = string({ minLength: 1 })\nconst tidSchema = string({ format: 'tid' })\nconst nsidSchema = string({ format: 'nsid' })\nconst selfLiteralSchema = literal('self')\n\nfunction recordKey<Key extends LexiconRecordKey>(\n key: Key,\n): RecordKeySchema<Key> {\n // @NOTE Use cached instances for common schemas\n if (key === 'any') return keySchema as any\n if (key === 'tid') return tidSchema as any\n if (key === 'nsid') return nsidSchema as any\n if (key.startsWith('literal:')) {\n const value = key.slice(8) as RecordKeySchemaOutput<Key>\n if (value === 'self') return selfLiteralSchema as any\n return literal(value)\n }\n\n throw new Error(`Unsupported record key type: ${key}`)\n}\n\n/**\n * Ensures that a `$type` used in a record is a valid NSID (i.e. no fragment).\n */\ntype AsNsid<T> = T extends `${string}#${string}` ? never : T\n\n/**\n * Creates a record schema for AT Protocol records.\n *\n * Records are the primary data unit in AT Protocol repositories. They have\n * a `$type` field identifying their Lexicon schema, and are stored at keys\n * following a specific format (TID, NSID, etc.).\n *\n * This function offers two overloads:\n * - One that infers the output type from the provided arguments (does not\n * support circular references)\n * - One with an explicitly defined interface for use with codegen and\n * circular references\n *\n * @param key - The record key type: 'tid', 'nsid', 'any', or 'literal:value'\n * @param type - The NSID identifying this record type (e.g., 'app.bsky.feed.post')\n * @param validator - Schema validator for the record's properties\n * @returns A new {@link RecordSchema} instance\n *\n * @example\n * ```ts\n * // Post record with TID key\n * const postSchema = l.record('tid', 'app.bsky.feed.post', l.object({\n * text: l.string({ maxGraphemes: 300 }),\n * createdAt: l.string({ format: 'datetime' }),\n * reply: l.optional(l.object({\n * root: l.ref(() => strongRefSchema),\n * parent: l.ref(() => strongRefSchema),\n * })),\n * }))\n *\n * // Profile record with literal 'self' key\n * const profileSchema = l.record('literal:self', 'app.bsky.actor.profile', l.object({\n * displayName: l.optional(l.string({ maxGraphemes: 64 })),\n * description: l.optional(l.string({ maxGraphemes: 256 })),\n * avatar: l.optional(l.blob({ accept: ['image/*'] })),\n * }))\n *\n * // Build a record with automatic $type injection\n * const post = postSchema.build({ text: 'Hello!', createdAt: new Date().toISOString() })\n * ```\n */\nexport function record<\n const K extends LexiconRecordKey,\n const T extends NsidString,\n const S extends Validator<{ [k: string]: unknown }>,\n>(key: K, type: AsNsid<T>, validator: S): RecordSchema<K, T, S>\nexport function record<\n const K extends LexiconRecordKey,\n const V extends { $type: NsidString },\n>(\n key: K,\n type: AsNsid<V['$type']>,\n validator: Validator<Omit<V, '$type'>>,\n): RecordSchema<K, V['$type'], Validator<Omit<V, '$type'>>>\n/*@__NO_SIDE_EFFECTS__*/\nexport function record<\n const K extends LexiconRecordKey,\n const T extends NsidString,\n const S extends Validator<{ [k: string]: unknown }>,\n>(key: K, type: T, validator: S) {\n return new RecordSchema<K, T, S>(key, type, validator)\n}\n"]}
|
package/dist/schema/ref.d.ts
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import { InferInput, InferOutput, Schema, ValidationContext, Validator, WrappedValidator } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Function type that returns a validator, used for lazy schema resolution.
|
|
4
|
+
*
|
|
5
|
+
* @template TValidator - The validator type that will be returned
|
|
6
|
+
*/
|
|
2
7
|
export type RefSchemaGetter<out TValidator extends Validator> = () => TValidator;
|
|
8
|
+
/**
|
|
9
|
+
* Schema for creating references to other schemas with lazy resolution.
|
|
10
|
+
*
|
|
11
|
+
* Useful for handling circular references or breaking module dependency cycles.
|
|
12
|
+
* The referenced schema is resolved lazily when first needed for validation.
|
|
13
|
+
*
|
|
14
|
+
* @template TValidator - The referenced validator type
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Self-referential schema for tree structure
|
|
19
|
+
* const nodeSchema = l.object({
|
|
20
|
+
* value: l.string(),
|
|
21
|
+
* children: l.array(l.ref(() => nodeSchema)),
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
3
25
|
export declare class RefSchema<const TValidator extends Validator> extends Schema<InferInput<TValidator>, InferOutput<TValidator>, TValidator['__lex']> implements WrappedValidator<TValidator> {
|
|
4
26
|
#private;
|
|
5
27
|
constructor(getter: RefSchemaGetter<TValidator>);
|
|
@@ -7,6 +29,34 @@ export declare class RefSchema<const TValidator extends Validator> extends Schem
|
|
|
7
29
|
unwrap(): TValidator;
|
|
8
30
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<InferInput<TValidator>>;
|
|
9
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a reference schema with lazy resolution.
|
|
34
|
+
*
|
|
35
|
+
* Allows referencing schemas that may not be defined yet, enabling
|
|
36
|
+
* circular references and breaking dependency cycles. The getter function
|
|
37
|
+
* is called lazily when validation is first performed.
|
|
38
|
+
*
|
|
39
|
+
* @param get - Function that returns the referenced validator
|
|
40
|
+
* @returns A new {@link RefSchema} instance
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // Circular reference - tree node that contains children of the same type
|
|
45
|
+
* const treeNodeSchema = l.object({
|
|
46
|
+
* name: l.string(),
|
|
47
|
+
* children: l.optional(l.array(l.ref(() => treeNodeSchema))),
|
|
48
|
+
* })
|
|
49
|
+
*
|
|
50
|
+
* // Cross-module reference
|
|
51
|
+
* const commentSchema = l.object({
|
|
52
|
+
* text: l.string(),
|
|
53
|
+
* author: l.ref(() => userSchema), // userSchema defined elsewhere
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* // Explicitly typed reference
|
|
57
|
+
* const itemSchema = l.ref<Item>(() => complexItemSchema)
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
10
60
|
export declare function ref<const TValidator extends Validator>(get: RefSchemaGetter<TValidator>): RefSchema<TValidator>;
|
|
11
61
|
export declare function ref<TInput, TOutput extends TInput = TInput>(get: RefSchemaGetter<Validator<TInput, TOutput>>): RefSchema<Validator<TInput, TOutput>>;
|
|
12
62
|
//# sourceMappingURL=ref.d.ts.map
|
package/dist/schema/ref.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../../src/schema/ref.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACT,gBAAgB,EACjB,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,eAAe,CAAC,GAAG,CAAC,UAAU,SAAS,SAAS,IAAI,MAAM,UAAU,CAAA;AAEhF,qBAAa,SAAS,CAAC,KAAK,CAAC,UAAU,SAAS,SAAS,CACvD,SAAQ,MAAM,CACZ,UAAU,CAAC,UAAU,CAAC,EACt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GAAG,EAAE,eAAe,CAAC,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC/C,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA"}
|
package/dist/schema/ref.js
CHANGED
|
@@ -3,6 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RefSchema = void 0;
|
|
4
4
|
exports.ref = ref;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for creating references to other schemas with lazy resolution.
|
|
8
|
+
*
|
|
9
|
+
* Useful for handling circular references or breaking module dependency cycles.
|
|
10
|
+
* The referenced schema is resolved lazily when first needed for validation.
|
|
11
|
+
*
|
|
12
|
+
* @template TValidator - The referenced validator type
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // Self-referential schema for tree structure
|
|
17
|
+
* const nodeSchema = l.object({
|
|
18
|
+
* value: l.string(),
|
|
19
|
+
* children: l.array(l.ref(() => nodeSchema)),
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
6
23
|
class RefSchema extends core_js_1.Schema {
|
|
7
24
|
#getter;
|
|
8
25
|
constructor(getter) {
|
package/dist/schema/ref.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../src/schema/ref.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../src/schema/ref.ts"],"names":[],"mappings":";;;AAoGA,kBAIC;AAxGD,wCAOmB;AASnB;;;;;;;;;;;;;;;;GAgBG;AACH,MAAa,SACX,SAAQ,gBAIP;IAGD,OAAO,CAA6B;IAEpC,YAAY,MAAmC;QAC7C,uEAAuE;QACvE,sEAAsE;QAEtE,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,SAAS,CAAA;IACvB,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;CACF;AA9BD,8BA8BC;AAqCD,SAAgB,GAAG,CACjB,GAAgC;IAEhC,OAAO,IAAI,SAAS,CAAa,GAAG,CAAC,CAAA;AACvC,CAAC","sourcesContent":["import {\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n WrappedValidator,\n} from '../core.js'\n\n/**\n * Function type that returns a validator, used for lazy schema resolution.\n *\n * @template TValidator - The validator type that will be returned\n */\nexport type RefSchemaGetter<out TValidator extends Validator> = () => TValidator\n\n/**\n * Schema for creating references to other schemas with lazy resolution.\n *\n * Useful for handling circular references or breaking module dependency cycles.\n * The referenced schema is resolved lazily when first needed for validation.\n *\n * @template TValidator - The referenced validator type\n *\n * @example\n * ```ts\n * // Self-referential schema for tree structure\n * const nodeSchema = l.object({\n * value: l.string(),\n * children: l.array(l.ref(() => nodeSchema)),\n * })\n * ```\n */\nexport class RefSchema<const TValidator extends Validator>\n extends Schema<\n InferInput<TValidator>,\n InferOutput<TValidator>,\n TValidator['__lex']\n >\n implements WrappedValidator<TValidator>\n{\n #getter: RefSchemaGetter<TValidator>\n\n constructor(getter: RefSchemaGetter<TValidator>) {\n // @NOTE In order to avoid circular dependency issues, we don't resolve\n // the schema here. Instead, we resolve it lazily when first accessed.\n\n super()\n\n this.#getter = getter\n }\n\n get validator(): TValidator {\n return this.#getter.call(null)\n }\n\n unwrap(): TValidator {\n return this.validator\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n return ctx.validate(input, this.validator)\n }\n}\n\n/**\n * Creates a reference schema with lazy resolution.\n *\n * Allows referencing schemas that may not be defined yet, enabling\n * circular references and breaking dependency cycles. The getter function\n * is called lazily when validation is first performed.\n *\n * @param get - Function that returns the referenced validator\n * @returns A new {@link RefSchema} instance\n *\n * @example\n * ```ts\n * // Circular reference - tree node that contains children of the same type\n * const treeNodeSchema = l.object({\n * name: l.string(),\n * children: l.optional(l.array(l.ref(() => treeNodeSchema))),\n * })\n *\n * // Cross-module reference\n * const commentSchema = l.object({\n * text: l.string(),\n * author: l.ref(() => userSchema), // userSchema defined elsewhere\n * })\n *\n * // Explicitly typed reference\n * const itemSchema = l.ref<Item>(() => complexItemSchema)\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function ref<const TValidator extends Validator>(\n get: RefSchemaGetter<TValidator>,\n): RefSchema<TValidator>\nexport function ref<TInput, TOutput extends TInput = TInput>(\n get: RefSchemaGetter<Validator<TInput, TOutput>>,\n): RefSchema<Validator<TInput, TOutput>>\nexport function ref<const TValidator extends Validator>(\n get: RefSchemaGetter<TValidator>,\n) {\n return new RefSchema<TValidator>(get)\n}\n"]}
|
package/dist/schema/refine.d.ts
CHANGED
|
@@ -1,32 +1,81 @@
|
|
|
1
1
|
import { InferInput, PropertyKey, Validator } from '../core.js';
|
|
2
2
|
import { CustomAssertionContext } from './custom.js';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for a refinement check that validates a condition.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type being validated
|
|
7
|
+
* @property check - Function that returns true if the value passes the check
|
|
8
|
+
* @property message - Error message when the check fails
|
|
9
|
+
* @property path - Optional path to associate with the error
|
|
10
|
+
*/
|
|
3
11
|
export type RefinementCheck<T> = {
|
|
4
12
|
check: (value: T, ctx: CustomAssertionContext) => boolean;
|
|
5
13
|
message: string;
|
|
6
14
|
path?: PropertyKey | readonly PropertyKey[];
|
|
7
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Configuration for a refinement assertion that narrows the type.
|
|
18
|
+
*
|
|
19
|
+
* @template T - The input type being validated
|
|
20
|
+
* @template Out - The narrowed output type
|
|
21
|
+
* @property check - Type guard function that narrows the type
|
|
22
|
+
* @property message - Error message when the assertion fails
|
|
23
|
+
* @property path - Optional path to associate with the error
|
|
24
|
+
*/
|
|
8
25
|
export type RefinementAssertion<T, Out extends T> = {
|
|
9
26
|
check: (this: null, value: T, ctx: CustomAssertionContext) => value is Out;
|
|
10
27
|
message: string;
|
|
11
28
|
path?: PropertyKey | readonly PropertyKey[];
|
|
12
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* Infers the input type from a refinement configuration.
|
|
32
|
+
*
|
|
33
|
+
* @template R - The refinement type
|
|
34
|
+
*/
|
|
13
35
|
export type InferRefinement<R> = R extends RefinementCheck<infer T> ? T : R extends RefinementAssertion<infer T, any> ? T : never;
|
|
36
|
+
/**
|
|
37
|
+
* Union type of refinement check or assertion.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The input type being validated
|
|
40
|
+
* @template Out - The output type (same as T for checks, narrowed for assertions)
|
|
41
|
+
*/
|
|
14
42
|
export type Refinement<T = any, Out extends T = T> = RefinementCheck<T> | RefinementAssertion<T, Out>;
|
|
15
43
|
/**
|
|
16
|
-
*
|
|
44
|
+
* Creates a refined schema by adding additional validation constraints.
|
|
17
45
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @returns A new schema that includes the refinement.
|
|
21
|
-
* @example
|
|
46
|
+
* Wraps an existing schema with an additional check function. The base schema
|
|
47
|
+
* is validated first, then the refinement check is applied to the result.
|
|
22
48
|
*
|
|
49
|
+
* @param schema - The base schema to refine
|
|
50
|
+
* @param refinement - The refinement check or assertion to apply
|
|
51
|
+
* @returns A new schema that includes the refinement
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
23
54
|
* ```ts
|
|
24
|
-
*
|
|
55
|
+
* // Simple check refinement
|
|
56
|
+
* const positiveInt = l.refine(l.integer(), {
|
|
25
57
|
* check: (value) => value > 0,
|
|
26
|
-
* message: 'Value must be
|
|
58
|
+
* message: 'Value must be positive',
|
|
27
59
|
* })
|
|
28
|
-
*
|
|
29
|
-
* //
|
|
60
|
+
*
|
|
61
|
+
* positiveInt.parse(5) // 5
|
|
62
|
+
* positiveInt.parse(-1) // throws
|
|
63
|
+
*
|
|
64
|
+
* // Type-narrowing assertion
|
|
65
|
+
* const nonEmptyString = l.refine(l.string(), {
|
|
66
|
+
* check: (value): value is string & { length: number } => value.length > 0,
|
|
67
|
+
* message: 'String must not be empty',
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* // With custom path for nested errors
|
|
71
|
+
* const validDateRange = l.refine(
|
|
72
|
+
* l.object({ start: l.string(), end: l.string() }),
|
|
73
|
+
* {
|
|
74
|
+
* check: (v) => new Date(v.start) < new Date(v.end),
|
|
75
|
+
* message: 'Start date must be before end date',
|
|
76
|
+
* path: ['end'],
|
|
77
|
+
* }
|
|
78
|
+
* )
|
|
30
79
|
* ```
|
|
31
80
|
*/
|
|
32
81
|
export declare function refine<const TValidator extends Validator, TInput extends InferInput<TValidator>>(schema: TValidator, refinement: RefinementAssertion<InferInput<TValidator>, TInput>): TValidator & Validator<TInput>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refine.d.ts","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAEV,WAAW,EAGX,SAAS,EACV,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAEpD,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,sBAAsB,KAAK,OAAO,CAAA;IACzD,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,WAAW,GAAG,SAAS,WAAW,EAAE,CAAA;CAC5C,CAAA;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,IAA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