@wenyan-md/core 2.0.2 → 2.0.3
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/dist/core.js +1 -0
- package/dist/publish.js +1 -39
- package/dist/types/core/index.d.ts +1 -0
- package/dist/types/wechat/core.d.ts +3 -0
- package/dist/wechat.js +53 -0
- package/package.json +5 -1
package/dist/core.js
CHANGED
package/dist/publish.js
CHANGED
|
@@ -3,48 +3,10 @@ import { fileFromPath } from "formdata-node/file-from-path";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { stat } from "node:fs/promises";
|
|
5
5
|
import { R as RuntimeEnv } from "./runtimeEnv-pU2mTDLR.js";
|
|
6
|
+
import { createWechatClient } from "./wechat.js";
|
|
6
7
|
import { FormDataEncoder } from "form-data-encoder";
|
|
7
8
|
import { FormData } from "formdata-node";
|
|
8
9
|
import { Readable } from "node:stream";
|
|
9
|
-
const tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
|
|
10
|
-
const publishUrl = "https://api.weixin.qq.com/cgi-bin/draft/add";
|
|
11
|
-
const uploadUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
|
|
12
|
-
function createWechatClient(adapter) {
|
|
13
|
-
return {
|
|
14
|
-
async fetchAccessToken(appId, appSecret) {
|
|
15
|
-
const res = await adapter.fetch(
|
|
16
|
-
`${tokenUrl}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
|
|
17
|
-
);
|
|
18
|
-
if (!res.ok) throw new Error(await res.text());
|
|
19
|
-
return res.json();
|
|
20
|
-
},
|
|
21
|
-
async uploadMaterial(type, file, filename, accessToken) {
|
|
22
|
-
const multipart = adapter.createMultipart("media", file, filename);
|
|
23
|
-
const res = await adapter.fetch(`${uploadUrl}?access_token=${accessToken}&type=${type}`, {
|
|
24
|
-
...multipart,
|
|
25
|
-
method: "POST"
|
|
26
|
-
});
|
|
27
|
-
const data = await res.json();
|
|
28
|
-
if (data.errcode && data.errcode !== 0) {
|
|
29
|
-
throw new Error(`${data.errcode}: ${data.errmsg}`);
|
|
30
|
-
}
|
|
31
|
-
if (data.url?.startsWith("http://")) {
|
|
32
|
-
data.url = data.url.replace(/^http:\/\//i, "https://");
|
|
33
|
-
}
|
|
34
|
-
return data;
|
|
35
|
-
},
|
|
36
|
-
async publishArticle(title, content, thumbMediaId, accessToken) {
|
|
37
|
-
const res = await adapter.fetch(`${publishUrl}?access_token=${accessToken}`, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
body: JSON.stringify({
|
|
40
|
-
articles: [{ title, content, thumb_media_id: thumbMediaId }]
|
|
41
|
-
})
|
|
42
|
-
});
|
|
43
|
-
if (!res.ok) throw new Error(await res.text());
|
|
44
|
-
return res.json();
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
10
|
const nodeHttpAdapter = {
|
|
49
11
|
fetch,
|
|
50
12
|
createMultipart(field, file, filename) {
|
|
@@ -4,3 +4,6 @@ export declare function createWechatClient(adapter: HttpAdapter): {
|
|
|
4
4
|
uploadMaterial(type: string, file: Blob, filename: string, accessToken: string): Promise<any>;
|
|
5
5
|
publishArticle(title: string, content: string, thumbMediaId: string, accessToken: string): Promise<any>;
|
|
6
6
|
};
|
|
7
|
+
export type WechatClient = ReturnType<typeof createWechatClient>;
|
|
8
|
+
export * from "./http.js";
|
|
9
|
+
export * from "./adapters/browser.js";
|
package/dist/wechat.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const browserHttpAdapter = {
|
|
2
|
+
fetch: window.fetch.bind(window),
|
|
3
|
+
createMultipart(field, file, filename) {
|
|
4
|
+
const form = new FormData();
|
|
5
|
+
form.append(field, file, filename);
|
|
6
|
+
return {
|
|
7
|
+
body: form
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
|
|
12
|
+
const publishUrl = "https://api.weixin.qq.com/cgi-bin/draft/add";
|
|
13
|
+
const uploadUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
|
|
14
|
+
function createWechatClient(adapter) {
|
|
15
|
+
return {
|
|
16
|
+
async fetchAccessToken(appId, appSecret) {
|
|
17
|
+
const res = await adapter.fetch(
|
|
18
|
+
`${tokenUrl}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`
|
|
19
|
+
);
|
|
20
|
+
if (!res.ok) throw new Error(await res.text());
|
|
21
|
+
return res.json();
|
|
22
|
+
},
|
|
23
|
+
async uploadMaterial(type, file, filename, accessToken) {
|
|
24
|
+
const multipart = adapter.createMultipart("media", file, filename);
|
|
25
|
+
const res = await adapter.fetch(`${uploadUrl}?access_token=${accessToken}&type=${type}`, {
|
|
26
|
+
...multipart,
|
|
27
|
+
method: "POST"
|
|
28
|
+
});
|
|
29
|
+
const data = await res.json();
|
|
30
|
+
if (data.errcode && data.errcode !== 0) {
|
|
31
|
+
throw new Error(`${data.errcode}: ${data.errmsg}`);
|
|
32
|
+
}
|
|
33
|
+
if (data.url?.startsWith("http://")) {
|
|
34
|
+
data.url = data.url.replace(/^http:\/\//i, "https://");
|
|
35
|
+
}
|
|
36
|
+
return data;
|
|
37
|
+
},
|
|
38
|
+
async publishArticle(title, content, thumbMediaId, accessToken) {
|
|
39
|
+
const res = await adapter.fetch(`${publishUrl}?access_token=${accessToken}`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
articles: [{ title, content, thumb_media_id: thumbMediaId }]
|
|
43
|
+
})
|
|
44
|
+
});
|
|
45
|
+
if (!res.ok) throw new Error(await res.text());
|
|
46
|
+
return res.json();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
browserHttpAdapter,
|
|
52
|
+
createWechatClient
|
|
53
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wenyan-md/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Core library for Wenyan markdown rendering & publishing",
|
|
5
5
|
"author": "Lei <caol64@gmail.com> (https://github.com/caol64)",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -38,6 +38,10 @@
|
|
|
38
38
|
"./wrapper": {
|
|
39
39
|
"import": "./dist/wrapper.js",
|
|
40
40
|
"types": "./dist/types/node/wrapper.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./wechat": {
|
|
43
|
+
"import": "./dist/wechat.js",
|
|
44
|
+
"types": "./dist/types/wechat/core.d.ts"
|
|
41
45
|
}
|
|
42
46
|
},
|
|
43
47
|
"devDependencies": {
|