refactor(modules): 拆分多模块工程并收口common基础模块
This commit is contained in:
30
frontend/src/api/__tests__/attachments.spec.ts
Normal file
30
frontend/src/api/__tests__/attachments.spec.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { uploadAttachment } from '../attachments';
|
||||
import { post } from '../request';
|
||||
|
||||
vi.mock('../request', () => ({
|
||||
post: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('attachments api', () => {
|
||||
it('uploads attachment with multipart form data', () => {
|
||||
const file = new File(['hello'], 'note.txt', { type: 'text/plain' });
|
||||
uploadAttachment({
|
||||
file,
|
||||
sourceType: 'RAG',
|
||||
sourceId: '1001',
|
||||
});
|
||||
|
||||
const [url, body] = vi.mocked(post).mock.calls[0];
|
||||
expect(url).toBe('/attachments/upload');
|
||||
expect(body).toBeInstanceOf(FormData);
|
||||
|
||||
const entries = Array.from((body as FormData).entries());
|
||||
expect(entries).toEqual([
|
||||
['file', file],
|
||||
['sourceType', 'RAG'],
|
||||
['sourceId', '1001'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
31
frontend/src/api/attachments.ts
Normal file
31
frontend/src/api/attachments.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { post } from './request';
|
||||
|
||||
export interface AttachmentUploadRequest {
|
||||
file: File;
|
||||
sourceType: string;
|
||||
sourceId?: string;
|
||||
}
|
||||
|
||||
export interface AttachmentResponse {
|
||||
id: string;
|
||||
sourceType: string;
|
||||
sourceId?: string | null;
|
||||
originalName: string;
|
||||
fileName: string;
|
||||
fileSuffix?: string | null;
|
||||
contentType?: string | null;
|
||||
fileSize: number;
|
||||
storageType: string;
|
||||
fileUrl?: string | null;
|
||||
remark?: string | null;
|
||||
}
|
||||
|
||||
export function uploadAttachment(data: AttachmentUploadRequest) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', data.file);
|
||||
formData.append('sourceType', data.sourceType);
|
||||
if (data.sourceId) {
|
||||
formData.append('sourceId', data.sourceId);
|
||||
}
|
||||
return post<AttachmentResponse, FormData>('/attachments/upload', formData);
|
||||
}
|
||||
Reference in New Issue
Block a user