@jetlinks-web/core 2.2.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetlinks-web/core",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "main": "index.ts",
5
5
  "module": "index.ts",
6
6
  "keywords": [
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,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 Promise((resolve, reject) => {
25
- const _url = this.getUrl(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
- console.log('[fetch GET]>', resp)
38
- let is_reader, cancellationRequest = false;
40
+ const reader = resp.body?.getReader();
41
+ const decoder = new TextDecoder();
42
+ let data_buf = "";
39
43
 
40
- const readable = new ReadableStream({
41
- start(controller) {
42
- const reader = resp.body.getReader()
43
- const decoder = new TextDecoder();
44
- let data_buf = "";
44
+ if (!reader) {
45
+ observer.error(new Error('No readable stream available'));
46
+ return;
47
+ }
45
48
 
46
- is_reader = !!reader
49
+ const read = () => {
47
50
 
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
- }
51
+ if (!that.isRead) {
52
+ reader.cancel()
53
+ observer.complete();
54
+ return
55
+ }
63
56
 
64
- controller.close()
65
- return
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
- 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
- }
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
- return resp.json()
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
- async post(url, data={}, extra = {}) {
101
-
102
- return new Promise(async (resolve, reject) => {
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
- 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()
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, 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';