import {
mutationPreview,
parseAction,
resendRequest,
unwrapList,
type MutationGuardInput,
type ResendAuthOptions,
} from './resend-core.ts'
/** GET /api-keys — metadata only (names/ids; never secret values). */
export async function listApiKeys(input: ResendAuthOptions = {}) {
const body = await resendRequest({
path: '/api-keys',
account: input.account,
secretName: input.secretName,
integration: input.integration,
})
return unwrapList(body)
}
/** GET /webhooks */
export async function listWebhooks(input: ResendAuthOptions = {}) {
const body = await resendRequest({
path: '/webhooks',
account: input.account,
secretName: input.secretName,
integration: input.integration,
})
return unwrapList(body)
}
export type CreateWebhookInput = MutationGuardInput & {
endpoint: string
events: string[]
name?: string
}
/** POST /webhooks — register an endpoint for events such as email.bounced. */
export async function createWebhook(input: CreateWebhookInput) {
if (!input.endpoint) throw new Error('createWebhook: endpoint is required')
if (!Array.isArray(input.events) || input.events.length === 0) {
throw new Error('createWebhook: events must be a non-empty array')
}
const body = { endpoint: input.endpoint, events: input.events, name: input.name }
const preview = mutationPreview(input, {
action: 'create webhook ' + input.endpoint,
method: 'POST',
path: '/webhooks',
body,
})
if (preview) return preview
return await resendRequest({
path: '/webhooks',
method: 'POST',
body,
account: input.account,
secretName: input.secretName,
integration: input.integration,
})
}
export type ListLogsInput = ResendAuthOptions & {
limit?: number
after?: string
before?: string
}
/** GET /logs — recent API request logs. */
export async function listLogs(input: ListLogsInput = {}) {
const body = await resendRequest({
path: '/logs',
query: { limit: input.limit, after: input.after, before: input.before },
account: input.account,
secretName: input.secretName,
integration: input.integration,
})
return unwrapList(body)
}
const accountActions = [
'list-api-keys',
'list-webhooks',
'create-webhook',
'list-logs',
] as const
/** Account dispatcher. Defaults to list-api-keys. */
export default async function account(input: Record<string, unknown> = {}) {
const action = parseAction(input.action, accountActions, 'list-api-keys', 'account')
switch (action) {
case 'list-api-keys':
return await listApiKeys(input)
case 'list-webhooks':
return await listWebhooks(input)
case 'create-webhook':
return await createWebhook(input as CreateWebhookInput)
case 'list-logs':
return await listLogs(input as ListLogsInput)
default: {
const exhaustive: never = action
throw new Error('Unhandled account action: ' + String(exhaustive))
}
}
}