@blueking/chat-helper 0.0.1-beta.32 → 0.0.1-beta.34

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.
@@ -22,33 +22,82 @@
22
22
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23
23
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
24
  * IN THE SOFTWARE.
25
- */ import { FetchClient } from './fetch.ts.js';
26
- export const useFetch = (options)=>{
25
+ */ function _define_property(obj, key, value) {
26
+ if (key in obj) {
27
+ Object.defineProperty(obj, key, {
28
+ value: value,
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true
32
+ });
33
+ } else {
34
+ obj[key] = value;
35
+ }
36
+ return obj;
37
+ }
38
+ function _object_spread(target) {
39
+ for(var i = 1; i < arguments.length; i++){
40
+ var source = arguments[i] != null ? arguments[i] : {};
41
+ var ownKeys = Object.keys(source);
42
+ if (typeof Object.getOwnPropertySymbols === "function") {
43
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
44
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
45
+ }));
46
+ }
47
+ ownKeys.forEach(function(key) {
48
+ _define_property(target, key, source[key]);
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ import { FetchClient } from './fetch.ts.js';
54
+ /**
55
+ * 注册所有拦截器(内部 requestData 注入 + 用户自定义)
56
+ * 抽取为独立函数,避免 useFetch 和 reset 中重复逻辑
57
+ */ function applyInterceptors(fetchClient, options) {
27
58
  var _options_interceptors, _options_interceptors1;
28
- const fetchClient = new FetchClient({
29
- baseURL: options.requestData.urlPrefix
30
- });
31
- // 用户定义拦截器
59
+ const { headers: customHeaders, data: customData } = options.requestData;
60
+ if (customHeaders || customData) {
61
+ fetchClient.interceptors.request.use((config)=>{
62
+ if (customHeaders) {
63
+ const resolved = typeof customHeaders === 'function' ? customHeaders() : customHeaders;
64
+ const existing = typeof config.headers === 'function' ? config.headers() : config.headers || {};
65
+ config.headers = _object_spread({}, existing, resolved);
66
+ }
67
+ const method = (config.method || 'GET').toUpperCase();
68
+ const supportsBody = ![
69
+ 'GET',
70
+ 'HEAD'
71
+ ].includes(method);
72
+ if (customData && supportsBody) {
73
+ const resolved = typeof customData === 'function' ? customData() : customData;
74
+ const existingData = typeof config.data === 'function' ? config.data() : config.data;
75
+ if (existingData && typeof existingData === 'object' && !Array.isArray(existingData)) {
76
+ config.data = _object_spread({}, resolved, existingData);
77
+ } else if (!existingData) {
78
+ config.data = resolved;
79
+ }
80
+ }
81
+ return config;
82
+ });
83
+ }
32
84
  if ((_options_interceptors = options.interceptors) === null || _options_interceptors === void 0 ? void 0 : _options_interceptors.request) {
33
85
  fetchClient.interceptors.request.use(options.interceptors.request);
34
86
  }
35
87
  if ((_options_interceptors1 = options.interceptors) === null || _options_interceptors1 === void 0 ? void 0 : _options_interceptors1.response) {
36
88
  fetchClient.interceptors.response.use(options.interceptors.response);
37
89
  }
38
- // 重置 fetchClient 配置
90
+ }
91
+ export const useFetch = (options)=>{
92
+ const fetchClient = new FetchClient({
93
+ baseURL: options.requestData.urlPrefix
94
+ });
95
+ applyInterceptors(fetchClient, options);
39
96
  const reset = (newOptions)=>{
40
- var _newOptions_interceptors, _newOptions_interceptors1;
41
- // 更新 baseURL
42
97
  fetchClient.defaults.baseURL = newOptions.requestData.urlPrefix;
43
- // 清空并重新注册拦截器
44
98
  fetchClient.interceptors.request.clear();
45
99
  fetchClient.interceptors.response.clear();
46
- if ((_newOptions_interceptors = newOptions.interceptors) === null || _newOptions_interceptors === void 0 ? void 0 : _newOptions_interceptors.request) {
47
- fetchClient.interceptors.request.use(newOptions.interceptors.request);
48
- }
49
- if ((_newOptions_interceptors1 = newOptions.interceptors) === null || _newOptions_interceptors1 === void 0 ? void 0 : _newOptions_interceptors1.response) {
50
- fetchClient.interceptors.response.use(newOptions.interceptors.response);
51
- }
100
+ applyInterceptors(fetchClient, newOptions);
52
101
  };
53
102
  return {
54
103
  fetchClient,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/chat-helper",
3
- "version": "0.0.1-beta.32",
3
+ "version": "0.0.1-beta.34",
4
4
  "description": "",
5
5
  "main": "./dist/index.ts.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,4 +25,4 @@
25
25
  "typescript": "^5.5.4",
26
26
  "vue-tsc": "^3.1.4"
27
27
  }
28
- }
28
+ }