|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import { resolve, basename } from 'node:path'; |
| 4 | +import { Client } from '../client'; |
| 5 | +import { |
| 6 | + fileUploadEndpoint, |
| 7 | + fileListEndpoint, |
| 8 | + fileDeleteEndpoint, |
| 9 | + fileRetrieveEndpoint, |
| 10 | +} from '../../client/endpoints'; |
| 11 | +import type { |
| 12 | + FileUploadResponse, |
| 13 | + FileListResponse, |
| 14 | + FileDeleteResponse, |
| 15 | + FileRetrieveResponse, |
| 16 | +} from '../../types/api'; |
| 17 | +import { SDKError } from '../../errors/base'; |
| 18 | +import { ExitCode } from '../../errors/codes'; |
| 19 | + |
| 20 | +export class FileSDK extends Client { |
| 21 | + /** |
| 22 | + * Upload a file to MiniMax storage. |
| 23 | + * |
| 24 | + * @param filePath - Absolute or relative path to the file on disk. |
| 25 | + * @param purpose - File purpose, defaults to `"retrieval"`. |
| 26 | + */ |
| 27 | + async upload(filePath: string, purpose = 'retrieval'): Promise<FileUploadResponse> { |
| 28 | + const fullPath = resolve(filePath); |
| 29 | + if (!existsSync(fullPath)) { |
| 30 | + throw new SDKError(`File not found: ${fullPath}`, ExitCode.USAGE); |
| 31 | + } |
| 32 | + |
| 33 | + const fileData = await readFile(fullPath); |
| 34 | + const fileName = basename(fullPath); |
| 35 | + |
| 36 | + const formData = new FormData(); |
| 37 | + formData.append('file', new Blob([fileData]), fileName); |
| 38 | + formData.append('purpose', purpose); |
| 39 | + |
| 40 | + const url = fileUploadEndpoint(this.config.baseUrl); |
| 41 | + return this.requestJson<FileUploadResponse>({ |
| 42 | + url, |
| 43 | + method: 'POST', |
| 44 | + body: formData, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + /** List all files in MiniMax storage. */ |
| 49 | + async list(): Promise<FileListResponse> { |
| 50 | + const url = fileListEndpoint(this.config.baseUrl); |
| 51 | + return this.requestJson<FileListResponse>({ url, method: 'GET' }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Delete a file from MiniMax storage by its file ID. |
| 56 | + * |
| 57 | + * @param fileId - The ID of the file to delete (string or number). |
| 58 | + */ |
| 59 | + async delete(fileId: string | number): Promise<FileDeleteResponse> { |
| 60 | + const url = fileDeleteEndpoint(this.config.baseUrl); |
| 61 | + return this.requestJson<FileDeleteResponse>({ |
| 62 | + url, |
| 63 | + method: 'POST', |
| 64 | + body: { file_id: Number(fileId) }, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Retrieve metadata (and optional download URL) for a file. |
| 70 | + * |
| 71 | + * @param fileId - The ID of the file to retrieve. |
| 72 | + */ |
| 73 | + async retrieve(fileId: string): Promise<FileRetrieveResponse> { |
| 74 | + const url = fileRetrieveEndpoint(this.config.baseUrl, fileId); |
| 75 | + return this.requestJson<FileRetrieveResponse>({ url, method: 'GET' }); |
| 76 | + } |
| 77 | +} |
0 commit comments