@blueking/ai-ui-sdk 0.0.15-beta.1 → 0.0.15-beta.2

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,4 @@
1
+ import { EventType } from '../types/enum';
2
+ export declare const addEventListener: (event: EventType, callback: (...args: unknown[]) => void) => void;
3
+ export declare const removeEventListener: (event: EventType, callback: (...args: unknown[]) => void) => void;
4
+ export declare const emitEvent: (event: EventType, ...args: unknown[]) => void;
@@ -0,0 +1,17 @@
1
+ import { EventType } from '../types/enum.js';
2
+ // 全局事件
3
+ const events = {
4
+ [EventType.Error]: []
5
+ };
6
+ export const addEventListener = (event, callback)=>{
7
+ events[event] = events[event] || [];
8
+ events[event].push(callback);
9
+ };
10
+ export const removeEventListener = (event, callback)=>{
11
+ events[event] = events[event].filter((e)=>e !== callback);
12
+ };
13
+ export const emitEvent = (event, ...args)=>{
14
+ events[event].forEach((e)=>{
15
+ e(...args);
16
+ });
17
+ };
@@ -1,6 +1,7 @@
1
+ import { emitEvent } from '../../event/index.js';
2
+ import { EventType } from '../../types/enum.js';
1
3
  // 请求执行失败拦截器
2
4
  export default ((error, config)=>{
3
- const { code, message, response } = error;
4
- console.log('error', code, message, response);
5
+ emitEvent(EventType.Error, error, config);
5
6
  return Promise.reject(error);
6
7
  });
@@ -0,0 +1,182 @@
1
+ /* eslint-disable no-param-reassign */ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _async_to_generator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ import errorInterceptor from './error-interceptor.js';
31
+ import RequestError from './request-error.js';
32
+ import successInterceptor from './success-interceptor.js';
33
+ import { deepMerge } from '../../common/util.js';
34
+ // Content-Type
35
+ const contentTypeMap = {
36
+ json: 'application/json',
37
+ text: 'text/plain',
38
+ formData: 'multipart/form-data'
39
+ };
40
+ const methodsWithoutData = [
41
+ 'delete',
42
+ 'get',
43
+ 'head',
44
+ 'options'
45
+ ];
46
+ const methodsWithData = [
47
+ 'post',
48
+ 'put',
49
+ 'patch'
50
+ ];
51
+ const allMethods = [
52
+ ...methodsWithoutData,
53
+ ...methodsWithData
54
+ ];
55
+ // 拼装发送请求配置
56
+ const getFetchConfig = (method, payload, config)=>{
57
+ const headers = {
58
+ 'X-Requested-With': 'fetch'
59
+ };
60
+ if (config.requestType !== 'formData') {
61
+ headers['Content-Type'] = contentTypeMap[config.requestType] || 'application/json';
62
+ }
63
+ if (!config.withoutSpace) {
64
+ headers['X-SPACE-ID'] = localStorage.getItem('spaceId') || '';
65
+ }
66
+ // 合并配置
67
+ let fetchConfig = deepMerge({
68
+ method: method.toLocaleUpperCase(),
69
+ mode: 'cors',
70
+ cache: 'default',
71
+ credentials: 'include',
72
+ headers,
73
+ redirect: 'follow',
74
+ referrerPolicy: 'no-referrer-when-downgrade',
75
+ responseType: 'json',
76
+ globalError: true
77
+ }, config);
78
+ // merge payload
79
+ if (methodsWithData.includes(method)) {
80
+ fetchConfig = deepMerge(fetchConfig, {
81
+ body: config.requestType === 'formData' ? payload : JSON.stringify(payload)
82
+ });
83
+ } else {
84
+ fetchConfig = deepMerge(fetchConfig, payload);
85
+ }
86
+ return fetchConfig;
87
+ };
88
+ // 拼装发送请求 url
89
+ const getFetchUrl = (url, method, payload = {})=>{
90
+ try {
91
+ // 基础 url
92
+ const baseUrl = location.origin;
93
+ // 构造 url 对象
94
+ const urlObject = new URL(url, baseUrl);
95
+ // add path
96
+ const configPath = window.SITE_URL;
97
+ const subPath = configPath.endsWith('/') ? configPath.slice(0, -1) : configPath;
98
+ urlObject.pathname = subPath + urlObject.pathname;
99
+ // get 请求需要将参数拼接到url上
100
+ // if (methodsWithoutData.includes(method)) {
101
+ // urlObject.search = qs.stringify(payload);
102
+ // }
103
+ // if (methodsWithoutData.includes(method)) {
104
+ // Object.keys(payload).forEach((key) => {
105
+ // const value = payload[key];
106
+ // if (!['', undefined, null].includes(value)) {
107
+ // urlObject.searchParams.append(key, value);
108
+ // }
109
+ // });
110
+ // }
111
+ if (methodsWithoutData.includes(method)) {
112
+ const appendSearchParams = (key, value)=>{
113
+ if (![
114
+ '',
115
+ undefined,
116
+ null
117
+ ].includes(value)) {
118
+ urlObject.searchParams.append(key, value);
119
+ }
120
+ };
121
+ // /api/cr/bkaidev/intelligence/code_review/mine_list/ 接口 status 参数需要 status=xx&&status=yy 此种方式接收
122
+ // 这里单独对此做处理,需要在参数中加上 noEncodeURI 标识,这种处理的缺陷是如果参数本身就有 noEncodeURI 就会出现问题,待处理
123
+ const isNoEncodURI = !!payload.noEncodeURI;
124
+ delete payload.noEncodeURI;
125
+ Object.keys(payload).forEach((key)=>{
126
+ const value = payload[key];
127
+ if (isNoEncodURI) {
128
+ if (Object.prototype.toString.call(value) === '[object Array]') {
129
+ value.forEach((item)=>{
130
+ appendSearchParams(key, item);
131
+ });
132
+ } else {
133
+ appendSearchParams(key, value);
134
+ }
135
+ } else {
136
+ appendSearchParams(key, value);
137
+ }
138
+ });
139
+ }
140
+ return urlObject.href;
141
+ } catch (error) {
142
+ throw new RequestError(-1, error.message);
143
+ }
144
+ };
145
+ // 在自定义对象 http 上添加各请求方法
146
+ const http = {};
147
+ allMethods.forEach((method)=>{
148
+ Object.defineProperty(http, method, {
149
+ get () {
150
+ return function() {
151
+ var _ref = _async_to_generator(function*(url, payload, config = {}) {
152
+ const fetchConfig = getFetchConfig(method, payload, config);
153
+ try {
154
+ const fetchUrl = getFetchUrl(url, method, payload);
155
+ const response = yield fetch(fetchUrl, fetchConfig);
156
+ return yield successInterceptor(response, fetchConfig);
157
+ } catch (err) {
158
+ return errorInterceptor(err, fetchConfig);
159
+ }
160
+ });
161
+ return function(url, payload) {
162
+ return _ref.apply(this, arguments);
163
+ };
164
+ }();
165
+ }
166
+ });
167
+ });
168
+ // 通过 withAbort 包装 http 方法,使其支持 abort
169
+ const withAbort = (fn)=>{
170
+ let controller = null;
171
+ const fnWithsignal = (...args)=>{
172
+ controller = new AbortController();
173
+ return fn(controller.signal, ...args);
174
+ };
175
+ fnWithsignal.abort = ()=>{
176
+ var _controller_abort;
177
+ return controller === null || controller === void 0 ? void 0 : (_controller_abort = controller.abort) === null || _controller_abort === void 0 ? void 0 : _controller_abort.call(controller);
178
+ };
179
+ return fnWithsignal;
180
+ };
181
+ export default http;
182
+ export { withAbort };
package/dist/main.d.ts CHANGED
@@ -13,5 +13,6 @@ export * from './hooks/use-chat';
13
13
  export * from './hooks/use-summary';
14
14
  export * from './hooks/use-style';
15
15
  export * from './hooks/use-http';
16
+ export * from './event';
16
17
  import fetch from '@/http/fetch';
17
18
  export { fetch };
package/dist/main.js CHANGED
@@ -17,6 +17,8 @@ export * from './hooks/use-chat.js';
17
17
  export * from './hooks/use-summary.js';
18
18
  export * from './hooks/use-style.js';
19
19
  export * from './hooks/use-http.js';
20
+ // event
21
+ export * from './event/index.js';
20
22
  // http
21
23
  import fetch from './http/fetch/index.js';
22
24
  export { fetch };
@@ -88,3 +88,6 @@ export declare enum FileLinkType {
88
88
  External = "external",
89
89
  BkRepo = "bk_repo"
90
90
  }
91
+ export declare enum EventType {
92
+ Error = "error"
93
+ }
@@ -99,3 +99,7 @@ export var FileLinkType;
99
99
  FileLinkType["External"] = "external";
100
100
  FileLinkType["BkRepo"] = "bk_repo";
101
101
  })(FileLinkType || (FileLinkType = {}));
102
+ export var EventType;
103
+ (function(EventType) {
104
+ EventType["Error"] = "error";
105
+ })(EventType || (EventType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/ai-ui-sdk",
3
- "version": "0.0.15-beta.1",
3
+ "version": "0.0.15-beta.2",
4
4
  "description": "蓝鲸AI UI SDK",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",