@wdywdy/electric-open-platform 1.0.0 → 1.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/README.md +23 -47
- package/dist/cjs/core/runtime.js +2 -2
- package/dist/esm/core/runtime.d.ts +2 -2
- package/dist/esm/core/runtime.js +2 -2
- package/dist/esm/core/storage.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
面向 **智慧式用电数据开放平台**(Mtsci Open Platform,`open.snd02.com`)的纯 TypeScript SDK。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
**零运行时依赖、零原生工程** —— 不依赖任何宿主框架,`npm install` 之后即可在
|
|
6
|
+
Node 后端、Electron、浏览器(配代理)、任意前端框架里直接 `import`。
|
|
7
7
|
|
|
8
8
|
- **OAuth2 全流程**:headless 登录(`authverify2.as`)与重定向登录两种授权方式,
|
|
9
9
|
`accessToken` / `refreshToken` 自动维护与提前续期,并发请求只刷新一次。
|
|
@@ -16,34 +16,17 @@
|
|
|
16
16
|
需要走原生网络栈、自建网关、Redis、文件时注入自己的实现即可。
|
|
17
17
|
- **已对真实平台联调验证**:见 [验证结果](#已验证行为)。
|
|
18
18
|
|
|
19
|
-
> ### 从 0.2.x 升级到 1.0.0
|
|
20
|
-
>
|
|
21
|
-
> | 项 | 0.2.x | 1.0.0 |
|
|
22
|
-
> | --- | --- | --- |
|
|
23
|
-
> | 包名 | `@mantunsci/cordova-electric-open-platform` | `@wdywdy/electric-open-platform` |
|
|
24
|
-
> | Capacitor / Cordova | 双形态,含原生工程与可选 peer 依赖 | **全部移除**,纯 JS/TS |
|
|
25
|
-
> | `storage: 'preferences' \| 'cordova'` | 内置平台存储 | 改为传 `store`(自定义实现)或 `'localStorage' \| 'memory'` |
|
|
26
|
-
> | `describe().host` | `cordova` / `capacitor-native` / `web` / `node` | `describe().runtime` → `node` / `browser` / `unknown` |
|
|
27
|
-
> | 传输选择 | 宿主探测自动装配三套实现 | 只认 `config.transport` / `setTransport()` / 内置 `fetch` |
|
|
28
|
-
> | 已删除的导出 | `createCapacitorTransport`、`createCordovaTransport`、`detectHost`、`CORDOVA_PLUGIN_ID` | 用 `Transport` 接口自行实现,见下文 |
|
|
29
|
-
>
|
|
30
|
-
> 原来的 Capacitor / Cordova 实现各约 70/120 行,迁移成本很低,本文给了现成片段。
|
|
31
|
-
|
|
32
19
|
---
|
|
33
20
|
|
|
34
21
|
## 安装
|
|
35
22
|
|
|
36
23
|
```bash
|
|
37
|
-
|
|
38
|
-
npm install git+https://github.com/lanyangayang/cordova-mantunsci-electric.git#v1.0.0
|
|
39
|
-
|
|
40
|
-
# 方式二:从 npm 安装(需先 npm publish,见「发布到 npm」)
|
|
41
|
-
# npm install @wdywdy/electric-open-platform
|
|
24
|
+
npm install @wdywdy/electric-open-platform
|
|
42
25
|
```
|
|
43
26
|
|
|
44
|
-
|
|
27
|
+
已发布到 npm:<https://www.npmjs.com/package/@wdywdy/electric-open-platform>
|
|
45
28
|
|
|
46
|
-
零依赖、无 peer
|
|
29
|
+
零依赖、无 peer 依赖,也不需要任何平台工程初始化步骤。要求 Node ≥ 18(需要全局 `fetch`),
|
|
47
30
|
或任何支持 ES2020 的浏览器/打包器。
|
|
48
31
|
|
|
49
32
|
## 快速开始
|
|
@@ -89,7 +72,7 @@ await ElectricOpenPlatform.control.switchCircuit({
|
|
|
89
72
|
| Electron 主进程 | ✅ | 直接用 |
|
|
90
73
|
| Electron 渲染进程 | ⚠️ | 走 IPC 到主进程,或在主进程建一个 transport |
|
|
91
74
|
| 浏览器单页应用 | ❌ | `baseUrl` 指向你自己的反向代理 / 后端中转 |
|
|
92
|
-
|
|
|
75
|
+
| 有原生网络能力的容器(App 壳、小程序等) | ⚠️ | 注入一个走原生网络的 `transport`(10 行) |
|
|
93
76
|
|
|
94
77
|
### 浏览器:用反向代理(推荐)
|
|
95
78
|
|
|
@@ -133,33 +116,23 @@ const viaMainProcess: Transport = {
|
|
|
133
116
|
await ElectricOpenPlatform.configure({ ...cfg, transport: viaMainProcess });
|
|
134
117
|
```
|
|
135
118
|
|
|
136
|
-
|
|
119
|
+
如果宿主的网络能力来自原生桥(而不是浏览器 `fetch`),同样只需要包一层 `postForm()`:
|
|
137
120
|
|
|
138
121
|
```ts
|
|
139
|
-
import { CapacitorHttp } from '@capacitor/core';
|
|
140
122
|
import { parseBody, type Transport } from '@wdywdy/electric-open-platform';
|
|
141
123
|
|
|
142
|
-
const
|
|
143
|
-
name: '
|
|
124
|
+
const viaNative: Transport = {
|
|
125
|
+
name: 'native-http',
|
|
144
126
|
async postForm(req) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
data: req.body,
|
|
149
|
-
responseType: 'text',
|
|
150
|
-
connectTimeout: req.timeoutMs,
|
|
151
|
-
readTimeout: req.timeoutMs,
|
|
152
|
-
});
|
|
153
|
-
const raw = typeof res.data === 'string' ? res.data : JSON.stringify(res.data);
|
|
154
|
-
return { status: res.status, data: parseBody(raw), raw };
|
|
127
|
+
// 由你的原生桥返回 { status, body };不受 WebView 同源策略限制
|
|
128
|
+
const res = await nativeBridge.httpRequest(req.url, req.body, req.headers);
|
|
129
|
+
return { status: res.status, data: parseBody(res.body), raw: res.body };
|
|
155
130
|
},
|
|
156
131
|
};
|
|
157
132
|
|
|
158
|
-
await ElectricOpenPlatform.configure({ ...cfg, transport:
|
|
133
|
+
await ElectricOpenPlatform.configure({ ...cfg, transport: viaNative });
|
|
159
134
|
```
|
|
160
135
|
|
|
161
|
-
Cordova 工程同理:把 `cordova.exec` 包一层 `postForm()` 即可,不再需要本包提供原生工程。
|
|
162
|
-
|
|
163
136
|
### 注入自己的存储实现
|
|
164
137
|
|
|
165
138
|
默认 `auto` 是 `localStorage → 内存`。想持久化到文件、Redis、数据库,或容器提供的原生偏好:
|
|
@@ -452,7 +425,7 @@ node scripts/diagnose-getbox.mjs # 对照实验:证明 GET_BOX 是未开通
|
|
|
452
425
|
```bash
|
|
453
426
|
npm install
|
|
454
427
|
npm test # 99 个单测:MD5 向量、签名样例、token 生命周期、接口参数、
|
|
455
|
-
#
|
|
428
|
+
# 传输/存储注入优先级,以及「不得出现平台耦合」的防回潮守卫
|
|
456
429
|
npm run build # 产出 dist/esm(ESM)+ dist/cjs(CJS)+ 完整 .d.ts
|
|
457
430
|
npm run demo # 打开 http://localhost:5178 调试台
|
|
458
431
|
```
|
|
@@ -533,10 +506,13 @@ export class PowerApiService {
|
|
|
533
506
|
包名 **`@wdywdy/electric-open-platform`**(`@wdywdy` 是 npm 个人账号 `wdywdy` 的用户名 scope,
|
|
534
507
|
无需建组织即可发布)。只发布**一个**包,版本以 `package.json` 为准:
|
|
535
508
|
|
|
536
|
-
>
|
|
537
|
-
>
|
|
538
|
-
>
|
|
539
|
-
>
|
|
509
|
+
> ℹ️ **1.0.0 已于 2026-11-04 成功发布**(用勾选了 *Bypass 2FA* 的 **Granular Access Token**,
|
|
510
|
+
> 权限 Read and write + All packages)。
|
|
511
|
+
> 该账号开了 2FA,日后发布同样需要「动态码」或这种 token,否则会返回
|
|
512
|
+
> `E403 Two-factor authentication ... is required to publish packages`。
|
|
513
|
+
>
|
|
514
|
+
> ⚠️ 注意:**只读 token 不行** —— 它连 `npm access list packages` 都会 403,
|
|
515
|
+
> publish 同样失败,必须先确认 token 具备写权限。
|
|
540
516
|
|
|
541
517
|
```bash
|
|
542
518
|
# 首次发布
|
|
@@ -555,13 +531,13 @@ git tag v1.0.1 && git push && git push --tags
|
|
|
555
531
|
```
|
|
556
532
|
|
|
557
533
|
工作流 `.github/workflows/publish-npm.yml` 会依次:装依赖 → 跑单测 → 重建产物并校验产物已提交
|
|
558
|
-
→ 校验版本与零依赖 → **grep
|
|
534
|
+
→ 校验版本与零依赖 → **grep 断言产物里没有平台耦合残留** → 打包自检 → 发布
|
|
559
535
|
(已发布过的版本自动跳过,便于重跑)。
|
|
560
536
|
|
|
561
537
|
**CI 自动发布需要配认证**,二选一:
|
|
562
538
|
|
|
563
539
|
1. **Trusted Publishing(推荐,无需存 token)**:npm 包页面 → Settings → Trusted Publisher →
|
|
564
|
-
|
|
540
|
+
GitHub 仓库(`lanyangayang/electric-open-platform`)与 workflow 文件名
|
|
565
541
|
`publish-npm.yml`(workflow 里已有 `id-token: write` 与 `--provenance`)。
|
|
566
542
|
注意:包必须先手动发布过一次才能绑定。
|
|
567
543
|
2. **Access Token**:仓库 Settings → Secrets and variables → Actions → `NPM_TOKEN`,
|
package/dist/cjs/core/runtime.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* 运行时探测 —— **只用于诊断输出,不参与任何能力选择**。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* 旧版本会按宿主环境自动装配多套传输实现(原生 HTTP / 原生桥 / fetch),
|
|
6
|
+
* 那是本包与外部宿主框架耦合的唯一来源。现在传输实现只来自:
|
|
7
7
|
*
|
|
8
8
|
* 1. `configure({ transport })` 显式指定
|
|
9
9
|
* 2. `configure()` 之前用 `setTransport()` 注入
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 运行时探测 —— **只用于诊断输出,不参与任何能力选择**。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* 旧版本会按宿主环境自动装配多套传输实现(原生 HTTP / 原生桥 / fetch),
|
|
5
|
+
* 那是本包与外部宿主框架耦合的唯一来源。现在传输实现只来自:
|
|
6
6
|
*
|
|
7
7
|
* 1. `configure({ transport })` 显式指定
|
|
8
8
|
* 2. `configure()` 之前用 `setTransport()` 注入
|
package/dist/esm/core/runtime.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 运行时探测 —— **只用于诊断输出,不参与任何能力选择**。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* 旧版本会按宿主环境自动装配多套传输实现(原生 HTTP / 原生桥 / fetch),
|
|
5
|
+
* 那是本包与外部宿主框架耦合的唯一来源。现在传输实现只来自:
|
|
6
6
|
*
|
|
7
7
|
* 1. `configure({ transport })` 显式指定
|
|
8
8
|
* 2. `configure()` 之前用 `setTransport()` 注入
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* | 自定义 | `config.store` | 想写入文件、Redis、数据库、iOS `UserDefaults`/Android
|
|
16
16
|
* `SharedPreferences` 时,自己实现 4 个方法传给 `configure({ store })` 即可 |
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* 之前版本内置了若干宿主原生存储适配(系统偏好、键值库等),现已移除:
|
|
19
19
|
* 它们需要宿主桥接或额外依赖,与「纯 npm 包、任何项目都能用」的目标冲突。
|
|
20
20
|
* 同样的能力可以用自定义 store 在 10 行内实现,见 README。
|
|
21
21
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wdywdy/electric-open-platform",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "智慧式用电数据开放平台(open.snd02.com)的纯 TypeScript SDK:OAuth2 token 生命周期、MD5 参数签名,以及全部 router.as 接口的类型化封装。零运行时依赖,Node / 浏览器 / Electron / 任意前端框架通用。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"repository": {
|
|
57
57
|
"type": "git",
|
|
58
|
-
"url": "https://github.com/lanyangayang/
|
|
58
|
+
"url": "https://github.com/lanyangayang/electric-open-platform.git"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"typescript": "^5.6.3",
|