about summary refs log tree commit diff
path: root/bskyembed/src/types
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2025-08-26 18:24:46 +0300
committerGitHub <noreply@github.com>2025-08-26 08:24:46 -0700
commitb70e5b2f387e8de6dac5d388aee1ccbf5b217adc (patch)
treee540d731ec004e58e1280c6382b00b6947d03e63 /bskyembed/src/types
parent5a074fa37acafb0cf11acbdd0a931411b1c63aa2 (diff)
downloadvoidsky-b70e5b2f387e8de6dac5d388aee1ccbf5b217adc.tar.zst
Add verification checkmarks to `embed.bsky.app` (#8644)
* update vite+typescript

* update atproto api to latest, split out utils

* add checkmark to post

* add checkie to embed

* revert change to example post

* fix ext link color
Diffstat (limited to 'bskyembed/src/types')
-rw-r--r--bskyembed/src/types/bsky/index.ts49
-rw-r--r--bskyembed/src/types/bsky/profile.ts10
2 files changed, 59 insertions, 0 deletions
diff --git a/bskyembed/src/types/bsky/index.ts b/bskyembed/src/types/bsky/index.ts
new file mode 100644
index 000000000..c462cdf65
--- /dev/null
+++ b/bskyembed/src/types/bsky/index.ts
@@ -0,0 +1,49 @@
+import {ValidationResult} from '@atproto/lexicon'
+
+export * as profile from './profile'
+
+/**
+ * Fast type checking without full schema validation, for use with data we
+ * trust, or for non-critical path use cases. Why? Our SDK's `is*` identity
+ * utils do not assert the type of the entire object, only the `$type` string.
+ *
+ * For full validation of the object schema, use the `validate` export from
+ * this file.
+ *
+ * Usage:
+ * ```ts
+ * import * as bsky from '#/types/bsky'
+ *
+ * if (bsky.dangerousIsType<AppBskyFeedPost.Record>(item, AppBskyFeedPost.isRecord)) {
+ *   // `item` has type `$Typed<AppBskyFeedPost.Record>` here
+ * }
+ * ```
+ */
+export function dangerousIsType<R extends {$type?: string}>(
+  record: unknown,
+  identity: <V>(v: V) => v is V & {$type: NonNullable<R['$type']>},
+): record is R {
+  return identity(record)
+}
+
+/**
+ * Fully validates the object schema, which has a performance cost.
+ *
+ * For faster checks with data we trust, like that from our app view, use the
+ * `dangerousIsType` export from this same file.
+ *
+ * Usage:
+ * ```ts
+ * import * as bsky from '#/types/bsky'
+ *
+ * if (bsky.validate(item, AppBskyFeedPost.validateRecord)) {
+ *   // `item` has type `$Typed<AppBskyFeedPost.Record>` here
+ * }
+ * ```
+ */
+export function validate<R extends {$type?: string}>(
+  record: unknown,
+  validator: (v: unknown) => ValidationResult<R>,
+): record is R {
+  return validator(record).success
+}
diff --git a/bskyembed/src/types/bsky/profile.ts b/bskyembed/src/types/bsky/profile.ts
new file mode 100644
index 000000000..12c8146ae
--- /dev/null
+++ b/bskyembed/src/types/bsky/profile.ts
@@ -0,0 +1,10 @@
+import {type AppBskyActorDefs, type ChatBskyActorDefs} from '@atproto/api'
+
+/**
+ * Matches any profile view exported by our SDK
+ */
+export type AnyProfileView =
+  | AppBskyActorDefs.ProfileViewBasic
+  | AppBskyActorDefs.ProfileView
+  | AppBskyActorDefs.ProfileViewDetailed
+  | ChatBskyActorDefs.ProfileViewBasic