nayota-show-sdk 0.0.1

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/.babelrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "node": "current"
8
+ }
9
+ }
10
+ ]
11
+ ]
12
+ }
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Nayota开放SDK文档
2
+
3
+
4
+ ## 1. SDK简介
5
+ nayota开放SDK是为了方便开发者快速接入nayota平台而提供的一套开发工具包。SDK提供了一系列的API接口,开发者可以通过调用这些接口实现与nayota平台的交互。
6
+
7
+ ## 2. SDK npm 安装
8
+ ```shell
9
+ npm install nayota-show-sdk
10
+ ```
11
+
12
+ ## 3. SDK使用
13
+ ### 3.1 初始化SDK
14
+ ```javascript
15
+ import NayotaSdk from 'nayota-show-sdk'
16
+ NayotaSdk.config({
17
+ authTokenName: 'Admin-Token', // api接口存储的token到cookie的key名称
18
+ showServer: 'http://localhost:8080/api', // noyota-impossible-pro配置服务地址
19
+ })
20
+ ```
@@ -0,0 +1,14 @@
1
+ import { parse } from 'cookie-es'
2
+
3
+ const config = {
4
+ authTokenName: 'Admin-Token',
5
+ getAuthToken: function() {
6
+ return parse(document.cookie)[this.authTokenName]
7
+ },
8
+ getUrl() {
9
+ return this.showServer
10
+ },
11
+ // 默认地址
12
+ showServer: '/api'
13
+ }
14
+ export default config
@@ -0,0 +1,35 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+
4
+ const directoryPath = path.join(__dirname, './api/message')
5
+ const indexFilePath = path.join(directoryPath, 'index.js')
6
+
7
+ fs.readdir(directoryPath, (err, files) => {
8
+ if (err) {
9
+ return console.log('Unable to scan directory: ' + err)
10
+ }
11
+
12
+ let exportString = ''
13
+
14
+ files.forEach((file) => {
15
+ if (path.extname(file) === '.js' && file !== 'index.js') {
16
+ const baseName = path.basename(file, '.js')
17
+ exportString += `import * as ${baseName} from './${baseName}';\n`
18
+ }
19
+ })
20
+
21
+ exportString += 'export {\n'
22
+ files.forEach((file) => {
23
+ if (path.extname(file) === '.js' && file !== 'index.js') {
24
+ const baseName = path.basename(file, '.js')
25
+ exportString += ` ${baseName},\n`
26
+ }
27
+ })
28
+ exportString += '};\n'
29
+
30
+ fs.writeFile(indexFilePath, exportString, (err) => {
31
+ if (err) {
32
+ return console.log('Unable to write file: ' + err)
33
+ }
34
+ })
35
+ })
package/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import config from './config/urlcfg'
2
+ import { init } from './utils'
3
+ import emitter from './utils/EventEmitter'
4
+
5
+ const api = {
6
+
7
+ }
8
+
9
+ export default { config: options => {
10
+ Object.assign(config, options)
11
+ init()
12
+ },
13
+ on: (...args) => {
14
+ emitter.on(...args)
15
+ },
16
+ ...api }
package/index.test.js ADDED
@@ -0,0 +1,38 @@
1
+ let modules
2
+ beforeAll(() => {
3
+ jest.mock('./__mocks__/requireContext')
4
+
5
+ modules = require('./index.js')
6
+ })
7
+
8
+ // Test the modules object
9
+ describe('modules', () => {
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
14
+
15
+ // Test the structure of the module
16
+ // const module = modules['api'] // Assuming the module name is 'api'
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
20
+
21
+ // // Test the structure of each sub-module
22
+ // expect(module['alarms']).toBeDefined()
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()
37
+ })
38
+ })
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ testEnvironment: 'jsdom',
3
+ transform: {
4
+ '^.+\\.jsx?$': 'babel-jest'
5
+ }
6
+ }
package/jsdoc.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "plugins": [],
3
+ "recurseDepth": 10,
4
+ "source": {
5
+ "include": [
6
+ "api"
7
+ ],
8
+ "includePattern": ".+\\.js(doc|x)?$",
9
+ "excludePattern": "(^|\\/|\\\\)_"
10
+ },
11
+ "sourceType": "module",
12
+ "tags": {
13
+ "allowUnknownTags": true,
14
+ "dictionaries": [
15
+ "jsdoc",
16
+ "closure"
17
+ ]
18
+ },
19
+ "templates": {
20
+ "systemName": "nayota-datav-sdk",
21
+ "footer": "宁波边缘物联有限公司",
22
+ "copyright": "宁波边缘物联有限公司",
23
+ "navType": "vertical",
24
+ "theme": "cerulean",
25
+ "linenums": true,
26
+ "collapseSymbols": false,
27
+ "inverseNav": false,
28
+ "outputSourceFiles": true,
29
+ "outputSourcePath": true,
30
+ "dateFormat": "MMMM Do YYYY, h:mm:ss a",
31
+ "syntaxTheme": "default",
32
+ "sort": true,
33
+ "search": true
34
+ }
35
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "nayota-show-sdk",
3
+ "version": "0.0.1",
4
+ "description": "nayota-show-server rest-api",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "author": "lifeng",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "doc": "jsdoc -c jsdoc.json -t ./node_modules/ink-docstrap/template -R README.md -r",
11
+ "test": "jest --config jest.config.cjs"
12
+ },
13
+ "dependencies": {
14
+ "axios": "^1.6.8",
15
+ "cookie-es": "^1.1.0"
16
+ },
17
+ "devDependencies": {
18
+ "@babel/preset-env": "^7.24.5",
19
+ "babel-jest": "^29.7.0",
20
+ "ink-docstrap": "^1.3.2",
21
+ "jest": "^29.7.0",
22
+ "jest-environment-jsdom": "^29.7.0",
23
+ "jsdoc": "^4.0.2",
24
+ "jsdom": "^24.0.0",
25
+ "taffydb": "^2.7.3"
26
+ }
27
+ }
@@ -0,0 +1,25 @@
1
+ class EventEmitter {
2
+ constructor() {
3
+ this.events = {}
4
+ }
5
+
6
+ on(eventName, listener) {
7
+ if (!this.events[eventName]) {
8
+ this.events[eventName] = []
9
+ }
10
+ this.events[eventName].push(listener)
11
+ }
12
+
13
+ emit(eventName, ...args) {
14
+ const listeners = this.events[eventName]
15
+ if (listeners) {
16
+ listeners.forEach(listener => {
17
+ listener(...args)
18
+ })
19
+ }
20
+ }
21
+ }
22
+
23
+ const emitter = new EventEmitter()
24
+
25
+ export default emitter
@@ -0,0 +1,76 @@
1
+ // 引入 axios
2
+ import axios from 'axios'
3
+
4
+ import urlcfg from '../config/urlcfg'
5
+ import emitter from './EventEmitter'
6
+ function createHttpInstance() {
7
+ const http = axios.create({
8
+ baseURL: urlcfg.getUrl(),
9
+ headers: {
10
+ 'Content-Type': 'application/json;charset=UTF-8'
11
+ },
12
+ withCredentials: true, // 跨域请求时是否需要使用凭证
13
+ timeout: 30000
14
+ })
15
+
16
+ // 数据请求拦截
17
+ http.interceptors.request.use(
18
+ config => {
19
+ // Do something before request is sent
20
+
21
+ const accessToken = urlcfg.getAuthToken()
22
+ if (accessToken) {
23
+ config.headers = {
24
+ ...config.headers,
25
+ Authorization: `Bearer ${accessToken}`
26
+ }
27
+ }
28
+
29
+ return config
30
+ },
31
+ error => {
32
+ return Promise.reject(error)
33
+ }
34
+ )
35
+
36
+ // 返回响应数据拦截
37
+ http.interceptors.response.use(
38
+ res => {
39
+ const data = res.data
40
+ if (data.code == null) return res
41
+
42
+ // 状态码为 2xx 范围时都会调用该函数,处理响应数据
43
+ if (res.status === 200) {
44
+ if (data.code !== 0) {
45
+ return Promise.reject(data)
46
+ }
47
+ return data
48
+ } else {
49
+ console.error('请求失败!')
50
+ }
51
+ },
52
+ error => {
53
+ if (error.response && error.response.status) {
54
+ // 状态码超过 2xx 范围时都会调用该函数,处理错误响应
55
+ switch (error.response.status) {
56
+ case 401:
57
+ console.error('登录过期!')
58
+ break
59
+ case 404:
60
+ console.error('请求路径找不到!')
61
+ break
62
+ case 502:
63
+ console.error('服务器内部报错!')
64
+ break
65
+ default:
66
+ break
67
+ }
68
+ }
69
+ emitter.emit('error', error)
70
+ return Promise.reject(error)
71
+ }
72
+ )
73
+ return http
74
+ }
75
+ export default createHttpInstance
76
+
package/utils/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import httpConfig from './http-config'
2
+ let request = httpConfig()
3
+ const init = () => {
4
+ request = httpConfig()
5
+ }
6
+ export {
7
+ init,
8
+ request
9
+ }