90 lines
3.4 KiB
TypeScript
90 lines
3.4 KiB
TypeScript
import type { EmployerResponse, PersonResponse, Guid, AttestSummary } from './types'
|
|
|
|
async function apiGet<T>(path: string, signal?: AbortSignal): Promise<T> {
|
|
const res = await fetch(path, { headers: { 'Accept': 'application/json' }, signal })
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => '')
|
|
throw new Error(`API GET ${path} failed: ${res.status} ${res.statusText} ${text}`)
|
|
}
|
|
return res.json() as Promise<T>
|
|
}
|
|
|
|
export const Api = {
|
|
getPerson: (id: Guid, signal?: AbortSignal) => apiGet<PersonResponse>(`/api/v1/persons/${id}`, signal),
|
|
listPersonAttests: (id: Guid, signal?: AbortSignal) => apiGet<AttestSummary[]>(`/api/v1/persons/${id}/attests`, signal),
|
|
getEmployer: (id: Guid, signal?: AbortSignal) => apiGet<EmployerResponse>(`/api/v1/employers/${id}`, signal),
|
|
listEmployerAttests: (id: Guid, signal?: AbortSignal) => apiGet<AttestSummary[]>(`/api/v1/employers/${id}/attests`, signal),
|
|
personAttestDownloadUrl: (personId: Guid, attestId: Guid) => `/api/v1/persons/${personId}/attests/${attestId}/download`,
|
|
employerAttestDownloadUrl: (employerId: Guid, attestId: Guid) => `/api/v1/employers/${employerId}/attests/${attestId}/download`,
|
|
personAttestPreviewUrl: (personId: Guid, attestId: Guid) => `/api/v1/persons/${personId}/attests/${attestId}/download?inline=true`,
|
|
employerAttestPreviewUrl: (employerId: Guid, attestId: Guid) => `/api/v1/employers/${employerId}/attests/${attestId}/download?inline=true`,
|
|
uploadPersonAttest: async (
|
|
personId: Guid,
|
|
req: {
|
|
title: string
|
|
from: string // YYYY-MM-DD
|
|
to: string // YYYY-MM-DD
|
|
summary?: string | null
|
|
contentBase64: string
|
|
contentType: string
|
|
}
|
|
): Promise<{ attestId: Guid }> => {
|
|
const body = {
|
|
title: req.title,
|
|
from: req.from,
|
|
to: req.to,
|
|
summary: req.summary ?? null,
|
|
blobPath: '',
|
|
blobHash: null,
|
|
contentBase64: req.contentBase64,
|
|
contentType: req.contentType,
|
|
}
|
|
const path = `/api/v1/persons/${personId}/attests`
|
|
const res = await fetch(path, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => '')
|
|
throw new Error(`API POST ${path} failed: ${res.status} ${res.statusText} ${text}`)
|
|
}
|
|
return res.json()
|
|
},
|
|
uploadEmployerAttest: async (
|
|
employerId: Guid,
|
|
req: {
|
|
personId: Guid
|
|
title: string
|
|
from: string // YYYY-MM-DD
|
|
to: string // YYYY-MM-DD
|
|
summary?: string | null
|
|
contentBase64: string
|
|
contentType: string
|
|
}
|
|
): Promise<{ attestId: Guid }> => {
|
|
const body = {
|
|
personId: req.personId,
|
|
title: req.title,
|
|
from: req.from,
|
|
to: req.to,
|
|
summary: req.summary ?? null,
|
|
blobPath: '',
|
|
blobHash: null,
|
|
contentBase64: req.contentBase64,
|
|
contentType: req.contentType,
|
|
}
|
|
const path = `/api/v1/employers/${employerId}/attests`
|
|
const res = await fetch(path, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => '')
|
|
throw new Error(`API POST ${path} failed: ${res.status} ${res.statusText} ${text}`)
|
|
}
|
|
return res.json()
|
|
},
|
|
}
|