@kevisual/api 0.0.42 → 0.0.43

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.
@@ -0,0 +1,447 @@
1
+ // node_modules/.pnpm/@kevisual+query@0.0.39/node_modules/@kevisual/query/dist/query-browser.js
2
+ var isTextForContentType = (contentType) => {
3
+ if (!contentType)
4
+ return false;
5
+ const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
6
+ return textTypes.some((type) => contentType.includes(type));
7
+ };
8
+ var adapter = async (opts = {}, overloadOpts) => {
9
+ const controller = new AbortController;
10
+ const signal = controller.signal;
11
+ const isPostFile = opts.isPostFile || false;
12
+ let responseType = opts.responseType || "json";
13
+ if (opts.isBlob) {
14
+ responseType = "blob";
15
+ } else if (opts.isText) {
16
+ responseType = "text";
17
+ }
18
+ const timeout = opts.timeout || 60000 * 3;
19
+ const timer = setTimeout(() => {
20
+ controller.abort();
21
+ }, timeout);
22
+ let method = overloadOpts?.method || opts?.method || "POST";
23
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
24
+ let origin = "";
25
+ let url;
26
+ if (opts?.url?.startsWith("http")) {
27
+ url = new URL(opts.url);
28
+ } else {
29
+ origin = window?.location?.origin || "http://localhost:51515";
30
+ url = new URL(opts.url, origin);
31
+ }
32
+ const isGet = method === "GET";
33
+ const oldSearchParams = url.searchParams;
34
+ if (isGet) {
35
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts.body });
36
+ url.search = searchParams.toString();
37
+ } else {
38
+ const params = {
39
+ ...Object.fromEntries(oldSearchParams),
40
+ ...opts.params
41
+ };
42
+ const searchParams = new URLSearchParams(params);
43
+ if (typeof opts.body === "object" && opts.body !== null) {
44
+ let body2 = opts.body || {};
45
+ if (!params.path && body2?.path) {
46
+ searchParams.set("path", body2.path);
47
+ if (body2?.key) {
48
+ searchParams.set("key", body2.key);
49
+ }
50
+ }
51
+ }
52
+ url.search = searchParams.toString();
53
+ }
54
+ let body = undefined;
55
+ if (isGet) {
56
+ body = undefined;
57
+ } else if (isPostFile) {
58
+ body = opts.body;
59
+ } else {
60
+ headers = {
61
+ "Content-Type": "application/json",
62
+ ...headers
63
+ };
64
+ body = JSON.stringify(opts.body);
65
+ }
66
+ return fetch(url, {
67
+ method: method.toUpperCase(),
68
+ signal,
69
+ body,
70
+ ...overloadOpts,
71
+ headers
72
+ }).then(async (response) => {
73
+ const contentType = response.headers.get("Content-Type");
74
+ if (responseType === "blob") {
75
+ return await response.blob();
76
+ }
77
+ const isText = responseType === "text";
78
+ const isJson = contentType && contentType.includes("application/json");
79
+ if (isJson && !isText) {
80
+ return await response.json();
81
+ } else if (isTextForContentType(contentType)) {
82
+ return {
83
+ code: response.status,
84
+ status: response.status,
85
+ data: await response.text()
86
+ };
87
+ } else {
88
+ return response;
89
+ }
90
+ }).catch((err) => {
91
+ if (err.name === "AbortError") {
92
+ return {
93
+ code: 408,
94
+ message: "请求超时"
95
+ };
96
+ }
97
+ return {
98
+ code: 500,
99
+ message: err.message || "网络错误"
100
+ };
101
+ }).finally(() => {
102
+ clearTimeout(timer);
103
+ });
104
+ };
105
+ var wrapperError = ({ code, message }) => {
106
+ const result = {
107
+ code: code || 500,
108
+ success: false,
109
+ message: message || "api request error",
110
+ showError: (fn) => {},
111
+ noMsg: true
112
+ };
113
+ return result;
114
+ };
115
+
116
+ class Query {
117
+ adapter;
118
+ url;
119
+ beforeRequest;
120
+ afterResponse;
121
+ headers;
122
+ timeout;
123
+ stop;
124
+ qws;
125
+ isClient = false;
126
+ constructor(opts) {
127
+ this.adapter = opts?.adapter || adapter;
128
+ const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
129
+ this.url = opts?.url || defaultURL;
130
+ this.headers = opts?.headers || {
131
+ "Content-Type": "application/json"
132
+ };
133
+ this.timeout = opts?.timeout || 60000 * 3;
134
+ if (opts.beforeRequest) {
135
+ this.beforeRequest = opts.beforeRequest;
136
+ } else {
137
+ this.beforeRequest = async (opts2) => {
138
+ const token = globalThis?.localStorage?.getItem("token");
139
+ if (token) {
140
+ opts2.headers = {
141
+ ...opts2.headers,
142
+ Authorization: `Bearer ${token}`
143
+ };
144
+ }
145
+ return opts2;
146
+ };
147
+ }
148
+ }
149
+ setQueryWs(qws) {
150
+ this.qws = qws;
151
+ }
152
+ setStop(stop) {
153
+ this.stop = stop;
154
+ }
155
+ async get(params, options) {
156
+ return this.post(params, options);
157
+ }
158
+ async post(body, options) {
159
+ const url = options?.url || this.url;
160
+ const { headers, adapter: adapter2, beforeRequest, afterResponse, timeout, ...rest } = options || {};
161
+ const _headers = { ...this.headers, ...headers };
162
+ const _adapter = adapter2 || this.adapter;
163
+ const _beforeRequest = beforeRequest || this.beforeRequest;
164
+ const _afterResponse = afterResponse || this.afterResponse;
165
+ const _timeout = timeout || this.timeout;
166
+ const req = {
167
+ url,
168
+ headers: _headers,
169
+ body,
170
+ timeout: _timeout,
171
+ ...rest
172
+ };
173
+ try {
174
+ if (_beforeRequest) {
175
+ const res = await _beforeRequest(req);
176
+ if (res === false) {
177
+ return wrapperError({
178
+ code: 500,
179
+ message: "request is cancel",
180
+ req
181
+ });
182
+ }
183
+ }
184
+ } catch (e) {
185
+ console.error("request beforeFn error", e, req);
186
+ return wrapperError({
187
+ code: 500,
188
+ message: "api request beforeFn error"
189
+ });
190
+ }
191
+ if (this.stop && !options?.noStop) {
192
+ const that = this;
193
+ await new Promise((resolve) => {
194
+ let timer = 0;
195
+ const detect = setInterval(() => {
196
+ if (!that.stop) {
197
+ clearInterval(detect);
198
+ resolve(true);
199
+ }
200
+ timer++;
201
+ if (timer > 30) {
202
+ console.error("request stop: timeout", req.url, timer);
203
+ }
204
+ }, 1000);
205
+ });
206
+ }
207
+ return _adapter(req).then(async (res) => {
208
+ try {
209
+ if (_afterResponse) {
210
+ return await _afterResponse(res, {
211
+ req,
212
+ res,
213
+ fetch: adapter2
214
+ });
215
+ }
216
+ return res;
217
+ } catch (e) {
218
+ console.error("request afterFn error", e, req);
219
+ return wrapperError({
220
+ code: 500,
221
+ message: "api request afterFn error"
222
+ });
223
+ }
224
+ });
225
+ }
226
+ before(fn) {
227
+ this.beforeRequest = fn;
228
+ }
229
+ after(fn) {
230
+ this.afterResponse = fn;
231
+ }
232
+ async fetchText(urlOrOptions, options) {
233
+ let _options = { ...options };
234
+ if (typeof urlOrOptions === "string" && !_options.url) {
235
+ _options.url = urlOrOptions;
236
+ }
237
+ if (typeof urlOrOptions === "object") {
238
+ _options = { ...urlOrOptions, ..._options };
239
+ }
240
+ const res = await adapter({
241
+ method: "GET",
242
+ ..._options,
243
+ headers: {
244
+ ...this.headers,
245
+ ..._options?.headers || {}
246
+ }
247
+ });
248
+ if (res && !res.code) {
249
+ return {
250
+ code: 200,
251
+ data: res
252
+ };
253
+ }
254
+ return res;
255
+ }
256
+ }
257
+
258
+ // node_modules/.pnpm/@kevisual+router@0.0.69/node_modules/@kevisual/router/dist/router-define.js
259
+ class Chain {
260
+ object;
261
+ app;
262
+ constructor(object, opts) {
263
+ this.object = object;
264
+ this.app = opts?.app;
265
+ }
266
+ get key() {
267
+ return this.object.key;
268
+ }
269
+ get path() {
270
+ return this.object.path;
271
+ }
272
+ setDescription(desc) {
273
+ this.object.description = desc;
274
+ return this;
275
+ }
276
+ setMeta(metadata) {
277
+ this.object.metadata = metadata;
278
+ return this;
279
+ }
280
+ setPath(path) {
281
+ this.object.path = path;
282
+ return this;
283
+ }
284
+ setMiddleware(middleware) {
285
+ this.object.middleware = middleware;
286
+ return this;
287
+ }
288
+ setKey(key) {
289
+ this.object.key = key;
290
+ return this;
291
+ }
292
+ setId(key) {
293
+ this.object.id = key;
294
+ return this;
295
+ }
296
+ setRun(run) {
297
+ this.object.run = run;
298
+ return this;
299
+ }
300
+ define(run) {
301
+ this.object.run = run;
302
+ return this;
303
+ }
304
+ createRoute() {
305
+ this.app.route(this.object).addTo(this.app);
306
+ return this;
307
+ }
308
+ }
309
+
310
+ class QueryChain {
311
+ obj = {};
312
+ query;
313
+ omitKeys = ["metadata", "description", "validator"];
314
+ constructor(value, opts) {
315
+ this.obj = value || {};
316
+ this.query = opts?.query;
317
+ if (opts?.omitKeys)
318
+ this.omitKeys = opts.omitKeys;
319
+ }
320
+ omit(obj, key = []) {
321
+ const newObj = { ...obj };
322
+ key.forEach((k) => {
323
+ delete newObj[k];
324
+ });
325
+ return newObj;
326
+ }
327
+ getKey(queryData) {
328
+ const obj = this.omit(this.obj, this.omitKeys);
329
+ return {
330
+ ...obj,
331
+ ...queryData
332
+ };
333
+ }
334
+ post(data, options) {
335
+ const _queryData = this.getKey(data);
336
+ return this.query.post(_queryData, options);
337
+ }
338
+ get(data, options) {
339
+ const _queryData = this.getKey(data);
340
+ return this.query.get(_queryData, options);
341
+ }
342
+ }
343
+ class QueryUtil {
344
+ obj;
345
+ app;
346
+ query;
347
+ constructor(object, opts) {
348
+ this.obj = object;
349
+ this.app = opts?.app;
350
+ this.query = opts?.query;
351
+ }
352
+ static createFormObj(object, opts) {
353
+ return new QueryUtil(object, opts);
354
+ }
355
+ static create(value, opts) {
356
+ const obj = value;
357
+ return new QueryUtil(obj, opts);
358
+ }
359
+ get(key) {
360
+ return this.obj[key];
361
+ }
362
+ chain(key, opts) {
363
+ const obj = this.obj[key];
364
+ let newOpts = { app: this.app, ...opts };
365
+ return new QueryUtil.Chain(obj, newOpts);
366
+ }
367
+ queryChain(key, opts) {
368
+ const value = this.obj[key];
369
+ let newOpts = { query: this.query, ...opts };
370
+ return new QueryUtil.QueryChain(value, newOpts);
371
+ }
372
+ static Chain = Chain;
373
+ static QueryChain = QueryChain;
374
+ get routeObject() {
375
+ return this.obj;
376
+ }
377
+ }
378
+
379
+ // query/index.ts
380
+ var query = new Query;
381
+ var clientQuery = new Query({ url: "/client/router" });
382
+
383
+ // query/query-shop/defines/query-shop-define.ts
384
+ var shopDefine = QueryUtil.create({
385
+ getRegistry: {
386
+ path: "shop",
387
+ key: "get-registry",
388
+ description: "获取应用商店注册表信息"
389
+ },
390
+ listInstalled: {
391
+ path: "shop",
392
+ key: "list-installed",
393
+ description: "列出当前已安装的所有应用"
394
+ },
395
+ install: {
396
+ path: "shop",
397
+ key: "install",
398
+ description: "安装指定的应用,可以指定 id、type、force 和 yes 参数"
399
+ },
400
+ uninstall: {
401
+ path: "shop",
402
+ key: "uninstall",
403
+ description: "卸载指定的应用,可以指定 id 和 type 参数"
404
+ }
405
+ });
406
+
407
+ // node_modules/.pnpm/@kevisual+query@0.0.39/node_modules/@kevisual/query/dist/query.js
408
+ class BaseQuery {
409
+ query;
410
+ queryDefine;
411
+ constructor(opts) {
412
+ if (opts?.clientQuery) {
413
+ this.query = opts.clientQuery;
414
+ } else {
415
+ this.query = opts?.query;
416
+ }
417
+ if (opts.queryDefine) {
418
+ this.queryDefine = opts.queryDefine;
419
+ this.queryDefine.query = this.query;
420
+ }
421
+ }
422
+ get chain() {
423
+ return this.queryDefine.queryChain;
424
+ }
425
+ post(data, options) {
426
+ return this.query.post(data, options);
427
+ }
428
+ get(data, options) {
429
+ return this.query.get(data, options);
430
+ }
431
+ }
432
+
433
+ // query/query-shop/query-shop.ts
434
+ class QueryShop extends BaseQuery {
435
+ constructor(opts) {
436
+ super({
437
+ query: opts?.query
438
+ });
439
+ }
440
+ getInstall(data, opts) {
441
+ return this.query.post(data, opts);
442
+ }
443
+ }
444
+ export {
445
+ shopDefine,
446
+ QueryShop
447
+ };
@@ -0,0 +1,153 @@
1
+ interface UploadNProgress {
2
+ start: (msg?: string) => void;
3
+ done: () => void;
4
+ set: (progress: number) => void;
5
+ }
6
+ type UploadProgressData = {
7
+ progress: number;
8
+ progressFixed: number;
9
+ filename?: string;
10
+ taskId?: string;
11
+ };
12
+ type UploadProgressOpts = {
13
+ onStart?: () => void;
14
+ onDone?: () => void;
15
+ onProgress?: (progress: number, data?: UploadProgressData) => void;
16
+ };
17
+ declare class UploadProgress implements UploadNProgress {
18
+ /**
19
+ * 进度
20
+ */
21
+ progress: number;
22
+ /**
23
+ * 开始回调
24
+ */
25
+ onStart: (() => void) | undefined;
26
+ /**
27
+ * 结束回调
28
+ */
29
+ onDone: (() => void) | undefined;
30
+ /**
31
+ * 消息回调
32
+ */
33
+ onProgress: ((progress: number, data?: UploadProgressData) => void) | undefined;
34
+ /**
35
+ * 数据
36
+ */
37
+ data: any;
38
+ /**
39
+ * 是否结束
40
+ */
41
+ end: boolean;
42
+ constructor(uploadOpts: UploadProgressOpts);
43
+ start(msg?: string): void;
44
+ done(): void;
45
+ set(progress: number, data?: UploadProgressData): void;
46
+ /**
47
+ * 开始回调
48
+ */
49
+ setOnStart(callback: () => void): void;
50
+ /**
51
+ * 结束回调
52
+ */
53
+ setOnDone(callback: () => void): void;
54
+ /**
55
+ * 消息回调
56
+ */
57
+ setOnProgress(callback: (progress: number, data?: UploadProgressData) => void): void;
58
+ /**
59
+ * 打印信息
60
+ */
61
+ info(msg: string): void;
62
+ /**
63
+ * 打印错误
64
+ */
65
+ error(msg: string): void;
66
+ /**
67
+ * 打印警告
68
+ */
69
+ warn(msg: string): void;
70
+ }
71
+
72
+ type ConvertOpts$2 = {
73
+ appKey?: string;
74
+ version?: string;
75
+ username?: string;
76
+ directory?: string;
77
+ isPublic?: boolean;
78
+ filename?: string;
79
+ /**
80
+ * 是否不检查应用文件, 默认 true,默认不检测
81
+ */
82
+ noCheckAppFiles?: boolean;
83
+ };
84
+ type UploadOpts = {
85
+ uploadProgress: UploadProgress;
86
+ /**
87
+ * 创建 EventSource 兼容 nodejs
88
+ * @param baseUrl 基础 URL
89
+ * @param searchParams 查询参数
90
+ * @returns EventSource
91
+ */
92
+ createEventSource: (baseUrl: string, searchParams: URLSearchParams) => EventSource;
93
+ baseUrl?: string;
94
+ token: string;
95
+ FormDataFn: any;
96
+ };
97
+ declare const uploadFileChunked: (file: File, opts: ConvertOpts$2, opts2: UploadOpts) => Promise<unknown>;
98
+
99
+ type ConvertOpts$1 = {
100
+ appKey?: string;
101
+ version?: string;
102
+ username?: string;
103
+ directory?: string;
104
+ /**
105
+ * 文件大小限制
106
+ */
107
+ maxSize?: number;
108
+ /**
109
+ * 文件数量限制
110
+ */
111
+ maxCount?: number;
112
+ /**
113
+ * 是否不检查应用文件, 默认 true,默认不检测
114
+ */
115
+ noCheckAppFiles?: boolean;
116
+ };
117
+ declare const uploadFiles: (files: File[], opts: ConvertOpts$1, opts2: UploadOpts) => Promise<unknown>;
118
+
119
+ /**
120
+ * 把字符串转为文件流,并返回文件流,根据filename的扩展名,自动设置文件类型.
121
+ * 当不是文本类型,自动需要把base64的字符串转为blob
122
+ * @param content 字符串
123
+ * @param filename 文件名
124
+ * @returns 文件流
125
+ */
126
+ declare const toFile: (content: string, filename: string) => File;
127
+
128
+ declare const randomId: () => string;
129
+
130
+ type UploadFileProps = {
131
+ onStart?: () => void;
132
+ onDone?: () => void;
133
+ onProgress?: (progress: number, data: UploadProgressData) => void;
134
+ onSuccess?: (res: any) => void;
135
+ onError?: (err: any) => void;
136
+ token?: string;
137
+ };
138
+ type ConvertOpts = {
139
+ appKey?: string;
140
+ version?: string;
141
+ username?: string;
142
+ directory?: string;
143
+ isPublic?: boolean;
144
+ filename?: string;
145
+ /**
146
+ * 是否不检查应用文件, 默认 true,默认不检测
147
+ */
148
+ noCheckAppFiles?: boolean;
149
+ };
150
+ declare const uploadChunk: (file: File, opts: ConvertOpts, props?: UploadFileProps) => Promise<unknown>;
151
+
152
+ export { UploadProgress, randomId, toFile, uploadChunk, uploadFileChunked, uploadFiles };
153
+ export type { ConvertOpts };