adapters.ts raw

   1  /**
   2   * Adapter functions for gradual migration from primitives to value objects.
   3   *
   4   * These functions allow existing code to continue using string primitives
   5   * while new code can use value objects. Use these at the boundaries
   6   * between old and new code.
   7   */
   8  
   9  import { Pubkey, RelayUrl, EventId, Timestamp } from './value-objects'
  10  
  11  // ============================================================================
  12  // Pubkey Adapters
  13  // ============================================================================
  14  
  15  /**
  16   * Convert a hex string to a Pubkey value object.
  17   * @throws InvalidPubkeyError if invalid
  18   */
  19  export const toPubkey = (hex: string): Pubkey => Pubkey.fromHex(hex)
  20  
  21  /**
  22   * Try to convert a string (hex, npub, nprofile) to a Pubkey.
  23   * Returns null if invalid.
  24   */
  25  export const tryToPubkey = (input: string): Pubkey | null => Pubkey.tryFromString(input)
  26  
  27  /**
  28   * Convert a Pubkey back to a hex string for interop with existing code.
  29   */
  30  export const fromPubkey = (pubkey: Pubkey): string => pubkey.hex
  31  
  32  /**
  33   * Convert an array of hex strings to Pubkeys.
  34   * Skips invalid entries.
  35   */
  36  export const toPubkeys = (hexes: string[]): Pubkey[] =>
  37    hexes.map((h) => Pubkey.tryFromString(h)).filter((p): p is Pubkey => p !== null)
  38  
  39  /**
  40   * Convert an array of Pubkeys to hex strings.
  41   */
  42  export const fromPubkeys = (pubkeys: Pubkey[]): string[] => pubkeys.map((p) => p.hex)
  43  
  44  // ============================================================================
  45  // RelayUrl Adapters
  46  // ============================================================================
  47  
  48  /**
  49   * Convert a URL string to a RelayUrl value object.
  50   * @throws InvalidRelayUrlError if invalid
  51   */
  52  export const toRelayUrl = (url: string): RelayUrl => RelayUrl.create(url)
  53  
  54  /**
  55   * Try to convert a URL string to a RelayUrl.
  56   * Returns null if invalid.
  57   */
  58  export const tryToRelayUrl = (url: string): RelayUrl | null => RelayUrl.tryCreate(url)
  59  
  60  /**
  61   * Convert a RelayUrl back to a string for interop with existing code.
  62   */
  63  export const fromRelayUrl = (relay: RelayUrl): string => relay.value
  64  
  65  /**
  66   * Convert an array of URL strings to RelayUrls.
  67   * Skips invalid entries.
  68   */
  69  export const toRelayUrls = (urls: string[]): RelayUrl[] =>
  70    urls.map((u) => RelayUrl.tryCreate(u)).filter((r): r is RelayUrl => r !== null)
  71  
  72  /**
  73   * Convert an array of RelayUrls to strings.
  74   */
  75  export const fromRelayUrls = (relays: RelayUrl[]): string[] => relays.map((r) => r.value)
  76  
  77  // ============================================================================
  78  // EventId Adapters
  79  // ============================================================================
  80  
  81  /**
  82   * Convert a hex string to an EventId value object.
  83   * @throws InvalidEventIdError if invalid
  84   */
  85  export const toEventId = (hex: string): EventId => EventId.fromHex(hex)
  86  
  87  /**
  88   * Try to convert a string (hex, note1, nevent1) to an EventId.
  89   * Returns null if invalid.
  90   */
  91  export const tryToEventId = (input: string): EventId | null => EventId.tryFromString(input)
  92  
  93  /**
  94   * Convert an EventId back to a hex string for interop with existing code.
  95   */
  96  export const fromEventId = (eventId: EventId): string => eventId.hex
  97  
  98  /**
  99   * Convert an array of hex strings to EventIds.
 100   * Skips invalid entries.
 101   */
 102  export const toEventIds = (hexes: string[]): EventId[] =>
 103    hexes.map((h) => EventId.tryFromString(h)).filter((e): e is EventId => e !== null)
 104  
 105  /**
 106   * Convert an array of EventIds to hex strings.
 107   */
 108  export const fromEventIds = (eventIds: EventId[]): string[] => eventIds.map((e) => e.hex)
 109  
 110  // ============================================================================
 111  // Timestamp Adapters
 112  // ============================================================================
 113  
 114  /**
 115   * Convert a Unix timestamp (seconds) to a Timestamp value object.
 116   * @throws InvalidTimestampError if invalid
 117   */
 118  export const toTimestamp = (unix: number): Timestamp => Timestamp.fromUnix(unix)
 119  
 120  /**
 121   * Try to convert a Unix timestamp to a Timestamp.
 122   * Returns null if invalid.
 123   */
 124  export const tryToTimestamp = (unix: number): Timestamp | null => Timestamp.tryFromUnix(unix)
 125  
 126  /**
 127   * Convert a Timestamp back to a Unix number for interop with existing code.
 128   */
 129  export const fromTimestamp = (timestamp: Timestamp): number => timestamp.unix
 130  
 131  // ============================================================================
 132  // Set Adapters (for mute lists, follow lists, etc.)
 133  // ============================================================================
 134  
 135  /**
 136   * Convert a Set of hex strings to a Set wrapper that uses Pubkey for lookups.
 137   */
 138  export const createPubkeySet = (
 139    hexSet: Set<string>
 140  ): {
 141    has: (pubkey: Pubkey) => boolean
 142    add: (pubkey: Pubkey) => void
 143    delete: (pubkey: Pubkey) => boolean
 144    toHexSet: () => Set<string>
 145    toPubkeys: () => Pubkey[]
 146  } => ({
 147    has: (pubkey: Pubkey) => hexSet.has(pubkey.hex),
 148    add: (pubkey: Pubkey) => hexSet.add(pubkey.hex),
 149    delete: (pubkey: Pubkey) => hexSet.delete(pubkey.hex),
 150    toHexSet: () => hexSet,
 151    toPubkeys: () => Array.from(hexSet).map((h) => Pubkey.fromHex(h)),
 152  })
 153  
 154  /**
 155   * Convert a Set of URL strings to a Set wrapper that uses RelayUrl for lookups.
 156   */
 157  export const createRelayUrlSet = (
 158    urlSet: Set<string>
 159  ): {
 160    has: (relay: RelayUrl) => boolean
 161    add: (relay: RelayUrl) => void
 162    delete: (relay: RelayUrl) => boolean
 163    toStringSet: () => Set<string>
 164    toRelayUrls: () => RelayUrl[]
 165  } => ({
 166    has: (relay: RelayUrl) => urlSet.has(relay.value),
 167    add: (relay: RelayUrl) => urlSet.add(relay.value),
 168    delete: (relay: RelayUrl) => urlSet.delete(relay.value),
 169    toStringSet: () => urlSet,
 170    toRelayUrls: () =>
 171      Array.from(urlSet)
 172        .map((u) => RelayUrl.tryCreate(u))
 173        .filter((r): r is RelayUrl => r !== null),
 174  })
 175