md2wechat-mcp 0.2.0 → 0.2.2
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 +304 -5
- package/dist/browser.js +2 -0
- package/dist/cache.js +4 -3
- package/dist/cli.js +10 -1
- package/dist/markdown.d.ts +2 -2
- package/dist/markdown.js +11 -7
- package/dist/server.js +76 -11
- package/dist/themes.d.ts +2 -0
- package/dist/themes.js +61 -35
- package/dist/tools.js +645 -16
- package/dist/wechat-api.d.ts +10 -0
- package/dist/wechat-api.js +51 -1
- package/package.json +1 -1
package/dist/wechat-api.d.ts
CHANGED
|
@@ -48,6 +48,16 @@ export declare function draftBatchGet(accessToken: string, offset: number, count
|
|
|
48
48
|
export declare function uploadImage(accessToken: string, filePath: string): Promise<{
|
|
49
49
|
url: string;
|
|
50
50
|
}>;
|
|
51
|
+
export type PermanentMaterialType = "image" | "voice" | "video" | "thumb";
|
|
52
|
+
export interface MaterialDescription {
|
|
53
|
+
title?: string;
|
|
54
|
+
introduction?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface AddMaterialResult {
|
|
57
|
+
media_id: string;
|
|
58
|
+
url?: string;
|
|
59
|
+
}
|
|
60
|
+
export declare function addMaterial(accessToken: string, type: PermanentMaterialType, filePath: string, description?: MaterialDescription): Promise<AddMaterialResult>;
|
|
51
61
|
export declare function draftDelete(accessToken: string, mediaId: string): Promise<{
|
|
52
62
|
errcode: number;
|
|
53
63
|
errmsg: string;
|
package/dist/wechat-api.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { basename, extname } from "node:path";
|
|
3
3
|
const WECHAT_BASE = "https://api.weixin.qq.com";
|
|
4
|
+
const DEFAULT_WECHAT_TIMEOUT_MS = 15000;
|
|
5
|
+
function getWechatTimeoutMs() {
|
|
6
|
+
const raw = process.env.WECHAT_API_TIMEOUT_MS;
|
|
7
|
+
const parsed = raw ? Number.parseInt(raw, 10) : NaN;
|
|
8
|
+
if (Number.isFinite(parsed) && parsed > 0) {
|
|
9
|
+
return parsed;
|
|
10
|
+
}
|
|
11
|
+
return DEFAULT_WECHAT_TIMEOUT_MS;
|
|
12
|
+
}
|
|
4
13
|
async function wechatFetch(url, options) {
|
|
5
|
-
const
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
const timeoutMs = getWechatTimeoutMs();
|
|
16
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
17
|
+
let res;
|
|
18
|
+
try {
|
|
19
|
+
res = await fetch(url, { ...options, signal: controller.signal });
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error.name === "AbortError") {
|
|
23
|
+
throw new Error(`WeChat API request timeout after ${timeoutMs}ms`);
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
}
|
|
6
30
|
if (!res.ok) {
|
|
7
31
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
8
32
|
}
|
|
@@ -49,6 +73,32 @@ export async function uploadImage(accessToken, filePath) {
|
|
|
49
73
|
form.append("media", new Blob([fileBuffer], { type: mimeType }), basename(filePath));
|
|
50
74
|
return wechatFetch(url, { method: "POST", body: form });
|
|
51
75
|
}
|
|
76
|
+
function getMimeTypeByExtension(filePath) {
|
|
77
|
+
const ext = extname(filePath).toLowerCase();
|
|
78
|
+
const map = {
|
|
79
|
+
".jpg": "image/jpeg",
|
|
80
|
+
".jpeg": "image/jpeg",
|
|
81
|
+
".png": "image/png",
|
|
82
|
+
".gif": "image/gif",
|
|
83
|
+
".bmp": "image/bmp",
|
|
84
|
+
".mp3": "audio/mpeg",
|
|
85
|
+
".wma": "audio/x-ms-wma",
|
|
86
|
+
".wav": "audio/wav",
|
|
87
|
+
".amr": "audio/amr",
|
|
88
|
+
".mp4": "video/mp4"
|
|
89
|
+
};
|
|
90
|
+
return map[ext] ?? "application/octet-stream";
|
|
91
|
+
}
|
|
92
|
+
export async function addMaterial(accessToken, type, filePath, description) {
|
|
93
|
+
const url = `${WECHAT_BASE}/cgi-bin/material/add_material?access_token=${encodeURIComponent(accessToken)}&type=${encodeURIComponent(type)}`;
|
|
94
|
+
const fileBuffer = await readFile(filePath);
|
|
95
|
+
const form = new FormData();
|
|
96
|
+
form.append("media", new Blob([fileBuffer], { type: getMimeTypeByExtension(filePath) }), basename(filePath));
|
|
97
|
+
if (type === "video" && description) {
|
|
98
|
+
form.append("description", JSON.stringify(description));
|
|
99
|
+
}
|
|
100
|
+
return wechatFetch(url, { method: "POST", body: form });
|
|
101
|
+
}
|
|
52
102
|
export async function draftDelete(accessToken, mediaId) {
|
|
53
103
|
const url = `${WECHAT_BASE}/cgi-bin/draft/delete?access_token=${encodeURIComponent(accessToken)}`;
|
|
54
104
|
return wechatFetch(url, {
|