@jetlinks-web/core 2.2.4 → 2.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetlinks-web/core",
3
- "version": "2.2.4",
3
+ "version": "2.2.6",
4
4
  "main": "index.ts",
5
5
  "module": "index.ts",
6
6
  "keywords": [
@@ -22,8 +22,8 @@
22
22
  "axios": "^1.7.4",
23
23
  "rxjs": "^7.8.1",
24
24
  "@jetlinks-web/constants": "^1.0.9",
25
- "@jetlinks-web/types": "^1.0.2",
26
- "@jetlinks-web/utils": "^1.2.8"
25
+ "@jetlinks-web/utils": "^1.2.8",
26
+ "@jetlinks-web/types": "^1.0.2"
27
27
  },
28
28
  "publishConfig": {
29
29
  "registry": "https://registry.npmjs.org/",
package/src/axios.ts CHANGED
@@ -18,7 +18,7 @@ interface Options {
18
18
  code?: number
19
19
  codeKey?: string
20
20
  timeout?: number
21
- handleRequest?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig
21
+ handleRequest?: () => void
22
22
  /**
23
23
  * 用以获取localstorage中的lang
24
24
  */
@@ -357,7 +357,7 @@ export class Request {
357
357
  url: undefined,
358
358
  method: undefined,
359
359
  }) {
360
- const { url=``, method = 'post', ...rest } = options
360
+ const { url=`/_create`, method = 'post', ...rest } = options
361
361
  return request[method](`${this.modulePath}${url}`, data, rest)
362
362
  }
363
363
 
@@ -371,7 +371,7 @@ export class Request {
371
371
  url: undefined,
372
372
  method: undefined,
373
373
  }) {
374
- const { url=``, method = 'patch', ...rest } = options
374
+ const { url=`/_update`, method = 'patch', ...rest } = options
375
375
  return patch(`${this.modulePath}${url}`, data, rest)
376
376
  }
377
377
 
package/src/fetch.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import {getToken} from "@jetlinks-web/utils";
2
2
  import {BASE_API, TOKEN_KEY} from "@jetlinks-web/constants";
3
- import {isFunction, isObject, isString} from "lodash-es";
3
+ import {isFunction, isObject} from "lodash-es";
4
+ import { Observable, } from 'rxjs'
4
5
 
5
6
  const controller = new AbortController();
6
7
 
7
- class NdJson {
8
+ export class NdJson {
8
9
  options: any = {
9
10
  code: 200,
10
11
  codeKey: 'status'
11
12
  }
13
+ isRead = false
12
14
  constructor() {}
13
15
 
14
16
  create(options) {
@@ -20,10 +22,10 @@ class NdJson {
20
22
  }
21
23
 
22
24
  get(url, data = '{}', extra = {}) {
23
-
24
- return new Promise((resolve, reject) => {
25
- const _url = this.getUrl(url)
26
-
25
+ const _url = this.getUrl(url)
26
+ const that = this
27
+ return new Observable(observer => {
28
+ let reader
27
29
  fetch(
28
30
  _url,
29
31
  {
@@ -34,74 +36,74 @@ class NdJson {
34
36
  ...this.handleRequest(_url)
35
37
  }
36
38
  ).then(resp => {
37
- console.log('[fetch GET]>', resp)
38
- let is_reader, cancellationRequest = false;
39
+ reader = resp.body?.getReader();
40
+ const decoder = new TextDecoder();
41
+ let data_buf = "";
39
42
 
40
- const readable = new ReadableStream({
41
- start(controller) {
42
- const reader = resp.body.getReader()
43
- const decoder = new TextDecoder();
44
- let data_buf = "";
43
+ if (!reader) {
44
+ observer.error(new Error('No readable stream available'));
45
+ return;
46
+ }
45
47
 
46
- is_reader = !!reader
48
+ const read = () => {
47
49
 
48
- reader.read().then(function processResult(result) {
49
- if (result.done) {
50
- if (cancellationRequest) {
51
- return
52
- }
53
- data_buf = data_buf.trim()
54
-
55
- if (data_buf.length !== 0) {
56
- try {
57
- controller.enqueue(JSON.parse(data_buf))
58
- } catch (e) {
59
- controller.error(e)
60
- return
61
- }
62
- }
50
+ if (!that.isRead) {
51
+ reader.cancel()
52
+ observer.complete();
53
+ return
54
+ }
63
55
 
64
- controller.close()
65
- return
56
+ reader.read().then(({ done, value }) => {
57
+ if (done) {
58
+ if (data_buf.trim().length > 0) {
59
+ try {
60
+ observer.next(JSON.parse(data_buf.trim()));
61
+ } catch (e) {
62
+ observer.error(e);
63
+ }
66
64
  }
67
-
68
- const data = decoder.decode(result.value, { stream: true })
69
- data_buf += data
70
-
71
- let lines = data_buf.split('/')
72
- for(let i = 0; i < lines.length - 1; ++i) {
73
- const l = lines[i].trim();
74
- if (l.length > 0) {
75
- try {
76
- controller.enqueue(JSON.parse(l));
77
- } catch(e) {
78
- controller.error(e);
79
- cancellationRequest = true;
80
- reader.cancel();
81
- return;
82
- }
65
+ observer.complete();
66
+ return;
67
+ }
68
+
69
+ const data = decoder.decode(value, { stream: true });
70
+ data_buf += data;
71
+
72
+ let lines = data_buf.split('\n');
73
+ for (let i = 0; i < lines.length - 1; ++i) {
74
+ const line = lines[i].trim();
75
+ if (line.length > 0) {
76
+ try {
77
+ observer.next(JSON.parse(line));
78
+ } catch (e) {
79
+ observer.error(e);
80
+ reader.cancel();
81
+ return;
83
82
  }
84
83
  }
85
- })
86
- }
87
- })
88
-
89
- return resp.json()
84
+ }
85
+ data_buf = lines[lines.length - 1];
86
+ read();
87
+ }).catch(err => observer.error(err));
88
+ };
89
+ that.isRead = true
90
+ read();
91
+ }).catch(e => {
92
+ observer.error(e)
90
93
  })
91
- .then((resp) => {
92
- console.log(resp)
93
- })
94
- .catch(e => {
95
- reject(e)
96
- })
94
+
95
+ return () => {
96
+ that.cancel()
97
+ }
97
98
  })
98
99
  }
99
100
 
100
- async post(url, data={}, extra = {}) {
101
-
102
- return new Promise(async (resolve, reject) => {
103
- const _url = this.getUrl(url)
101
+ post(url, data={}, extra = {}) {
102
+ const _url = this.getUrl(url)
103
+ const that = this
104
104
 
105
+ return new Observable(observer => {
106
+ let reader
105
107
  fetch(
106
108
  _url,
107
109
  {
@@ -113,77 +115,65 @@ class NdJson {
113
115
  ...this.handleRequest(_url)
114
116
  }
115
117
  ).then(async resp => {
116
- console.log('[fetch 1]>', resp.headers.get('content-type'))
117
- let is_reader, cancellationRequest = false;
118
-
119
- // return new ReadableStream({
120
- // start(controller) {
121
- // const reader = resp.body.getReader()
122
- // const decoder = new TextDecoder();
123
- // let data_buf = "";
124
- //
125
- // is_reader = !!reader
126
- //
127
- // reader.read().then(function processResult(result) {
128
- // if (result.done) {
129
- // if (cancellationRequest) {
130
- // return
131
- // }
132
- // data_buf = data_buf.trim()
133
- //
134
- // if (data_buf.length !== 0) {
135
- // try {
136
- // controller.enqueue(JSON.parse(data_buf))
137
- // } catch (e) {
138
- // controller.error(e)
139
- // return
140
- // }
141
- // }
142
- //
143
- // controller.close()
144
- // return
145
- // }
146
- //
147
- // const data = decoder.decode(result.value, { stream: true })
148
- // data_buf += data
149
- // console.log(data)
150
- // let lines = data_buf.split('/')
151
- // for(let i = 0; i < lines.length - 1; ++i) {
152
- // const l = lines[i].trim();
153
- // if (l.length > 0) {
154
- // try {
155
- // console.log(l)
156
- // controller.enqueue( isString(l) ? JSON.parse(l) : l);
157
- // } catch(e) {
158
- // controller.error(e);
159
- // cancellationRequest = true;
160
- // reader.cancel();
161
- // return;
162
- // }
163
- // }
164
- // }
165
- // })
166
- // },
167
- // cancel(reason) {
168
- // console.log("Cancel registered due to ", reason);
169
- // cancellationRequest = true;
170
- // is_reader.cancel();
171
- // }
172
- // })
173
- // let msg = await readable.getReader()
174
- // console.log(resp, msg)
175
- return resp.json()
118
+ reader = resp.body?.getReader();
119
+ const decoder = new TextDecoder();
120
+ let data_buf = "";
121
+
122
+ if (!reader) {
123
+ observer.error(new Error('No readable stream available'));
124
+ return;
125
+ }
126
+
127
+ const read = () => {
128
+
129
+ if (!that.isRead) {
130
+ reader.cancel()
131
+ observer.complete();
132
+ return
133
+ }
134
+
135
+ reader.read().then(({ done, value }) => {
136
+ if (done) {
137
+ if (data_buf.trim().length > 0) {
138
+ try {
139
+ observer.next(JSON.parse(data_buf.trim()));
140
+ } catch (e) {
141
+ observer.error(e);
142
+ }
143
+ }
144
+ observer.complete();
145
+ return;
146
+ }
147
+
148
+ const data = decoder.decode(value, { stream: true });
149
+ data_buf += data;
150
+
151
+ let lines = data_buf.split('\n');
152
+ for (let i = 0; i < lines.length - 1; ++i) {
153
+ const line = lines[i].trim();
154
+ if (line.length > 0) {
155
+ try {
156
+ observer.next(JSON.parse(line));
157
+ } catch (e) {
158
+ observer.error(e);
159
+ reader.cancel();
160
+ return;
161
+ }
162
+ }
163
+ }
164
+ data_buf = lines[lines.length - 1];
165
+ read();
166
+ }).catch(err => observer.error(err));
167
+ };
168
+ that.isRead = true
169
+ read();
170
+ }).catch(e => {
171
+ observer.error(e)
176
172
  })
177
- .then((resp) => {
178
- // const reader = resp.getReader();
179
- // reader.read().then(result => {
180
- // console.log(result)
181
- // })
182
- resolve(this.handleResponse(resp))
183
- })
184
- .catch(e => {
185
- reject(e)
186
- })
173
+
174
+ return () => {
175
+ that.cancel()
176
+ }
187
177
  })
188
178
  }
189
179
  handleRequest(url): RequestInit {
@@ -222,14 +212,17 @@ class NdJson {
222
212
  return this.options.handleResponse(response)
223
213
  }
224
214
 
225
- const status = response[this.options.codeKey || 'status']
226
- response.success = status === this.options.code
215
+ // const status = response[this.options.codeKey || 'status']
216
+ // response.success = status === this.options.code
227
217
 
228
218
  return response
229
219
  }
230
220
 
231
221
  cancel() {
232
-
222
+ if (this.isRead) {
223
+ this.isRead = false
224
+ }
225
+ controller.abort()
233
226
  }
234
227
  }
235
228
 
package/src/websocket.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
1
+ import { webSocket, } from 'rxjs/webSocket';
2
+ import type { WebSocketSubject } from 'rxjs/webSocket';
2
3
  import { Observable, Subject, timer, Subscription, EMPTY } from 'rxjs';
3
4
  import { retry, catchError } from 'rxjs/operators';
4
5
  import { notification } from 'ant-design-vue';