-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (60 loc) · 1.95 KB
/
Copy pathindex.ts
File metadata and controls
64 lines (60 loc) · 1.95 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Client } from '../client';
import {
fileListEndpoint,
fileDeleteEndpoint,
fileRetrieveEndpoint,
} from '../../client/endpoints';
import { uploadFile } from '../../files/upload';
import type {
FileUploadResponse,
FileListResponse,
FileDeleteResponse,
FileRetrieveResponse,
} from '../../types/api';
import { SDKError } from '../../errors/base';
import { ExitCode } from '../../errors/codes';
export class FileSDK extends Client {
/**
* Upload a file to MiniMax storage.
*
* @param filePath - Absolute or relative path to the file on disk.
* @param purpose - File purpose, defaults to `"retrieval"`.
*/
async upload(filePath: string, purpose = 'retrieval'): Promise<FileUploadResponse> {
return uploadFile({
filePath,
purpose,
baseUrl: this.config.baseUrl,
requestJson: (opts) => this.requestJson<FileUploadResponse>(opts),
createFileNotFoundError: (fullPath) =>
new SDKError(`File not found: ${fullPath}`, ExitCode.USAGE),
});
}
/** List all files in MiniMax storage. */
async list(): Promise<FileListResponse> {
const url = fileListEndpoint(this.config.baseUrl);
return this.requestJson<FileListResponse>({ url, method: 'GET' });
}
/**
* Delete a file from MiniMax storage by its file ID.
*
* @param fileId - The ID of the file to delete (string or number).
*/
async delete(fileId: string | number): Promise<FileDeleteResponse> {
const url = fileDeleteEndpoint(this.config.baseUrl);
return this.requestJson<FileDeleteResponse>({
url,
method: 'POST',
body: { file_id: Number(fileId) },
});
}
/**
* Retrieve metadata (and optional download URL) for a file.
*
* @param fileId - The ID of the file to retrieve.
*/
async retrieve(fileId: string): Promise<FileRetrieveResponse> {
const url = fileRetrieveEndpoint(this.config.baseUrl, fileId);
return this.requestJson<FileRetrieveResponse>({ url, method: 'GET' });
}
}