nayota-show-sdk 1.3.63 → 1.3.65
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/http-config-iot.test.js +46 -0
- package/iot-adapter-helpers.test.js +30 -0
- package/iot-module-specs.test.js +72 -0
- package/package.json +1 -1
- package/utils/http-config-iot.js +19 -2
- package/utils/http-factory.js +12 -3
- package/utils/iot-adapter-helpers.js +8 -7
- package/utils/iot-module-specs.js +8 -2
package/http-config-iot.test.js
CHANGED
|
@@ -46,4 +46,50 @@ describe('httpConfigIot', () => {
|
|
|
46
46
|
}
|
|
47
47
|
})
|
|
48
48
|
})
|
|
49
|
+
|
|
50
|
+
test('normalizes any 2xx iot code into legacy success code 0', () => {
|
|
51
|
+
const http = httpConfigIot()
|
|
52
|
+
const responseInterceptor = http.interceptors.response.use.mock.calls[0][0]
|
|
53
|
+
|
|
54
|
+
expect(
|
|
55
|
+
responseInterceptor({
|
|
56
|
+
status: 201,
|
|
57
|
+
headers: {},
|
|
58
|
+
data: {
|
|
59
|
+
code: 201,
|
|
60
|
+
data: {
|
|
61
|
+
id: 'space-2'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
).toEqual({
|
|
66
|
+
code: 0,
|
|
67
|
+
data: {
|
|
68
|
+
id: 'space-2'
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('accepts string based 2xx iot codes as legacy success too', () => {
|
|
74
|
+
const http = httpConfigIot()
|
|
75
|
+
const responseInterceptor = http.interceptors.response.use.mock.calls[0][0]
|
|
76
|
+
|
|
77
|
+
expect(
|
|
78
|
+
responseInterceptor({
|
|
79
|
+
status: 204,
|
|
80
|
+
headers: {},
|
|
81
|
+
data: {
|
|
82
|
+
code: '204',
|
|
83
|
+
data: {
|
|
84
|
+
id: 'space-3'
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
).toEqual({
|
|
89
|
+
code: 0,
|
|
90
|
+
data: {
|
|
91
|
+
id: 'space-3'
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
})
|
|
49
95
|
})
|
|
@@ -26,4 +26,34 @@ describe('iot adapter helpers', () => {
|
|
|
26
26
|
}
|
|
27
27
|
})
|
|
28
28
|
})
|
|
29
|
+
|
|
30
|
+
test('unwraps nested iot data.items responses before mapping to legacy rows', () => {
|
|
31
|
+
const result = buildLegacyListResponse({
|
|
32
|
+
code: 0,
|
|
33
|
+
success: true,
|
|
34
|
+
data: {
|
|
35
|
+
total: 1,
|
|
36
|
+
items: [
|
|
37
|
+
{
|
|
38
|
+
id: 'depart-1',
|
|
39
|
+
name: '研发园B区'
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
expect(result).toEqual({
|
|
46
|
+
code: 0,
|
|
47
|
+
data: {
|
|
48
|
+
total: 1,
|
|
49
|
+
rows: [
|
|
50
|
+
{
|
|
51
|
+
id: 'depart-1',
|
|
52
|
+
_id: 'depart-1',
|
|
53
|
+
name: '研发园B区'
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
})
|
|
29
59
|
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { iotModuleSpecs } from './utils/iot-module-specs'
|
|
2
|
+
|
|
3
|
+
describe('iot module specs', () => {
|
|
4
|
+
test('devices.easyList unwraps nested data.rows payloads', () => {
|
|
5
|
+
const response = iotModuleSpecs.devices.operations.easyList.fromResponse({
|
|
6
|
+
code: 0,
|
|
7
|
+
success: true,
|
|
8
|
+
data: {
|
|
9
|
+
total: 2,
|
|
10
|
+
rows: [
|
|
11
|
+
{
|
|
12
|
+
id: 'device-1',
|
|
13
|
+
name: '风速仪'
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
id: 'device-2',
|
|
17
|
+
name: '温湿度传感器'
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
expect(response).toEqual({
|
|
24
|
+
code: 0,
|
|
25
|
+
data: {
|
|
26
|
+
total: 2,
|
|
27
|
+
rows: [
|
|
28
|
+
{
|
|
29
|
+
id: 'device-1',
|
|
30
|
+
_id: 'device-1',
|
|
31
|
+
name: '风速仪'
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'device-2',
|
|
35
|
+
_id: 'device-2',
|
|
36
|
+
name: '温湿度传感器'
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('devices.easyList also supports nested data.items payloads', () => {
|
|
44
|
+
const response = iotModuleSpecs.devices.operations.easyList.fromResponse({
|
|
45
|
+
code: 0,
|
|
46
|
+
success: true,
|
|
47
|
+
data: {
|
|
48
|
+
total: 1,
|
|
49
|
+
items: [
|
|
50
|
+
{
|
|
51
|
+
id: 'device-3',
|
|
52
|
+
name: '层级设备'
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
expect(response).toEqual({
|
|
59
|
+
code: 0,
|
|
60
|
+
data: {
|
|
61
|
+
total: 1,
|
|
62
|
+
rows: [
|
|
63
|
+
{
|
|
64
|
+
id: 'device-3',
|
|
65
|
+
_id: 'device-3',
|
|
66
|
+
name: '层级设备'
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
})
|
package/package.json
CHANGED
package/utils/http-config-iot.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import urlcfg from '../config/urlcfg'
|
|
2
2
|
import createHttpInstance from './http-factory'
|
|
3
3
|
|
|
4
|
+
function isLegacySuccessCode(code) {
|
|
5
|
+
if (typeof code === 'number') {
|
|
6
|
+
return code >= 200 && code < 300
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (typeof code === 'string' && /^2\d{2}$/.test(code)) {
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
|
|
4
16
|
function normalizeIotResponseData(data) {
|
|
5
17
|
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
6
18
|
return data
|
|
7
19
|
}
|
|
8
20
|
|
|
9
|
-
if (data.code
|
|
21
|
+
if (isLegacySuccessCode(data.code)) {
|
|
10
22
|
return {
|
|
11
23
|
...data,
|
|
12
24
|
code: 0
|
|
@@ -16,10 +28,15 @@ function normalizeIotResponseData(data) {
|
|
|
16
28
|
return data
|
|
17
29
|
}
|
|
18
30
|
|
|
31
|
+
function isIotSuccessResponseData(data) {
|
|
32
|
+
return data && typeof data === 'object' && data.code === 0
|
|
33
|
+
}
|
|
34
|
+
|
|
19
35
|
export default function httpConfigIot() {
|
|
20
36
|
return createHttpInstance({
|
|
21
37
|
getBaseUrl: () => urlcfg.getIotUrl(),
|
|
22
38
|
contentType: 'application/json;charset=UTF-8',
|
|
23
|
-
normalizeResponseData: normalizeIotResponseData
|
|
39
|
+
normalizeResponseData: normalizeIotResponseData,
|
|
40
|
+
isSuccessResponseData: isIotSuccessResponseData
|
|
24
41
|
})
|
|
25
42
|
}
|
package/utils/http-factory.js
CHANGED
|
@@ -4,10 +4,15 @@ import axios from 'axios'
|
|
|
4
4
|
import urlcfg from '../config/urlcfg'
|
|
5
5
|
import emitter from './EventEmitter'
|
|
6
6
|
|
|
7
|
+
function isSuccessStatus(status) {
|
|
8
|
+
return typeof status === 'number' && status >= 200 && status < 300
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
function createHttpInstance({
|
|
8
12
|
getBaseUrl,
|
|
9
13
|
contentType,
|
|
10
|
-
normalizeResponseData
|
|
14
|
+
normalizeResponseData,
|
|
15
|
+
isSuccessResponseData
|
|
11
16
|
}) {
|
|
12
17
|
const http = axios.create({
|
|
13
18
|
baseURL: getBaseUrl(),
|
|
@@ -53,8 +58,12 @@ function createHttpInstance({
|
|
|
53
58
|
return res
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
if (res.status
|
|
57
|
-
|
|
61
|
+
if (isSuccessStatus(res.status)) {
|
|
62
|
+
const success = typeof isSuccessResponseData === 'function'
|
|
63
|
+
? isSuccessResponseData(data, res)
|
|
64
|
+
: data.code === 0
|
|
65
|
+
|
|
66
|
+
if (!success) {
|
|
58
67
|
return Promise.reject(data)
|
|
59
68
|
}
|
|
60
69
|
return data
|
|
@@ -199,18 +199,19 @@ export function parseLegacySort(sortValue, sortFieldMap = {}) {
|
|
|
199
199
|
|
|
200
200
|
export function buildLegacyListResponse(response, mapItem) {
|
|
201
201
|
const payload = extractResponsePayload(response)
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
202
|
+
const data = payload?.data !== undefined ? payload.data : payload
|
|
203
|
+
const items = Array.isArray(data?.items)
|
|
204
|
+
? data.items
|
|
205
|
+
: Array.isArray(data?.rows)
|
|
206
|
+
? data.rows
|
|
207
|
+
: Array.isArray(data)
|
|
208
|
+
? data
|
|
208
209
|
: []
|
|
209
210
|
|
|
210
211
|
return {
|
|
211
212
|
code: 0,
|
|
212
213
|
data: {
|
|
213
|
-
total: Number(
|
|
214
|
+
total: Number(data?.total ?? items.length ?? 0),
|
|
214
215
|
rows: items.map(item => (mapItem ? mapItem(item) : withLegacyId(item)))
|
|
215
216
|
}
|
|
216
217
|
}
|
|
@@ -503,11 +503,17 @@ export const iotModuleSpecs = {
|
|
|
503
503
|
},
|
|
504
504
|
fromResponse(response) {
|
|
505
505
|
const payload = response && response.data && response.status ? response.data : response
|
|
506
|
+
const data = payload?.data !== undefined ? payload.data : payload
|
|
507
|
+
const rows = Array.isArray(data?.rows)
|
|
508
|
+
? data.rows
|
|
509
|
+
: Array.isArray(data?.items)
|
|
510
|
+
? data.items
|
|
511
|
+
: []
|
|
506
512
|
return {
|
|
507
513
|
code: 0,
|
|
508
514
|
data: {
|
|
509
|
-
total: Number(
|
|
510
|
-
rows:
|
|
515
|
+
total: Number(data?.total ?? rows.length ?? 0),
|
|
516
|
+
rows: rows.map(item => withLegacyId(item))
|
|
511
517
|
}
|
|
512
518
|
}
|
|
513
519
|
}
|