@zhin.js/plugin-short-url 0.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/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
package/lib/index.js ADDED
@@ -0,0 +1,80 @@
1
+ import { usePlugin, MessageCommand, ZhinTool } from 'zhin.js';
2
+ const { addCommand, addTool, logger } = usePlugin();
3
+ // ── helpers ──────────────────────────────────────────────
4
+ function isValidUrl(input) {
5
+ try {
6
+ const u = new URL(input);
7
+ return u.protocol === 'http:' || u.protocol === 'https:';
8
+ }
9
+ catch {
10
+ return false;
11
+ }
12
+ }
13
+ async function shortenUrl(url) {
14
+ const api = `https://is.gd/create.php?format=json&url=${encodeURIComponent(url)}`;
15
+ const res = await fetch(api);
16
+ if (!res.ok)
17
+ throw new Error(`is.gd 返回 ${res.status}`);
18
+ const data = (await res.json());
19
+ if (data.errorcode)
20
+ throw new Error(data.errormessage ?? '缩短失败');
21
+ return data.shorturl;
22
+ }
23
+ async function expandUrl(url) {
24
+ const res = await fetch(url, { redirect: 'manual' });
25
+ const location = res.headers.get('location');
26
+ if (location)
27
+ return location;
28
+ if (res.ok)
29
+ return url;
30
+ throw new Error(`无法展开链接 (${res.status})`);
31
+ }
32
+ // ── commands ─────────────────────────────────────────────
33
+ addCommand(new MessageCommand('短链 <url:text>')
34
+ .desc('缩短一个 URL')
35
+ .action(async (_message, result) => {
36
+ const url = result.params.url;
37
+ if (!isValidUrl(url))
38
+ return '请提供有效的 HTTP/HTTPS 链接';
39
+ try {
40
+ const short = await shortenUrl(url);
41
+ return `短链接: ${short}`;
42
+ }
43
+ catch (e) {
44
+ logger.warn('短链生成失败', e);
45
+ return `缩短失败: ${e.message}`;
46
+ }
47
+ }));
48
+ addCommand(new MessageCommand('展开 <url:text>')
49
+ .desc('展开一个短链接,显示原始地址')
50
+ .action(async (_message, result) => {
51
+ const url = result.params.url;
52
+ if (!isValidUrl(url))
53
+ return '请提供有效的 HTTP/HTTPS 链接';
54
+ try {
55
+ const original = await expandUrl(url);
56
+ return `原始链接: ${original}`;
57
+ }
58
+ catch (e) {
59
+ logger.warn('链接展开失败', e);
60
+ return `展开失败: ${e.message}`;
61
+ }
62
+ }));
63
+ // ── AI tool ──────────────────────────────────────────────
64
+ addTool(new ZhinTool('short_url')
65
+ .desc('缩短一个 URL,返回短链接')
66
+ .keyword('短链', '缩短', 'shorten', 'short url')
67
+ .param('url', { type: 'string', description: '要缩短的完整 URL' }, true)
68
+ .execute(async (args) => {
69
+ const url = args.url;
70
+ if (!isValidUrl(url))
71
+ return '无效的 URL,需要 http:// 或 https:// 开头';
72
+ try {
73
+ return await shortenUrl(url);
74
+ }
75
+ catch (e) {
76
+ return `缩短失败: ${e.message}`;
77
+ }
78
+ })
79
+ .toTool());
80
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAE7D,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,CAAA;AAEnD,4DAA4D;AAE5D,SAAS,UAAU,CAAC,KAAa;IAC/B,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAA;QACxB,OAAO,CAAC,CAAC,QAAQ,KAAK,OAAO,IAAI,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAA;IAC1D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,GAAW;IACnC,MAAM,GAAG,GAAG,4CA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MAAM,UAAU,CAAC,GAAG,CAAC,CAAA;IAC9B,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,OAAO,SAAS,CAAC,CAAC,OAAO,EAAE,CAAA;IAC7B,CAAC;AACH,CAAC,CAAC;KACD,MAAM,EAAE,CACZ,CAAA"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@zhin.js/plugin-short-url",
3
+ "version": "0.0.1",
4
+ "description": "URL shortener plugin for Zhin.js — shorten & expand URLs",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "development": "./src/index.ts",
12
+ "import": "./lib/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": ["src", "lib", "README.md"],
17
+ "scripts": {
18
+ "build": "tsc --build",
19
+ "clean": "rimraf lib"
20
+ },
21
+ "keywords": ["zhin", "zhin.js", "bot", "plugin", "short-url", "url-shortener"],
22
+ "author": { "name": "lc-cn", "email": "admin@liucl.cn", "url": "https://github.com/lc-cn" },
23
+ "license": "MIT",
24
+ "dependencies": {},
25
+ "devDependencies": { "typescript": "^5.9.3", "zhin.js": "workspace:*" },
26
+ "peerDependencies": { "zhin.js": ">=1.0.60" },
27
+ "repository": { "type": "git", "url": "git+https://github.com/zhinjs/zhin.git", "directory": "plugins/utils/short-url" },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org"
31
+ }
32
+ }
package/src/index.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { usePlugin, MessageCommand, ZhinTool } from 'zhin.js'
2
+
3
+ const { addCommand, addTool, logger } = usePlugin()
4
+
5
+ // ── helpers ──────────────────────────────────────────────
6
+
7
+ function isValidUrl(input: string): boolean {
8
+ try {
9
+ const u = new URL(input)
10
+ return u.protocol === 'http:' || u.protocol === 'https:'
11
+ } catch {
12
+ return false
13
+ }
14
+ }
15
+
16
+ async function shortenUrl(url: string): Promise<string> {
17
+ const api = `https://is.gd/create.php?format=json&url=${encodeURIComponent(url)}`
18
+ const res = await fetch(api)
19
+ if (!res.ok) throw new Error(`is.gd 返回 ${res.status}`)
20
+ const data = (await res.json()) as { shorturl?: string; errorcode?: number; errormessage?: string }
21
+ if (data.errorcode) throw new Error(data.errormessage ?? '缩短失败')
22
+ return data.shorturl!
23
+ }
24
+
25
+ async function expandUrl(url: string): Promise<string> {
26
+ const res = await fetch(url, { redirect: 'manual' })
27
+ const location = res.headers.get('location')
28
+ if (location) return location
29
+ if (res.ok) return url
30
+ throw new Error(`无法展开链接 (${res.status})`)
31
+ }
32
+
33
+ // ── commands ─────────────────────────────────────────────
34
+
35
+ addCommand(
36
+ new MessageCommand('短链 <url:text>')
37
+ .desc('缩短一个 URL')
38
+ .action(async (_message, result) => {
39
+ const url = result.params.url as string
40
+ if (!isValidUrl(url)) return '请提供有效的 HTTP/HTTPS 链接'
41
+ try {
42
+ const short = await shortenUrl(url)
43
+ return `短链接: ${short}`
44
+ } catch (e: any) {
45
+ logger.warn('短链生成失败', e)
46
+ return `缩短失败: ${e.message}`
47
+ }
48
+ }),
49
+ )
50
+
51
+ addCommand(
52
+ new MessageCommand('展开 <url:text>')
53
+ .desc('展开一个短链接,显示原始地址')
54
+ .action(async (_message, result) => {
55
+ const url = result.params.url as string
56
+ if (!isValidUrl(url)) return '请提供有效的 HTTP/HTTPS 链接'
57
+ try {
58
+ const original = await expandUrl(url)
59
+ return `原始链接: ${original}`
60
+ } catch (e: any) {
61
+ logger.warn('链接展开失败', e)
62
+ return `展开失败: ${e.message}`
63
+ }
64
+ }),
65
+ )
66
+
67
+ // ── AI tool ──────────────────────────────────────────────
68
+
69
+ addTool(
70
+ new ZhinTool('short_url')
71
+ .desc('缩短一个 URL,返回短链接')
72
+ .keyword('短链', '缩短', 'shorten', 'short url')
73
+ .param('url', { type: 'string', description: '要缩短的完整 URL' }, true)
74
+ .execute(async (args) => {
75
+ const url = args.url as string
76
+ if (!isValidUrl(url)) return '无效的 URL,需要 http:// 或 https:// 开头'
77
+ try {
78
+ return await shortenUrl(url)
79
+ } catch (e: any) {
80
+ return `缩短失败: ${e.message}`
81
+ }
82
+ })
83
+ .toTool(),
84
+ )