forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type ValidationResult} from '@atproto/lexicon'
2
3export * as post from '#/types/bsky/post'
4export * as profile from '#/types/bsky/profile'
5export * as starterPack from '#/types/bsky/starterPack'
6
7/**
8 * Fast type checking without full schema validation, for use with data we
9 * trust, or for non-critical path use cases. Why? Our SDK's `is*` identity
10 * utils do not assert the type of the entire object, only the `$type` string.
11 *
12 * For full validation of the object schema, use the `validate` export from
13 * this file.
14 *
15 * Usage:
16 * ```ts
17 * import * as bsky from '#/types/bsky'
18 *
19 * if (bsky.dangerousIsType<AppBskyFeedPost.Record>(item, AppBskyFeedPost.isRecord)) {
20 * // `item` has type `$Typed<AppBskyFeedPost.Record>` here
21 * }
22 * ```
23 */
24export function dangerousIsType<R extends {$type?: string}>(
25 record: unknown,
26 identity: <V>(v: V) => v is V & {$type: NonNullable<R['$type']>},
27): record is R {
28 return identity(record)
29}
30
31/**
32 * Fully validates the object schema, which has a performance cost.
33 *
34 * For faster checks with data we trust, like that from our app view, use the
35 * `dangerousIsType` export from this same file.
36 *
37 * Usage:
38 * ```ts
39 * import * as bsky from '#/types/bsky'
40 *
41 * if (bsky.validate(item, AppBskyFeedPost.validateRecord)) {
42 * // `item` has type `$Typed<AppBskyFeedPost.Record>` here
43 * }
44 * ```
45 */
46export function validate<R extends {$type?: string}>(
47 record: unknown,
48 validator: (v: unknown) => ValidationResult<R>,
49): record is R {
50 return validator(record).success
51}