@octarq/plugin-links 0.0.0-stage → 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jungley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/api.ts ADDED
@@ -0,0 +1,174 @@
1
+ import { getAppLang, pluginURL, req, type StatKV } from "@octarq/plugin-sdk";
2
+
3
+
4
+ export type { StatKV };
5
+
6
+ export interface HostEntry {
7
+ host: string;
8
+ enabled: boolean;
9
+ }
10
+
11
+ export interface Domain {
12
+ id: number;
13
+ name: string;
14
+ providerAccountId?: number;
15
+ zoneId?: string;
16
+ note?: string;
17
+ forMail?: boolean;
18
+ forLink?: boolean;
19
+ linkHosts?: HostEntry[] | null;
20
+ mailHosts?: HostEntry[] | null;
21
+ createdAt?: string;
22
+ [key: string]: any;
23
+ }
24
+
25
+ export function effectiveLinkHosts(d: Domain | { hosts?: string[]; linkHosts?: HostEntry[]; name?: string }): string[] {
26
+ if ("linkHosts" in d && Array.isArray(d.linkHosts)) {
27
+ return d.linkHosts.filter((h) => h.enabled).map((h) => h.host);
28
+ }
29
+ if ("hosts" in d && Array.isArray(d.hosts)) {
30
+ return d.hosts;
31
+ }
32
+ return [];
33
+ }
34
+
35
+ export interface Overview extends Record<string, unknown> {
36
+ tokens?: number;
37
+ links?: number;
38
+ activeLinks?: number;
39
+ domains?: number;
40
+ linkDomains?: number;
41
+ mailDomains?: number;
42
+ mailboxes?: number;
43
+ emails?: number;
44
+ unread?: number;
45
+ totalClicks?: number;
46
+ clicks7d?: number;
47
+ clicks30d?: number;
48
+ botClicks7d?: number;
49
+ botClicks30d?: number;
50
+ series?: StatKV[] | null;
51
+ topLinks?: { id: number; slug: string; host: string; title: string; tags: string; clicks: number }[] | null;
52
+ devices?: StatKV[] | null;
53
+ countries?: StatKV[] | null;
54
+ cities?: StatKV[] | null;
55
+ recentEmails?: { id: number; from: string; subject: string; read: boolean; receivedAt: string }[] | null;
56
+ }
57
+
58
+ export interface Link {
59
+ id: number;
60
+ host: string;
61
+ slug: string;
62
+ target: string;
63
+ note: string;
64
+ title: string;
65
+ tags: string;
66
+ expiresAt: string | null;
67
+ expiredUrl: string;
68
+ clickLimit: number;
69
+ archived: boolean;
70
+ enabled: boolean;
71
+ clicks: number;
72
+ hasPassword: boolean;
73
+ routingRules?: any[];
74
+ createdAt: string;
75
+ }
76
+
77
+ export interface LinkStats {
78
+ total: number;
79
+ windowed: number;
80
+ days: number;
81
+ metric?: "pv" | "uv";
82
+ series: StatKV[];
83
+ referers: StatKV[] | null;
84
+ channels: StatKV[] | null;
85
+ countries: StatKV[] | null;
86
+ regions: StatKV[] | null;
87
+ devices: StatKV[] | null;
88
+ browsers: StatKV[] | null;
89
+ utmSources: StatKV[] | null;
90
+ utmMediums: StatKV[] | null;
91
+ utmCampaigns: StatKV[] | null;
92
+ variants: StatKV[] | null;
93
+ }
94
+
95
+ export interface LinksCapabilities {
96
+ geoReady: boolean;
97
+ mode: "configured" | "unavailable";
98
+ }
99
+
100
+ export const linksAiStatus = () => req<{ configured: boolean; provider: string }>("GET", "/api/links/ai/status");
101
+ export const suggestLinkSlug = (target: string, title?: string) =>
102
+ req<{ slugs: string[] }>("POST", "/api/links/ai/suggest-slug", { target, title });
103
+
104
+ export interface AbuseReport {
105
+ id: number;
106
+ slug: string;
107
+ target: string;
108
+ reason: "spam" | "phishing" | "malware" | "other" | string;
109
+ description: string;
110
+ reporterIp: string;
111
+ status: "open" | "reviewed" | "dismissed" | string;
112
+ createdAt: string;
113
+ }
114
+
115
+ export interface LinksSettingsData {
116
+ reservedSlugs: string;
117
+ }
118
+
119
+ export const linksApi = {
120
+ getSettings: () => req<LinksSettingsData>("GET", "/api/links/settings"),
121
+ updateSettings: (s: LinksSettingsData) => req<LinksSettingsData>("PUT", "/api/links/settings", s),
122
+ links: (params: { q?: string; tag?: string; host?: string; archived?: boolean; limit?: number; offset?: number } = {}) => {
123
+ const sp = new URLSearchParams();
124
+ if (params.q) sp.set("q", params.q);
125
+ if (params.tag) sp.set("tag", params.tag);
126
+ if (params.host) sp.set("host", params.host);
127
+ if (params.archived) sp.set("archived", "1");
128
+ if (params.limit) sp.set("limit", params.limit.toString());
129
+ if (params.offset) sp.set("offset", params.offset.toString());
130
+ const qs = sp.toString();
131
+ return req<Link[]>("GET", `/api/links${qs ? `?${qs}` : ""}`);
132
+ },
133
+ createLink: (l: Partial<Link> & { password?: string }) => req<Link>("POST", "/api/links", l),
134
+ updateLink: (id: number, l: Partial<Link> & { password?: string }) =>
135
+ req<Link>("PUT", `/api/links/${id}`, l),
136
+ deleteLink: (id: number) => req("DELETE", `/api/links/${id}`),
137
+ exportCsv: async (): Promise<Blob> => {
138
+ const res = await fetch(pluginURL("/api/links/export.csv"), {
139
+ credentials: "same-origin",
140
+ headers: { "Accept-Language": getAppLang() },
141
+ });
142
+ if (!res.ok) {
143
+ throw new Error(`HTTP ${res.status}`);
144
+ }
145
+ return res.blob();
146
+ },
147
+ linkStats: (id: number, days = 30, metric: "pv" | "uv" = "uv") =>
148
+ req<LinkStats>("GET", `/api/links/${id}/stats?days=${days}&metric=${metric}`),
149
+ linkMetadata: (url: string) =>
150
+ req<{ title: string; description: string; favicon: string }>(
151
+ "GET",
152
+ `/api/links/metadata?url=${encodeURIComponent(url)}`,
153
+ ),
154
+ capabilities: () => req<LinksCapabilities>("GET", "/api/links/capabilities"),
155
+ domains: (q?: { q?: string; limit?: number; offset?: number }) => {
156
+ const sp = new URLSearchParams();
157
+ if (q?.q) sp.set("q", q.q);
158
+ if (q?.limit) sp.set("limit", String(q.limit));
159
+ if (q?.offset) sp.set("offset", String(q.offset));
160
+ const qs = sp.toString();
161
+ return req<Domain[]>("GET", `/api/domains${qs ? `?${qs}` : ""}`);
162
+ },
163
+ overview: () => req<Overview>("GET", "/api/overview"),
164
+ linksAiStatus,
165
+ suggestLinkSlug,
166
+ abuseReports: (status?: string) => {
167
+ const qs = status ? `?status=${encodeURIComponent(status)}` : "";
168
+ return req<AbuseReport[]>("GET", `/api/links/abuse${qs}`);
169
+ },
170
+ updateAbuseReport: (id: number, status: string) =>
171
+ req<AbuseReport>("PUT", `/api/links/abuse/${id}`, { status }),
172
+ };
173
+
174
+ export { linksApi as api };
@@ -0,0 +1,59 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { parseLinksFilter, buildLinksFilterQuery } from "./filters";
3
+
4
+ describe("Links url-backed filters pure functions", () => {
5
+ it("1. parses query params correctly and defaults empty query to defaults", () => {
6
+ const parsed = parseLinksFilter("?q=promo&archived=1");
7
+ expect(parsed).toEqual({ q: "promo", archived: true });
8
+
9
+ const defaultParsed = parseLinksFilter("");
10
+ expect(defaultParsed).toEqual({ q: "", archived: false });
11
+
12
+ const searchOnly = parseLinksFilter("?q=hello");
13
+ expect(searchOnly).toEqual({ q: "hello", archived: false });
14
+
15
+ const archivedZero = parseLinksFilter("?archived=0");
16
+ expect(archivedZero).toEqual({ q: "", archived: false });
17
+ });
18
+
19
+ it("2. serializes filter values to query without including default values", () => {
20
+ const queryDefault = buildLinksFilterQuery({ q: "", archived: false });
21
+ expect(queryDefault.toString()).toBe("");
22
+
23
+ const querySearch = buildLinksFilterQuery({ q: "promo", archived: false });
24
+ expect(querySearch.toString()).toBe("q=promo");
25
+
26
+ const queryArchived = buildLinksFilterQuery({ q: "", archived: true });
27
+ expect(queryArchived.toString()).toBe("archived=1");
28
+
29
+ const queryBoth = buildLinksFilterQuery({ q: "promo", archived: true });
30
+ expect(queryBoth.toString()).toBe("q=promo&archived=1");
31
+ });
32
+
33
+ it("3. satisfies roundtrip consistency (parse -> serialize -> parse)", () => {
34
+ const originalFilters = { q: "promo", archived: true };
35
+ const serialized = buildLinksFilterQuery(originalFilters);
36
+ const reParsed = parseLinksFilter(serialized);
37
+ expect(reParsed).toEqual(originalFilters);
38
+
39
+ const defaultFilters = { q: "", archived: false };
40
+ const serializedDefault = buildLinksFilterQuery(defaultFilters);
41
+ const reParsedDefault = parseLinksFilter(serializedDefault);
42
+ expect(reParsedDefault).toEqual(defaultFilters);
43
+ });
44
+
45
+ it("4. preserves existing query parameters such as create=1", () => {
46
+ const existing = new URLSearchParams("create=1&foo=bar");
47
+ const updated = buildLinksFilterQuery({ q: "promo", archived: true }, existing);
48
+ expect(updated.get("create")).toBe("1");
49
+ expect(updated.get("foo")).toBe("bar");
50
+ expect(updated.get("q")).toBe("promo");
51
+ expect(updated.get("archived")).toBe("1");
52
+
53
+ const cleaned = buildLinksFilterQuery({ q: "", archived: false }, updated);
54
+ expect(cleaned.get("create")).toBe("1");
55
+ expect(cleaned.get("foo")).toBe("bar");
56
+ expect(cleaned.get("q")).toBeNull();
57
+ expect(cleaned.get("archived")).toBeNull();
58
+ });
59
+ });
package/filters.ts ADDED
@@ -0,0 +1,29 @@
1
+ export interface LinksFilterState {
2
+ q: string;
3
+ archived: boolean;
4
+ }
5
+
6
+ export function parseLinksFilter(searchParams: URLSearchParams | string): LinksFilterState {
7
+ const params = typeof searchParams === "string" ? new URLSearchParams(searchParams) : searchParams;
8
+ const q = params.get("q") || "";
9
+ const archived = params.get("archived") === "1";
10
+ return { q, archived };
11
+ }
12
+
13
+ export function buildLinksFilterQuery(
14
+ filters: LinksFilterState,
15
+ prevParams?: URLSearchParams
16
+ ): URLSearchParams {
17
+ const next = new URLSearchParams(prevParams);
18
+ if (filters.q) {
19
+ next.set("q", filters.q);
20
+ } else {
21
+ next.delete("q");
22
+ }
23
+ if (filters.archived) {
24
+ next.set("archived", "1");
25
+ } else {
26
+ next.delete("archived");
27
+ }
28
+ return next;
29
+ }