ajax-hooker 1.0.2 → 1.0.6

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.
@@ -24,9 +24,14 @@ jobs:
24
24
  uses: actions/setup-node@v3
25
25
  with:
26
26
  node-version: '18'
27
-
27
+
28
+ - name: 安装pnpm
29
+ uses: pnpm/action-setup@v2
30
+ with:
31
+ version: 10
32
+
28
33
  - name: 安装依赖
29
- run: npm ci
34
+ run: pnpm i
30
35
 
31
36
  - name: 更新版本
32
37
  id: version
@@ -65,4 +70,4 @@ jobs:
65
70
  echo "包已发布!"
66
71
  echo "版本: ${{ steps.version.outputs.version }}"
67
72
  echo "类型: ${{ steps.publish.outputs.type }}"
68
- echo "URL: https://www.npmjs.com/package/web-hooker/v/${{ steps.version.outputs.version }}"
73
+ echo "URL: https://www.npmjs.com/package/ajax-hooker/v/${{ steps.version.outputs.version }}"
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  const AJAX_TYPE = {
4
6
  XHR: "xhr",
5
7
  FETCH: "fetch",
@@ -3594,7 +3596,7 @@ class XhrInterceptor {
3594
3596
  url: resolveUrl(args[1]),
3595
3597
  async: args[2] || true,
3596
3598
  headers: new Headers(),
3597
- body: null,
3599
+ data: null,
3598
3600
  response: () => { },
3599
3601
  };
3600
3602
  hooker.xhrOpenRestArgs = args.slice(2);
@@ -3608,7 +3610,7 @@ class XhrInterceptor {
3608
3610
  send: function (self, target) {
3609
3611
  return async function (body) {
3610
3612
  const hooker = target[CYCLE_SCHEDULER];
3611
- hooker.request.body = body ?? null;
3613
+ hooker.request.data = body ?? null;
3612
3614
  hooker.request.headers = new Headers(mapValues(hooker.xhrSetRequestHeadersAfterOpen, (val) => val.join(', ')));
3613
3615
  const oldRequest = cloneDeep(hooker.request);
3614
3616
  let newRequest = hooker.request;
@@ -3637,7 +3639,7 @@ class XhrInterceptor {
3637
3639
  target.setRequestHeader(key, val);
3638
3640
  });
3639
3641
  }
3640
- self.nativeXhrPrototype.send.apply(target, [hooker.request.body]);
3642
+ self.nativeXhrPrototype.send.apply(target, [hooker.request.data]);
3641
3643
  };
3642
3644
  },
3643
3645
  setRequestHeader: function (self, target) {
@@ -3786,7 +3788,7 @@ class FetchInterceptor {
3786
3788
  let url = '';
3787
3789
  let method = null;
3788
3790
  let headers = null;
3789
- let body = null;
3791
+ let data = null;
3790
3792
  if (typeof req === 'string') {
3791
3793
  url = resolveUrl(req);
3792
3794
  }
@@ -3797,13 +3799,13 @@ class FetchInterceptor {
3797
3799
  url = resolveUrl(req.url);
3798
3800
  method = req.method ?? null;
3799
3801
  headers = req.headers ?? null;
3800
- body = req.body ?? null;
3802
+ data = req.body ?? null;
3801
3803
  }
3802
3804
  return {
3803
3805
  url,
3804
3806
  method,
3805
3807
  headers,
3806
- body,
3808
+ data,
3807
3809
  };
3808
3810
  }
3809
3811
  resolveRequest(req, newRequest) {
@@ -3825,9 +3827,9 @@ class FetchInterceptor {
3825
3827
  return {
3826
3828
  ...(options ? options : {}),
3827
3829
  ...(newRequest.headers ? { headers: newRequest.headers } : {}),
3828
- ...(newRequest.body ? { body: newRequest.body } : {}),
3830
+ ...(newRequest.data ? { body: newRequest.data } : {}),
3829
3831
  ...(newRequest.method ? { method: newRequest.method } : {}),
3830
- ...(newRequest.body instanceof ReadableStream ? streamOptions : {}),
3832
+ ...(newRequest.data instanceof ReadableStream ? streamOptions : {}),
3831
3833
  };
3832
3834
  }
3833
3835
  _generateProxyFetch() {
@@ -3866,7 +3868,7 @@ class FetchInterceptor {
3866
3868
  method: request.method ?? options.method ?? 'GET',
3867
3869
  // TODO: 这里需要处理 headers 的类型
3868
3870
  headers: resolveHeaders(request.headers ?? options.headers ?? null),
3869
- body: request.body ?? options.body ?? null,
3871
+ data: request.data ?? options.body ?? null,
3870
3872
  response: () => { },
3871
3873
  }, self.hooks);
3872
3874
  }
@@ -4078,42 +4080,6 @@ class AjaxInterceptor {
4078
4080
  }
4079
4081
  }
4080
4082
  }
4081
- const ajaxInterceptor = AjaxInterceptor.getInstance();
4082
- ajaxInterceptor.inject();
4083
- let count = 0;
4084
- ajaxInterceptor.hook((request) => {
4085
- console.log(`%c${++count} twices-x200 pbx`, 'color: red', request);
4086
- if (request.url === '/api/outer/ats-apply/website/jobs/v2') {
4087
- const body = JSON.parse(request.body);
4088
- body.keyword = '后端';
4089
- request.body = JSON.stringify(body);
4090
- }
4091
- request.response = async (response) => {
4092
- console.log(response, 'response');
4093
- if (request.url.includes('/portal/searchHome')) {
4094
- const body = JSON.parse(request.body);
4095
- console.log(request.body, 'req');
4096
- if (body.code === 'zjcgCategory103') {
4097
- console.log('12');
4098
- await new Promise((resolve) => setTimeout(resolve, 500));
4099
- }
4100
- // const result = JSON.parse(response.response as string);
4101
- // console.log(result, 'result');
4102
- // result.result.data.children = result.result.data.children?.slice?.(0, 2);
4103
- // response.response = JSON.stringify(result);
4104
- }
4105
- if (request.type === 'fetch') {
4106
- console.log(response.headers, 'response.headers');
4107
- console.log(response.finalUrl, 'finalUrl');
4108
- console.log(`%c fetch Result new`, 'color: purple', response.json);
4109
- const data = response.json.data;
4110
- if (data.length > 0) {
4111
- data[0].name = '草泥马';
4112
- }
4113
- response.json.data = data;
4114
- }
4115
- };
4116
- return request;
4117
- });
4118
4083
 
4119
- module.exports = AjaxInterceptor;
4084
+ exports.AjaxInterceptor = AjaxInterceptor;
4085
+ exports.default = AjaxInterceptor;
@@ -3592,7 +3592,7 @@ class XhrInterceptor {
3592
3592
  url: resolveUrl(args[1]),
3593
3593
  async: args[2] || true,
3594
3594
  headers: new Headers(),
3595
- body: null,
3595
+ data: null,
3596
3596
  response: () => { },
3597
3597
  };
3598
3598
  hooker.xhrOpenRestArgs = args.slice(2);
@@ -3606,7 +3606,7 @@ class XhrInterceptor {
3606
3606
  send: function (self, target) {
3607
3607
  return async function (body) {
3608
3608
  const hooker = target[CYCLE_SCHEDULER];
3609
- hooker.request.body = body ?? null;
3609
+ hooker.request.data = body ?? null;
3610
3610
  hooker.request.headers = new Headers(mapValues(hooker.xhrSetRequestHeadersAfterOpen, (val) => val.join(', ')));
3611
3611
  const oldRequest = cloneDeep(hooker.request);
3612
3612
  let newRequest = hooker.request;
@@ -3635,7 +3635,7 @@ class XhrInterceptor {
3635
3635
  target.setRequestHeader(key, val);
3636
3636
  });
3637
3637
  }
3638
- self.nativeXhrPrototype.send.apply(target, [hooker.request.body]);
3638
+ self.nativeXhrPrototype.send.apply(target, [hooker.request.data]);
3639
3639
  };
3640
3640
  },
3641
3641
  setRequestHeader: function (self, target) {
@@ -3784,7 +3784,7 @@ class FetchInterceptor {
3784
3784
  let url = '';
3785
3785
  let method = null;
3786
3786
  let headers = null;
3787
- let body = null;
3787
+ let data = null;
3788
3788
  if (typeof req === 'string') {
3789
3789
  url = resolveUrl(req);
3790
3790
  }
@@ -3795,13 +3795,13 @@ class FetchInterceptor {
3795
3795
  url = resolveUrl(req.url);
3796
3796
  method = req.method ?? null;
3797
3797
  headers = req.headers ?? null;
3798
- body = req.body ?? null;
3798
+ data = req.body ?? null;
3799
3799
  }
3800
3800
  return {
3801
3801
  url,
3802
3802
  method,
3803
3803
  headers,
3804
- body,
3804
+ data,
3805
3805
  };
3806
3806
  }
3807
3807
  resolveRequest(req, newRequest) {
@@ -3823,9 +3823,9 @@ class FetchInterceptor {
3823
3823
  return {
3824
3824
  ...(options ? options : {}),
3825
3825
  ...(newRequest.headers ? { headers: newRequest.headers } : {}),
3826
- ...(newRequest.body ? { body: newRequest.body } : {}),
3826
+ ...(newRequest.data ? { body: newRequest.data } : {}),
3827
3827
  ...(newRequest.method ? { method: newRequest.method } : {}),
3828
- ...(newRequest.body instanceof ReadableStream ? streamOptions : {}),
3828
+ ...(newRequest.data instanceof ReadableStream ? streamOptions : {}),
3829
3829
  };
3830
3830
  }
3831
3831
  _generateProxyFetch() {
@@ -3864,7 +3864,7 @@ class FetchInterceptor {
3864
3864
  method: request.method ?? options.method ?? 'GET',
3865
3865
  // TODO: 这里需要处理 headers 的类型
3866
3866
  headers: resolveHeaders(request.headers ?? options.headers ?? null),
3867
- body: request.body ?? options.body ?? null,
3867
+ data: request.data ?? options.body ?? null,
3868
3868
  response: () => { },
3869
3869
  }, self.hooks);
3870
3870
  }
@@ -4076,43 +4076,6 @@ class AjaxInterceptor {
4076
4076
  }
4077
4077
  }
4078
4078
  }
4079
- const ajaxInterceptor = AjaxInterceptor.getInstance();
4080
- ajaxInterceptor.inject();
4081
- let count = 0;
4082
- ajaxInterceptor.hook((request) => {
4083
- console.log(`%c${++count} twices-x200 pbx`, 'color: red', request);
4084
- if (request.url === '/api/outer/ats-apply/website/jobs/v2') {
4085
- const body = JSON.parse(request.body);
4086
- body.keyword = '后端';
4087
- request.body = JSON.stringify(body);
4088
- }
4089
- request.response = async (response) => {
4090
- console.log(response, 'response');
4091
- if (request.url.includes('/portal/searchHome')) {
4092
- const body = JSON.parse(request.body);
4093
- console.log(request.body, 'req');
4094
- if (body.code === 'zjcgCategory103') {
4095
- console.log('12');
4096
- await new Promise((resolve) => setTimeout(resolve, 500));
4097
- }
4098
- // const result = JSON.parse(response.response as string);
4099
- // console.log(result, 'result');
4100
- // result.result.data.children = result.result.data.children?.slice?.(0, 2);
4101
- // response.response = JSON.stringify(result);
4102
- }
4103
- if (request.type === 'fetch') {
4104
- console.log(response.headers, 'response.headers');
4105
- console.log(response.finalUrl, 'finalUrl');
4106
- console.log(`%c fetch Result new`, 'color: purple', response.json);
4107
- const data = response.json.data;
4108
- if (data.length > 0) {
4109
- data[0].name = '草泥马';
4110
- }
4111
- response.json.data = data;
4112
- }
4113
- };
4114
- return request;
4115
- });
4116
4079
 
4117
- export { AjaxInterceptor as default };
4080
+ export { AjaxInterceptor, AjaxInterceptor as default };
4118
4081
  //# sourceMappingURL=index.js.map