mrac-sn-refer-sdk 1.0.0

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/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # mrac-sn-refer-sdk
2
+
3
+ 【MRAC内部专用】SN REFER。
4
+
5
+ ## 📦 安装
6
+
7
+ 编译SDK(维护者):
8
+
9
+ ```bash
10
+ npm install
11
+ npm run build
12
+ ```
13
+
14
+ ## 使用方法
15
+
16
+ #### 集成sdk:
17
+
18
+ ```bash
19
+ npm install mrac-sn-refer-sdk
20
+ ```
21
+
22
+ #### JS调用示例:
23
+
24
+ ```js
25
+ import { getReferSN, reportBase } from 'mrac-sn-refer-sdk'
26
+
27
+ const storageModule = weex.requireModule('storage') //或 nativeService,需实现setItem & getItem
28
+
29
+ //或自定义缓存模块,如
30
+ const storageModule = {
31
+ setItem: (key, value, callback) => {
32
+ storage.setItem(key, JSON.stringify(value))
33
+ if (callback) {
34
+ callback()
35
+ }
36
+ },
37
+ getItem: (key, callback) => {
38
+ const value = storage.getItem(key)
39
+ callback(value ? JSON.parse(value) : null)
40
+ }
41
+ }
42
+
43
+ const httpModule = {
44
+ post: (api, body, success) => {
45
+ //示例。插件中应替换为相应nativeService.request方法
46
+ fetch(api, {
47
+ method: 'POST',
48
+ headers: { 'Content-Type': 'application/json' },
49
+ body: JSON.stringify(body)
50
+ })
51
+ .then(res => res.json())
52
+ .then(data =>
53
+ //...先判断数据有效
54
+ const dataValid = xxxxx
55
+ if (dataValid) {
56
+ success(data.result) //根据实际接口返回结果数据
57
+ }
58
+ )
59
+ }
60
+ }
61
+
62
+ const run = async () => {
63
+ //示例。获取refer sn
64
+ const shouldCheck = true or false //当前sn无本地配置时为true
65
+ const result = await getReferSN('1234A', shouldCheck, storageModule, httpModule)
66
+ if (result.valid) {
67
+ console.log('refer sn:', result.refer)
68
+ }
69
+
70
+ //示例。上报
71
+ const shouldReport = !shouldCheck //当前sn有本地配置时为true
72
+ const serial = '_WDAD3' //应从SnFuncMap动态获取
73
+ reportBase('1234A', shouldReport, serial, storageModule, httpModule)
74
+ }
75
+
76
+ run()
77
+ ```
package/lib/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { type HttpModule, type ReferResult, type StorageModule } from './internal';
2
+ /**
3
+ * 产品之间的关联关系检查(验证A产品与B产品存在关联关系)
4
+ * @param sn 当前产品SN
5
+ * @param shouldCheck 是否需要检查
6
+ * @param storageModule 外部传入存储模块
7
+ * @param httpModule 外部传入HTTP模块
8
+ */
9
+ export declare const getReferSN: (sn: string, shouldCheck: boolean, storageModule: StorageModule, httpModule: HttpModule) => Promise<ReferResult>;
10
+ /**
11
+ * 上报本地是否已配置当前产品的SN
12
+ * @param sn 当前产品SN
13
+ * @param shouldReport 是否需要上报
14
+ * @param serial 产品系列名(必填,SnFuncMap中的系列)
15
+ * @param storageModule 外部传入存储模块
16
+ * @param httpModule 外部传入HTTP模块
17
+ */
18
+ export declare const reportBase: (sn: string, shouldReport: boolean, serial: string, storageModule: StorageModule, httpModule: HttpModule) => Promise<void>;
19
+ export type { StorageModule, HttpModule, ReferResult };
package/lib/index.js ADDED
@@ -0,0 +1,76 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { getStorageItem, parseReferCache, postRequest, REPORT_CACHE_SUFFIX, setStorageItem, ONE_DAY_MS } from './internal';
11
+ const API_QUERY = '/product/app/config/query';
12
+ const API_REPORT = '/product/app/config/report';
13
+ /**
14
+ * 产品之间的关联关系检查(验证A产品与B产品存在关联关系)
15
+ * @param sn 当前产品SN
16
+ * @param shouldCheck 是否需要检查
17
+ * @param storageModule 外部传入存储模块
18
+ * @param httpModule 外部传入HTTP模块
19
+ */
20
+ export const getReferSN = (sn, shouldCheck, storageModule, httpModule) => __awaiter(void 0, void 0, void 0, function* () {
21
+ var _a;
22
+ if (!shouldCheck) {
23
+ return { valid: false, refer: null };
24
+ }
25
+ const sn5 = sn.length === 5
26
+ ? sn
27
+ : sn.length === 32
28
+ ? sn.slice(12, 17)
29
+ : sn.length === 22
30
+ ? sn.slice(6, 11)
31
+ : '';
32
+ if (sn5.length === 0) {
33
+ return { valid: false, refer: null };
34
+ }
35
+ const cached = parseReferCache(yield getStorageItem(storageModule, sn5));
36
+ const now = Date.now();
37
+ if (cached && cached.referSn && cached.expiredAt > now) {
38
+ return { valid: true, refer: cached.referSn };
39
+ }
40
+ const response = yield postRequest(httpModule, API_QUERY, { sn8: sn5 });
41
+ const referSn = (_a = response === null || response === void 0 ? void 0 : response.inherit_from) !== null && _a !== void 0 ? _a : null;
42
+ if (referSn) {
43
+ const cacheValue = {
44
+ referSn,
45
+ expiredAt: Date.now() + ONE_DAY_MS
46
+ };
47
+ //直接存储即可,不需要await
48
+ setStorageItem(storageModule, sn5, cacheValue);
49
+ }
50
+ return { valid: Boolean(referSn), refer: referSn };
51
+ });
52
+ /**
53
+ * 上报本地是否已配置当前产品的SN
54
+ * @param sn 当前产品SN
55
+ * @param shouldReport 是否需要上报
56
+ * @param serial 产品系列名(必填,SnFuncMap中的系列)
57
+ * @param storageModule 外部传入存储模块
58
+ * @param httpModule 外部传入HTTP模块
59
+ */
60
+ export const reportBase = (sn, shouldReport, serial, storageModule, httpModule) => __awaiter(void 0, void 0, void 0, function* () {
61
+ if (!shouldReport) {
62
+ return;
63
+ }
64
+ const cacheKey = `${sn}${REPORT_CACHE_SUFFIX}`;
65
+ const cached = yield getStorageItem(storageModule, cacheKey);
66
+ if (cached) {
67
+ return;
68
+ }
69
+ yield postRequest(httpModule, API_REPORT, {
70
+ sn,
71
+ is_base: shouldReport,
72
+ profile_key: serial
73
+ });
74
+ //直接存储即可,不需要await
75
+ setStorageItem(storageModule, cacheKey, true);
76
+ });
@@ -0,0 +1,22 @@
1
+ export type StorageModule = {
2
+ setItem: (key: string, value: unknown, callback?: () => void) => void;
3
+ getItem: (key: string, callback: (result: unknown) => void) => void;
4
+ };
5
+ export type HttpModule = {
6
+ post: (api: string, body: Record<string, unknown>, success: (result: unknown) => void) => void;
7
+ };
8
+ export type ReferCache = {
9
+ referSn: string;
10
+ expiredAt: number;
11
+ };
12
+ export type ReferResult = {
13
+ valid: boolean;
14
+ refer: string | null;
15
+ };
16
+ export declare const ONE_DAY_MS: number;
17
+ export declare const REPORT_CACHE_SUFFIX = "__reported__";
18
+ export declare const toPromise: <T>(executor: (resolve: (value: T) => void) => void) => Promise<T>;
19
+ export declare const getStorageItem: <T>(storage: StorageModule, key: string) => Promise<T | null>;
20
+ export declare const setStorageItem: (storage: StorageModule, key: string, value: unknown) => Promise<void>;
21
+ export declare const postRequest: <T>(httpModule: HttpModule, api: string, body: Record<string, unknown>) => Promise<T>;
22
+ export declare const parseReferCache: (cache: unknown) => ReferCache | null;
@@ -0,0 +1,52 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export const ONE_DAY_MS = 24 * 60 * 60 * 1000;
11
+ export const REPORT_CACHE_SUFFIX = '__reported__';
12
+ export const toPromise = (executor) => new Promise(resolve => executor(resolve));
13
+ export const getStorageItem = (storage, key) => __awaiter(void 0, void 0, void 0, function* () {
14
+ return toPromise(resolve => {
15
+ storage.getItem(key, result => {
16
+ if (typeof result === 'undefined' || result === null) {
17
+ resolve(null);
18
+ return;
19
+ }
20
+ resolve(result);
21
+ });
22
+ });
23
+ });
24
+ export const setStorageItem = (storage, key, value) => __awaiter(void 0, void 0, void 0, function* () {
25
+ return toPromise(resolve => {
26
+ storage.setItem(key, value, () => resolve());
27
+ });
28
+ });
29
+ export const postRequest = (httpModule, api, body) => __awaiter(void 0, void 0, void 0, function* () {
30
+ return toPromise(resolve => {
31
+ httpModule.post(api, body, result => resolve(result));
32
+ });
33
+ });
34
+ export const parseReferCache = (cache) => {
35
+ if (!cache) {
36
+ return null;
37
+ }
38
+ if (typeof cache === 'string') {
39
+ try {
40
+ return JSON.parse(cache);
41
+ }
42
+ catch (error) {
43
+ return null;
44
+ }
45
+ }
46
+ if (typeof cache === 'object' &&
47
+ 'referSn' in cache &&
48
+ 'expiredAt' in cache) {
49
+ return cache;
50
+ }
51
+ return null;
52
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "mrac-sn-refer-sdk",
3
+ "version": "1.0.0",
4
+ "description": "** Internal use for mrac department. A refer tool.",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "default": "./lib/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "lib"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "prepublishOnly": "npm run build",
20
+ "test": "echo \"Error: no test specified\" && exit 1"
21
+ },
22
+ "keywords": [
23
+ "mrac",
24
+ "sn",
25
+ "refer"
26
+ ],
27
+ "author": "weicheng.chen@midea.com",
28
+ "license": "MIT",
29
+ "dependencies": {},
30
+ "devDependencies": {
31
+ "typescript": "^5.4.5"
32
+ },
33
+ "engines": {
34
+ "node": ">=14.0.0"
35
+ }
36
+ }