Skip to content
← Public packages

@kody/agent-mail

AgentMail inboxes for agents: create/list inboxes, messages, threads, and Svix webhook ingress.

FollowFork

Files

get-rule.ts

48 lines · 1.6 KB · TypeScript
import { agentMailRequest } from './core.ts'
import { listPath, normalizeEntry, slimEntry, type ListSelectorInput } from './lists.ts'

export type GetRuleInput = ListSelectorInput & {
	/** Email address or domain to look up. */
	entry?: string
	/** Alias for `entry`. */
	match?: string
}

/**
 * Read one AgentMail allow/block list entry. Read-only; returns
 * `{ found: false }` instead of throwing when AgentMail answers 404.
 * Uses GET /v0/lists/{direction}/{type}/{entry} (or the pod / inbox variant).
 *
 * @param input.type - `allow` or `block` (required)
 * @param input.entry - Email address or domain (or `match`)
 * @param input.direction - `receive` (default), `reply`, or `send`
 * @param input.inboxId - Optional inbox scope
 * @param input.podId - Optional pod scope
 * @returns `{ found, scope, scopeId, direction, type, entry }`
 *
 * @example
 * import getRule from 'kody:@kody/agent-mail/get-rule'
 * const { found } = await getRule({ type: 'block', entry: 'spam.example' })
 */
export default async function getRule(input: GetRuleInput) {
	const { entry } = normalizeEntry(input.entry ?? input.match)
	const target = listPath(input, entry)
	const base = {
		scope: target.scope,
		scopeId: target.scopeId,
		direction: target.direction,
		type: target.type,
	}
	try {
		const raw = await agentMailRequest({
			path: target.path,
			label: 'AgentMail get list entry',
		})
		return { found: true as const, ...base, entry: slimEntry(raw) }
	} catch (error) {
		if (/ HTTP 404:/.test(String((error as Error)?.message ?? error))) {
			return { found: false as const, ...base, entry: null }
		}
		throw error
	}
}