Files
forwarder/admin-ui/src/lib/api.ts
T
1sonhador bd2d04aba7 feat: batch retry for failed messages and visible retry errors
Show API errors when single retry fails and add POST /messages/retry-batch
plus a "Repetir 5 falhadas" button for post-deploy testing.
2026-07-01 09:23:41 +01:00

175 lines
4.8 KiB
TypeScript

const BASE = '/api'
function authHeader(): Record<string, string> {
try {
const cred = sessionStorage.getItem('fwd_auth')
if (cred) return { Authorization: `Basic ${cred}` }
} catch { /* ignore */ }
return {}
}
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, {
headers: { 'Content-Type': 'application/json', ...authHeader(), ...init?.headers },
...init,
})
if (!res.ok) {
const text = await res.text().catch(() => res.statusText)
throw new Error(`${res.status} ${text}`)
}
if (res.status === 204) return undefined as T
return res.json()
}
export const DEFAULT_DESTINATION_EMAIL = '[email protected]'
export interface Account {
id: number
name: string
email: string
imap_host: string
imap_port: number
imap_user: string
smtp_host: string
smtp_port: number
smtp_user: string
destination_email: string
sync_interval_minutes: number
active: boolean
created_at: string
updated_at: string
}
export type AccountCreate = Omit<Account, 'id' | 'created_at' | 'updated_at'> & {
imap_password: string
smtp_password: string
}
export interface Message {
id: number
account_id: number
account_name: string
message_uid: string
subject: string
sender: string
received_at: string
status: 'pending' | 'forwarded' | 'failed'
error: string | null
forwarded_at: string | null
}
export interface RetryBatchResult {
attempted: number
succeeded: number
failed: number
errors: string[]
}
export interface MessageList {
items: Message[]
total: number
limit: number
offset: number
}
export interface Stats {
accounts_total: number
accounts_active: number
messages_total: number
messages_pending: number
messages_forwarded: number
messages_failed: number
last_sync_at: string | null
}
export interface ConnectionTestResult {
imap_ok: boolean
imap_error: string | null
smtp_ok: boolean
smtp_error: string | null
bizdocs_ok: boolean
bizdocs_error: string | null
}
export interface SyncRunResult {
id: number
account_id: number
status: string
dry_run: boolean
max_emails: number | null
messages_scanned: number
messages_matched: number
messages_forwarded: number
messages_skipped: number
messages_failed: number
error_message: string | null
started_at: string
finished_at: string | null
}
export interface TestPayload {
imap_host: string
imap_port: number
imap_user: string
imap_password: string
smtp_host: string
smtp_port: number
smtp_user: string
smtp_password: string
bizdocs_api_key: string
bizdocs_entity_id: string
}
// Accounts
export const api = {
accounts: {
list: () => request<Account[]>('/accounts'),
get: (id: number) => request<Account>(`/accounts/${id}`),
create: (data: AccountCreate) =>
request<Account>('/accounts', { method: 'POST', body: JSON.stringify(data) }),
update: (id: number, data: Partial<AccountCreate>) =>
request<Account>(`/accounts/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
delete: (id: number) => request<void>(`/accounts/${id}`, { method: 'DELETE' }),
sync: (id: number, opts?: { dry_run?: boolean; max_emails?: number }) => {
const q = new URLSearchParams()
if (opts?.dry_run) q.set('dry_run', 'true')
if (opts?.max_emails != null) q.set('max_emails', String(opts.max_emails))
const qs = q.toString()
return request<SyncRunResult>(`/accounts/${id}/sync${qs ? '?' + qs : ''}`, {
method: 'POST',
})
},
},
messages: {
list: (params?: { account_id?: number; status?: string; limit?: number; offset?: number }) => {
const q = new URLSearchParams()
if (params?.account_id) q.set('account_id', String(params.account_id))
if (params?.status) q.set('status', params.status)
if (params?.limit != null) q.set('limit', String(params.limit))
if (params?.offset != null) q.set('offset', String(params.offset))
return request<MessageList>(`/messages${q.toString() ? '?' + q : ''}`)
},
retry: (id: number) => request<Message>(`/messages/${id}/retry`, { method: 'POST' }),
retryBatch: (opts?: { limit?: number; account_id?: number }) => {
const q = new URLSearchParams()
if (opts?.limit != null) q.set('limit', String(opts.limit))
if (opts?.account_id != null) q.set('account_id', String(opts.account_id))
return request<RetryBatchResult>(`/messages/retry-batch${q.toString() ? '?' + q : ''}`, {
method: 'POST',
})
},
},
stats: {
get: () => request<Stats>('/stats'),
},
connection: {
test: (payload: TestPayload) =>
request<ConnectionTestResult>('/connection/test', {
method: 'POST',
body: JSON.stringify(payload),
}),
testAccount: (accountId: number) =>
request<ConnectionTestResult>(`/accounts/${accountId}/test`, { method: 'POST' }),
},
}