@jetlinks-web/core 2.2.3 → 2.2.5
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 +1 -1
- package/src/axios.ts +2 -2
- package/src/fetch.ts +118 -132
- package/src/websocket.ts +2 -1
package/package.json
CHANGED
package/src/axios.ts
CHANGED
|
@@ -357,7 +357,7 @@ export class Request {
|
|
|
357
357
|
url: undefined,
|
|
358
358
|
method: undefined,
|
|
359
359
|
}) {
|
|
360
|
-
const { url
|
|
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
|
|
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,6 +1,7 @@
|
|
|
1
1
|
import {getToken} from "@jetlinks-web/utils";
|
|
2
2
|
import {BASE_API, TOKEN_KEY} from "@jetlinks-web/constants";
|
|
3
3
|
import {isFunction, isObject, isString} from "lodash-es";
|
|
4
|
+
import { Observable, } from 'rxjs'
|
|
4
5
|
|
|
5
6
|
const controller = new AbortController();
|
|
6
7
|
|
|
@@ -9,6 +10,7 @@ class NdJson {
|
|
|
9
10
|
code: 200,
|
|
10
11
|
codeKey: 'status'
|
|
11
12
|
}
|
|
13
|
+
isRead = false
|
|
12
14
|
constructor() {}
|
|
13
15
|
|
|
14
16
|
create(options) {
|
|
@@ -20,9 +22,10 @@ class NdJson {
|
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
get(url, data = '{}', extra = {}) {
|
|
23
|
-
|
|
24
|
-
return new
|
|
25
|
-
const _url =
|
|
25
|
+
const that = this
|
|
26
|
+
return new Observable(observer => {
|
|
27
|
+
const _url = that.getUrl(url)
|
|
28
|
+
const that = this
|
|
26
29
|
|
|
27
30
|
fetch(
|
|
28
31
|
_url,
|
|
@@ -34,74 +37,69 @@ class NdJson {
|
|
|
34
37
|
...this.handleRequest(_url)
|
|
35
38
|
}
|
|
36
39
|
).then(resp => {
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
const reader = resp.body?.getReader();
|
|
41
|
+
const decoder = new TextDecoder();
|
|
42
|
+
let data_buf = "";
|
|
39
43
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
let data_buf = "";
|
|
44
|
+
if (!reader) {
|
|
45
|
+
observer.error(new Error('No readable stream available'));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
45
48
|
|
|
46
|
-
|
|
49
|
+
const read = () => {
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
}
|
|
51
|
+
if (!that.isRead) {
|
|
52
|
+
reader.cancel()
|
|
53
|
+
observer.complete();
|
|
54
|
+
return
|
|
55
|
+
}
|
|
63
56
|
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
reader.read().then(({ done, value }) => {
|
|
58
|
+
if (done) {
|
|
59
|
+
if (data_buf.trim().length > 0) {
|
|
60
|
+
try {
|
|
61
|
+
observer.next(JSON.parse(data_buf.trim()));
|
|
62
|
+
} catch (e) {
|
|
63
|
+
observer.error(e);
|
|
64
|
+
}
|
|
66
65
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
66
|
+
observer.complete();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const data = decoder.decode(value, { stream: true });
|
|
71
|
+
data_buf += data;
|
|
72
|
+
|
|
73
|
+
let lines = data_buf.split('\n');
|
|
74
|
+
for (let i = 0; i < lines.length - 1; ++i) {
|
|
75
|
+
const line = lines[i].trim();
|
|
76
|
+
if (line.length > 0) {
|
|
77
|
+
try {
|
|
78
|
+
observer.next(JSON.parse(line));
|
|
79
|
+
} catch (e) {
|
|
80
|
+
observer.error(e);
|
|
81
|
+
reader.cancel();
|
|
82
|
+
return;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
}
|
|
86
|
+
data_buf = lines[lines.length - 1];
|
|
87
|
+
read();
|
|
88
|
+
}).catch(err => observer.error(err));
|
|
89
|
+
};
|
|
90
|
+
that.isRead = true
|
|
91
|
+
read();
|
|
92
|
+
}).catch(e => {
|
|
93
|
+
observer.error(e)
|
|
90
94
|
})
|
|
91
|
-
.then((resp) => {
|
|
92
|
-
console.log(resp)
|
|
93
|
-
})
|
|
94
|
-
.catch(e => {
|
|
95
|
-
reject(e)
|
|
96
|
-
})
|
|
97
95
|
})
|
|
98
96
|
}
|
|
99
97
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const _url = this.getUrl(url)
|
|
98
|
+
post(url, data={}, extra = {}) {
|
|
99
|
+
const _url = this.getUrl(url)
|
|
100
|
+
const that = this
|
|
104
101
|
|
|
102
|
+
return new Observable(observer => {
|
|
105
103
|
fetch(
|
|
106
104
|
_url,
|
|
107
105
|
{
|
|
@@ -113,77 +111,61 @@ class NdJson {
|
|
|
113
111
|
...this.handleRequest(_url)
|
|
114
112
|
}
|
|
115
113
|
).then(async resp => {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// is_reader.cancel();
|
|
171
|
-
// }
|
|
172
|
-
// })
|
|
173
|
-
// let msg = await readable.getReader()
|
|
174
|
-
// console.log(resp, msg)
|
|
175
|
-
return resp.json()
|
|
114
|
+
const reader = resp.body?.getReader();
|
|
115
|
+
const decoder = new TextDecoder();
|
|
116
|
+
let data_buf = "";
|
|
117
|
+
|
|
118
|
+
if (!reader) {
|
|
119
|
+
observer.error(new Error('No readable stream available'));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const read = () => {
|
|
124
|
+
|
|
125
|
+
if (!that.isRead) {
|
|
126
|
+
reader.cancel()
|
|
127
|
+
observer.complete();
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
reader.read().then(({ done, value }) => {
|
|
132
|
+
if (done) {
|
|
133
|
+
if (data_buf.trim().length > 0) {
|
|
134
|
+
try {
|
|
135
|
+
observer.next(JSON.parse(data_buf.trim()));
|
|
136
|
+
} catch (e) {
|
|
137
|
+
observer.error(e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
observer.complete();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const data = decoder.decode(value, { stream: true });
|
|
145
|
+
data_buf += data;
|
|
146
|
+
|
|
147
|
+
let lines = data_buf.split('\n');
|
|
148
|
+
for (let i = 0; i < lines.length - 1; ++i) {
|
|
149
|
+
const line = lines[i].trim();
|
|
150
|
+
if (line.length > 0) {
|
|
151
|
+
try {
|
|
152
|
+
observer.next(JSON.parse(line));
|
|
153
|
+
} catch (e) {
|
|
154
|
+
observer.error(e);
|
|
155
|
+
reader.cancel();
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
data_buf = lines[lines.length - 1];
|
|
161
|
+
read();
|
|
162
|
+
}).catch(err => observer.error(err));
|
|
163
|
+
};
|
|
164
|
+
that.isRead = true
|
|
165
|
+
read();
|
|
166
|
+
}).catch(e => {
|
|
167
|
+
observer.error(e)
|
|
176
168
|
})
|
|
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
|
-
})
|
|
187
169
|
})
|
|
188
170
|
}
|
|
189
171
|
handleRequest(url): RequestInit {
|
|
@@ -222,14 +204,18 @@ class NdJson {
|
|
|
222
204
|
return this.options.handleResponse(response)
|
|
223
205
|
}
|
|
224
206
|
|
|
225
|
-
const status = response[this.options.codeKey || 'status']
|
|
226
|
-
response.success = status === this.options.code
|
|
207
|
+
// const status = response[this.options.codeKey || 'status']
|
|
208
|
+
// response.success = status === this.options.code
|
|
227
209
|
|
|
228
210
|
return response
|
|
229
211
|
}
|
|
230
212
|
|
|
231
213
|
cancel() {
|
|
232
|
-
|
|
214
|
+
if (this.isRead) {
|
|
215
|
+
this.isRead = false
|
|
216
|
+
} else {
|
|
217
|
+
controller.abort()
|
|
218
|
+
}
|
|
233
219
|
}
|
|
234
220
|
}
|
|
235
221
|
|
package/src/websocket.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { 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';
|