@atproto/lex 0.0.22 → 0.0.23
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 +10 -0
- package/README.md +28 -15
- package/package.json +7 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @atproto/lex
|
|
2
2
|
|
|
3
|
+
## 0.0.23
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`527f5d4`](https://github.com/bluesky-social/atproto/commit/527f5d4c5d0c9264c2ff6f23ad06a41163fc6809), [`527f5d4`](https://github.com/bluesky-social/atproto/commit/527f5d4c5d0c9264c2ff6f23ad06a41163fc6809), [`c4df84c`](https://github.com/bluesky-social/atproto/commit/c4df84cd78df68ee8cb7289e7b61b3a032ad484e), [`527f5d4`](https://github.com/bluesky-social/atproto/commit/527f5d4c5d0c9264c2ff6f23ad06a41163fc6809), [`a99dd58`](https://github.com/bluesky-social/atproto/commit/a99dd58b5fe1995e571cf5e7b0105355583efa93), [`e5e5bcf`](https://github.com/bluesky-social/atproto/commit/e5e5bcf85fbc0d418f05724d684e7265be6a0be9), [`ac6bd18`](https://github.com/bluesky-social/atproto/commit/ac6bd18f1dc3397dd29008eff2a1e40702a4e138), [`c5c6c7d`](https://github.com/bluesky-social/atproto/commit/c5c6c7dac3b08e5f63cc918f57705573028ad797)]:
|
|
8
|
+
- @atproto/lex-schema@0.0.17
|
|
9
|
+
- @atproto/lex-client@0.0.18
|
|
10
|
+
- @atproto/lex-builder@0.0.20
|
|
11
|
+
- @atproto/lex-installer@0.0.23
|
|
12
|
+
|
|
3
13
|
## 0.0.22
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Type-safe Lexicon tooling for AT Protocol data.
|
|
|
10
10
|
- Tree-shaking and composition friendly
|
|
11
11
|
|
|
12
12
|
```typescript
|
|
13
|
-
// Build data with generated
|
|
13
|
+
// Build and validate data with generated utilities
|
|
14
14
|
|
|
15
15
|
const newPost = app.bsky.feed.post.$build({
|
|
16
16
|
text: 'Hello, world!',
|
|
@@ -222,7 +222,7 @@ function renderPost(p: app.bsky.feed.post.Main) {
|
|
|
222
222
|
|
|
223
223
|
### Building data
|
|
224
224
|
|
|
225
|
-
It is recommended to use the generated builders to create data that conforms to the schema.
|
|
225
|
+
It is recommended to use the generated builders to create data that conforms to the schema. TypeScript ensures that all required fields are present at compile time.
|
|
226
226
|
|
|
227
227
|
```typescript
|
|
228
228
|
import { l } from '@atproto/lex'
|
|
@@ -234,6 +234,10 @@ const post = app.bsky.feed.post.$build({
|
|
|
234
234
|
text: 'Hello, world!',
|
|
235
235
|
createdAt: l.toDatetimeString(new Date()),
|
|
236
236
|
})
|
|
237
|
+
|
|
238
|
+
// For runtime validation, use $parse()/$validate() instead
|
|
239
|
+
const postWithDefaults = app.bsky.feed.post.$parse(post)
|
|
240
|
+
app.bsky.feed.post.$validate(post)
|
|
237
241
|
```
|
|
238
242
|
|
|
239
243
|
### Validation Helpers
|
|
@@ -359,7 +363,7 @@ app.bsky.feed.post.$safeParse(data, { strict: false })
|
|
|
359
363
|
|
|
360
364
|
#### `$build(data)` - Build with Defaults
|
|
361
365
|
|
|
362
|
-
Builds data
|
|
366
|
+
Builds data by adding the `$type` property and properly types the result. Note that `$build()` does not perform validation - use `$parse()` if you need validation:
|
|
363
367
|
|
|
364
368
|
```typescript
|
|
365
369
|
import { l } from '@atproto/lex'
|
|
@@ -662,7 +666,8 @@ console.log(result.cid)
|
|
|
662
666
|
Options:
|
|
663
667
|
|
|
664
668
|
- `rkey` - Custom record key (auto-generated if not provided)
|
|
665
|
-
- `validate` -
|
|
669
|
+
- `validate` - Asks the PDS to validate the record against schema when processing the request
|
|
670
|
+
- `validateRequest` - Validate the record locally against schema before submitting the request
|
|
666
671
|
- `swapCommit` - CID for optimistic concurrency control
|
|
667
672
|
|
|
668
673
|
#### `client.get()`
|
|
@@ -703,6 +708,8 @@ await client.put(app.bsky.actor.profile, {
|
|
|
703
708
|
Options:
|
|
704
709
|
|
|
705
710
|
- `rkey` - Record key (required for non-literal keys)
|
|
711
|
+
- `validate` - Validate record against schema before updating (falls back to `validateRequest` option if not specified)
|
|
712
|
+
- `validateRequest` - Alternative way to enable validation (used if `validate` is not specified)
|
|
706
713
|
- `swapCommit` - Expected repo commit CID
|
|
707
714
|
- `swapRecord` - Expected record CID
|
|
708
715
|
|
|
@@ -759,7 +766,7 @@ The `xrpcSafe()` method returns a union type that includes the success case (`Xr
|
|
|
759
766
|
import {
|
|
760
767
|
Client,
|
|
761
768
|
XrpcResponseError,
|
|
762
|
-
|
|
769
|
+
XrpcInvalidResponseError,
|
|
763
770
|
XrpcInternalError,
|
|
764
771
|
} from '@atproto/lex'
|
|
765
772
|
import * as com from './lexicons/com.js'
|
|
@@ -777,20 +784,26 @@ if (result.success) {
|
|
|
777
784
|
} else {
|
|
778
785
|
// Handle failure - result is an XrpcFailure
|
|
779
786
|
if (result instanceof XrpcResponseError) {
|
|
780
|
-
// The server
|
|
781
|
-
|
|
787
|
+
// The server responded with an error status code (4xx or 5xx).
|
|
788
|
+
// This is used for all error responses, whether or not they have a valid XRPC error payload.
|
|
789
|
+
|
|
790
|
+
result.error // string (e.g. "HandleNotFound", "AuthenticationRequired", "UpstreamFailure", etc.)
|
|
782
791
|
result.message // string
|
|
783
792
|
result.response.status // number
|
|
784
793
|
result.response.headers // Headers
|
|
785
|
-
result.payload //
|
|
786
|
-
|
|
787
|
-
//
|
|
788
|
-
//
|
|
794
|
+
result.payload // undefined | { body: unknown; encoding: string }
|
|
795
|
+
|
|
796
|
+
// Coerce to a valid XRPC error payload using toJSON():
|
|
797
|
+
result.toJSON() // { error: string, message?: string }
|
|
798
|
+
} else if (result instanceof XrpcInvalidResponseError) {
|
|
799
|
+
// The response was truly invalid (3xx redirect, malformed JSON, schema mismatch, etc.).
|
|
800
|
+
// This is a more specific error for responses that are not processable.
|
|
801
|
+
|
|
789
802
|
result.error // "UpstreamFailure"
|
|
790
803
|
result.message // string
|
|
791
804
|
result.response.status // number
|
|
792
805
|
result.response.headers // Headers
|
|
793
|
-
result.payload //
|
|
806
|
+
result.payload // undefined | { body: unknown; encoding: string }
|
|
794
807
|
} else if (result instanceof XrpcInternalError) {
|
|
795
808
|
// Something went wrong on the client side (network error, etc.)
|
|
796
809
|
result.error // "InternalServerError"
|
|
@@ -810,9 +823,9 @@ if (result.success) {
|
|
|
810
823
|
|
|
811
824
|
The `XrpcFailure<M>` type is a union of three error classes:
|
|
812
825
|
|
|
813
|
-
1. **`XrpcResponseError`** - The server
|
|
826
|
+
1. **`XrpcResponseError`** - The server responded with a 4xx/5xx error status code. This is used for all error responses from the upstream server.
|
|
814
827
|
|
|
815
|
-
2. **`
|
|
828
|
+
2. **`XrpcInvalidResponseError`** - The upstream server returned a 2xx/3xx that does not comply with XRPC specifications for successful responses. A sub-class, `XrpcResponseValidationError`, is used for payload schema validation failures specifically.
|
|
816
829
|
|
|
817
830
|
3. **`XrpcInternalError`** - Client-side errors (network failures, timeouts, etc.)
|
|
818
831
|
|
|
@@ -947,7 +960,7 @@ isLanguageString('en-US') // true
|
|
|
947
960
|
|
|
948
961
|
### Datetime Strings
|
|
949
962
|
|
|
950
|
-
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
|
|
963
|
+
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 0 or after year 9999 will produce non-conforming strings). To ensure correctness and type safety, use the `DatetimeString` utilities exported from `@atproto/lex`:
|
|
951
964
|
|
|
952
965
|
- **`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.
|
|
953
966
|
- **`asDatetimeString(input: string)`** - Validates and casts an arbitrary string to `DatetimeString`, throwing an `InvalidDatetimeError` if the string does not conform.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon tooling for AT",
|
|
6
6
|
"keywords": [
|
|
@@ -38,19 +38,20 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"tslib": "^2.8.1",
|
|
40
40
|
"yargs": "^17.0.0",
|
|
41
|
-
"@atproto/lex-builder": "^0.0.
|
|
42
|
-
"@atproto/lex-client": "^0.0.
|
|
41
|
+
"@atproto/lex-builder": "^0.0.20",
|
|
42
|
+
"@atproto/lex-client": "^0.0.18",
|
|
43
43
|
"@atproto/lex-data": "^0.0.14",
|
|
44
44
|
"@atproto/lex-json": "^0.0.14",
|
|
45
|
-
"@atproto/lex-installer": "^0.0.
|
|
46
|
-
"@atproto/lex-schema": "^0.0.
|
|
45
|
+
"@atproto/lex-installer": "^0.0.23",
|
|
46
|
+
"@atproto/lex-schema": "^0.0.17"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/yargs": "^17.0.33",
|
|
50
50
|
"vitest": "^4.0.16"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
|
-
"
|
|
53
|
+
"codegen": "./bin/lex build --override --indexFile --lexicons ./lexicons --out ./tests/lexicons --lib @atproto/lex-schema",
|
|
54
|
+
"prebuild": "pnpm run codegen",
|
|
54
55
|
"build": "tsc --build tsconfig.build.json",
|
|
55
56
|
"test": "vitest run"
|
|
56
57
|
}
|