@kevisual/noco
Version:
一个轻量级的 NocoDB API SDK,支持表记录操作和 Base 管理功能。
79 lines (74 loc) • 1.49 kB
text/typescript
import { Query } from '../api.ts';
export class Upload {
private query: Query;
constructor(query: Query) {
this.query = query;
}
/**
* 创建上传, 上传后,自动返回的数据是列表
* @param opts
* @returns
*/
public async createUpload(opts: UploadOpts): Promise<{
code: number;
list: UploadItem[];
}> {
const formData = new FormData();
const { path, mimeType, file, size, title, url } = opts;
if (url) {
formData.append('url', url);
}
if (file) {
formData.append('file', file, title);
}
if (path) {
formData.append('path', path);
}
if (mimeType) {
formData.append('mimetype', mimeType);
}
if (size) {
formData.append('size', size.toString());
}
if (title) {
formData.append('title', title);
}
return await this.query.makeRequest('/api/v2/storage/upload', {
method: 'POST',
body: formData,
isFromData: true,
});
}
}
type UploadOpts = {
path?: string;
mimeType?: string;
file: any;
size?: number;
title: string;
url?: string; // 可选,互联网资源链接
}
type UploadItem = {
/**
* nocodb 资源路径
*/
path: string;
/**
* 文件标题
*/
title: string;
/**
* 文件大小,单位字节
*/
size: number;
/**
* 文件 MIME 类型
*/
mimetype: string;
width: number;
height: number;
/**
* 签名后的完整访问路径
*/
signedPath: string;
}