@kevisual/api 0.0.26 → 0.0.29

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/api",
3
- "version": "0.0.26",
3
+ "version": "0.0.29",
4
4
  "description": "",
5
5
  "main": "mod.ts",
6
6
  "scripts": {
@@ -23,11 +23,13 @@
23
23
  "devDependencies": {
24
24
  "@kevisual/cache": "^0.0.5",
25
25
  "@kevisual/query": "^0.0.38",
26
- "@kevisual/router": "^0.0.60",
27
- "@kevisual/types": "^0.0.11",
26
+ "@kevisual/router": "^0.0.62",
27
+ "@kevisual/types": "^0.0.12",
28
28
  "@kevisual/use-config": "^1.0.28",
29
29
  "@types/bun": "^1.3.6",
30
- "@types/node": "^25.0.9",
30
+ "@types/crypto-js": "^4.2.2",
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
  },
@@ -36,7 +38,9 @@
36
38
  "@kevisual/load": "^0.0.6",
37
39
  "es-toolkit": "^1.44.0",
38
40
  "eventemitter3": "^5.0.4",
39
- "nanoid": "^5.1.6"
41
+ "fuse.js": "^7.1.0",
42
+ "nanoid": "^5.1.6",
43
+ "path-browserify-esm": "^1.0.6"
40
44
  },
41
45
  "exports": {
42
46
  ".": "./mod.ts",
@@ -44,6 +48,8 @@
44
48
  "./login-node": "./query/query-login/query-login-node.ts",
45
49
  "./config": "./query/query-config/query-config.ts",
46
50
  "./proxy": "./query/query-proxy/index.ts",
47
- "./query/**/*.ts": "./query/**/*.ts"
51
+ "./secret": "./query/query-secret/index.ts",
52
+ "./resources": "./query/query-resources/index.ts",
53
+ "./query/*": "./query/*"
48
54
  }
49
55
  }
@@ -3,6 +3,7 @@ import { QueryRouterServer, App, Route } from '@kevisual/router';
3
3
  import { filter } from '@kevisual/js-filter'
4
4
  import { EventEmitter } from 'eventemitter3';
5
5
  import { initApi } from './router-api-proxy.ts';
6
+ import Fuse from 'fuse.js';
6
7
 
7
8
  export const RouteTypeList = ['api', 'context', 'worker', 'page'] as const;
8
9
  export type RouterViewItemInfo = RouterViewApi | RouterViewContext | RouterViewWorker | RouteViewPage;
@@ -395,7 +396,17 @@ export class QueryProxy {
395
396
  }
396
397
  const routes = this.router.routes.filter(filterFn || (() => true));
397
398
  if (query) {
398
- return filter(routes, query);
399
+ if (query.toLocaleUpperCase().startsWith('WHERE')) {
400
+ return filter(routes, query);
401
+ } else {
402
+ const fuse = new Fuse(routes, {
403
+ keys: ['path', 'key', 'description'],
404
+ threshold: 0.4,
405
+ });
406
+ let findsRoutes = fuse.search(query);
407
+ const resultRoutes = findsRoutes.map(r => r.item);
408
+ return resultRoutes;
409
+ }
399
410
  }
400
411
  return routes;
401
412
  }
@@ -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,93 @@ export class QueryResources {
54
59
  });
55
60
  }
56
61
  async fetchFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
57
- return fetch(`${this.prefix}${filepath}`, {
58
- method: 'GET',
59
- headers: this.header(opts?.headers, false),
60
- }).then(async (res) => {
61
- if (!res.ok) {
62
- return {
63
- code: 500,
64
- success: false,
65
- message: `Failed to fetch file: ${res.status} ${res.statusText}`,
66
- } as Result<any>;
67
- }
68
- return { code: 200, data: await res.text(), success: true } as Result<any>;
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,
69
80
  });
70
81
  }
71
82
  }
83
+
84
+ export const getContentType = (filename: string): string => {
85
+ const ext = path.extname(filename);
86
+ let type = 'text/plain';
87
+
88
+ switch (ext) {
89
+ case '':
90
+ type = 'application/octet-stream';
91
+ break;
92
+ case '.json':
93
+ type = 'application/json';
94
+ break;
95
+ case '.txt':
96
+ type = 'text/plain';
97
+ break;
98
+ case '.csv':
99
+ type = 'text/csv';
100
+ break;
101
+ case '.md':
102
+ type = 'text/markdown';
103
+ break;
104
+ case '.html':
105
+ case '.htm':
106
+ type = 'text/html';
107
+ break;
108
+ case '.xml':
109
+ type = 'application/xml';
110
+ break;
111
+ case '.js':
112
+ type = 'application/javascript';
113
+ break;
114
+ case '.css':
115
+ type = 'text/css';
116
+ break;
117
+ case '.ts':
118
+ type = 'application/typescript';
119
+ break;
120
+ case '.pdf':
121
+ type = 'application/pdf';
122
+ break;
123
+ case '.zip':
124
+ type = 'application/zip';
125
+ break;
126
+ case '.docx':
127
+ type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
128
+ break;
129
+ case '.xlsx':
130
+ type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
131
+ break;
132
+ case '.mp3':
133
+ type = 'audio/mpeg';
134
+ break;
135
+ case '.mp4':
136
+ type = 'video/mp4';
137
+ break;
138
+ case '.png':
139
+ case '.jpg':
140
+ case '.jpeg':
141
+ case '.gif':
142
+ case '.webp':
143
+ type = `image/${ext.slice(1)}`;
144
+ break;
145
+ case '.svg':
146
+ type = 'image/svg+xml';
147
+ break;
148
+ }
149
+
150
+ return type;
151
+ };
@@ -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 @@
1
+ export * from './query-secret.ts'