prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Infer blob lexicon type as BlobRef instead of browser Blob (#103)

Replaces the browser's `Blob` type with the correct type for Blob references over atproto.
I've defined a local `BlobRef` type (`{ $type, ref, mimeType, size }`) which mirrors the type from `@atproto/api`/`@atproto/lex-data`, without
adding either as a dependency.

authored by

JP Hastings-Edrei and committed by
GitHub
8bf9b2b3 e655ec18

+47 -5
+20
.changeset/blob-ref-type.md
··· 1 + --- 2 + "prototypey": minor 3 + --- 4 + 5 + Infer `lx.blob(...)` as `BlobRef` instead of the browser's `Blob`. 6 + 7 + The browser's `Blob` describes binary data on the client, not the AT Protocol blob shape that actually appears in records over the wire. Lexicons using `lx.blob(...)` now infer to a locally-defined `BlobRef`: 8 + 9 + ```ts 10 + type BlobRef = { 11 + $type: "blob"; 12 + ref: { $link: string } | Cid; 13 + mimeType: string; 14 + size: number; 15 + }; 16 + ``` 17 + 18 + This mirrors `BlobRef` from [`@atproto/api`](https://github.com/bluesky-social/atproto/tree/main/packages/api) / [`@atproto/lex-data`](https://github.com/bluesky-social/atproto/tree/main/packages/lex-data) without taking either as a dependency. `BlobRef` is exported from the package root for direct use. 19 + 20 + **Breaking:** code that previously read blob fields as `Blob` (e.g. calling `.arrayBuffer()` on them) will need to update to the `BlobRef` shape.
+24 -2
packages/prototypey/core/infer.ts
··· 10 10 type NestedObjectError = 11 11 '[Nested objects are not supported in lexicon definitions. Per the Lexicon spec, objects can be "nested inside other definitions by reference" (https://atproto.com/specs/lexicon#object). Define each object in its own lexicon def and use lx.ref() instead.]'; 12 12 13 + /** 14 + * Minimal structural CID, compatible with the `CID` class from 15 + * [`multiformats`](https://github.com/multiformats/js-multiformats) and the `Cid` interface from 16 + * [`@atproto/lex-data`](https://github.com/bluesky-social/atproto/tree/main/packages/lex-data). 17 + */ 18 + type Cid = { readonly bytes: Uint8Array }; 19 + 20 + /** 21 + * AT Protocol blob reference. Mirrors `BlobRef` from 22 + * [`@atproto/api`](https://github.com/bluesky-social/atproto/tree/main/packages/api) / 23 + * [`@atproto/lex-data`](https://github.com/bluesky-social/atproto/tree/main/packages/lex-data), 24 + * but defined locally to avoid pulling in those packages as dependencies. 25 + * 26 + * @see https://atproto.com/specs/data-model#blob-type 27 + */ 28 + export type BlobRef = { 29 + $type: "blob"; 30 + ref: { $link: string } | Cid; 31 + mimeType: string; 32 + size: number; 33 + }; 34 + 13 35 /** Resolves property type, returning NestedObjectError for inline nested objects. */ 14 36 type InferPropertyType<T> = T extends { type: "object" } 15 37 ? NestedObjectError ··· 61 83 : T extends { type: "cid-link" } 62 84 ? string 63 85 : T extends { type: "blob" } 64 - ? Blob 86 + ? BlobRef 65 87 : never; 66 88 67 89 type InferToken<T> = T extends { enum: readonly (infer U)[] } ? U : string; ··· 223 245 : // Reference not found in definitions 224 246 `[Reference not found: #${DefName}]` 225 247 : // Handle arrays (but not Uint8Array or other typed arrays) 226 - T extends Uint8Array | Blob 248 + T extends Uint8Array | BlobRef 227 249 ? T 228 250 : T extends readonly (infer Item)[] 229 251 ? ReplaceRefsInType<Item, Defs, Visited>[]
+1 -1
packages/prototypey/core/main.ts
··· 1 1 export { lx, fromJSON } from "./lib.ts"; 2 - export { type Infer } from "./infer.ts"; 2 + export { type Infer, type BlobRef } from "./infer.ts"; 3 3 export type * from "@atproto/lexicon";
+1 -1
packages/prototypey/core/tests/from-json-infer.test.ts
··· 215 215 }); 216 216 217 217 attest(lexicon["~infer"]).type.toString.snap( 218 - '{ $type: "test.blob"; image?: Blob | undefined }', 218 + '{ $type: "test.blob"; image?: BlobRef | undefined }', 219 219 ); 220 220 }); 221 221
+1 -1
packages/prototypey/core/tests/infer.test.ts
··· 248 248 }); 249 249 250 250 attest(lexicon["~infer"]).type.toString.snap( 251 - '{ $type: "test.blob"; image?: Blob | undefined }', 251 + '{ $type: "test.blob"; image?: BlobRef | undefined }', 252 252 ); 253 253 }); 254 254