Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import type { AnyKeyValueFragment } from "@hey/indexer";
2import type { Address } from "viem";
3
4interface AddressKeyValue {
5 __typename: "AddressKeyValue";
6 key: string;
7 address: string;
8}
9
10interface BigDecimalKeyValue {
11 __typename: "BigDecimalKeyValue";
12 key: string;
13 bigDecimal: string;
14}
15
16interface StringKeyValue {
17 __typename: "StringKeyValue";
18 key: string;
19 string: string;
20}
21
22type ValidKeyValue = AddressKeyValue | BigDecimalKeyValue | StringKeyValue;
23
24const isValidKeyValue = (kv: AnyKeyValueFragment): kv is ValidKeyValue =>
25 kv.__typename === "AddressKeyValue" ||
26 kv.__typename === "BigDecimalKeyValue" ||
27 kv.__typename === "StringKeyValue";
28
29const getAnyKeyValue = (
30 anyKeyValue: AnyKeyValueFragment[],
31 key: string
32): { address?: Address; bigDecimal?: string; string?: string } | null => {
33 const item = anyKeyValue.find(
34 (kv): kv is ValidKeyValue => isValidKeyValue(kv) && kv.key === key
35 );
36
37 if (!item) return null;
38
39 switch (item.__typename) {
40 case "AddressKeyValue":
41 return { address: item.address as Address };
42 case "BigDecimalKeyValue":
43 return { bigDecimal: item.bigDecimal };
44 case "StringKeyValue":
45 return { string: item.string };
46 default:
47 return null;
48 }
49};
50
51export default getAnyKeyValue;