* chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers Remove 33 dead files, ~270 unreferenced exports/types, 13 dead i18n namespaces and 4 unused dependencies; fold byte-identical helper copies into one canonical home each (lib/utils chunk/sleep/utcDateStamp, lib/dates/iso, lib/invariants/uuid, lib/xml/escape, lib/reports/sru/format, lib/pdf/number-text, lib/browser/panel-request, lib/api/v1/body + v1ValidationError rolled out to ~55 v1 routes, booking-template schemas). No behaviour change: v1 bodies and status codes, MCP tool schemas, DB writes and money math are untouched. Naive ore rounding was deliberately not swapped for roundOre; see DECISIONS.md 2026-09-02 for the full list of things left alone on purpose. tsc, lint, 19588 unit tests and check:guards green; antipattern baseline ratcheted (naive-ore-round 622 -> 620, hand-rolled-invariant 115 -> 113). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(transactions): import RawTransaction from @/types after the ingest re-export removal CI's type ratchet (check:types, full tsconfig) caught the one test file that still imported the type through lib/transactions/ingest. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
155 lines
4.8 KiB
TypeScript
155 lines
4.8 KiB
TypeScript
/**
|
|
* HTTP client for the Arcim Sync gateway API.
|
|
*
|
|
* Targets the consent-based resource API (/api/v1/consents/...) which
|
|
* provides typed, normalized access to any Swedish accounting provider.
|
|
*/
|
|
|
|
import type {
|
|
ArcimProvider,
|
|
ConsentRecord,
|
|
PaginatedResponse,
|
|
CompanyInformationDto,
|
|
CustomerDto,
|
|
} from '../types'
|
|
|
|
function getBaseUrl(): string {
|
|
const url = process.env.ARCIM_SYNC_GATEWAY_URL
|
|
if (!url) throw new Error('ARCIM_SYNC_GATEWAY_URL is not configured')
|
|
return url.replace(/\/$/, '')
|
|
}
|
|
|
|
function getApiKey(): string {
|
|
const key = process.env.ARCIM_SYNC_API_KEY
|
|
if (!key) throw new Error('ARCIM_SYNC_API_KEY is not configured')
|
|
return key
|
|
}
|
|
|
|
const MAX_RETRIES = 2
|
|
const RETRY_DELAY_MS = 1_000
|
|
const RETRYABLE_STATUSES = [429, 502, 503, 504]
|
|
|
|
async function request<T>(
|
|
path: string,
|
|
options: RequestInit = {},
|
|
timeoutMs: number = 120_000
|
|
): Promise<T> {
|
|
const url = `${getBaseUrl()}${path}`
|
|
|
|
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
const controller = new AbortController()
|
|
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
|
|
let response: Response
|
|
try {
|
|
response = await fetch(url, {
|
|
...options,
|
|
signal: controller.signal,
|
|
headers: {
|
|
'Authorization': `Bearer ${getApiKey()}`,
|
|
'Content-Type': 'application/json',
|
|
...options.headers,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
const isAbort = err instanceof DOMException || (err instanceof Error && err.name === 'AbortError')
|
|
if (attempt < MAX_RETRIES && isAbort) {
|
|
console.warn(`[arcim] ${path} timed out, retrying (attempt ${attempt + 1})`)
|
|
await new Promise(r => setTimeout(r, RETRY_DELAY_MS * (attempt + 1)))
|
|
continue
|
|
}
|
|
if (isAbort) {
|
|
throw new Error(`Arcim API timeout after ${Math.round(timeoutMs / 1000)}s: ${path}`)
|
|
}
|
|
throw err
|
|
} finally {
|
|
clearTimeout(timer)
|
|
}
|
|
|
|
if (attempt < MAX_RETRIES && RETRYABLE_STATUSES.includes(response.status)) {
|
|
console.warn(`[arcim] ${path} returned ${response.status}, retrying (attempt ${attempt + 1})`)
|
|
await new Promise(r => setTimeout(r, RETRY_DELAY_MS * (attempt + 1)))
|
|
continue
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const body = await response.text().catch(() => '')
|
|
throw new Error(`Arcim API ${response.status}: ${body || response.statusText}`)
|
|
}
|
|
|
|
return response.json()
|
|
}
|
|
|
|
throw new Error(`Arcim API failed after ${MAX_RETRIES + 1} attempts: ${path}`)
|
|
}
|
|
|
|
// ── Consent lifecycle ───────────────────────────────────────────────
|
|
|
|
export async function createConsent(
|
|
provider: ArcimProvider,
|
|
name: string,
|
|
orgNumber?: string,
|
|
companyName?: string
|
|
): Promise<ConsentRecord> {
|
|
return request<ConsentRecord>('/api/v1/consents', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name, provider, orgNumber, companyName }),
|
|
})
|
|
}
|
|
|
|
export async function getConsent(consentId: string): Promise<ConsentRecord> {
|
|
return request<ConsentRecord>(`/api/v1/consents/${consentId}`)
|
|
}
|
|
|
|
// ── Resource fetching (paginated) ───────────────────────────────────
|
|
|
|
async function fetchAllPages<T>(
|
|
consentId: string,
|
|
resource: string,
|
|
params?: Record<string, string>,
|
|
pageSize: number = 100,
|
|
maxPages: number = 500
|
|
): Promise<T[]> {
|
|
const all: T[] = []
|
|
let page = 1
|
|
|
|
while (page <= maxPages) {
|
|
const query = new URLSearchParams({
|
|
page: String(page),
|
|
pageSize: String(pageSize),
|
|
...params,
|
|
})
|
|
const result = await request<PaginatedResponse<T>>(
|
|
`/api/v1/consents/${consentId}/${resource}?${query}`
|
|
)
|
|
all.push(...result.data)
|
|
|
|
if (!result.hasMore || result.data.length === 0) break
|
|
page++
|
|
}
|
|
|
|
return all
|
|
}
|
|
|
|
// ── Typed resource accessors ────────────────────────────────────────
|
|
|
|
export async function fetchCompanyInfo(
|
|
consentId: string
|
|
): Promise<CompanyInformationDto | null> {
|
|
// CompanyInformation is a singleton resource: gateway returns { data: object }
|
|
const result = await request<{ data: CompanyInformationDto }>(
|
|
`/api/v1/consents/${consentId}/companyinformation`
|
|
)
|
|
return result.data ?? null
|
|
}
|
|
|
|
export async function fetchCustomers(consentId: string): Promise<CustomerDto[]> {
|
|
return fetchAllPages<CustomerDto>(consentId, 'customers')
|
|
}
|
|
|
|
// The gateway SIE export path (fetchSIEExport/SIEExportFile) was deliberately
|
|
// removed: it returned SIE as a pre-decoded string, and the gateway's decode of
|
|
// CP437 bytes as windows-1252 caused the 2026-03-17 mojibake incident. Provider
|
|
// SIE now travels as raw bytes through lib/sie-fetcher.ts and the repo's own
|
|
// encoding detection. Do not re-add a string-typed SIE fetch here.
|