Skip to content

Commit 7e97abc

Browse files
sij411claude
andcommitted
Replace type casting with type predicate in relay
Replaces unsafe type casting with a type predicate function following Fedify's established patterns (similar to isActor()). Changes: - Add isRelayFollower() type predicate in types.ts - Replace "value as RelayFollower" cast with isRelayFollower(value) in builder.ts - Remove unused RelayFollower type import Benefits: - Compile-time type narrowing after guard check - Runtime validation of KV store data - Consistent with Fedify's type-safe design philosophy - No unsafe type assertions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 7dbe704 commit 7e97abc

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/relay/src/builder.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import { Application, isActor, Object } from "@fedify/fedify/vocab";
1010
import type { Actor } from "@fedify/fedify/vocab";
1111
import {
12+
isRelayFollower,
1213
RELAY_SERVER_ACTOR,
13-
type RelayFollower,
1414
type RelayOptions,
1515
} from "./types.ts";
1616

@@ -79,9 +79,8 @@ async function getFollowerActors(
7979
const actors: Actor[] = [];
8080

8181
for await (const { value } of ctx.data.kv.list(["follower"])) {
82-
const follower = value as RelayFollower;
83-
if (!follower) continue;
84-
const actor = await Object.fromJsonLd(follower.actor);
82+
if (!isRelayFollower(value)) continue;
83+
const actor = await Object.fromJsonLd(value.actor);
8584
if (!isActor(actor)) continue;
8685
actors.push(actor);
8786
}

packages/relay/src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,21 @@ export interface RelayFollower {
3737
readonly actor: unknown;
3838
readonly state: "pending" | "accepted";
3939
}
40+
41+
/**
42+
* Type predicate to check if a value is a valid RelayFollower.
43+
* Provides both runtime validation and compile-time type narrowing.
44+
*
45+
* @param value The value to check
46+
* @returns true if the value is a RelayFollower
47+
*/
48+
export function isRelayFollower(value: unknown): value is RelayFollower {
49+
if (!value || typeof value !== "object") return false;
50+
const obj = value as Record<string, unknown>;
51+
return (
52+
"actor" in obj &&
53+
"state" in obj &&
54+
typeof obj.state === "string" &&
55+
(obj.state === "pending" || obj.state === "accepted")
56+
);
57+
}

0 commit comments

Comments
 (0)