import { domainSummary, mutationPreview, pagingQuery, postmarkRequest, type MutationGuardInput, type PostmarkAuthOptions, type PostmarkObject, } from './core.ts' function auth(input: PostmarkAuthOptions) { return { account: input.account, secretName: input.secretName, accountSecretName: input.accountSecretName } } export type ListDomainsInput = PostmarkAuthOptions & { count?: number offset?: number } /** GET /domains — account sending domains. Requires `postmark-account`. */ export async function listDomains(input: ListDomainsInput = {}) { const paging = pagingQuery(input) const body = (await postmarkRequest({ path: '/domains', tokenKind: 'account', query: paging, ...auth(input), })) as { TotalCount?: number; Domains?: PostmarkObject[] } const domains = Array.isArray(body.Domains) ? body.Domains : [] return { totalCount: body.TotalCount ?? domains.length, domains: domains.map((domain) => domainSummary(domain)).filter(Boolean), } } /** GET /domains/{id} — domain detail including DNS records. */ export async function getDomain(input: PostmarkAuthOptions & { domainId: number | string }) { if (input.domainId == null || input.domainId === '') { throw new Error('getDomain: domainId is required') } return await postmarkRequest({ path: '/domains/' + encodeURIComponent(String(input.domainId)), tokenKind: 'account', ...auth(input), }) } export type CreateDomainInput = MutationGuardInput & { name: string returnPathDomain?: string } /** POST /domains — register a sending domain. */ export async function createDomain(input: CreateDomainInput) { if (!input.name) throw new Error('createDomain: name is required (e.g. "mail.example.com")') const body = { Name: input.name, ReturnPathDomain: input.returnPathDomain, } const preview = mutationPreview(input, { action: 'create domain ' + input.name, method: 'POST', path: '/domains', body, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path: '/domains', method: 'POST', body, tokenKind: 'account', ...auth(input), }) } export type EditDomainInput = MutationGuardInput & { domainId: number | string returnPathDomain?: string } /** PUT /domains/{id} — update Return-Path. */ export async function editDomain(input: EditDomainInput) { if (input.domainId == null || input.domainId === '') { throw new Error('editDomain: domainId is required') } const path = '/domains/' + encodeURIComponent(String(input.domainId)) const body = { ReturnPathDomain: input.returnPathDomain } const preview = mutationPreview(input, { action: 'edit domain ' + input.domainId, method: 'PUT', path, body, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path, method: 'PUT', body, tokenKind: 'account', ...auth(input), }) } /** DELETE /domains/{id} — delete a domain. Requires `confirm: true`. */ export async function deleteDomain(input: MutationGuardInput & { domainId: number | string }) { if (input.domainId == null || input.domainId === '') { throw new Error('deleteDomain: domainId is required') } const path = '/domains/' + encodeURIComponent(String(input.domainId)) const preview = mutationPreview(input, { action: 'delete domain ' + input.domainId, method: 'DELETE', path, requireConfirm: true, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path, method: 'DELETE', tokenKind: 'account', ...auth(input), }) } /** PUT /domains/{id}/verifyDkim — trigger DKIM verification. */ export async function verifyDkim(input: MutationGuardInput & { domainId: number | string }) { if (input.domainId == null || input.domainId === '') { throw new Error('verifyDkim: domainId is required') } const path = '/domains/' + encodeURIComponent(String(input.domainId)) + '/verifyDkim' const preview = mutationPreview(input, { action: 'verify DKIM for domain ' + input.domainId, method: 'PUT', path, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path, method: 'PUT', tokenKind: 'account', ...auth(input), }) } /** PUT /domains/{id}/verifyReturnPath — trigger Return-Path verification. */ export async function verifyReturnPath(input: MutationGuardInput & { domainId: number | string }) { if (input.domainId == null || input.domainId === '') { throw new Error('verifyReturnPath: domainId is required') } const path = '/domains/' + encodeURIComponent(String(input.domainId)) + '/verifyReturnPath' const preview = mutationPreview(input, { action: 'verify Return-Path for domain ' + input.domainId, method: 'PUT', path, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path, method: 'PUT', tokenKind: 'account', ...auth(input), }) } /** POST /domains/{id}/rotatedkim — start a DKIM rotation. Requires `confirm: true`. */ export async function rotateDkim(input: MutationGuardInput & { domainId: number | string }) { if (input.domainId == null || input.domainId === '') { throw new Error('rotateDkim: domainId is required') } const path = '/domains/' + encodeURIComponent(String(input.domainId)) + '/rotatedkim' const preview = mutationPreview(input, { action: 'rotate DKIM for domain ' + input.domainId, method: 'POST', path, requireConfirm: true, tokenKind: 'account', }) if (preview) return preview return await postmarkRequest({ path, method: 'POST', tokenKind: 'account', ...auth(input), }) } export default listDomains