Skip to content

Kody is live

Watch the launch video — what Kody is, and why it exists.

← Public packages

@noah/hey-email

HEY.com email integration with Kody: search threads, read messages/attachments, create drafts, and send with explicit confirmation.

src/search.js

32 lines · 1.3 KB · JavaScript
import { heyFetch } from './client.js'

/**
 * Search HEY mail via advanced_search.
 * Use when finding purchase/shipping/sale threads by keyword or sender.
 * Sender filters should be passed as from: "example.com" (embedded in q as from:domain).
 *
 * @param {{ q?: string, query?: string, from?: string, date?: string, in?: string }} [input]
 * @returns {Promise<{ matches: unknown[], raw: unknown }>}
 *
 * @example
 * import search from 'kody:@noah/hey-email/search'
 * const { matches } = await search({ from: 'example.com', date: 'last_30_days' })
 */
export default async function search(input = {}) {
  const params = new URLSearchParams()
  const parts = []
  const q = input.q ?? input.query
  if (q) parts.push(String(q))
  if (input.from) parts.push(`from:${String(input.from)}`)
  if (parts.length) params.set('q', parts.join(' '))
  if (input.date) params.set('date', String(input.date))
  if (input.in) params.set('in', String(input.in))
  for (const [k, v] of Object.entries(input)) {
    if (['q', 'query', 'from', 'date', 'in'].includes(k)) continue
    if (v == null || v === '') continue
    params.set(k, String(v))
  }
  const qs = params.toString()
  const raw = await heyFetch(`/advanced_search.json${qs ? `?${qs}` : ''}`)
  return { matches: raw?.matches || [], raw }
}