nayota-show-sdk 1.3.61 → 1.3.63
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 +49 -0
- package/index.test.js +21 -32
- package/iot-adapter-helpers.test.js +29 -0
- package/package.json +1 -1
- package/utils/http-config-iot.js +17 -1
- package/utils/http-factory.js +10 -2
- package/utils/iot-adapter-helpers.js +4 -2
- package/utils/iot-module-specs.js +4 -2
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import httpConfigIot from './utils/http-config-iot'
|
|
2
|
+
|
|
3
|
+
jest.mock('axios', () => {
|
|
4
|
+
const create = jest.fn(() => {
|
|
5
|
+
const instance = jest.fn()
|
|
6
|
+
instance.interceptors = {
|
|
7
|
+
request: { use: jest.fn() },
|
|
8
|
+
response: { use: jest.fn() }
|
|
9
|
+
}
|
|
10
|
+
return instance
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
__esModule: true,
|
|
15
|
+
default: {
|
|
16
|
+
create,
|
|
17
|
+
defaults: {
|
|
18
|
+
headers: {
|
|
19
|
+
common: {}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('httpConfigIot', () => {
|
|
27
|
+
test('normalizes iot success code 200 into legacy success code 0', () => {
|
|
28
|
+
const http = httpConfigIot()
|
|
29
|
+
const responseInterceptor = http.interceptors.response.use.mock.calls[0][0]
|
|
30
|
+
|
|
31
|
+
expect(
|
|
32
|
+
responseInterceptor({
|
|
33
|
+
status: 200,
|
|
34
|
+
headers: {},
|
|
35
|
+
data: {
|
|
36
|
+
code: 200,
|
|
37
|
+
data: {
|
|
38
|
+
id: 'space-1'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
).toEqual({
|
|
43
|
+
code: 0,
|
|
44
|
+
data: {
|
|
45
|
+
id: 'space-1'
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
})
|
package/index.test.js
CHANGED
|
@@ -1,38 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
beforeAll(() => {
|
|
3
|
-
jest.mock('./__mocks__/requireContext')
|
|
1
|
+
import sdk from './index'
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
describe('nayota-show-sdk exports', () => {
|
|
4
|
+
test('exposes config entry and core API modules', () => {
|
|
5
|
+
expect(sdk).toBeDefined()
|
|
6
|
+
expect(typeof sdk.config).toBe('function')
|
|
7
|
+
expect(typeof sdk.on).toBe('function')
|
|
8
|
+
|
|
9
|
+
expect(sdk.area).toBeDefined()
|
|
10
|
+
expect(typeof sdk.area.list).toBe('function')
|
|
11
|
+
|
|
12
|
+
expect(sdk.areaClass).toBeDefined()
|
|
13
|
+
expect(typeof sdk.areaClass.getOne).toBe('function')
|
|
14
|
+
|
|
15
|
+
expect(sdk.components).toBeDefined()
|
|
16
|
+
expect(typeof sdk.components.create).toBe('function')
|
|
7
17
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
test('should have the correct structure', () => {
|
|
11
|
-
expect(modules).toBeDefined()
|
|
12
|
-
expect(typeof modules).toBe('object')
|
|
13
|
-
expect(Object.keys(modules)).toHaveLength(17) // Assuming there is only one module in the littleTool directory
|
|
18
|
+
expect(sdk.departs).toBeDefined()
|
|
19
|
+
expect(typeof sdk.departs.updateOne).toBe('function')
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// expect(module).toBeDefined()
|
|
18
|
-
// expect(typeof module).toBe('object')
|
|
19
|
-
// expect(Object.keys(module)).toHaveLength(13) // Assuming there are 13 sub-modules in the api directory
|
|
21
|
+
expect(sdk.devices).toBeDefined()
|
|
22
|
+
expect(typeof sdk.devices.easyList).toBe('function')
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// expect(module['checkReduces']).toBeDefined()
|
|
24
|
-
// expect(module['user']).toBeDefined()
|
|
25
|
-
// expect(module['httpDrives']).toBeDefined()
|
|
26
|
-
// expect(module['hardwares']).toBeDefined()
|
|
27
|
-
// expect(module['checks']).toBeDefined()
|
|
28
|
-
// expect(module['cloudHardwares']).toBeDefined()
|
|
29
|
-
// expect(module['loraSlaves']).toBeDefined()
|
|
30
|
-
// expect(module['lorawanDevices']).toBeDefined()
|
|
31
|
-
// expect(module['nbiotDrives']).toBeDefined()
|
|
32
|
-
// expect(module['nbm2mDevices']).toBeDefined()
|
|
33
|
-
// expect(module['netDrives']).toBeDefined()
|
|
34
|
-
// expect(module['netMqttDevices']).toBeDefined()
|
|
35
|
-
// // 添加alarms list请求的测试
|
|
36
|
-
// expect(module['alarms']['list']).toBeDefined()
|
|
24
|
+
expect(sdk.deviceClass).toBeDefined()
|
|
25
|
+
expect(typeof sdk.deviceClass.deleteOne).toBe('function')
|
|
37
26
|
})
|
|
38
27
|
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { buildLegacyListResponse } from './utils/iot-adapter-helpers'
|
|
2
|
+
|
|
3
|
+
describe('iot adapter helpers', () => {
|
|
4
|
+
test('wraps list responses as code + data.total/data.rows', () => {
|
|
5
|
+
const result = buildLegacyListResponse({
|
|
6
|
+
total: 1,
|
|
7
|
+
items: [
|
|
8
|
+
{
|
|
9
|
+
id: 'type-1',
|
|
10
|
+
name: '风速仪'
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
expect(result).toEqual({
|
|
16
|
+
code: 0,
|
|
17
|
+
data: {
|
|
18
|
+
total: 1,
|
|
19
|
+
rows: [
|
|
20
|
+
{
|
|
21
|
+
id: 'type-1',
|
|
22
|
+
_id: 'type-1',
|
|
23
|
+
name: '风速仪'
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
})
|
package/package.json
CHANGED
package/utils/http-config-iot.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import urlcfg from '../config/urlcfg'
|
|
2
2
|
import createHttpInstance from './http-factory'
|
|
3
3
|
|
|
4
|
+
function normalizeIotResponseData(data) {
|
|
5
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
6
|
+
return data
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (data.code === 200) {
|
|
10
|
+
return {
|
|
11
|
+
...data,
|
|
12
|
+
code: 0
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return data
|
|
17
|
+
}
|
|
18
|
+
|
|
4
19
|
export default function httpConfigIot() {
|
|
5
20
|
return createHttpInstance({
|
|
6
21
|
getBaseUrl: () => urlcfg.getIotUrl(),
|
|
7
|
-
contentType: 'application/json;charset=UTF-8'
|
|
22
|
+
contentType: 'application/json;charset=UTF-8',
|
|
23
|
+
normalizeResponseData: normalizeIotResponseData
|
|
8
24
|
})
|
|
9
25
|
}
|
package/utils/http-factory.js
CHANGED
|
@@ -6,7 +6,8 @@ import emitter from './EventEmitter'
|
|
|
6
6
|
|
|
7
7
|
function createHttpInstance({
|
|
8
8
|
getBaseUrl,
|
|
9
|
-
contentType
|
|
9
|
+
contentType,
|
|
10
|
+
normalizeResponseData
|
|
10
11
|
}) {
|
|
11
12
|
const http = axios.create({
|
|
12
13
|
baseURL: getBaseUrl(),
|
|
@@ -40,7 +41,14 @@ function createHttpInstance({
|
|
|
40
41
|
axios.defaults.headers.common.Authorization = `Bearer ${newToken}`
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
const data =
|
|
44
|
+
const data = typeof normalizeResponseData === 'function'
|
|
45
|
+
? normalizeResponseData(res.data, res)
|
|
46
|
+
: res.data
|
|
47
|
+
|
|
48
|
+
if (data !== res.data) {
|
|
49
|
+
res.data = data
|
|
50
|
+
}
|
|
51
|
+
|
|
44
52
|
if (data == null || data.code == null) {
|
|
45
53
|
return res
|
|
46
54
|
}
|
|
@@ -209,8 +209,10 @@ export function buildLegacyListResponse(response, mapItem) {
|
|
|
209
209
|
|
|
210
210
|
return {
|
|
211
211
|
code: 0,
|
|
212
|
-
|
|
213
|
-
|
|
212
|
+
data: {
|
|
213
|
+
total: Number(payload?.total ?? items.length ?? 0),
|
|
214
|
+
rows: items.map(item => (mapItem ? mapItem(item) : withLegacyId(item)))
|
|
215
|
+
}
|
|
214
216
|
}
|
|
215
217
|
}
|
|
216
218
|
|
|
@@ -505,8 +505,10 @@ export const iotModuleSpecs = {
|
|
|
505
505
|
const payload = response && response.data && response.status ? response.data : response
|
|
506
506
|
return {
|
|
507
507
|
code: 0,
|
|
508
|
-
|
|
509
|
-
|
|
508
|
+
data: {
|
|
509
|
+
total: Number(payload?.total || 0),
|
|
510
|
+
rows: Array.isArray(payload?.rows) ? payload.rows.map(item => withLegacyId(item)) : []
|
|
511
|
+
}
|
|
510
512
|
}
|
|
511
513
|
}
|
|
512
514
|
}
|