@atproto/lex 0.0.18 → 0.0.20

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 CHANGED
@@ -1,5 +1,29 @@
1
1
  # @atproto/lex
2
2
 
3
+ ## 0.0.20
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4689](https://github.com/bluesky-social/atproto/pull/4689) [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update readme
8
+
9
+ - Updated dependencies [[`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7)]:
10
+ - @atproto/lex-schema@0.0.14
11
+ - @atproto/lex-data@0.0.13
12
+ - @atproto/lex-client@0.0.15
13
+ - @atproto/lex-installer@0.0.20
14
+ - @atproto/lex-builder@0.0.17
15
+ - @atproto/lex-json@0.0.13
16
+
17
+ ## 0.0.19
18
+
19
+ ### Patch Changes
20
+
21
+ - [#4672](https://github.com/bluesky-social/atproto/pull/4672) [`38852f0`](https://github.com/bluesky-social/atproto/commit/38852f0ddfa9fbce8036233dc6af87614e9ae4b2) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve readme
22
+
23
+ - Updated dependencies [[`38852f0`](https://github.com/bluesky-social/atproto/commit/38852f0ddfa9fbce8036233dc6af87614e9ae4b2)]:
24
+ - @atproto/lex-client@0.0.14
25
+ - @atproto/lex-installer@0.0.19
26
+
3
27
  ## 0.0.18
4
28
 
5
29
  ### Patch Changes
package/README.md CHANGED
@@ -1,48 +1,47 @@
1
- # @atproto/lex
1
+ > [!IMPORTANT]
2
+ >
3
+ > This package is currently in **preview**. The API and features are subject to change before the stable release. See the [Changelog](./CHANGELOG.md) for version history.
2
4
 
3
- Type-safe Lexicon tooling for creating great API clients. See the [Changelog](./CHANGELOG.md) for version history.
5
+ Type-safe Lexicon tooling for AT Protocol data.
4
6
 
5
- ```bash
6
- npm install -g @atproto/lex
7
- lex --help
8
- ```
7
+ - Fetch and manage Lexicon schemas, generate TypeScript validators
8
+ - Compile-time and runtime type safety for AT Protocol data structures
9
+ - Fully typed XRPC client with authentication support
10
+ - Tree-shaking and composition friendly
9
11
 
10
- - Install and manage Lexicon schemas
11
- - Generate TypeScript client and data validators
12
- - Handle common tasks like OAuth
12
+ ```typescript
13
+ // Build data with generated builders and validators
13
14
 
14
- > [!IMPORTANT]
15
- >
16
- > This package is currently in **preview**. The API and features are subject to change before the stable release.
15
+ const newPost = app.bsky.feed.post.$build({
16
+ text: 'Hello, world!',
17
+ createdAt: new Date().toISOString(),
18
+ })
17
19
 
18
- **What is this?**
20
+ app.bsky.actor.profile.$validate({
21
+ $type: 'app.bsky.actor.profile',
22
+ displayName: 'Ha'.repeat(32) + '!',
23
+ }) // Error: grapheme too big (maximum 64) at $.displayName (got 65)
24
+ ```
19
25
 
20
- Working directly with XRPC endpoints requires manually tracking schema definitions, validation data structures, and managing authentication. `@atproto/lex` automates this by:
26
+ ```typescript
27
+ // Trivially make type-safe XRPC requests towards a service
21
28
 
22
- 1. Fetching lexicons from the network and generating TypeScript types
23
- 2. Providing runtime validation to ensure data matches schemas
24
- 3. Offering a type-safe client that knows which parameters each endpoint expects
25
- 4. Support modern patterns like tree-shaking and composition
29
+ const profile = await xrpc('https://api.bsky.app', app.bsky.actor.getProfile, {
30
+ params: { actor: 'pfrazee.com' },
31
+ })
32
+ ```
26
33
 
27
34
  ```typescript
28
- const profile = await client.call(app.bsky.actor.getProfile, {
29
- actor: 'atproto.com',
30
- })
35
+ // Manipulate records with the Client API in the context of an authenticated session
36
+
37
+ const client = new Client(oauthSession)
31
38
 
32
39
  await client.create(app.bsky.feed.post, {
33
40
  text: 'Hello, world!',
34
41
  createdAt: new Date().toISOString(),
35
42
  })
36
43
 
37
- const posts = await client.list(app.bsky.feed.post, {
38
- limit: 10,
39
- repo: 'atproto.com',
40
- })
41
-
42
- app.bsky.actor.profile.$validate({
43
- $type: 'app.bsky.actor.profile',
44
- displayName: 'Ha'.repeat(32) + '!',
45
- }) // { success: false, error: Error: grapheme too big (maximum 64) at $.displayName (got 65) }
44
+ const posts = await client.list(app.bsky.feed.post, { limit: 10 })
46
45
  ```
47
46
 
48
47
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
@@ -59,6 +58,7 @@ app.bsky.actor.profile.$validate({
59
58
  - [Types](#types)
60
59
  - [JSON Encoding](#json-encoding)
61
60
  - [CBOR Encoding](#cbor-encoding)
61
+ - [Making simple XRPC Requests](#making-simple-xrpc-requests)
62
62
  - [Client API](#client-api)
63
63
  - [Creating a Client](#creating-a-client)
64
64
  - [Core Methods](#core-methods)
@@ -67,6 +67,7 @@ app.bsky.actor.profile.$validate({
67
67
  - [Labeler Configuration](#labeler-configuration)
68
68
  - [Low-Level XRPC](#low-level-xrpc)
69
69
  - [Utilities](#utilities)
70
+ - [Datetime Strings](#datetime-strings)
70
71
  - [Advanced Usage](#advanced-usage)
71
72
  - [Workflow Integration](#workflow-integration)
72
73
  - [Tree-Shaking](#tree-shaking)
@@ -135,15 +136,11 @@ This generates TypeScript files in `./src/lexicons` (by default) with type-safe
135
136
  **4. Use in your code**
136
137
 
137
138
  ```typescript
138
- import { Client } from '@atproto/lex'
139
- import * as app from './lexicons/app.js'
139
+ import { xrpc } from '@atproto/lex'
140
+ import { app } from './lexicons/index.js'
140
141
 
141
- // Create a client instance
142
- const client = new Client('https://public.api.bsky.app')
143
-
144
- // Start making requests using generated schemas
145
- const response = await client.call(app.bsky.actor.getProfile, {
146
- actor: 'pfrazee.com',
142
+ const profile = await xrpc('https://api.bsky.app', app.bsky.actor.getProfile, {
143
+ params: { actor: 'pfrazee.com' },
147
144
  })
148
145
  ```
149
146
 
@@ -227,13 +224,14 @@ function renderPost(p: app.bsky.feed.post.Main) {
227
224
  It is recommended to use the generated builders to create data that conforms to the schema. This ensures that all required fields are present.
228
225
 
229
226
  ```typescript
227
+ import { l } from '@atproto/lex'
230
228
  import * as app from './lexicons/app.js'
231
229
 
232
230
  // variable type will be inferred as "app.bsky.feed.post.Main"
233
231
  const post = app.bsky.feed.post.$build({
234
232
  // No need to specify $type when using $build
235
233
  text: 'Hello, world!',
236
- createdAt: new Date().toISOString(),
234
+ createdAt: l.toDatetimeString(new Date()),
237
235
  })
238
236
  ```
239
237
 
@@ -267,12 +265,13 @@ console.log(app.bsky.actor.defs.profileViewBasic.$type) // 'app.bsky.actor.defs#
267
265
  Returns `true` if data matches the schema, `false` otherwise. Acts as a TypeScript type guard:
268
266
 
269
267
  ```typescript
268
+ import { l } from '@atproto/lex'
270
269
  import * as app from './lexicons/app.js'
271
270
 
272
271
  const data = {
273
272
  $type: 'app.bsky.feed.post',
274
273
  text: 'Hello!',
275
- createdAt: new Date().toISOString(),
274
+ createdAt: l.toDatetimeString(new Date()),
276
275
  }
277
276
 
278
277
  if (app.bsky.feed.post.$check(data)) {
@@ -286,13 +285,14 @@ if (app.bsky.feed.post.$check(data)) {
286
285
  Validates and returns typed data, throwing an error if validation fails:
287
286
 
288
287
  ```typescript
288
+ import { l } from '@atproto/lex'
289
289
  import * as app from './lexicons/app.js'
290
290
 
291
291
  try {
292
292
  const post = app.bsky.feed.post.$main.$parse({
293
293
  $type: 'app.bsky.feed.post',
294
294
  text: 'Hello!',
295
- createdAt: new Date().toISOString(),
295
+ createdAt: l.toDatetimeString(new Date()),
296
296
  })
297
297
  // post is now typed and validated
298
298
  console.log(post.text)
@@ -310,12 +310,13 @@ try {
310
310
  Validates an existing value against a schema, returning the value itself if, and only if, it already matches the schema (ie. without applying defaults or coercion).
311
311
 
312
312
  ```typescript
313
+ import { l } from '@atproto/lex'
313
314
  import * as app from './lexicons/app.js'
314
315
 
315
316
  const value = {
316
317
  $type: 'app.bsky.feed.post',
317
318
  text: 'Hello!',
318
- createdAt: new Date().toISOString(),
319
+ createdAt: l.toDatetimeString(new Date()),
319
320
  }
320
321
 
321
322
  // Throws if no valid
@@ -329,12 +330,13 @@ value === result // true
329
330
  Returns a detailed validation result object without throwing:
330
331
 
331
332
  ```typescript
333
+ import { l } from '@atproto/lex'
332
334
  import * as app from './lexicons/app.js'
333
335
 
334
336
  const result = app.bsky.feed.post.$safeParse({
335
337
  $type: 'app.bsky.feed.post',
336
338
  text: 'Hello!',
337
- createdAt: new Date().toISOString(),
339
+ createdAt: l.toDatetimeString(new Date()),
338
340
  })
339
341
 
340
342
  if (result.success) {
@@ -349,6 +351,7 @@ if (result.success) {
349
351
  Builds data without needing to specify the `$type` property, and properly types the result:
350
352
 
351
353
  ```typescript
354
+ import { l } from '@atproto/lex'
352
355
  import * as app from './lexicons/app.js'
353
356
 
354
357
  // The type of the "like" variable will be "app.bsky.feed.like.Main"
@@ -357,7 +360,7 @@ const like = app.bsky.feed.like.$build({
357
360
  uri: 'at://did:plc:abc/app.bsky.feed.post/123',
358
361
  cid: 'bafyrei...',
359
362
  },
360
- createdAt: new Date().toISOString(),
363
+ createdAt: l.toDatetimeString(new Date()),
361
364
  })
362
365
  ```
363
366
 
@@ -414,7 +417,7 @@ if (isTypedLexMap(data)) {
414
417
  In JSON, CIDs are represented as `{"$link": "bafyrei..."}` and bytes as `{"$bytes": "base64..."}`. This package provides utilities to parse and stringify data model values to/from JSON:
415
418
 
416
419
  ```typescript
417
- import { lexParse, lexStringify, jsonToLex, lexToJson } from '@atproto/lex'
420
+ import { Cid, lexParse, lexStringify, jsonToLex, lexToJson } from '@atproto/lex'
418
421
 
419
422
  // Parse JSON string → data model (decodes $link and $bytes)
420
423
  const parsed = lexParse<{
@@ -425,6 +428,9 @@ const parsed = lexParse<{
425
428
  "data": { "$bytes": "SGVsbG8sIHdvcmxkIQ==" }
426
429
  }`)
427
430
 
431
+ assert(isCid(parsed.ref))
432
+ assert(parsed.data instanceof Uint8Array)
433
+
428
434
  const someCid = lexParse<Cid>('{"$link": "bafyrei..."}')
429
435
  const someBytes = lexParse<Uint8Array>('{"$bytes": "SGVsbG8sIHdvcmxkIQ=="}')
430
436
 
@@ -458,8 +464,53 @@ const cborBytes = encode(someLexValue)
458
464
  const lexValue: LexValue = decode(cborBytes)
459
465
  ```
460
466
 
467
+ ## Making simple XRPC Requests
468
+
469
+ [XRPC](https://atproto.com/specs/xrpc) (short for "Lexicon RPC") is the set of HTTP conventions used by AT Protocol for client-server and server-server communication. Endpoints follow the pattern `/xrpc/<nsid>`, where the NSID maps to a Lexicon schema that defines the request and response types. XRPC has three method types: **queries** (HTTP GET) for read operations, **procedures** (HTTP POST) for mutations and **subscriptions** (WebSockets) for real-time updates.
470
+
471
+ The `xrpc()` and `xrpcSafe()` functions can be used to make simple XRPC requests. They are typically used in places that don't require an authenticated session, or when more granular control over the request/response is needed. For most use cases, the `Client` API provides a more ergonomic way to work with XRPC in the context of an authenticated session.
472
+
473
+ ```typescript
474
+ import { xrpc, xrpcSafe } from '@atproto/lex'
475
+ import * as com from './lexicons/com.js'
476
+
477
+ const response = await xrpc(
478
+ 'https://bsky.network',
479
+ com.atproto.identity.resolveHandle,
480
+ {
481
+ params: { handle: 'atproto.com' },
482
+ headers: { 'user-agent': 'MyApp/1.0.0' },
483
+ },
484
+ )
485
+
486
+ response.status // number
487
+ response.headers // Headers
488
+ response.body.did // `did:${string}:${string}`
489
+
490
+ // Or use the safe variant (returns errors instead of throwing)
491
+ const result = await xrpcSafe(
492
+ 'https://bsky.network',
493
+ com.atproto.identity.resolveHandle,
494
+ {
495
+ params: { handle: 'atproto.com' },
496
+ signal: AbortSignal.timeout(5000), // Abort after 5 seconds
497
+ },
498
+ )
499
+
500
+ if (result.success) {
501
+ console.log(result.body)
502
+ } else {
503
+ console.error(result.error) // XRPC error code
504
+ console.error(result.message) // Error message
505
+ }
506
+ ```
507
+
461
508
  ## Client API
462
509
 
510
+ The `Client` class provides high-level helpers for common AT Protocol "repo" operations: `create()`, `get()`, `put()`, `delete()`, `list()`, `uploadBlob()`, and more. A `Client` instance is typically useful for making requests in the context of an authenticated user session, as it automatically handles headers and provides default values based on the authenticated user's DID.
511
+
512
+ A `Client` instance is also useful to encapsulate configuration for a specific service, by specifying the `service` option (for proxying) and `labelers` option (for content labeling). Additionally, a `Client` can be used as an `Agent` for another `Client`, allowing you to compose headers and configuration across multiple services.
513
+
463
514
  ### Creating a Client
464
515
 
465
516
  #### Unauthenticated Client
@@ -558,14 +609,15 @@ const timeline = await client.call(
558
609
 
559
610
  #### `client.create()`
560
611
 
561
- Create a new record.
612
+ Create a new record un the authenticated user's repo.
562
613
 
563
614
  ```typescript
615
+ import { l } from '@atproto/lex'
564
616
  import * as app from './lexicons/app.js'
565
617
 
566
618
  const result = await client.create(app.bsky.feed.post, {
567
619
  text: 'Hello, world!',
568
- createdAt: new Date().toISOString(),
620
+ createdAt: l.toDatetimeString(new Date()),
569
621
  })
570
622
 
571
623
  console.log(result.uri) // at://did:plc:...
@@ -585,6 +637,7 @@ Retrieve a record.
585
637
  ```typescript
586
638
  import * as app from './lexicons/app.js'
587
639
 
640
+ // No need to specify the "rkey" for records with literal keys (e.g. profile)
588
641
  const profile = await client.get(app.bsky.actor.profile)
589
642
 
590
643
  console.log(profile.displayName)
@@ -712,7 +765,7 @@ if (result.success) {
712
765
  // All XrpcFailure types have these properties:
713
766
  result.shouldRetry() // boolean - whether the error is transient
714
767
 
715
- if (result.matchesSchema()) {
768
+ if (result.matchesSchemaErrors()) {
716
769
  // Check if the error matches a declared error in the schema.
717
770
  // TypeScript knows this is a declared error for the method.
718
771
  result.error // "HandleNotFound"
@@ -805,7 +858,7 @@ console.log(response.body)
805
858
 
806
859
  ## Utilities
807
860
 
808
- Various utilities for working with CIDs, string lengths, language tags, and low-level JSON encoding are exported from the package:
861
+ Various utilities for working with CIDs, datetime strings, string lengths, language tags, and low-level JSON encoding are exported from the package:
809
862
 
810
863
  ```typescript
811
864
  import {
@@ -814,6 +867,12 @@ import {
814
867
  ifCid, // Coerce to Cid or null
815
868
  isCid, // Type guard for Cid values
816
869
 
870
+ // Datetime string utilities
871
+ toDatetimeString, // Convert Date to DatetimeString (throws on invalid)
872
+ asDatetimeString, // Cast string to DatetimeString (throws on invalid)
873
+ isDatetimeString, // Type guard for DatetimeString
874
+ ifDatetimeString, // Returns DatetimeString or undefined
875
+
817
876
  // Blob references
818
877
  BlobRef, // { $type: 'blob', ref: Cid, mimeType: string, size: number }
819
878
  isBlobRef, // Type guard for BlobRef objects
@@ -841,6 +900,35 @@ utf8Len('👨‍👩‍👧‍👦') // 25
841
900
  isLanguageString('en-US') // true
842
901
  ```
843
902
 
903
+ ### Datetime Strings
904
+
905
+ Many AT Protocol records (such as posts, likes, and follows) include a `createdAt` field that expects a valid `DatetimeString`. While `new Date().toISOString()` produces a string that looks like a valid datetime, it is not guaranteed to always conform to the AT Protocol's [datetime format requirements](https://atproto.com/specs/lexicon#datetime) (for example, `Date` objects representing dates before year 10 or after year 9999 will produce non-conforming strings). To ensure correctness and type safety, use the `DatetimeString` utilities exported from `@atproto/lex`:
906
+
907
+ - **`toDatetimeString(date: Date)`** - Converts a `Date` object into a valid `DatetimeString`, throwing an `InvalidDatetimeError` if the date cannot be represented as a valid AT Protocol datetime.
908
+ - **`asDatetimeString(input: string)`** - Validates and casts an arbitrary string to `DatetimeString`, throwing an `InvalidDatetimeError` if the string does not conform.
909
+ - **`isDatetimeString(input)`** - Type guard that returns `true` if the input is a valid `DatetimeString`.
910
+ - **`ifDatetimeString(input)`** - Returns the input as a `DatetimeString` if valid, or `undefined` otherwise.
911
+ - **`currentDatetimeString()`** - Returns the current date and time as `DatetimeString`.
912
+
913
+ ```typescript
914
+ import { l } from '@atproto/lex'
915
+
916
+ // Convert a Date object to a DatetimeString (or throws)
917
+ const someDate = new Date('2024-01-15T12:30:00Z')
918
+ const now = l.toDatetimeString(someDate)
919
+
920
+ // Get the current datetime as a DatetimeString
921
+ const now = l.currentDatetimeString()
922
+
923
+ // Validate and cast an existing string
924
+ const dt = l.asDatetimeString('2024-01-15T12:30:00.000Z')
925
+
926
+ // Type guard for conditional checks
927
+ if (l.isDatetimeString(someString)) {
928
+ // someString is now typed as DatetimeString
929
+ }
930
+ ```
931
+
844
932
  ## Advanced Usage
845
933
 
846
934
  ### Workflow Integration
@@ -946,7 +1034,7 @@ Actions receive:
946
1034
  Actions are called using `client.call()`, the same method used for XRPC queries and procedures:
947
1035
 
948
1036
  ```typescript
949
- import { Action, Client } from '@atproto/lex'
1037
+ import { Action, Client, l } from '@atproto/lex'
950
1038
  import * as app from './lexicons/app.js'
951
1039
 
952
1040
  // Define an action
@@ -960,7 +1048,7 @@ export const likePost: Action<
960
1048
  app.bsky.feed.like,
961
1049
  {
962
1050
  subject: { uri, cid },
963
- createdAt: new Date().toISOString(),
1051
+ createdAt: l.toDatetimeString(new Date()),
964
1052
  },
965
1053
  options,
966
1054
  )
@@ -1161,7 +1249,7 @@ Actions enable you to create high-level, convenience APIs similar to [@atproto/a
1161
1249
  #### Creating Posts
1162
1250
 
1163
1251
  ```typescript
1164
- import { Action } from '@atproto/lex'
1252
+ import { Action, l } from '@atproto/lex'
1165
1253
  import * as app from './lexicons/app.js'
1166
1254
 
1167
1255
  type PostInput = Partial<app.bsky.feed.post.Main> &
@@ -1176,7 +1264,7 @@ export const post: Action<PostInput, { uri: string; cid: string }> = async (
1176
1264
  app.bsky.feed.post,
1177
1265
  {
1178
1266
  ...record,
1179
- createdAt: record.createdAt || new Date().toISOString(),
1267
+ createdAt: record.createdAt || l.currentDatetimeString(),
1180
1268
  },
1181
1269
  options,
1182
1270
  )
@@ -1192,7 +1280,7 @@ await client.call(post, {
1192
1280
  #### Following Users
1193
1281
 
1194
1282
  ```typescript
1195
- import { Action } from '@atproto/lex'
1283
+ import { Action, l } from '@atproto/lex'
1196
1284
  import { AtUri } from '@atproto/syntax'
1197
1285
  import * as app from './lexicons/app.js'
1198
1286
 
@@ -1204,7 +1292,7 @@ export const follow: Action<
1204
1292
  app.bsky.graph.follow,
1205
1293
  {
1206
1294
  subject: did,
1207
- createdAt: new Date().toISOString(),
1295
+ createdAt: l.currentDatetimeString(),
1208
1296
  },
1209
1297
  options,
1210
1298
  )
package/dist/index.d.ts CHANGED
@@ -3,30 +3,35 @@
3
3
  * lexicons, including data types, JSON encoding/decoding, schema validation,
4
4
  * and HTTP client functionality.
5
5
  *
6
- * ## `@atproto/lex-client`
7
- *
8
- * - {@link client.Client} - Type-safe XRPC client for making ATProtocol API calls
9
- * - {@link client.XrpcError} - Base error class for XRPC request failures
10
- * - {@link client.Agent} - Interface used by {@link client.Client} for making HTTP requests
11
- * - {@link client.xrpc} - Utility function for making XRPC requests
12
- *
13
- * ## `@atproto/lex-data`
14
- *
15
- * - {@link data.LexValue} - Union type representing any valid Lexicon value
16
- * - {@link data.LexMap} - Object type with string keys and {@link data.LexValue} values
17
- * - {@link data.Cid} - Content Identifier for referencing data by hash
18
- * - {@link data.BlobRef} - Reference to binary data (images, videos, etc.)
19
- *
20
- * ## `@atproto/lex-json`
21
- *
22
- * - {@link json.lexStringify} - Serialize Lex values to JSON strings
23
- * - {@link json.lexParse} - Parse JSON strings into Lex values
24
- * - {@link json.lexToJson} - Convert Lex values to plain JSON objects
25
- * - {@link json.jsonToLex} - Convert plain JSON objects to Lex values
26
- *
27
- * ## `@atproto/lex-schema`
28
- *
29
- * The {@link l} namespace provides a fluent API for building schemas:
6
+ * @packageDocumentation
7
+ */
8
+ export {
9
+ /**
10
+ * The Client class is the primary interface for interacting with AT Protocol
11
+ * services though an authenticated session. It provides methods for making
12
+ * XRPC requests, handling records, and managing blobs.
13
+ */
14
+ Client,
15
+ /**
16
+ * The `xrpc` function is a low-level utility for making XRPC requests towards
17
+ * a specific service. It allows for detailed control over the request,
18
+ * including custom parameters, body, and headers. This function is useful for
19
+ * advanced use cases where the higher-level `Client` methods may not provide
20
+ * enough flexibility.
21
+ */
22
+ xrpc,
23
+ /**
24
+ * The `xrpcSafe` function is a wrapper around `xrpc` that provides additional
25
+ * safety checks and error handling. It ensures that the request is properly
26
+ * formed and that any errors are caught and handled gracefully. This function
27
+ * is recommended for most use cases, as it provides a safer interface for
28
+ * making XRPC requests.
29
+ */
30
+ xrpcSafe, } from '@atproto/lex-client';
31
+ export * from '@atproto/lex-client';
32
+ export {
33
+ /**
34
+ * The {@link l} namespace (from `@atproto/lex-schema`) provides an imperative API for building schemas:
30
35
  *
31
36
  * ### Primitive Types
32
37
  * - {@link l.string | l.string()} - String values with optional format/length constraints
@@ -57,11 +62,50 @@
57
62
  * - {@link l.query | l.query()} - Define a Lexicon query method
58
63
  * - {@link l.procedure | l.procedure()} - Define a Lexicon procedure method
59
64
  * - {@link l.subscription | l.subscription()} - Define a Lexicon subscription method
60
- *
61
- * @packageDocumentation
62
65
  */
66
+ l, } from '@atproto/lex-schema';
67
+ export * from '@atproto/lex-schema';
68
+ export {
69
+ /**
70
+ * The `LexMap` type represents an object with string keys and `LexValue` values.
71
+ * It is used to represent arbitrary objects in Lexicon schemas, where the
72
+ * properties are not predefined. This type allows for flexible data structures
73
+ * while still ensuring that all values conform to the `LexValue` type.
74
+ */
75
+ type LexMap,
76
+ /**
77
+ * The `LexValue` type represents any valid value that can be used in a
78
+ * Lexicon schema. It is a union of all the primitive and composite types
79
+ * defined in `@atproto/lex-data`, including strings, integers, booleans,
80
+ * bytes, CIDs, blob references, objects, arrays, and maps. This type is used
81
+ * throughout the library to represent data that conforms to Lexicon schemas.
82
+ */
83
+ type LexValue, } from '@atproto/lex-data';
63
84
  export * from '@atproto/lex-data';
85
+ export {
86
+ /**
87
+ * The `jsonToLex` function takes a plain JavaScript object (typically parsed from
88
+ * JSON) and converts it back into a LexValue, reconstructing any complex types as needed. This is useful
89
+ * for processing data received from the network or loaded from JSON storage.
90
+ */
91
+ jsonToLex,
92
+ /**
93
+ * The `lexParse` function takes a JSON string and parses it into a LexValue. It
94
+ * performs the necessary conversions to reconstruct complex LexValue types from
95
+ * their JSON representations.
96
+ */
97
+ lexParse,
98
+ /**
99
+ * The `lexStringify` function takes a LexValue and serializes it to a JSON string.
100
+ * It handles the conversion of complex LexValue types (like BlobRef and Cid) into
101
+ * a JSON-friendly format.
102
+ */
103
+ lexStringify,
104
+ /**
105
+ * The `lexToJson` function converts a LexValue into a plain JavaScript object
106
+ * that can be safely serialized to JSON. This is useful for preparing data to be
107
+ * sent over the network or stored in a JSON format.
108
+ */
109
+ lexToJson, } from '@atproto/lex-json';
64
110
  export * from '@atproto/lex-json';
65
- export * from '@atproto/lex-schema';
66
- export * from '@atproto/lex-client';
67
111
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAEH,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO;AACL;;;;GAIG;AACH,MAAM;AACN;;;;;;GAMG;AACH,IAAI;AACJ;;;;;;GAMG;AACH,QAAQ,GACT,MAAM,qBAAqB,CAAA;AAC5B,cAAc,qBAAqB,CAAA;AAEnC,OAAO;AACL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,CAAC,GACF,MAAM,qBAAqB,CAAA;AAC5B,cAAc,qBAAqB,CAAA;AAEnC,OAAO;AACL;;;;;GAKG;AACH,KAAK,MAAM;AACX;;;;;;GAMG;AACH,KAAK,QAAQ,GACd,MAAM,mBAAmB,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AAEjC,OAAO;AACL;;;;GAIG;AACH,SAAS;AACT;;;;GAIG;AACH,QAAQ;AACR;;;;GAIG;AACH,YAAY;AACZ;;;;GAIG;AACH,SAAS,GACV,MAAM,mBAAmB,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
package/dist/index.js CHANGED
@@ -1,36 +1,41 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/no-unused-vars */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- const tslib_1 = require("tslib");
5
2
  /**
6
3
  * The `@atproto/lex` package provides utilities for working with ATProtocol
7
4
  * lexicons, including data types, JSON encoding/decoding, schema validation,
8
5
  * and HTTP client functionality.
9
6
  *
10
- * ## `@atproto/lex-client`
11
- *
12
- * - {@link client.Client} - Type-safe XRPC client for making ATProtocol API calls
13
- * - {@link client.XrpcError} - Base error class for XRPC request failures
14
- * - {@link client.Agent} - Interface used by {@link client.Client} for making HTTP requests
15
- * - {@link client.xrpc} - Utility function for making XRPC requests
16
- *
17
- * ## `@atproto/lex-data`
18
- *
19
- * - {@link data.LexValue} - Union type representing any valid Lexicon value
20
- * - {@link data.LexMap} - Object type with string keys and {@link data.LexValue} values
21
- * - {@link data.Cid} - Content Identifier for referencing data by hash
22
- * - {@link data.BlobRef} - Reference to binary data (images, videos, etc.)
23
- *
24
- * ## `@atproto/lex-json`
25
- *
26
- * - {@link json.lexStringify} - Serialize Lex values to JSON strings
27
- * - {@link json.lexParse} - Parse JSON strings into Lex values
28
- * - {@link json.lexToJson} - Convert Lex values to plain JSON objects
29
- * - {@link json.jsonToLex} - Convert plain JSON objects to Lex values
30
- *
31
- * ## `@atproto/lex-schema`
32
- *
33
- * The {@link l} namespace provides a fluent API for building schemas:
7
+ * @packageDocumentation
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.lexToJson = exports.lexStringify = exports.lexParse = exports.jsonToLex = exports.l = exports.xrpcSafe = exports.xrpc = exports.Client = void 0;
11
+ const tslib_1 = require("tslib");
12
+ var lex_client_1 = require("@atproto/lex-client");
13
+ /**
14
+ * The Client class is the primary interface for interacting with AT Protocol
15
+ * services though an authenticated session. It provides methods for making
16
+ * XRPC requests, handling records, and managing blobs.
17
+ */
18
+ Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return lex_client_1.Client; } });
19
+ /**
20
+ * The `xrpc` function is a low-level utility for making XRPC requests towards
21
+ * a specific service. It allows for detailed control over the request,
22
+ * including custom parameters, body, and headers. This function is useful for
23
+ * advanced use cases where the higher-level `Client` methods may not provide
24
+ * enough flexibility.
25
+ */
26
+ Object.defineProperty(exports, "xrpc", { enumerable: true, get: function () { return lex_client_1.xrpc; } });
27
+ /**
28
+ * The `xrpcSafe` function is a wrapper around `xrpc` that provides additional
29
+ * safety checks and error handling. It ensures that the request is properly
30
+ * formed and that any errors are caught and handled gracefully. This function
31
+ * is recommended for most use cases, as it provides a safer interface for
32
+ * making XRPC requests.
33
+ */
34
+ Object.defineProperty(exports, "xrpcSafe", { enumerable: true, get: function () { return lex_client_1.xrpcSafe; } });
35
+ tslib_1.__exportStar(require("@atproto/lex-client"), exports);
36
+ var lex_schema_1 = require("@atproto/lex-schema");
37
+ /**
38
+ * The {@link l} namespace (from `@atproto/lex-schema`) provides an imperative API for building schemas:
34
39
  *
35
40
  * ### Primitive Types
36
41
  * - {@link l.string | l.string()} - String values with optional format/length constraints
@@ -61,11 +66,34 @@ const tslib_1 = require("tslib");
61
66
  * - {@link l.query | l.query()} - Define a Lexicon query method
62
67
  * - {@link l.procedure | l.procedure()} - Define a Lexicon procedure method
63
68
  * - {@link l.subscription | l.subscription()} - Define a Lexicon subscription method
64
- *
65
- * @packageDocumentation
66
69
  */
70
+ Object.defineProperty(exports, "l", { enumerable: true, get: function () { return lex_schema_1.l; } });
71
+ tslib_1.__exportStar(require("@atproto/lex-schema"), exports);
67
72
  tslib_1.__exportStar(require("@atproto/lex-data"), exports);
73
+ var lex_json_1 = require("@atproto/lex-json");
74
+ /**
75
+ * The `jsonToLex` function takes a plain JavaScript object (typically parsed from
76
+ * JSON) and converts it back into a LexValue, reconstructing any complex types as needed. This is useful
77
+ * for processing data received from the network or loaded from JSON storage.
78
+ */
79
+ Object.defineProperty(exports, "jsonToLex", { enumerable: true, get: function () { return lex_json_1.jsonToLex; } });
80
+ /**
81
+ * The `lexParse` function takes a JSON string and parses it into a LexValue. It
82
+ * performs the necessary conversions to reconstruct complex LexValue types from
83
+ * their JSON representations.
84
+ */
85
+ Object.defineProperty(exports, "lexParse", { enumerable: true, get: function () { return lex_json_1.lexParse; } });
86
+ /**
87
+ * The `lexStringify` function takes a LexValue and serializes it to a JSON string.
88
+ * It handles the conversion of complex LexValue types (like BlobRef and Cid) into
89
+ * a JSON-friendly format.
90
+ */
91
+ Object.defineProperty(exports, "lexStringify", { enumerable: true, get: function () { return lex_json_1.lexStringify; } });
92
+ /**
93
+ * The `lexToJson` function converts a LexValue into a plain JavaScript object
94
+ * that can be safely serialized to JSON. This is useful for preparing data to be
95
+ * sent over the network or stored in a JSON format.
96
+ */
97
+ Object.defineProperty(exports, "lexToJson", { enumerable: true, get: function () { return lex_json_1.lexToJson; } });
68
98
  tslib_1.__exportStar(require("@atproto/lex-json"), exports);
69
- tslib_1.__exportStar(require("@atproto/lex-schema"), exports);
70
- tslib_1.__exportStar(require("@atproto/lex-client"), exports);
71
99
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;;;AAOtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAEH,4DAAiC;AACjC,4DAAiC;AACjC,8DAAmC;AACnC,8DAAmC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\n\nimport type * as client from '@atproto/lex-client'\nimport type * as data from '@atproto/lex-data'\nimport type * as json from '@atproto/lex-json'\nimport { l } from '@atproto/lex-schema'\n\n/**\n * The `@atproto/lex` package provides utilities for working with ATProtocol\n * lexicons, including data types, JSON encoding/decoding, schema validation,\n * and HTTP client functionality.\n *\n * ## `@atproto/lex-client`\n *\n * - {@link client.Client} - Type-safe XRPC client for making ATProtocol API calls\n * - {@link client.XrpcError} - Base error class for XRPC request failures\n * - {@link client.Agent} - Interface used by {@link client.Client} for making HTTP requests\n * - {@link client.xrpc} - Utility function for making XRPC requests\n *\n * ## `@atproto/lex-data`\n *\n * - {@link data.LexValue} - Union type representing any valid Lexicon value\n * - {@link data.LexMap} - Object type with string keys and {@link data.LexValue} values\n * - {@link data.Cid} - Content Identifier for referencing data by hash\n * - {@link data.BlobRef} - Reference to binary data (images, videos, etc.)\n *\n * ## `@atproto/lex-json`\n *\n * - {@link json.lexStringify} - Serialize Lex values to JSON strings\n * - {@link json.lexParse} - Parse JSON strings into Lex values\n * - {@link json.lexToJson} - Convert Lex values to plain JSON objects\n * - {@link json.jsonToLex} - Convert plain JSON objects to Lex values\n *\n * ## `@atproto/lex-schema`\n *\n * The {@link l} namespace provides a fluent API for building schemas:\n *\n * ### Primitive Types\n * - {@link l.string | l.string()} - String values with optional format/length constraints\n * - {@link l.integer | l.integer()} - Integer values with optional min/max constraints\n * - {@link l.boolean | l.boolean()} - Boolean values\n * - {@link l.bytes | l.bytes()} - Binary data (Uint8Array)\n * - {@link l.cid | l.cid()} - Content Identifier values\n * - {@link l.blob | l.blob()} - Blob references with mime type and size\n *\n * ### Composite Types\n * - {@link l.object | l.object()} - Objects with defined property schemas\n * - {@link l.array | l.array()} - Arrays with element type validation\n * - {@link l.union | l.union()} - Union of multiple possible types\n * - {@link l.ref | l.ref()} - Reference to another schema definition\n * - {@link l.literal | l.literal()} - Literal constant values\n * - {@link l.enum | l.enum()} - Enum of allowed string values\n * - {@link l.typedRef | l.typedRef()} - Reference to a {@link l.typedObject | l.typedObject()}\n * - {@link l.typedUnion | l.typedUnion()} - Discriminated union between multiple {@link l.typedRef | l.typedRef()} or {@link l.typedObject | l.typedObject()} types\n *\n * ### Modifiers\n * - {@link l.optional | l.optional()} - Mark a property as optional\n * - {@link l.nullable | l.nullable()} - Allow null values\n * - {@link l.withDefault | l.withDefault()} - Provide a default value\n *\n * ### Lexicon Definitions\n * - {@link l.typedObject | l.typedObject()} - Define a typed object with a `$type` property\n * - {@link l.record | l.record()} - Define a Lexicon record type\n * - {@link l.query | l.query()} - Define a Lexicon query method\n * - {@link l.procedure | l.procedure()} - Define a Lexicon procedure method\n * - {@link l.subscription | l.subscription()} - Define a Lexicon subscription method\n *\n * @packageDocumentation\n */\n\nexport * from '@atproto/lex-data'\nexport * from '@atproto/lex-json'\nexport * from '@atproto/lex-schema'\nexport * from '@atproto/lex-client'\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;AAEH,kDAuB4B;AAtB1B;;;;GAIG;AACH,oGAAA,MAAM,OAAA;AACN;;;;;;GAMG;AACH,kGAAA,IAAI,OAAA;AACJ;;;;;;GAMG;AACH,sGAAA,QAAQ,OAAA;AAEV,8DAAmC;AAEnC,kDAmC4B;AAlC1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,+FAAA,CAAC,OAAA;AAEH,8DAAmC;AAmBnC,4DAAiC;AAEjC,8CAyB0B;AAxBxB;;;;GAIG;AACH,qGAAA,SAAS,OAAA;AACT;;;;GAIG;AACH,oGAAA,QAAQ,OAAA;AACR;;;;GAIG;AACH,wGAAA,YAAY,OAAA;AACZ;;;;GAIG;AACH,qGAAA,SAAS,OAAA;AAEX,4DAAiC","sourcesContent":["/**\n * The `@atproto/lex` package provides utilities for working with ATProtocol\n * lexicons, including data types, JSON encoding/decoding, schema validation,\n * and HTTP client functionality.\n *\n * @packageDocumentation\n */\n\nexport {\n /**\n * The Client class is the primary interface for interacting with AT Protocol\n * services though an authenticated session. It provides methods for making\n * XRPC requests, handling records, and managing blobs.\n */\n Client,\n /**\n * The `xrpc` function is a low-level utility for making XRPC requests towards\n * a specific service. It allows for detailed control over the request,\n * including custom parameters, body, and headers. This function is useful for\n * advanced use cases where the higher-level `Client` methods may not provide\n * enough flexibility.\n */\n xrpc,\n /**\n * The `xrpcSafe` function is a wrapper around `xrpc` that provides additional\n * safety checks and error handling. It ensures that the request is properly\n * formed and that any errors are caught and handled gracefully. This function\n * is recommended for most use cases, as it provides a safer interface for\n * making XRPC requests.\n */\n xrpcSafe,\n} from '@atproto/lex-client'\nexport * from '@atproto/lex-client'\n\nexport {\n /**\n * The {@link l} namespace (from `@atproto/lex-schema`) provides an imperative API for building schemas:\n *\n * ### Primitive Types\n * - {@link l.string | l.string()} - String values with optional format/length constraints\n * - {@link l.integer | l.integer()} - Integer values with optional min/max constraints\n * - {@link l.boolean | l.boolean()} - Boolean values\n * - {@link l.bytes | l.bytes()} - Binary data (Uint8Array)\n * - {@link l.cid | l.cid()} - Content Identifier values\n * - {@link l.blob | l.blob()} - Blob references with mime type and size\n *\n * ### Composite Types\n * - {@link l.object | l.object()} - Objects with defined property schemas\n * - {@link l.array | l.array()} - Arrays with element type validation\n * - {@link l.union | l.union()} - Union of multiple possible types\n * - {@link l.ref | l.ref()} - Reference to another schema definition\n * - {@link l.literal | l.literal()} - Literal constant values\n * - {@link l.enum | l.enum()} - Enum of allowed string values\n * - {@link l.typedRef | l.typedRef()} - Reference to a {@link l.typedObject | l.typedObject()}\n * - {@link l.typedUnion | l.typedUnion()} - Discriminated union between multiple {@link l.typedRef | l.typedRef()} or {@link l.typedObject | l.typedObject()} types\n *\n * ### Modifiers\n * - {@link l.optional | l.optional()} - Mark a property as optional\n * - {@link l.nullable | l.nullable()} - Allow null values\n * - {@link l.withDefault | l.withDefault()} - Provide a default value\n *\n * ### Lexicon Definitions\n * - {@link l.typedObject | l.typedObject()} - Define a typed object with a `$type` property\n * - {@link l.record | l.record()} - Define a Lexicon record type\n * - {@link l.query | l.query()} - Define a Lexicon query method\n * - {@link l.procedure | l.procedure()} - Define a Lexicon procedure method\n * - {@link l.subscription | l.subscription()} - Define a Lexicon subscription method\n */\n l,\n} from '@atproto/lex-schema'\nexport * from '@atproto/lex-schema'\n\nexport {\n /**\n * The `LexMap` type represents an object with string keys and `LexValue` values.\n * It is used to represent arbitrary objects in Lexicon schemas, where the\n * properties are not predefined. This type allows for flexible data structures\n * while still ensuring that all values conform to the `LexValue` type.\n */\n type LexMap,\n /**\n * The `LexValue` type represents any valid value that can be used in a\n * Lexicon schema. It is a union of all the primitive and composite types\n * defined in `@atproto/lex-data`, including strings, integers, booleans,\n * bytes, CIDs, blob references, objects, arrays, and maps. This type is used\n * throughout the library to represent data that conforms to Lexicon schemas.\n */\n type LexValue,\n} from '@atproto/lex-data'\nexport * from '@atproto/lex-data'\n\nexport {\n /**\n * The `jsonToLex` function takes a plain JavaScript object (typically parsed from\n * JSON) and converts it back into a LexValue, reconstructing any complex types as needed. This is useful\n * for processing data received from the network or loaded from JSON storage.\n */\n jsonToLex,\n /**\n * The `lexParse` function takes a JSON string and parses it into a LexValue. It\n * performs the necessary conversions to reconstruct complex LexValue types from\n * their JSON representations.\n */\n lexParse,\n /**\n * The `lexStringify` function takes a LexValue and serializes it to a JSON string.\n * It handles the conversion of complex LexValue types (like BlobRef and Cid) into\n * a JSON-friendly format.\n */\n lexStringify,\n /**\n * The `lexToJson` function converts a LexValue into a plain JavaScript object\n * that can be safely serialized to JSON. This is useful for preparing data to be\n * sent over the network or stored in a JSON format.\n */\n lexToJson,\n} from '@atproto/lex-json'\nexport * from '@atproto/lex-json'\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon tooling for AT",
6
6
  "keywords": [
@@ -38,12 +38,12 @@
38
38
  "dependencies": {
39
39
  "tslib": "^2.8.1",
40
40
  "yargs": "^17.0.0",
41
- "@atproto/lex-builder": "^0.0.16",
42
- "@atproto/lex-client": "^0.0.13",
43
- "@atproto/lex-data": "^0.0.12",
44
- "@atproto/lex-json": "^0.0.12",
45
- "@atproto/lex-installer": "^0.0.18",
46
- "@atproto/lex-schema": "^0.0.13"
41
+ "@atproto/lex-builder": "^0.0.17",
42
+ "@atproto/lex-client": "^0.0.15",
43
+ "@atproto/lex-data": "^0.0.13",
44
+ "@atproto/lex-json": "^0.0.13",
45
+ "@atproto/lex-installer": "^0.0.20",
46
+ "@atproto/lex-schema": "^0.0.14"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/yargs": "^17.0.33",