import type { HttpClient } from '@services' const downloadCheckPdf = (http: HttpClient) => { return { query: async (input: { checkId: string }): Promise<[Blob, string]> => { if (typeof http.raw !== 'function') { throw new Error('HttpClient does not implement the .raw method') } const response = await http.raw( `v3/user/checks/${input.checkId}.pdf`, { responseType: 'blob', }, ) const { headers, _data: blob } = response if (!blob) throw new Error('noContent') const contentDisposition = headers.get('content-disposition')?.toString() const [disposition, rest] = contentDisposition?.split('filename=') ?? [] if (disposition?.startsWith('attachment')) { const [filename] = rest.split(';').map((str) => str.replace(/"/g, '')) return [blob, decodeURIComponent(filename)] } throw new Error('noAttachment') }, } } export default downloadCheckPdf