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