@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 +3 -3
- package/src/axios.ts +3 -3
- package/src/fetch.ts +128 -135
- package/src/websocket.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetlinks-web/core",
|
|
3
|
-
"version": "2.2.
|
|
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/
|
|
26
|
-
"@jetlinks-web/
|
|
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?: (
|
|
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
|
|
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,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
|
|
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
|
-
|
|
25
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
+
reader = resp.body?.getReader();
|
|
40
|
+
const decoder = new TextDecoder();
|
|
41
|
+
let data_buf = "";
|
|
39
42
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
let data_buf = "";
|
|
43
|
+
if (!reader) {
|
|
44
|
+
observer.error(new Error('No readable stream available'));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
const read = () => {
|
|
47
49
|
|
|
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
|
-
}
|
|
50
|
+
if (!that.isRead) {
|
|
51
|
+
reader.cancel()
|
|
52
|
+
observer.complete();
|
|
53
|
+
return
|
|
54
|
+
}
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
reject(e)
|
|
96
|
-
})
|
|
94
|
+
|
|
95
|
+
return () => {
|
|
96
|
+
that.cancel()
|
|
97
|
+
}
|
|
97
98
|
})
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
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()
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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,
|
|
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';
|