vite-uni-dev-tool 0.0.1

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.
Files changed (69) hide show
  1. package/README.md +120 -0
  2. package/dev/components/Button/index.vue +34 -0
  3. package/dev/components/Checkbox/index.vue +40 -0
  4. package/dev/components/CloseButton/index.vue +25 -0
  5. package/dev/components/Connection/index.vue +98 -0
  6. package/dev/components/ConsoleList/ConsoleItem.vue +89 -0
  7. package/dev/components/ConsoleList/index.vue +98 -0
  8. package/dev/components/DevTool/index.vue +165 -0
  9. package/dev/components/DevToolButton/index.vue +213 -0
  10. package/dev/components/DevToolWindow/index.vue +847 -0
  11. package/dev/components/DeviceInfo/index.vue +32 -0
  12. package/dev/components/Empty/empty.png +0 -0
  13. package/dev/components/Empty/index.vue +28 -0
  14. package/dev/components/FilterInput/index.vue +86 -0
  15. package/dev/components/JsonPretty/components/Brackets/index.vue +23 -0
  16. package/dev/components/JsonPretty/components/Carets/index.vue +63 -0
  17. package/dev/components/JsonPretty/components/CheckController/index.vue +108 -0
  18. package/dev/components/JsonPretty/components/TreeNode/index.vue +348 -0
  19. package/dev/components/JsonPretty/hooks/useClipboard.ts +21 -0
  20. package/dev/components/JsonPretty/hooks/useError.ts +21 -0
  21. package/dev/components/JsonPretty/index.vue +463 -0
  22. package/dev/components/JsonPretty/type.ts +123 -0
  23. package/dev/components/JsonPretty/utils/index.ts +172 -0
  24. package/dev/components/NetworkList/NetworkDetail.vue +197 -0
  25. package/dev/components/NetworkList/NetworkItem.vue +106 -0
  26. package/dev/components/NetworkList/index.vue +108 -0
  27. package/dev/components/PiniaList/index.vue +64 -0
  28. package/dev/components/RouteList/index.vue +98 -0
  29. package/dev/components/SettingList/index.vue +235 -0
  30. package/dev/components/StorageList/index.vue +170 -0
  31. package/dev/components/SystemInfo/index.vue +34 -0
  32. package/dev/components/Tabs/index.vue +110 -0
  33. package/dev/components/Tag/index.vue +89 -0
  34. package/dev/components/UploadList/UploadDetail.vue +208 -0
  35. package/dev/components/UploadList/UploadItem.vue +111 -0
  36. package/dev/components/UploadList/index.vue +94 -0
  37. package/dev/components/VuexList/index.vue +54 -0
  38. package/dev/components/WebSocket/WebSocketItem.vue +98 -0
  39. package/dev/components/WebSocket/WebSocketList.vue +176 -0
  40. package/dev/components/WebSocket/index.vue +99 -0
  41. package/dev/components/WindowInfo/index.vue +33 -0
  42. package/dev/const.ts +95 -0
  43. package/dev/core.ts +103 -0
  44. package/dev/devConsole/index.ts +334 -0
  45. package/dev/devEvent/index.ts +665 -0
  46. package/dev/devIntercept/index.ts +629 -0
  47. package/dev/devStore/index.ts +581 -0
  48. package/dev/index.d.ts +6 -0
  49. package/dev/index.d.ts.map +1 -0
  50. package/dev/index.js +1 -0
  51. package/dev/plugins/uniDevTool/uniDevTool.d.ts +66 -0
  52. package/dev/plugins/uniDevTool/uniDevTool.d.ts.map +1 -0
  53. package/dev/plugins/uniDevTool/uniDevTool.js +13 -0
  54. package/dev/plugins/uniGlobalComponents/uniGlobalComponents.d.ts +28 -0
  55. package/dev/plugins/uniGlobalComponents/uniGlobalComponents.d.ts.map +1 -0
  56. package/dev/plugins/uniGlobalComponents/uniGlobalComponents.js +5 -0
  57. package/dev/shims-uni.d.ts +43 -0
  58. package/dev/type.ts +188 -0
  59. package/dev/utils/date.ts +75 -0
  60. package/dev/utils/file.ts +121 -0
  61. package/dev/utils/function.ts +192 -0
  62. package/dev/utils/index.ts +25 -0
  63. package/dev/utils/ip.ts +79 -0
  64. package/dev/utils/language.ts +19 -0
  65. package/dev/utils/object.ts +235 -0
  66. package/dev/utils/page.ts +13 -0
  67. package/dev/utils/string.ts +23 -0
  68. package/dev/utils/utils.ts +198 -0
  69. package/package.json +34 -0
@@ -0,0 +1,629 @@
1
+ import { backup } from '../core';
2
+ import { DevEvent } from '../devEvent';
3
+ import type { DevTool } from '../type';
4
+ import {
5
+ getCurrentDate,
6
+ getCurrentPagePath,
7
+ serializeCircular,
8
+ } from '../utils/index';
9
+ import { isObject } from '../utils/language';
10
+
11
+ import { getCurrentInstance } from 'vue';
12
+
13
+ /**
14
+ * 拦截器
15
+ *
16
+ * @export
17
+ * @class DevIntercept
18
+ */
19
+ export class DevIntercept {
20
+ private event: DevEvent;
21
+
22
+ vue3instance = getCurrentInstance();
23
+
24
+ initPinia = false;
25
+
26
+ constructor(options: DevTool.DevInterceptOptions) {
27
+ this.event = options.event;
28
+ this.init(options);
29
+ }
30
+
31
+ init(options: DevTool.DevInterceptOptions) {
32
+ if (this.vue3instance) {
33
+ this.vue3instance.appContext.config.errorHandler = (error) => {
34
+ this.interceptErrorApp(error);
35
+ };
36
+ this.vue3instance.appContext.config.warnHandler = (msg: string) => {
37
+ this.interceptWarnApp(msg);
38
+ };
39
+ }
40
+
41
+ this.interceptAppConsole();
42
+
43
+ this.interceptSetStorage();
44
+ this.interceptRemoveStorage();
45
+ this.interceptClearStorage();
46
+
47
+ this.interceptUploadFile();
48
+ this.interceptRequest();
49
+ this.interceptSocket();
50
+
51
+ this.interceptSwitchTab();
52
+ this.interceptNavigateTo();
53
+
54
+ options.enableInterceptPromiseReject && this.interceptPromiseReject();
55
+ }
56
+
57
+ /**
58
+ * app 中拦截 console
59
+ * app
60
+ * @memberof DevIntercept
61
+ */
62
+ interceptAppConsole() {
63
+ if (!(uni as any).__log__) return;
64
+ const that = this;
65
+ (uni as any).__log__ = function (
66
+ type: DevTool.ConsoleType,
67
+ line: string,
68
+ ...args: any
69
+ ) {
70
+ const path = getCurrentPagePath();
71
+
72
+ backup['__log__'](type, line, ...args);
73
+
74
+ that.event.updateConsoleList([
75
+ {
76
+ type,
77
+ position: path,
78
+ time: getCurrentDate(),
79
+ args: args.map((item: any) => {
80
+ // 将循环引用的对象转为字符串
81
+ if (isObject(item)) {
82
+ return serializeCircular(item);
83
+ } else if (
84
+ item === null ||
85
+ item === undefined ||
86
+ isNaN(item) ||
87
+ item === Infinity
88
+ ) {
89
+ return item + '';
90
+ } else {
91
+ return item;
92
+ }
93
+ }),
94
+ stack: line,
95
+ },
96
+ ]);
97
+ };
98
+ }
99
+
100
+ /**
101
+ * 拦截 app 错误
102
+ *
103
+ * @param {*} error
104
+ * @memberof DevIntercept
105
+ */
106
+ interceptErrorApp(error: any) {
107
+ const path = getCurrentPagePath();
108
+ const argList = error.toString();
109
+
110
+ const stack = new Error()?.stack?.split('\n').slice(2)?.[0];
111
+
112
+ console.error(error);
113
+
114
+ this.event.updateConsoleList([
115
+ {
116
+ type: 'error',
117
+ args: [argList],
118
+ position: path,
119
+ time: getCurrentDate(),
120
+ stack,
121
+ },
122
+ ]);
123
+ }
124
+
125
+ preWarn: any;
126
+ /**
127
+ * 拦截 app 警告
128
+ *
129
+ * @param {*} warn
130
+ * @return {*}
131
+ * @memberof DevIntercept
132
+ */
133
+ interceptWarnApp(warn: any) {
134
+ if (this.preWarn === warn) return;
135
+ this.preWarn = warn;
136
+ const path = getCurrentPagePath();
137
+ const stack = new Error()?.stack?.split('\n').slice(2)?.[0];
138
+
139
+ console.warn(warn);
140
+
141
+ this.event.updateConsoleList([
142
+ {
143
+ type: 'warn',
144
+ args: [warn.toString()],
145
+ position: path,
146
+ time: getCurrentDate(),
147
+ stack,
148
+ },
149
+ ]);
150
+ }
151
+
152
+ /**
153
+ * 拦截 promise reject
154
+ *
155
+ * @memberof DevIntercept
156
+ */
157
+ interceptPromiseReject() {
158
+ Object.defineProperty(Promise, 'reject', {
159
+ value: function (reason: any) {
160
+ this.interceptErrorApp(reason);
161
+ return backup.reject.call(Promise, reason);
162
+ },
163
+ });
164
+ }
165
+
166
+ /**
167
+ * 拦截 nav 跳转
168
+ *
169
+ * @memberof DevIntercept
170
+ */
171
+ interceptSwitchTab() {
172
+ const that = this;
173
+ uni.addInterceptor('switchTab', {
174
+ invoke(args: UniNamespace.NavigateToOptions) {
175
+ const complete = args.complete;
176
+ args.complete = function (e: any) {
177
+ complete && complete(e);
178
+
179
+ const url = args.url?.slice(1)?.split('?')?.[0] || '/';
180
+ that.event.updateCurrentPagePath(url);
181
+ };
182
+ },
183
+ });
184
+ }
185
+
186
+ /**
187
+ * 拦截页面跳转
188
+ *
189
+ * @memberof DevIntercept
190
+ */
191
+ interceptNavigateTo() {
192
+ const that = this;
193
+ uni.addInterceptor('navigateTo', {
194
+ invoke(args: UniNamespace.NavigateToOptions) {
195
+ const complete = args.complete;
196
+ args.complete = function (e: any) {
197
+ complete && complete(e);
198
+ const url = args.url?.slice(1)?.split('?')?.[0] || '/';
199
+ that.event.updateCurrentPagePath(url);
200
+ };
201
+ },
202
+ });
203
+ }
204
+
205
+ /**
206
+ * 拦截网络请求
207
+ *
208
+ * @memberof DevIntercept
209
+ */
210
+ interceptRequest() {
211
+ const that = this;
212
+ uni.addInterceptor('request', {
213
+ invoke(args: UniNamespace.RequestOptions) {
214
+ const preIndex = that.event.getRequestIndex();
215
+ const index = that.event.setRequestIndex(preIndex + 1);
216
+ const url = args.url;
217
+ const noParams = url?.split('?')?.[0];
218
+ const lastG = noParams?.lastIndexOf('/');
219
+ const name = noParams?.slice(lastG + 1) ?? '';
220
+ const addInterceptorRequestData: DevTool.NetworkItem = {
221
+ index,
222
+ url: args.url,
223
+ name,
224
+ startTime: Date.now(),
225
+ endTime: 0,
226
+ time: '-',
227
+ headers: {
228
+ requestHeader: Object.entries(args.header ?? {}).map(
229
+ ([key, value]) => ({
230
+ key,
231
+ value,
232
+ }),
233
+ ),
234
+ responseHeader: [],
235
+ },
236
+ method: args.method || 'GET',
237
+ status: 'pending',
238
+ payload: args?.data ? JSON.stringify(args.data) : '',
239
+ response: '',
240
+ size: '',
241
+ };
242
+ that.event.updateNetworkList([addInterceptorRequestData]);
243
+
244
+ let _complete = args.complete;
245
+ args.complete = function (e: any) {
246
+ _complete && _complete(e);
247
+
248
+ addInterceptorRequestData.status = e.statusCode ?? 'error';
249
+ addInterceptorRequestData.endTime = Date.now();
250
+
251
+ const diff = Date.now() - addInterceptorRequestData.startTime;
252
+
253
+ addInterceptorRequestData.time =
254
+ diff < 1000 ? diff + 'ms' : diff / 1000 + 's';
255
+
256
+ const len =
257
+ e?.header?.['Content-Length'] || e?.header?.['content-length'] || 0;
258
+ addInterceptorRequestData.size =
259
+ len > 1024 ? (len / 1024).toFixed(2) + 'k' : len + 'b';
260
+ addInterceptorRequestData.response = e;
261
+ addInterceptorRequestData.headers.responseHeader = Object.entries(
262
+ e.header ?? {},
263
+ ).map(([key, value]) => ({
264
+ key,
265
+ value,
266
+ }));
267
+ that.event.updateNetworkList([addInterceptorRequestData], index);
268
+ };
269
+ },
270
+ });
271
+ }
272
+
273
+ /**
274
+ * 拦截 websocket
275
+ *
276
+ * @memberof DevIntercept
277
+ */
278
+ interceptSocket() {
279
+ const that = this;
280
+
281
+ const connectSocket = function (
282
+ connectOption: UniNamespace.ConnectSocketOption,
283
+ ) {
284
+ const url = connectOption.url;
285
+ const headers = Object.entries(connectOption.header ?? {}).map(
286
+ ([key, value]) => {
287
+ return { key, value } as { key: string; value: string };
288
+ },
289
+ );
290
+ const method = (connectOption.method ?? 'GET').toLowerCase();
291
+ const protocols = connectOption.protocols ?? [];
292
+ that.event.updateWsList({
293
+ url,
294
+ headers,
295
+ method,
296
+ protocols,
297
+ readyState: 'connection',
298
+ message: [],
299
+ });
300
+
301
+ const socketTask = backup.connectSocket({
302
+ ...connectOption,
303
+ success: (res) => {
304
+ connectOption.success?.(res);
305
+ that.event.updateWsList({
306
+ url,
307
+ readyState: 'open',
308
+ headers,
309
+ protocols,
310
+ message: [
311
+ {
312
+ data: '连接成功',
313
+ type: 'success',
314
+ time: Date.now(),
315
+ },
316
+ ],
317
+ });
318
+ },
319
+ fail: (error) => {
320
+ connectOption.fail?.(error);
321
+ that.event.updateWsList({
322
+ url,
323
+ readyState: 'error',
324
+ headers,
325
+ protocols,
326
+ message: [
327
+ {
328
+ data: '连接失败',
329
+ type: 'error',
330
+ time: Date.now(),
331
+ },
332
+ ],
333
+ });
334
+ },
335
+ complete: (res) => {
336
+ connectOption?.complete?.(res);
337
+ },
338
+ });
339
+
340
+ const send = socketTask.send;
341
+ socketTask.send = (options: UniApp.SendSocketMessageOptions) => {
342
+ send.call(socketTask, {
343
+ data: options.data,
344
+ fail: (error) => {
345
+ options?.fail?.(error);
346
+ that.event.updateWsList({
347
+ url,
348
+ readyState: 'error',
349
+ headers,
350
+ protocols,
351
+ message: [
352
+ {
353
+ data: JSON.stringify(error),
354
+ type: 'error',
355
+ time: Date.now(),
356
+ },
357
+ ],
358
+ });
359
+ },
360
+ success: (res) => {
361
+ options?.success?.(res);
362
+ that.event.updateWsList({
363
+ readyState: 'open',
364
+ url,
365
+ headers,
366
+ protocols,
367
+ message: [
368
+ {
369
+ type: 'success',
370
+ time: Date.now(),
371
+ data: options.data?.toString(),
372
+ },
373
+ ],
374
+ });
375
+ },
376
+ complete: (res) => {
377
+ options?.complete?.(res);
378
+ },
379
+ });
380
+ };
381
+
382
+ const close = socketTask.close;
383
+ socketTask.close = (options: UniApp.CloseSocketOptions) => {
384
+ that.event.updateWsList({
385
+ url,
386
+ readyState: 'closing',
387
+ headers,
388
+ protocols,
389
+ message: [],
390
+ });
391
+
392
+ close.call(socketTask, {
393
+ ...options,
394
+ fail: (error) => {
395
+ options?.fail?.(error);
396
+ that.event.updateWsList({
397
+ url,
398
+ headers,
399
+ readyState: 'open',
400
+ protocols,
401
+ message: [],
402
+ });
403
+ },
404
+ success: (res) => {
405
+ options?.success?.(res);
406
+ that.event.updateWsList({
407
+ url,
408
+ headers,
409
+ readyState: 'closed',
410
+ protocols,
411
+ message: [],
412
+ });
413
+ },
414
+ complete: (res) => {
415
+ options?.complete?.(res);
416
+ },
417
+ });
418
+ };
419
+
420
+ socketTask.onMessage((res) => {
421
+ that.event.updateWsList({
422
+ url,
423
+ headers,
424
+ protocols,
425
+ message: [
426
+ {
427
+ type: 'success',
428
+ time: Date.now(),
429
+ data: JSON.parse(res.data),
430
+ },
431
+ ],
432
+ });
433
+ });
434
+
435
+ return socketTask;
436
+ };
437
+
438
+ uni.connectSocket = connectSocket;
439
+ }
440
+
441
+ /**
442
+ * 拦截 uni.setStorageSync 和 uni.setStorage
443
+ *
444
+ * @memberof DevIntercept
445
+ */
446
+ interceptSetStorage() {
447
+ uni.setStorageSync = (key, value) => {
448
+ backup.setStorageSync(key.toString(), value);
449
+
450
+ this.event.updateStoreList([
451
+ {
452
+ key: key.toString(),
453
+ _oldKey: key.toString(),
454
+ value: value,
455
+ },
456
+ ]);
457
+ };
458
+ uni.setStorage = (options: UniNamespace.SetStorageOptions) => {
459
+ backup
460
+ .setStorage({
461
+ ...options,
462
+ key: options.key?.toString(),
463
+ })
464
+ .then(() => {
465
+ this.event.updateStoreList([
466
+ {
467
+ key: options.key?.toString(),
468
+ _oldKey: options.key?.toString(),
469
+ value: options.data,
470
+ },
471
+ ]);
472
+ });
473
+ };
474
+ }
475
+
476
+ /**
477
+ * 拦截 uni.clearStorageSync 和 uni.clearStorage
478
+ *
479
+ * @memberof DevIntercept
480
+ */
481
+ interceptClearStorage() {
482
+ uni.clearStorageSync = () => {
483
+ backup.clearStorageSync();
484
+ this.event.clearStorage();
485
+ };
486
+ uni.clearStorage = () => {
487
+ backup.clearStorage();
488
+ this.event.clearStorage();
489
+ };
490
+ }
491
+
492
+ /**
493
+ * 拦截 uni.removeStorageSync 和 uni.removeStorage
494
+ *
495
+ * @memberof DevIntercept
496
+ */
497
+ interceptRemoveStorage() {
498
+ uni.removeStorageSync = (key) => {
499
+ backup.removeStorageSync(key);
500
+ // remove(key);
501
+ this.event.removeStorage(key);
502
+ };
503
+ uni.removeStorage = (options: UniNamespace.RemoveStorageOptions) => {
504
+ backup.removeStorage(options).then(() => {
505
+ this.event.removeStorage(options.key);
506
+ });
507
+ };
508
+ }
509
+
510
+ /** 拦截vuex */
511
+ interceptVuexStorage(store: any) {
512
+ this.event.setVuexList(store.state);
513
+
514
+ store?.subscribe?.((_: any, state: any) => {
515
+ this.event.setVuexList(state);
516
+ });
517
+ }
518
+
519
+ /** 拦截pinia */
520
+ interceptPiniaStore(context: any) {
521
+ if (!this.initPinia) {
522
+ this.initPinia = true;
523
+ this.event.setPiniaStore(context.pinia);
524
+ }
525
+
526
+ this.event.setPiniaList(context.pinia.state.value);
527
+
528
+ context?.store?.$subscribe(() => {
529
+ this.event.setPiniaList({
530
+ [context.store.$id]: context.store.$state,
531
+ });
532
+ });
533
+ return context.pinia;
534
+ }
535
+
536
+ /**
537
+ * 拦截 uni.uploadFile
538
+ *
539
+ * @memberof DevIntercept
540
+ */
541
+ interceptUploadFile() {
542
+ const that = this;
543
+ const uploadFile = (uploadOption: UniNamespace.UploadFileOption) => {
544
+ const preIndex = that.event.getUploadIndex();
545
+
546
+ const index = that.event.setUploadIndex(preIndex + 1);
547
+
548
+ that.event.updateUploadList(
549
+ [
550
+ {
551
+ index,
552
+ name: uploadOption.name,
553
+ url: uploadOption.url,
554
+ filePath: uploadOption.filePath,
555
+ fileType: uploadOption.fileType,
556
+ headers: {
557
+ requestHeader: Object.entries(uploadOption.header || {}).map(
558
+ ([key, value]) => ({ key, value }),
559
+ ),
560
+ responseHeader: [],
561
+ },
562
+ formData: uploadOption.formData,
563
+ status: 'pending',
564
+ progress: 0,
565
+ totalBytesSent: 0,
566
+ totalBytesExpectedToSend: 0,
567
+ startTime: Date.now(),
568
+ },
569
+ ],
570
+ index,
571
+ );
572
+ const uploadTask = backup.uploadFile({
573
+ ...uploadOption,
574
+ success: (res) => {
575
+ uploadOption?.success?.(res);
576
+ that.event.updateUploadList(
577
+ [
578
+ {
579
+ index,
580
+ status: 'success',
581
+ endTime: Date.now(),
582
+ response: {
583
+ ...res,
584
+ data: JSON.parse(res.data),
585
+ },
586
+ },
587
+ ],
588
+ index,
589
+ );
590
+ that.event.removeUploadTask(index);
591
+ },
592
+ fail: (error) => {
593
+ uploadOption?.fail?.(error);
594
+ that.event.updateUploadList(
595
+ [
596
+ {
597
+ index,
598
+ status: 'error',
599
+ response: error,
600
+ endTime: Date.now(),
601
+ },
602
+ ],
603
+ index,
604
+ );
605
+ that.event.removeUploadTask(index);
606
+ },
607
+ });
608
+ uploadTask.onProgressUpdate((res) => {
609
+ that.event.updateUploadList(
610
+ [
611
+ {
612
+ index,
613
+ progress: res.progress,
614
+ totalBytesSent: res.totalBytesSent,
615
+ totalBytesExpectedToSend: res.totalBytesExpectedToSend,
616
+ status: 'uploading',
617
+ },
618
+ ],
619
+ index,
620
+ );
621
+ });
622
+
623
+ that.event.addUploadTask(index, uploadTask);
624
+
625
+ return uploadTask;
626
+ };
627
+ uni.uploadFile = uploadFile.bind(uni);
628
+ }
629
+ }