@perk-net/perk-pushplus-sdk 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 +42 -0
- package/dist/index.cjs +224 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +149 -1
- package/dist/index.d.ts +149 -1
- package/dist/index.global.js +224 -5
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +223 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/api/image-api.ts +241 -0
- package/src/client.ts +3 -0
- package/src/config.ts +1 -1
- package/src/http.ts +101 -6
- package/src/index.ts +11 -0
- package/src/models.ts +62 -0
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
- **同时支持 Node.js 与浏览器**:Node.js 18+ 使用内置 `fetch`,浏览器使用原生 `fetch`,无运行时依赖。
|
|
6
6
|
- **三种产物**:CommonJS (`.cjs`) + ESModule (`.js`) + 浏览器 IIFE (`.global.js`),可通过 npm / `<script>` 直接加载。
|
|
7
7
|
- **完整 TypeScript 类型**:所有请求 / 响应 / 枚举 / 回调全部带类型声明。
|
|
8
|
+
- **全部开放接口**:用户、消息、消息 token、群组、群组用户、好友、webhook、渠道、ClawBot、功能设置、预处理、图片服务(含一键上传到 PushPlus 图床)。
|
|
8
9
|
- **AccessKey 自动管理**:缓存 + 过期前自动刷新;`code=401` 自动刷新并重试一次。
|
|
9
10
|
- **本地限流守卫**:命中 `code=900` 后按 token 短路同 token 后续发送,避免被服务端长期封禁。
|
|
10
11
|
- **Builder 链式 API**:与 Java/Python SDK 风格保持一致。
|
|
@@ -163,8 +164,49 @@ await client.setting.changeOpenMessageType(0);
|
|
|
163
164
|
|
|
164
165
|
// 预处理(仅会员)
|
|
165
166
|
const out = await client.pre.test({ content: '...', message: 'hi' });
|
|
167
|
+
|
|
168
|
+
// 图片服务(一行上传到 PushPlus 图床,30 天有效)
|
|
169
|
+
import { readFile } from 'node:fs/promises';
|
|
170
|
+
const bytes = await readFile('/tmp/logo.png');
|
|
171
|
+
const uploaded = await client.image.uploadBytes(bytes, { fileName: 'logo.png' });
|
|
172
|
+
console.log(uploaded.url); // 直接拿到可访问的图片 URL
|
|
173
|
+
const imgs = await client.image.list({ current: 1, pageSize: 10 });
|
|
174
|
+
await client.image.delete(imgs.list[0].id);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 图片服务
|
|
178
|
+
|
|
179
|
+
PushPlus 基于七牛云提供图片图床(30 天有效,可主动删除)。SDK 把「获取上传凭证 → multipart 表单上传 → 解析 URL」封装成一步:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
// Node.js:从文件读取
|
|
183
|
+
import { readFile } from 'node:fs/promises';
|
|
184
|
+
const bytes = await readFile('/tmp/a.png');
|
|
185
|
+
const r = await client.image.uploadBytes(bytes, { fileName: 'a.png' });
|
|
186
|
+
console.log(r.url);
|
|
187
|
+
|
|
188
|
+
// 浏览器:input[type=file]
|
|
189
|
+
const file = (document.querySelector('input[type=file]') as HTMLInputElement).files![0];
|
|
190
|
+
await client.image.uploadBytes(file, { fileName: file.name, contentType: file.type });
|
|
191
|
+
|
|
192
|
+
// 已上传图片列表
|
|
193
|
+
const page = await client.image.list({ current: 1, pageSize: 10 });
|
|
194
|
+
|
|
195
|
+
// 主动删除(未删除的图片默认 30 天后由系统自动清理)
|
|
196
|
+
await client.image.delete(page.list![0].id!);
|
|
166
197
|
```
|
|
167
198
|
|
|
199
|
+
需要自己控制凭证的获取与上传过程时(如缓存 token、分布式上传),可拆开调用:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
const token = await client.image.getUploadToken();
|
|
203
|
+
const r = await client.image.upload(token, bytes, { fileName: 'a.png', contentType: 'image/png' });
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
> 上传图片的真正请求会按七牛云规范以 `multipart/form-data` 提交到 `uploadUrl`,**不会**携带 PushPlus 的 `access-key`;其余三个接口(获取凭证 / 列表 / 删除)走 PushPlus 开放接口,自动带上 `access-key`。
|
|
207
|
+
>
|
|
208
|
+
> 接受的二进制形态:`Uint8Array`(Node 中 `Buffer` 是其子类,可直接传)、`ArrayBuffer`、`Blob`/`File`(浏览器 + Node 18+)。
|
|
209
|
+
|
|
168
210
|
### 5. 回调解析
|
|
169
211
|
|
|
170
212
|
PushPlus 在消息发送完成、群组新增用户、新增好友时会回调你预置的 URL。SDK 提供类型安全的解析:
|
package/dist/index.cjs
CHANGED
|
@@ -185,6 +185,25 @@ var AccessKeyManager = class {
|
|
|
185
185
|
};
|
|
186
186
|
|
|
187
187
|
// src/http.ts
|
|
188
|
+
async function callExecuteRaw(requester, options) {
|
|
189
|
+
if (typeof requester.executeRaw === "function") {
|
|
190
|
+
return requester.executeRaw(options);
|
|
191
|
+
}
|
|
192
|
+
const { method, url, headers, body } = options;
|
|
193
|
+
let text = null;
|
|
194
|
+
if (body != null) {
|
|
195
|
+
if (typeof Blob !== "undefined" && body instanceof Blob) {
|
|
196
|
+
text = await body.text();
|
|
197
|
+
} else if (body instanceof Uint8Array) {
|
|
198
|
+
text = new TextDecoder("utf-8").decode(body);
|
|
199
|
+
} else if (body instanceof ArrayBuffer) {
|
|
200
|
+
text = new TextDecoder("utf-8").decode(new Uint8Array(body));
|
|
201
|
+
} else {
|
|
202
|
+
text = String(body);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return requester.execute({ method, url, headers, body: text });
|
|
206
|
+
}
|
|
188
207
|
var FetchHttpRequester = class {
|
|
189
208
|
constructor(config, fetchImpl) {
|
|
190
209
|
this.readTimeoutMs = config.readTimeoutMs;
|
|
@@ -200,7 +219,29 @@ var FetchHttpRequester = class {
|
|
|
200
219
|
}
|
|
201
220
|
async execute(options) {
|
|
202
221
|
var _a, _b;
|
|
203
|
-
|
|
222
|
+
return this.doExecute({
|
|
223
|
+
method: options.method,
|
|
224
|
+
url: options.url,
|
|
225
|
+
headers: options.headers,
|
|
226
|
+
body: (_a = options.body) != null ? _a : null,
|
|
227
|
+
bodyForLog: (_b = options.body) != null ? _b : null,
|
|
228
|
+
defaultContentType: "application/json;charset=UTF-8"
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
async executeRaw(options) {
|
|
232
|
+
var _a;
|
|
233
|
+
return this.doExecute({
|
|
234
|
+
method: options.method,
|
|
235
|
+
url: options.url,
|
|
236
|
+
headers: options.headers,
|
|
237
|
+
body: (_a = options.body) != null ? _a : null,
|
|
238
|
+
bodyForLog: null,
|
|
239
|
+
defaultContentType: "application/octet-stream"
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
async doExecute(args) {
|
|
243
|
+
var _a, _b;
|
|
244
|
+
const { method, url, headers, body, bodyForLog, defaultContentType } = args;
|
|
204
245
|
const finalHeaders = {};
|
|
205
246
|
let hasContentType = false;
|
|
206
247
|
if (headers) {
|
|
@@ -210,14 +251,19 @@ var FetchHttpRequester = class {
|
|
|
210
251
|
if (k.toLowerCase() === "content-type") hasContentType = true;
|
|
211
252
|
}
|
|
212
253
|
}
|
|
213
|
-
if (body != null && !hasContentType) {
|
|
214
|
-
finalHeaders["Content-Type"] =
|
|
254
|
+
if (body != null && !hasContentType && defaultContentType) {
|
|
255
|
+
finalHeaders["Content-Type"] = defaultContentType;
|
|
215
256
|
}
|
|
216
257
|
if (typeof window === "undefined" && !finalHeaders["User-Agent"] && !finalHeaders["user-agent"]) {
|
|
217
258
|
finalHeaders["User-Agent"] = this.userAgent;
|
|
218
259
|
}
|
|
219
260
|
if (this.logRequest) {
|
|
220
|
-
|
|
261
|
+
if (bodyForLog != null) {
|
|
262
|
+
console.debug("[pushplus] >>>", method, url, "body=", bodyForLog);
|
|
263
|
+
} else {
|
|
264
|
+
const len = bodyLength(body);
|
|
265
|
+
console.debug("[pushplus] >>>", method, url, "bodyBytes=", len);
|
|
266
|
+
}
|
|
221
267
|
}
|
|
222
268
|
const controller = new AbortController();
|
|
223
269
|
const timer = this.readTimeoutMs > 0 ? setTimeout(() => controller.abort(), this.readTimeoutMs) : null;
|
|
@@ -249,6 +295,14 @@ var FetchHttpRequester = class {
|
|
|
249
295
|
}
|
|
250
296
|
}
|
|
251
297
|
};
|
|
298
|
+
function bodyLength(body) {
|
|
299
|
+
if (body == null) return 0;
|
|
300
|
+
if (typeof body === "string") return body.length;
|
|
301
|
+
if (body instanceof Uint8Array) return body.byteLength;
|
|
302
|
+
if (body instanceof ArrayBuffer) return body.byteLength;
|
|
303
|
+
if (typeof Blob !== "undefined" && body instanceof Blob) return body.size;
|
|
304
|
+
return -1;
|
|
305
|
+
}
|
|
252
306
|
function isSuccessfulHttpStatus(status) {
|
|
253
307
|
return status >= 200 && status < 300;
|
|
254
308
|
}
|
|
@@ -520,6 +574,168 @@ var FriendApi = class extends OpenAbstractApi {
|
|
|
520
574
|
}
|
|
521
575
|
};
|
|
522
576
|
|
|
577
|
+
// src/api/image-api.ts
|
|
578
|
+
var ImageApi = class extends OpenAbstractApi {
|
|
579
|
+
constructor(config, http, mgr) {
|
|
580
|
+
super(config, http, mgr);
|
|
581
|
+
}
|
|
582
|
+
/** 1. 获取上传凭证。 */
|
|
583
|
+
getUploadToken() {
|
|
584
|
+
return this.executeOpen("GET", "/api/open/userImage/uploadToken");
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* 2. 上传图片到七牛云。
|
|
588
|
+
*
|
|
589
|
+
* 使用「获取上传凭证」返回的 `uploadUrl` 与 `uploadToken`,
|
|
590
|
+
* 按七牛云表单上传规范以 `multipart/form-data` 提交。该请求
|
|
591
|
+
* **不会** 携带 PushPlus 的 `access-key` 头。
|
|
592
|
+
*/
|
|
593
|
+
async upload(token, file, options) {
|
|
594
|
+
if (token == null) {
|
|
595
|
+
throw new PushPlusError("\u4E0A\u4F20\u51ED\u8BC1 token \u4E0D\u80FD\u4E3A null");
|
|
596
|
+
}
|
|
597
|
+
if (!token.uploadToken) {
|
|
598
|
+
throw new PushPlusError("\u4E0A\u4F20\u51ED\u8BC1 uploadToken \u4E0D\u80FD\u4E3A\u7A7A");
|
|
599
|
+
}
|
|
600
|
+
const uploadUrl = token.uploadUrl || token.uploadHost;
|
|
601
|
+
if (!uploadUrl) {
|
|
602
|
+
throw new PushPlusError("\u4E0A\u4F20\u51ED\u8BC1\u672A\u8FD4\u56DE uploadUrl/uploadHost");
|
|
603
|
+
}
|
|
604
|
+
return this.uploadToQiniu(uploadUrl, token.uploadToken, file, options);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* 2. 上传图片到七牛云(低层方法)。直接指定上传地址与 token。
|
|
608
|
+
*/
|
|
609
|
+
async uploadToQiniu(uploadUrl, uploadToken, file, options) {
|
|
610
|
+
var _a, _b;
|
|
611
|
+
if (!uploadUrl) {
|
|
612
|
+
throw new PushPlusError("uploadUrl \u4E0D\u80FD\u4E3A\u7A7A");
|
|
613
|
+
}
|
|
614
|
+
if (!uploadToken) {
|
|
615
|
+
throw new PushPlusError("uploadToken \u4E0D\u80FD\u4E3A\u7A7A");
|
|
616
|
+
}
|
|
617
|
+
const bytes = await toUint8Array(file);
|
|
618
|
+
if (bytes.byteLength === 0) {
|
|
619
|
+
throw new PushPlusError("\u4E0A\u4F20\u6587\u4EF6\u5185\u5BB9\u4E0D\u80FD\u4E3A\u7A7A");
|
|
620
|
+
}
|
|
621
|
+
const fileName = options.fileName || "file";
|
|
622
|
+
const contentType = options.contentType || guessContentTypeByName(fileName) || "application/octet-stream";
|
|
623
|
+
const boundary = "----PushPlusBoundary" + randomBoundarySuffix();
|
|
624
|
+
const body = buildMultipartBody(boundary, uploadToken, fileName, contentType, bytes);
|
|
625
|
+
const resp = await callExecuteRaw(this.http, {
|
|
626
|
+
method: "POST",
|
|
627
|
+
url: uploadUrl,
|
|
628
|
+
headers: { "Content-Type": `multipart/form-data; boundary=${boundary}` },
|
|
629
|
+
body
|
|
630
|
+
});
|
|
631
|
+
if (!isSuccessfulHttpStatus(resp.statusCode)) {
|
|
632
|
+
throw new PushPlusError(
|
|
633
|
+
`\u4E0A\u4F20\u56FE\u7247\u5230\u4E03\u725B\u4E91\u5931\u8D25: status=${resp.statusCode}, body=${resp.body}`,
|
|
634
|
+
resp.statusCode
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
let result;
|
|
638
|
+
try {
|
|
639
|
+
result = JSON.parse(resp.body);
|
|
640
|
+
} catch (e) {
|
|
641
|
+
throw new PushPlusError(
|
|
642
|
+
`\u89E3\u6790\u4E03\u725B\u4E91\u54CD\u5E94\u5931\u8D25: ${e.message}, payload=${resp.body}`,
|
|
643
|
+
-1,
|
|
644
|
+
{ cause: e }
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
if (result == null || typeof result !== "object") {
|
|
648
|
+
throw new PushPlusError(`\u4E03\u725B\u4E91\u8FD4\u56DE\u975E JSON \u5BF9\u8C61: ${resp.body}`);
|
|
649
|
+
}
|
|
650
|
+
if (result.errno !== 0) {
|
|
651
|
+
throw new PushPlusError(
|
|
652
|
+
`\u4E03\u725B\u4E91\u4E0A\u4F20\u5931\u8D25: errno=${result.errno}, msg=${(_a = result.msg) != null ? _a : ""}`,
|
|
653
|
+
(_b = result.errno) != null ? _b : -1
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
return result;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* 便捷方法:自动获取上传凭证后上传字节数组 / Blob / ArrayBuffer。
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* ```ts
|
|
663
|
+
* await client.image.uploadBytes(buffer, { fileName: 'a.png' });
|
|
664
|
+
* await client.image.uploadBytes(blob, { fileName: 'b.jpg', contentType: 'image/jpeg' });
|
|
665
|
+
* ```
|
|
666
|
+
*/
|
|
667
|
+
async uploadBytes(file, options) {
|
|
668
|
+
const token = await this.getUploadToken();
|
|
669
|
+
return this.upload(token, file, options);
|
|
670
|
+
}
|
|
671
|
+
/** 3. 图片列表。 */
|
|
672
|
+
list(query) {
|
|
673
|
+
return this.executeOpen(
|
|
674
|
+
"POST",
|
|
675
|
+
"/api/open/userImage/list",
|
|
676
|
+
query != null ? query : {}
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* 4. 主动删除图片;未删除的图片默认 30 天后由系统自动清理。
|
|
681
|
+
*/
|
|
682
|
+
async delete(id) {
|
|
683
|
+
await this.executeOpen(
|
|
684
|
+
"DELETE",
|
|
685
|
+
this.appendQuery("/api/open/userImage/delete", { id })
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
async function toUint8Array(file) {
|
|
690
|
+
if (file == null) {
|
|
691
|
+
throw new PushPlusError("\u4E0A\u4F20\u6587\u4EF6\u4E0D\u80FD\u4E3A null");
|
|
692
|
+
}
|
|
693
|
+
if (file instanceof Uint8Array) {
|
|
694
|
+
return file;
|
|
695
|
+
}
|
|
696
|
+
if (file instanceof ArrayBuffer) {
|
|
697
|
+
return new Uint8Array(file);
|
|
698
|
+
}
|
|
699
|
+
if (typeof Blob !== "undefined" && file instanceof Blob) {
|
|
700
|
+
const ab = await file.arrayBuffer();
|
|
701
|
+
return new Uint8Array(ab);
|
|
702
|
+
}
|
|
703
|
+
throw new PushPlusError(`\u4E0D\u652F\u6301\u7684\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B: ${Object.prototype.toString.call(file)}`);
|
|
704
|
+
}
|
|
705
|
+
function buildMultipartBody(boundary, uploadToken, fileName, contentType, fileBytes) {
|
|
706
|
+
const crlf = "\r\n";
|
|
707
|
+
const enc = new TextEncoder();
|
|
708
|
+
const head = enc.encode(
|
|
709
|
+
`--${boundary}${crlf}Content-Disposition: form-data; name="token"${crlf}${crlf}${uploadToken}${crlf}--${boundary}${crlf}Content-Disposition: form-data; name="file"; filename="${escapeFileName(fileName)}"${crlf}Content-Type: ${contentType}${crlf}${crlf}`
|
|
710
|
+
);
|
|
711
|
+
const tail = enc.encode(`${crlf}--${boundary}--${crlf}`);
|
|
712
|
+
const out = new Uint8Array(head.byteLength + fileBytes.byteLength + tail.byteLength);
|
|
713
|
+
out.set(head, 0);
|
|
714
|
+
out.set(fileBytes, head.byteLength);
|
|
715
|
+
out.set(tail, head.byteLength + fileBytes.byteLength);
|
|
716
|
+
return out;
|
|
717
|
+
}
|
|
718
|
+
function escapeFileName(name) {
|
|
719
|
+
return name.replace(/"/g, "_").replace(/\r/g, " ").replace(/\n/g, " ");
|
|
720
|
+
}
|
|
721
|
+
function guessContentTypeByName(name) {
|
|
722
|
+
const lower = name.toLowerCase();
|
|
723
|
+
if (lower.endsWith(".png")) return "image/png";
|
|
724
|
+
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg";
|
|
725
|
+
if (lower.endsWith(".gif")) return "image/gif";
|
|
726
|
+
if (lower.endsWith(".webp")) return "image/webp";
|
|
727
|
+
if (lower.endsWith(".bmp")) return "image/bmp";
|
|
728
|
+
if (lower.endsWith(".svg")) return "image/svg+xml";
|
|
729
|
+
return null;
|
|
730
|
+
}
|
|
731
|
+
function randomBoundarySuffix() {
|
|
732
|
+
let s = "";
|
|
733
|
+
for (let i = 0; i < 32; i++) {
|
|
734
|
+
s += Math.floor(Math.random() * 16).toString(16);
|
|
735
|
+
}
|
|
736
|
+
return s;
|
|
737
|
+
}
|
|
738
|
+
|
|
523
739
|
// src/api/message-api.ts
|
|
524
740
|
var MessageApi = class extends AbstractApi {
|
|
525
741
|
constructor(config, http, rateLimitGuard) {
|
|
@@ -919,7 +1135,7 @@ function resolveConfig(input) {
|
|
|
919
1135
|
logRequest: (_g = cfg.logRequest) != null ? _g : false,
|
|
920
1136
|
rateLimitGuardEnabled: (_h = cfg.rateLimitGuardEnabled) != null ? _h : true,
|
|
921
1137
|
rateLimitCooldownMs: (_i = cfg.rateLimitCooldownMs) != null ? _i : 0,
|
|
922
|
-
userAgent: (_j = cfg.userAgent) != null ? _j : `@perk-net/perk-pushplus-sdk/1.0.
|
|
1138
|
+
userAgent: (_j = cfg.userAgent) != null ? _j : `@perk-net/perk-pushplus-sdk/1.0.1`
|
|
923
1139
|
};
|
|
924
1140
|
}
|
|
925
1141
|
|
|
@@ -1013,6 +1229,7 @@ var PushPlusClient = class _PushPlusClient {
|
|
|
1013
1229
|
this.clawBot = new ClawBotApi(this.config, this.httpRequester, this.accessKeyManager);
|
|
1014
1230
|
this.setting = new SettingApi(this.config, this.httpRequester, this.accessKeyManager);
|
|
1015
1231
|
this.pre = new PreApi(this.config, this.httpRequester, this.accessKeyManager);
|
|
1232
|
+
this.image = new ImageApi(this.config, this.httpRequester, this.accessKeyManager);
|
|
1016
1233
|
}
|
|
1017
1234
|
/** 与 Java SDK 风格一致的 Builder 入口。 */
|
|
1018
1235
|
static builder() {
|
|
@@ -1251,6 +1468,7 @@ exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL;
|
|
|
1251
1468
|
exports.ErrorCode = ErrorCode;
|
|
1252
1469
|
exports.FetchHttpRequester = FetchHttpRequester;
|
|
1253
1470
|
exports.FriendApi = FriendApi;
|
|
1471
|
+
exports.ImageApi = ImageApi;
|
|
1254
1472
|
exports.MessageApi = MessageApi;
|
|
1255
1473
|
exports.MessageTokenApi = MessageTokenApi;
|
|
1256
1474
|
exports.OpenAbstractApi = OpenAbstractApi;
|
|
@@ -1273,6 +1491,7 @@ exports.WebhookApi = WebhookApi;
|
|
|
1273
1491
|
exports.WebhookType = WebhookType;
|
|
1274
1492
|
exports.WebhookTypeDescription = WebhookTypeDescription;
|
|
1275
1493
|
exports.batchSendRequest = batchSendRequest;
|
|
1494
|
+
exports.callExecuteRaw = callExecuteRaw;
|
|
1276
1495
|
exports.errorCodeFromValue = errorCodeFromValue;
|
|
1277
1496
|
exports.isRateLimitedCode = isRateLimitedCode;
|
|
1278
1497
|
exports.isSuccessfulHttpStatus = isSuccessfulHttpStatus;
|