@kevisual/api 0.0.28 → 0.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/query/query-resources/index.ts +100 -12
- package/query/query-resources/utils.ts +42 -0
- package/query/utils/random.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.30",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "mod.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -27,7 +27,9 @@
|
|
|
27
27
|
"@kevisual/types": "^0.0.12",
|
|
28
28
|
"@kevisual/use-config": "^1.0.28",
|
|
29
29
|
"@types/bun": "^1.3.6",
|
|
30
|
+
"@types/crypto-js": "^4.2.2",
|
|
30
31
|
"@types/node": "^25.0.10",
|
|
32
|
+
"crypto-js": "^4.2.0",
|
|
31
33
|
"dotenv": "^17.2.3",
|
|
32
34
|
"fast-glob": "^3.3.3"
|
|
33
35
|
},
|
|
@@ -37,7 +39,8 @@
|
|
|
37
39
|
"es-toolkit": "^1.44.0",
|
|
38
40
|
"eventemitter3": "^5.0.4",
|
|
39
41
|
"fuse.js": "^7.1.0",
|
|
40
|
-
"nanoid": "^5.1.6"
|
|
42
|
+
"nanoid": "^5.1.6",
|
|
43
|
+
"path-browserify-esm": "^1.0.6"
|
|
41
44
|
},
|
|
42
45
|
"exports": {
|
|
43
46
|
".": "./mod.ts",
|
|
@@ -46,6 +49,7 @@
|
|
|
46
49
|
"./config": "./query/query-config/query-config.ts",
|
|
47
50
|
"./proxy": "./query/query-proxy/index.ts",
|
|
48
51
|
"./secret": "./query/query-secret/index.ts",
|
|
52
|
+
"./resources": "./query/query-resources/index.ts",
|
|
49
53
|
"./query/*": "./query/*"
|
|
50
54
|
}
|
|
51
55
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { adapter, DataOpts, Result } from '@kevisual/query';
|
|
2
|
+
import path from 'path-browserify-esm';
|
|
3
|
+
import { hashContent } from './utils';
|
|
2
4
|
|
|
3
5
|
type QueryResourcesOptions = {
|
|
4
6
|
prefix?: string;
|
|
@@ -20,6 +22,9 @@ export class QueryResources {
|
|
|
20
22
|
setUsername(username: string) {
|
|
21
23
|
this.prefix = `/${username}/resources/`;
|
|
22
24
|
}
|
|
25
|
+
setPrefix(prefix: string) {
|
|
26
|
+
this.prefix = prefix;
|
|
27
|
+
}
|
|
23
28
|
header(headers?: Record<string, string>, json = true): Record<string, string> {
|
|
24
29
|
const token = this.storage.getItem('token');
|
|
25
30
|
const _headers: Record<string, string> = {
|
|
@@ -54,18 +59,101 @@ export class QueryResources {
|
|
|
54
59
|
});
|
|
55
60
|
}
|
|
56
61
|
async fetchFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
const url = `${this.prefix}${filepath}`;
|
|
63
|
+
return this.get({}, { url, method: 'GET', headers: this.header(opts?.headers, false), isText: true });
|
|
64
|
+
}
|
|
65
|
+
async uploadFile(filepath: string, content: string, opts?: DataOpts): Promise<Result<any>> {
|
|
66
|
+
const pathname = `${this.prefix}${filepath}`;
|
|
67
|
+
const filename = path.basename(pathname);
|
|
68
|
+
const type = getContentType(filename);
|
|
69
|
+
const url = new URL(pathname, window.location.origin);
|
|
70
|
+
const hash = hashContent(content);
|
|
71
|
+
url.searchParams.set('hash', hash);
|
|
72
|
+
const formData = new FormData();
|
|
73
|
+
formData.append('file', new Blob([content], { type }));
|
|
74
|
+
return adapter({
|
|
75
|
+
url: url.toString(),
|
|
76
|
+
headers: { ...this.header(opts?.headers, false) },
|
|
77
|
+
isPostFile: true,
|
|
78
|
+
method: 'POST',
|
|
79
|
+
body: formData,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async deleteFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
|
83
|
+
const url = `${this.prefix}${filepath}`;
|
|
84
|
+
return adapter({
|
|
85
|
+
url,
|
|
86
|
+
method: 'DELETE' as any,
|
|
87
|
+
headers: this.header(opts?.headers),
|
|
69
88
|
});
|
|
70
89
|
}
|
|
71
90
|
}
|
|
91
|
+
|
|
92
|
+
export const getContentType = (filename: string): string => {
|
|
93
|
+
const ext = path.extname(filename);
|
|
94
|
+
let type = 'text/plain';
|
|
95
|
+
|
|
96
|
+
switch (ext) {
|
|
97
|
+
case '':
|
|
98
|
+
type = 'application/octet-stream';
|
|
99
|
+
break;
|
|
100
|
+
case '.json':
|
|
101
|
+
type = 'application/json';
|
|
102
|
+
break;
|
|
103
|
+
case '.txt':
|
|
104
|
+
type = 'text/plain';
|
|
105
|
+
break;
|
|
106
|
+
case '.csv':
|
|
107
|
+
type = 'text/csv';
|
|
108
|
+
break;
|
|
109
|
+
case '.md':
|
|
110
|
+
type = 'text/markdown';
|
|
111
|
+
break;
|
|
112
|
+
case '.html':
|
|
113
|
+
case '.htm':
|
|
114
|
+
type = 'text/html';
|
|
115
|
+
break;
|
|
116
|
+
case '.xml':
|
|
117
|
+
type = 'application/xml';
|
|
118
|
+
break;
|
|
119
|
+
case '.js':
|
|
120
|
+
type = 'application/javascript';
|
|
121
|
+
break;
|
|
122
|
+
case '.css':
|
|
123
|
+
type = 'text/css';
|
|
124
|
+
break;
|
|
125
|
+
case '.ts':
|
|
126
|
+
type = 'application/typescript';
|
|
127
|
+
break;
|
|
128
|
+
case '.pdf':
|
|
129
|
+
type = 'application/pdf';
|
|
130
|
+
break;
|
|
131
|
+
case '.zip':
|
|
132
|
+
type = 'application/zip';
|
|
133
|
+
break;
|
|
134
|
+
case '.docx':
|
|
135
|
+
type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
136
|
+
break;
|
|
137
|
+
case '.xlsx':
|
|
138
|
+
type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
139
|
+
break;
|
|
140
|
+
case '.mp3':
|
|
141
|
+
type = 'audio/mpeg';
|
|
142
|
+
break;
|
|
143
|
+
case '.mp4':
|
|
144
|
+
type = 'video/mp4';
|
|
145
|
+
break;
|
|
146
|
+
case '.png':
|
|
147
|
+
case '.jpg':
|
|
148
|
+
case '.jpeg':
|
|
149
|
+
case '.gif':
|
|
150
|
+
case '.webp':
|
|
151
|
+
type = `image/${ext.slice(1)}`;
|
|
152
|
+
break;
|
|
153
|
+
case '.svg':
|
|
154
|
+
type = 'image/svg+xml';
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return type;
|
|
159
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import MD5 from 'crypto-js/md5';
|
|
2
|
+
|
|
3
|
+
export const hashContent = (str: string | Buffer): string => {
|
|
4
|
+
if (typeof str === 'string') {
|
|
5
|
+
return MD5(str).toString();
|
|
6
|
+
} else if (Buffer.isBuffer(str)) {
|
|
7
|
+
return MD5(str.toString()).toString();
|
|
8
|
+
}
|
|
9
|
+
console.error('hashContent error: input must be a string or Buffer');
|
|
10
|
+
return '';
|
|
11
|
+
};
|
|
12
|
+
export const hashFile = (file: File): Promise<string> => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const reader = new FileReader();
|
|
15
|
+
|
|
16
|
+
reader.onload = async (event) => {
|
|
17
|
+
try {
|
|
18
|
+
const content = event.target?.result;
|
|
19
|
+
if (content instanceof ArrayBuffer) {
|
|
20
|
+
const contentString = new TextDecoder().decode(content);
|
|
21
|
+
const hashHex = MD5(contentString).toString();
|
|
22
|
+
resolve(hashHex);
|
|
23
|
+
} else if (typeof content === 'string') {
|
|
24
|
+
const hashHex = MD5(content).toString();
|
|
25
|
+
resolve(hashHex);
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error('Invalid content type');
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('hashFile error', error);
|
|
31
|
+
reject(error);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
reader.onerror = (error) => {
|
|
36
|
+
reject(error);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// 读取文件为 ArrayBuffer
|
|
40
|
+
reader.readAsArrayBuffer(file);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { customAlphabet } from 'nanoid';
|
|
2
|
+
|
|
3
|
+
export const letter = 'abcdefghijklmnopqrstuvwxyz';
|
|
4
|
+
export const number = '0123456789';
|
|
5
|
+
const alphanumeric = `${letter}${number}`;
|
|
6
|
+
export const alphanumericWithDash = `${alphanumeric}-`;
|
|
7
|
+
export const uuid = customAlphabet(letter);
|
|
8
|
+
|
|
9
|
+
export const nanoid = customAlphabet(alphanumeric, 10);
|
|
10
|
+
|
|
11
|
+
export const nanoidWithDash = customAlphabet(alphanumericWithDash, 10);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 创建一个随机的 id,以字母开头的字符串
|
|
15
|
+
* @param number
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export const randomId = (number: number) => {
|
|
19
|
+
const _letter = uuid(1);
|
|
20
|
+
return `${_letter}${nanoid(number)}`;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const randomLetter = (number: number = 8, opts?: { before?: string; after?: string }) => {
|
|
24
|
+
const { before = '', after = '' } = opts || {};
|
|
25
|
+
return `${before}${uuid(number)}${after}`;
|
|
26
|
+
};
|