feat: 更新版本至 0.0.64,添加 getUrl 方法以简化 URL 处理
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/api",
|
||||
"version": "0.0.63",
|
||||
"version": "0.0.64",
|
||||
"description": "",
|
||||
"main": "mod.ts",
|
||||
"scripts": {
|
||||
|
||||
@@ -59,19 +59,25 @@ export class QueryResources {
|
||||
headers: this.header(opts?.headers),
|
||||
});
|
||||
}
|
||||
getUrl(prefix: string): string {
|
||||
if (prefix.startsWith('http')) {
|
||||
return prefix;
|
||||
}
|
||||
return `${this.prefix}${prefix}`;
|
||||
}
|
||||
async getList(prefix: string, data?: { recursive?: boolean }, opts?: DataOpts): Promise<Result<any[]>> {
|
||||
return this.get(data, {
|
||||
url: `${this.prefix}${prefix}`,
|
||||
url: this.getUrl(prefix),
|
||||
body: data,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
async fetchFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
||||
const url = `${this.prefix}${filepath}`;
|
||||
const url = this.getUrl(filepath);
|
||||
return this.get({}, { url, method: 'GET', ...opts, headers: this.header(opts?.headers, false), isText: true });
|
||||
}
|
||||
async uploadFile(filepath: string, content: string | Blob, opts?: DataOpts & { chunkSize?: number, maxSize?: number }): Promise<Result<any>> {
|
||||
const pathname = `${this.prefix}${filepath}`;
|
||||
const pathname = this.getUrl(filepath);
|
||||
const filename = path.basename(pathname);
|
||||
const type = getContentType(filename);
|
||||
const url = new URL(pathname, window.location.origin);
|
||||
@@ -114,12 +120,12 @@ export class QueryResources {
|
||||
return res;
|
||||
}
|
||||
async uploadChunkedFile(filepath: string, file: Blob, hash: string, opts?: DataOpts & { chunkSize?: number }): Promise<Result<any>> {
|
||||
const pathname = `${this.prefix}${filepath}`;
|
||||
const pathname = this.getUrl(filepath);
|
||||
const filename = path.basename(pathname);
|
||||
const url = new URL(pathname, window.location.origin);
|
||||
url.searchParams.set('hash', hash);
|
||||
url.searchParams.set('chunk', '1');
|
||||
console.log(`url,`, url, hash);
|
||||
// console.log(`url,`, url, hash);
|
||||
// 预留 eventSource 支持(暂不处理)
|
||||
// const createEventSource = opts?.createEventSource;
|
||||
const { chunkSize: _chunkSize, ...restOpts } = opts || {};
|
||||
@@ -183,7 +189,7 @@ export class QueryResources {
|
||||
}
|
||||
|
||||
async getStat(filepath: string, opts?: DataOpts): Promise<Result<Stat>> {
|
||||
const url = `${this.prefix}${filepath}`;
|
||||
const url = this.getUrl(filepath);
|
||||
return adapter({
|
||||
url,
|
||||
params: {
|
||||
@@ -207,8 +213,8 @@ export class QueryResources {
|
||||
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
|
||||
}
|
||||
async rename(oldpath: string, newpath: string, opts?: DataOpts): Promise<Result<any>> {
|
||||
const pathname = `${this.prefix}${oldpath}`;
|
||||
const newName = `${this.prefix}${newpath}`;
|
||||
const pathname = this.getUrl(oldpath);
|
||||
const newName = this.getUrl(newpath);
|
||||
const params = {
|
||||
newName: newName,
|
||||
};
|
||||
@@ -221,7 +227,7 @@ export class QueryResources {
|
||||
});
|
||||
}
|
||||
async deleteFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
||||
const url = `${this.prefix}${filepath}`;
|
||||
const url = this.getUrl(filepath);
|
||||
return adapter({
|
||||
url,
|
||||
method: 'DELETE' as any,
|
||||
|
||||
Reference in New Issue
Block a user