# All files / src/api documents.ts

0% Statements 0/28
0% Branches 0/18
0% Functions 0/7
0% Lines 0/21

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
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34

     import { api } from './client';
    import type { DocumentItem } from './types';

    export const documentApi = {
      getList: (params?: { page?: number; pageSize?: number; status?: string }) => {
        const qs = new URLSearchParams();
        if (params?.page) qs.set('page', String(params.page));
        if (params?.pageSize) qs.set('pageSize', String(params.pageSize));
        if (params?.status) qs.set('status', params.status);
        const query = qs.toString();
        return api.get<{ list: DocumentItem[]; total: number; page: number; pageSize: number }>(`/user/documents${query ? `?${query}` : ''}`);
      },
      getDetail: (docId: number) => api.get<DocumentItem>(`/user/documents/${docId}`),
      delete: (docId: number) => api.delete<null>(`/user/documents/${docId}`),
      cancel: (docId: number) => api.post<null>(`/user/documents/${docId}/cancel`),
      retry: (docId: number) => api.post<null>(`/user/documents/${docId}/retry`),
      upload: (file: File, params?: { sourceLang?: string; targetLang?: string; mode?: string }) => {
        const formData = new FormData();
        formData.append('file', file);
        if (params?.sourceLang) formData.append('sourceLang', params.sourceLang);
        if (params?.targetLang) formData.append('targetLang', params.targetLang);
        if (params?.mode) formData.append('mode', params.mode);
        return api.upload<DocumentItem>('/user/documents/upload', formData);
      },
      download: async (docId: number) => {
        const token = localStorage.getItem('authToken');
        const res = await fetch(`/api/user/documents/${docId}/download`, {
          headers: token ? { Authorization: `Bearer ${token}` } : {},
        });
        if (!res.ok) throw new Error(`Download failed: ${res.status}`);
        return res.blob();
      },
    };