All files / src/services export.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21    1x   7x         7x 7x   7x 7x 7x 7x 7x 7x      
import api from './api'
 
export const exportService = {
  async download(listId: string, format: 'csv' | 'pdf', listName: string) {
    const response = await api.get(`/lists/${listId}/export`, {
      params: { format },
      responseType: 'blob',
    })
 
    const ext = format === 'pdf' ? 'pdf' : 'csv'
    const filename = `${listName.replace(/[^a-z0-9äöüß ]/gi, '').trim() || 'liste'}.${ext}`
 
    const url = URL.createObjectURL(response.data as Blob)
    const a = document.createElement('a')
    a.href = url
    a.download = filename
    a.click()
    URL.revokeObjectURL(url)
  },
}