@we-debug/plugin-api 0.0.0-stage → 0.50.0
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/apiManage.js +1 -0
- package/dist/hook.js +1 -0
- package/dist/index.js +1 -0
- package/dist/record.js +1 -0
- package/package.json +18 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.50.0](https://github.com/dlhandsome/we-debug/compare/v0.49.2...v0.50.0) (2026-11-27)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **api:** 新增 wx API 抓包面板 ([da85c30](https://github.com/dlhandsome/we-debug/commit/da85c30bbdd5d40b314b74d64dd22f1137157304))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Sail
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# we-debug api plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
we-debug wx API 抓包插件,在调试面板中提供与 `Network` 面板风格一致的 `API` 面板
|
|
4
|
+
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- 自动劫持 `wx` 上的 API 并记录调用,包含异步(success/fail 回调或 Promise)与同步(`*Sync`)API
|
|
8
|
+
- 不记录 `on*` / `off*` / `create*` 类 API、`wx.request` / `wx.downloadFile` / `wx.uploadFile`(见 Network 面板)以及 we-debug 自身通过 `weDebug.silent(fn)` 发起的调用
|
|
9
|
+
- 调用列表:Name、Status、Type、Time,失败调用标红,底部汇总调用数、失败数与耗时
|
|
10
|
+
- 工具栏:开始/暂停录制、清空、API 名称过滤、Preserve log(切换页面时保留日志)
|
|
11
|
+
- 类型筛选:All / Async / Sync / Failed
|
|
12
|
+
- 调用详情:Overview(状态、所在页面、耗时、调用栈)、Params、Result(JSON 树)
|
|
13
|
+
- 支持复制调用代码、参数与结果
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
`@we-debug/core` 已内置该插件,通过 init 方法初始化即可
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import weDebug from '@we-debug/core/libs/index'
|
|
21
|
+
|
|
22
|
+
weDebug.init({
|
|
23
|
+
plugin: {
|
|
24
|
+
api: {
|
|
25
|
+
// ...
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 配置
|
|
32
|
+
|
|
33
|
+
参数 | 说明 | 类型 | 默认值 | 版本
|
|
34
|
+
-|-|-|-|-|
|
|
35
|
+
maxRecords | 最多保留的调用记录条数,超出后丢弃最早的记录 | `Number` | `500` | - |
|
|
36
|
+
include | 只劫持指定的 API,设置后忽略默认劫持范围 | `Array<String>` | - | - |
|
|
37
|
+
exclude | 额外不劫持的 API | `Array<String>` | `[]` | - |
|
|
38
|
+
|
|
39
|
+
## 示例
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
weDebug.init({
|
|
43
|
+
plugin: {
|
|
44
|
+
api: {
|
|
45
|
+
exclude: ['getStorageSync', 'setStorageSync']
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;let apiManage={_queue:[],_maxRecords:500,setMaxRecords(e){0<e&&(this._maxRecords=e)},get(){return this._queue},getById(t){return this._queue.find(e=>e.id===t)},add(e){this._queue.push(e),this._queue.length>this._maxRecords&&this._queue.splice(0,this._queue.length-this._maxRecords)},clear(){this._queue=[]}};var _default=exports.default=apiManage;
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.hookWxApi=hookWxApi;let isFunc=t=>"function"==typeof t,isPlainObject=t=>"[object Object]"===Object.prototype.toString.call(t),SKIP_PATTERN=/^(on|off|create)[A-Z]/,DEFAULT_EXCLUDE=["request","downloadFile","uploadFile","canIUse","nextTick"],HOOK_FRAMES=2,MAX_INITIATOR_FRAMES=8,callId=0;function getInitiator(){return((new Error).stack||"").split("\n").map(t=>t.trim()).filter(t=>t&&!/^Error\b/.test(t)).slice(HOOK_FRAMES,HOOK_FRAMES+MAX_INITIATOR_FRAMES)}function getCurrentRoute(){try{var t="function"==typeof getCurrentPages?getCurrentPages():[],e=t[t.length-1];return e&&(e.route||e.__route__)||""}catch(t){return""}}function getApiNames(t,e){if(Array.isArray(t)&&t.length)return t.filter(t=>isFunc(wx[t]));let r=DEFAULT_EXCLUDE.concat(Array.isArray(e)?e:[]);return Object.keys(wx).filter(t=>{if(SKIP_PATTERN.test(t)||-1<r.indexOf(t))return!1;try{return isFunc(wx[t])}catch(t){return!1}})}function wrap(s,u,{shouldRecord:a,onStart:l,onFinish:f}){let p=/Sync$/.test(s);return function(...t){if(!a())return u.apply(this,t);let e=++callId,r=!1,n=t=>{r||(r=!0,f(Object.assign({id:e,name:s,endTime:Date.now()},t)))},i=(l({id:e,name:s,sync:p,args:t,route:getCurrentRoute(),initiator:getInitiator(),startTime:Date.now()}),t[0]);var c;if(!p&&isPlainObject(i)&&(isFunc(i.success)||isFunc(i.fail)||isFunc(i.complete)))return(c=Object.assign({},i)).success=function(...t){n({result:t[0]}),isFunc(i.success)&&i.success.apply(this,t)},c.fail=function(...t){n({error:t[0]}),isFunc(i.fail)&&i.fail.apply(this,t)},u.apply(this,[c].concat(t.slice(1)));let o;try{o=u.apply(this,t)}catch(t){throw n({error:t}),t}return!p&&o&&isFunc(o.then)?o.then(t=>n({result:t}),t=>n({error:t})):n({result:o,returned:!0}),o}}function hookWxApi({include:t,exclude:e,shouldRecord:r,onStart:n,onFinish:i}){let c=[];return getApiNames(t,e).forEach(t=>{var e=wx[t];try{Object.defineProperty(wx,t,{value:wrap(t,e,{shouldRecord:r,onStart:n,onFinish:i}),writable:!0,configurable:!0,enumerable:!0}),c.push(t)}catch(t){}}),c}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _apiManage=_interopRequireDefault(require("./apiManage")),_hook=require("./hook"),_record=require("./record");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}let Api={},API_GROUP="API",PRESERVE_LOG_CACHE_KEY="__wedebug_api_preserve_log__";class ApiPlugin{constructor(e,i={}){this.weDebug=e,this.options=i,this.recording=!0,this._pending={},this._preserveLogCache=e.createCache(PRESERVE_LOG_CACHE_KEY),this.preserveLog=!!this._preserveLogCache.get(),_apiManage.default.setMaxRecords(i.maxRecords)}createUI(){let i=this;var{createApiView:e,addApiView:r}=i.weDebug;e&&r&&(i._apiView=e({handler:{getRecords:()=>_apiManage.default.get(),getState:()=>({recording:i.recording,preserveLog:i.preserveLog}),bindClear:()=>i.clear(),bindRecordChange:e=>{i.recording=!!e,i.notify()},bindPreserveLogChange:e=>{i.preserveLog=!!e,i._preserveLogCache.set(i.preserveLog),i.notify()}}}),r(i._apiView,{group:{name:API_GROUP}}))}clear(){_apiManage.default.clear(),this.notify()}notify(){this._apiView&&this._apiView.emit()}createProxy(){let i=this.weDebug.event;var{include:e,exclude:r}=this.options;(0,_hook.hookWxApi)({include:e,exclude:r,shouldRecord:()=>this.recording&&!(this.weDebug.isSilent&&this.weDebug.isSilent()),onStart:e=>i.emit("wxapi:call-start",e),onFinish:e=>i.emit("wxapi:call-finished",e)}),i.on("wxapi:call-start",e=>{this._pending[e.id]=e,_apiManage.default.add((0,_record.createRecord)(e)),this.notify()}),i.on("wxapi:call-finished",e=>{var i=this._pending[e.id];delete this._pending[e.id];let r=_apiManage.default.getById(e.id);if(!r){if(!this.recording||!i)return;r=(0,_record.createRecord)(i),_apiManage.default.add(r)}(0,_record.finishRecord)(r,e),this.notify()}),wx.onAppRoute&&wx.onAppRoute(()=>{this.preserveLog||this.clear()})}}Api.install=function(e,i={}){e=new ApiPlugin(e,i);e.createUI(),e.createProxy()};var _default=exports.default=Api;
|
package/dist/record.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.createRecord=createRecord,exports.finishRecord=finishRecord;let isPlainObject=e=>"[object Object]"===Object.prototype.toString.call(e);function sanitize(r){return"function"==typeof r?`ƒ ${r.name||"anonymous"}()`:isPlainObject(r)?Object.keys(r).reduce((e,t)=>(e[t]="function"==typeof r[t]?`ƒ ${t}()`:r[t],e),{}):r}function getParams(e=[]){if(0!==e.length)return 1===e.length?sanitize(e[0]):e.map(sanitize)}function getErrMsg(e){return e?e.errMsg||e.message||String(e):""}function createRecord({id:e,name:t,sync:r,args:s,route:n,initiator:i,startTime:a}){return{id:e,name:t,type:r?"sync":"async",status:"pending",params:getParams(s),multiArgs:!!s&&1<s.length,result:void 0,errMsg:"",route:n||"",initiator:i||[],startTime:a||Date.now(),endTime:0,duration:0}}function finishRecord(e,{result:t,error:r,returned:s,endTime:n}){n=n||Date.now(),n={endTime:n,duration:n-e.startTime};return r?Object.assign(n,{status:"fail",errMsg:getErrMsg(r),result:r instanceof Error?{errMsg:r.message,stack:r.stack}:r}):s&&"async"===e.type&&void 0===t?n.status="done":(Object.assign(n,{status:"success",result:t,errMsg:t&&"string"==typeof t.errMsg&&t.errMsg||""}),s&&"async"===e.type&&(n.type="sync")),Object.assign(e,n)}
|
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@we-debug/plugin-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.50.0",
|
|
4
|
+
"description": "wx api capture plugin for we-debug",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"we-debug"
|
|
14
|
+
],
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "dlhandsome"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"gitHead": "2304486ada029231f6e93b89f7491fe0e5d298c6"
|
|
20
|
+
}
|