@kgma/linkdevi-tools 0.0.0-stage → 0.1.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/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ export * from "./campaign.js";
2
+ import { validateCampaign } from "./campaign.js";
3
+ export class LinkDeviError extends Error {
4
+ status;
5
+ code;
6
+ retryAfter;
7
+ ambiguous;
8
+ constructor(status, code, retryAfter = null, ambiguous = false) {
9
+ super(ambiguous ? "Response unavailable; creation may have committed. Reuse the original requestId and exact payload." : `LinkDevi request failed (HTTP ${status || "unavailable"}).`);
10
+ this.status = status;
11
+ this.code = code;
12
+ this.retryAfter = retryAfter;
13
+ this.ambiguous = ambiguous;
14
+ this.name = "LinkDeviError";
15
+ }
16
+ }
17
+ export class LinkDeviClient {
18
+ origin;
19
+ apiKey;
20
+ fetchImpl;
21
+ constructor(options) {
22
+ this.origin = new URL(options.baseUrl ?? "https://linkdevi.com");
23
+ const u = this.origin;
24
+ if (u.username || u.password || u.search || u.hash || u.pathname !== "/" || !(u.protocol === "https:" || (u.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(u.hostname))))
25
+ throw new Error("Use an HTTPS service origin, or localhost HTTP for testing");
26
+ if (!/^su_[a-zA-Z0-9]+$/.test(options.apiKey ?? ""))
27
+ throw new Error("A scoped LinkDevi API key is required");
28
+ this.apiKey = options.apiKey;
29
+ this.fetchImpl = options.fetch ?? fetch;
30
+ }
31
+ async call(action, args = {}, mutates = false) {
32
+ let response;
33
+ try {
34
+ response = await this.fetchImpl(new URL("/api/v1/agent", this.origin), { method: "POST", redirect: "error", signal: AbortSignal.timeout(25000), headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ action, ...args }) });
35
+ }
36
+ catch {
37
+ throw new LinkDeviError(0, "NETWORK_UNAVAILABLE", null, mutates);
38
+ }
39
+ let data;
40
+ try {
41
+ data = await response.json();
42
+ }
43
+ catch {
44
+ throw new LinkDeviError(response.status, "INVALID_RESPONSE", null, mutates);
45
+ }
46
+ if (!response.ok) {
47
+ const retry = response.headers.get("retry-after"), seconds = retry ? Number(retry) : NaN;
48
+ const rawCode = data && typeof data === "object" && "error" in data ? String(data.error) : "HTTP_ERROR";
49
+ const code = /^[a-zA-Z_]{1,64}$/.test(rawCode) && !rawCode.includes(this.apiKey) ? rawCode : "HTTP_ERROR";
50
+ throw new LinkDeviError(response.status, code, Number.isFinite(seconds) && seconds >= 0 ? seconds : null);
51
+ }
52
+ if (!data || typeof data !== "object" || Array.isArray(data))
53
+ throw new LinkDeviError(response.status, "INVALID_RESPONSE", null, mutates);
54
+ return data;
55
+ }
56
+ capabilities() { return this.call("capabilities"); }
57
+ previewCampaign(campaign) { return this.call("preview_campaign", { campaign: validateCampaign(campaign, true) }); }
58
+ createCampaign(campaign) { return this.call("create_campaign", { campaign: validateCampaign(campaign, true) }, true); }
59
+ listCampaigns(page = 1) {
60
+ if (!Number.isInteger(page) || page < 1 || page > 10000)
61
+ throw new Error("Invalid page");
62
+ return this.call("list_campaigns", { page });
63
+ }
64
+ campaignStats(campaignId) { return this.call("campaign_stats", { campaignId: this.id(campaignId) }); }
65
+ linkQr(linkId, size = 300) {
66
+ if (!Number.isInteger(size) || size < 100 || size > 512)
67
+ throw new Error("Size must be 100–512");
68
+ return this.call("link_qr", { linkId: this.id(linkId), size });
69
+ }
70
+ id(value) { if (!/^[a-zA-Z0-9_-]{1,100}$/.test(value))
71
+ throw new Error("Invalid resource ID"); return value; }
72
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "requestId": "a43c37c0-350a-44ef-9d40-a5900c246891",
3
+ "name": "December launch",
4
+ "url": "https://example.com/sale",
5
+ "utmCampaign": "december-launch",
6
+ "domain": "linkdevi.com",
7
+ "channels": [
8
+ "facebook",
9
+ "line",
10
+ "print"
11
+ ]
12
+ }
@@ -0,0 +1,2 @@
1
+ url,name,utmCampaign,channels
2
+ https://example.com/sale,December launch,december-launch,facebook|line|print
@@ -0,0 +1,13 @@
1
+ import { LinkDeviClient, previewCampaign, type CampaignRequest } from "../dist/index.js";
2
+ // Save this intent's UUID and payload before creating. Keep both unchanged on retry.
3
+ const intent: CampaignRequest = {
4
+ requestId: "a43c37c0-350a-44ef-9d40-a5900c246891",
5
+ name: "December launch", url: "https://example.com/sale", utmCampaign: "december-launch",
6
+ domain: "linkdevi.com", channels: ["facebook", "line", "print"],
7
+ };
8
+ console.log(previewCampaign(intent)); // Offline, no key and no quota.
9
+ // Opt in deliberately after reviewing the preview. No model is invoked.
10
+ if (process.env.LINKDEVI_CREATE === "1") {
11
+ const client = new LinkDeviClient({ apiKey: process.env.LINKDEVI_API_KEY ?? "" });
12
+ console.log(await client.createCampaign(intent));
13
+ }
package/package.json CHANGED
@@ -1,6 +1,61 @@
1
1
  {
2
2
  "name": "@kgma/linkdevi-tools",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Offline campaign launch kits, a typed LinkDevi API client and a campaign CLI.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": ">=22"
9
+ },
10
+ "bin": {
11
+ "linkdevi": "dist/cli.js"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./campaign": {
19
+ "types": "./dist/campaign.d.ts",
20
+ "import": "./dist/campaign.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "examples",
26
+ "workflows",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "registry": "https://registry.npmjs.org/"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc -p tsconfig.json",
36
+ "test": "npm run build && node --test test/*.test.mjs",
37
+ "prepack": "npm run build"
38
+ },
39
+ "devDependencies": {
40
+ "typescript": "5.9.3",
41
+ "@types/node": "25.5.0"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/kgma-glitch/linkdevi-tools.git"
46
+ },
47
+ "homepage": "https://linkdevi.com/tools/campaign-launch-kit",
48
+ "bugs": {
49
+ "url": "https://github.com/kgma-glitch/linkdevi-tools/issues"
50
+ },
51
+ "keywords": [
52
+ "linkdevi",
53
+ "url-shortener",
54
+ "utm",
55
+ "campaign",
56
+ "cli",
57
+ "sdk",
58
+ "n8n",
59
+ "ai"
60
+ ]
61
+ }
@@ -0,0 +1,211 @@
1
+ {
2
+ "name": "LinkDevi — Google Sheets to saved campaign links",
3
+ "nodes": [
4
+ {
5
+ "id": "run-manually",
6
+ "name": "Run manually",
7
+ "type": "n8n-nodes-base.manualTrigger",
8
+ "typeVersion": 1,
9
+ "position": [
10
+ 0,
11
+ 0
12
+ ],
13
+ "parameters": {}
14
+ },
15
+ {
16
+ "id": "read-campaign-rows",
17
+ "name": "Read campaign rows",
18
+ "type": "n8n-nodes-base.googleSheets",
19
+ "typeVersion": 4.7,
20
+ "position": [
21
+ 250,
22
+ 0
23
+ ],
24
+ "parameters": {
25
+ "operation": "read",
26
+ "documentId": {
27
+ "__rl": true,
28
+ "value": "REPLACE_WITH_SPREADSHEET_ID",
29
+ "mode": "id"
30
+ },
31
+ "sheetName": {
32
+ "__rl": true,
33
+ "value": "Campaigns",
34
+ "mode": "name"
35
+ },
36
+ "options": {}
37
+ }
38
+ },
39
+ {
40
+ "id": "prepare-campaign-rows",
41
+ "name": "Prepare campaign rows",
42
+ "type": "n8n-nodes-base.code",
43
+ "typeVersion": 2,
44
+ "position": [
45
+ 500,
46
+ 0
47
+ ],
48
+ "parameters": {
49
+ "mode": "runOnceForAllItems",
50
+ "jsCode": "const uuid=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\nconst allowed=['facebook','instagram','tiktok','line','email','print','youtube','x'];\nconst input=$input.all(); if(input.length>100) throw new Error('Read at most 100 sheet rows per run');\nconst used=new Set();\nreturn input.flatMap((item,index)=>{\n const r=item.json; if(r.shortUrls) return [];\n if(!uuid.test(String(r.requestId)) || used.has(r.requestId)) throw new Error('Each row needs its own UUID requestId, saved in the sheet before running');\n used.add(r.requestId);\n const channels=String(r.channels??'').split('|').map(x=>x.trim());\n if(!channels.length || new Set(channels).size!==channels.length || channels.some(c=>!allowed.includes(c))) throw new Error('Invalid channels');\n if(!r.name || !r.utmCampaign || !/^https?:\\/\\//i.test(String(r.url)) || /^https?:\\/\\/[^/]*@/i.test(String(r.url))) throw new Error('Provide name, URL and utmCampaign');\n return [{json:{requestId:r.requestId,campaign:{requestId:r.requestId,name:String(r.name),url:String(r.url),utmCampaign:String(r.utmCampaign),domain:String(r.domain||'linkdevi.com'),channels}},pairedItem:{item:index}}];\n});"
51
+ }
52
+ },
53
+ {
54
+ "id": "create-saved-campaign",
55
+ "name": "Create saved campaign",
56
+ "type": "n8n-nodes-base.httpRequest",
57
+ "typeVersion": 4.3,
58
+ "position": [
59
+ 750,
60
+ 0
61
+ ],
62
+ "parameters": {
63
+ "method": "POST",
64
+ "url": "https://linkdevi.com/api/v1/agent",
65
+ "authentication": "genericCredentialType",
66
+ "genericAuthType": "httpHeaderAuth",
67
+ "sendBody": true,
68
+ "specifyBody": "json",
69
+ "jsonBody": "={{ { action: 'create_campaign', campaign: $json.campaign } }}",
70
+ "options": {
71
+ "timeout": 25000,
72
+ "redirect": {
73
+ "redirect": {
74
+ "followRedirects": false
75
+ }
76
+ },
77
+ "batching": {
78
+ "batch": {
79
+ "batchSize": 1,
80
+ "batchInterval": 2500
81
+ }
82
+ }
83
+ }
84
+ },
85
+ "retryOnFail": false,
86
+ "onError": "stopWorkflow"
87
+ },
88
+ {
89
+ "id": "write-links-to-sheet",
90
+ "name": "Write links to sheet",
91
+ "type": "n8n-nodes-base.googleSheets",
92
+ "typeVersion": 4.7,
93
+ "position": [
94
+ 1000,
95
+ 0
96
+ ],
97
+ "parameters": {
98
+ "operation": "update",
99
+ "documentId": {
100
+ "__rl": true,
101
+ "value": "REPLACE_WITH_SPREADSHEET_ID",
102
+ "mode": "id"
103
+ },
104
+ "sheetName": {
105
+ "__rl": true,
106
+ "value": "Campaigns",
107
+ "mode": "name"
108
+ },
109
+ "columns": {
110
+ "mappingMode": "defineBelow",
111
+ "value": {
112
+ "requestId": "={{ $('Prepare campaign rows').item.json.requestId }}",
113
+ "shortUrls": "={{ $json.campaign.channels.map(c => c.link?.shortUrl || '').filter(Boolean).join(' | ') }}",
114
+ "campaignId": "={{ $json.campaign.id }}"
115
+ },
116
+ "matchingColumns": [
117
+ "requestId"
118
+ ],
119
+ "schema": [
120
+ {
121
+ "id": "requestId",
122
+ "displayName": "requestId",
123
+ "type": "string",
124
+ "canBeUsedToMatch": true
125
+ },
126
+ {
127
+ "id": "shortUrls",
128
+ "displayName": "shortUrls",
129
+ "type": "string"
130
+ },
131
+ {
132
+ "id": "campaignId",
133
+ "displayName": "campaignId",
134
+ "type": "string"
135
+ }
136
+ ]
137
+ },
138
+ "options": {
139
+ "cellFormat": "RAW"
140
+ }
141
+ }
142
+ },
143
+ {
144
+ "id": "read-before-running",
145
+ "name": "Read before running",
146
+ "type": "n8n-nodes-base.stickyNote",
147
+ "typeVersion": 1,
148
+ "position": [
149
+ 0,
150
+ -430
151
+ ],
152
+ "parameters": {
153
+ "content": "## Setup\n1. Create a LinkDevi account, then a scoped key with links:read and links:write. Hosted plan limits apply.\n2. In Create saved campaign, select a Header Auth credential: header name Authorization, value Bearer YOUR_SCOPED_KEY. Keep the key only in n8n Credentials.\n3. Keep retries disabled. Redirects are disabled so credentials stay on the configured origin.\n4. Preview your data before running. Each selected channel uses one stored-link slot. A timeout may have committed: preserve requestId and the exact campaign payload on retry.\n5. These workflows do not call an LLM, send email, post to social media or enable a schedule. Activate only after reviewing your own setup.\nGuide: https://linkdevi.com/features/ai\n\n## Sheet columns\nrequestId, name, url, utmCampaign, channels, domain, shortUrls, campaignId\nUse one UNIQUE, pre-saved UUID per row. channels uses |, e.g. facebook|line|print. Rows with shortUrls are skipped. Configure the same spreadsheet in both Sheets nodes and select your Google Sheets credential. Output matching uses requestId, not a changing row position. Limit input to 100 rows; 429 stops the workflow. Review your data and credentials before running.",
154
+ "width": 700,
155
+ "height": 360
156
+ }
157
+ }
158
+ ],
159
+ "connections": {
160
+ "Run manually": {
161
+ "main": [
162
+ [
163
+ {
164
+ "node": "Read campaign rows",
165
+ "type": "main",
166
+ "index": 0
167
+ }
168
+ ]
169
+ ]
170
+ },
171
+ "Read campaign rows": {
172
+ "main": [
173
+ [
174
+ {
175
+ "node": "Prepare campaign rows",
176
+ "type": "main",
177
+ "index": 0
178
+ }
179
+ ]
180
+ ]
181
+ },
182
+ "Prepare campaign rows": {
183
+ "main": [
184
+ [
185
+ {
186
+ "node": "Create saved campaign",
187
+ "type": "main",
188
+ "index": 0
189
+ }
190
+ ]
191
+ ]
192
+ },
193
+ "Create saved campaign": {
194
+ "main": [
195
+ [
196
+ {
197
+ "node": "Write links to sheet",
198
+ "type": "main",
199
+ "index": 0
200
+ }
201
+ ]
202
+ ]
203
+ }
204
+ },
205
+ "active": false,
206
+ "settings": {
207
+ "executionOrder": "v1"
208
+ },
209
+ "pinData": {},
210
+ "tags": []
211
+ }
@@ -0,0 +1,164 @@
1
+ {
2
+ "name": "LinkDevi — Campaign brief to saved links and CSV",
3
+ "nodes": [
4
+ {
5
+ "id": "run-manually",
6
+ "name": "Run manually",
7
+ "type": "n8n-nodes-base.manualTrigger",
8
+ "typeVersion": 1,
9
+ "position": [
10
+ 0,
11
+ 0
12
+ ],
13
+ "parameters": {}
14
+ },
15
+ {
16
+ "id": "campaign-brief",
17
+ "name": "Campaign brief",
18
+ "type": "n8n-nodes-base.code",
19
+ "typeVersion": 2,
20
+ "position": [
21
+ 250,
22
+ 0
23
+ ],
24
+ "parameters": {
25
+ "mode": "runOnceForAllItems",
26
+ "jsCode": "return [{json:{campaign:{requestId:'a43c37c0-350a-44ef-9d40-a5900c246891',name:'December launch',url:'https://example.com/sale',utmCampaign:'december-launch',domain:'linkdevi.com',channels:['facebook','line','print']}}}];"
27
+ }
28
+ },
29
+ {
30
+ "id": "create-saved-campaign",
31
+ "name": "Create saved campaign",
32
+ "type": "n8n-nodes-base.httpRequest",
33
+ "typeVersion": 4.3,
34
+ "position": [
35
+ 500,
36
+ 0
37
+ ],
38
+ "parameters": {
39
+ "method": "POST",
40
+ "url": "https://linkdevi.com/api/v1/agent",
41
+ "authentication": "genericCredentialType",
42
+ "genericAuthType": "httpHeaderAuth",
43
+ "sendBody": true,
44
+ "specifyBody": "json",
45
+ "jsonBody": "={{ { action: 'create_campaign', campaign: $json.campaign } }}",
46
+ "options": {
47
+ "timeout": 25000,
48
+ "redirect": {
49
+ "redirect": {
50
+ "followRedirects": false
51
+ }
52
+ },
53
+ "batching": {
54
+ "batch": {
55
+ "batchSize": 1,
56
+ "batchInterval": 2500
57
+ }
58
+ }
59
+ }
60
+ },
61
+ "retryOnFail": false,
62
+ "onError": "stopWorkflow"
63
+ },
64
+ {
65
+ "id": "csv-rows",
66
+ "name": "CSV rows",
67
+ "type": "n8n-nodes-base.code",
68
+ "typeVersion": 2,
69
+ "position": [
70
+ 750,
71
+ 0
72
+ ],
73
+ "parameters": {
74
+ "mode": "runOnceForAllItems",
75
+ "jsCode": "const safe = value => { const s=String(value ?? ''); return /^[\\s\\u0000-\\u001f]*[=+@-]/.test(s) ? \"'\"+s : s; };\nreturn $input.all().flatMap((item,index)=>{\n const c=item.json.campaign;\n if(!c || !Array.isArray(c.channels)) throw new Error('Missing saved campaign response');\n return c.channels.map(row=>({json:{campaign:safe(c.name),channel:row.key,short_url:row.link?.shortUrl ?? '',destination_url:row.link?.url ?? '',status:row.link?'saved':'unavailable'},pairedItem:{item:index}}));\n});"
76
+ }
77
+ },
78
+ {
79
+ "id": "download-csv",
80
+ "name": "Download CSV",
81
+ "type": "n8n-nodes-base.convertToFile",
82
+ "typeVersion": 1.1,
83
+ "position": [
84
+ 1000,
85
+ 0
86
+ ],
87
+ "parameters": {
88
+ "operation": "csv",
89
+ "binaryPropertyName": "data",
90
+ "options": {
91
+ "fileName": "linkdevi-campaign-links.csv",
92
+ "headerRow": true
93
+ }
94
+ }
95
+ },
96
+ {
97
+ "id": "read-before-running",
98
+ "name": "Read before running",
99
+ "type": "n8n-nodes-base.stickyNote",
100
+ "typeVersion": 1,
101
+ "position": [
102
+ 0,
103
+ -430
104
+ ],
105
+ "parameters": {
106
+ "content": "## Setup\n1. Create a LinkDevi account, then a scoped key with links:read and links:write. Hosted plan limits apply.\n2. In Create saved campaign, select a Header Auth credential: header name Authorization, value Bearer YOUR_SCOPED_KEY. Keep the key only in n8n Credentials.\n3. Keep retries disabled. Redirects are disabled so credentials stay on the configured origin.\n4. Preview your data before running. Each selected channel uses one stored-link slot. A timeout may have committed: preserve requestId and the exact campaign payload on retry.\n5. These workflows do not call an LLM, send email, post to social media or enable a schedule. Activate only after reviewing your own setup.\nGuide: https://linkdevi.com/features/ai\n\n## Campaign brief\nReplace the example destination and names with your own. Use a NEW UUID for each NEW campaign. Keep the same UUID and unchanged fields after a timeout. This workflow creates one campaign when manually executed; it is not an offline preview. Download the binary data from Download CSV. Existing UTM values are replaced by the campaign service.",
107
+ "width": 700,
108
+ "height": 360
109
+ }
110
+ }
111
+ ],
112
+ "connections": {
113
+ "Run manually": {
114
+ "main": [
115
+ [
116
+ {
117
+ "node": "Campaign brief",
118
+ "type": "main",
119
+ "index": 0
120
+ }
121
+ ]
122
+ ]
123
+ },
124
+ "Campaign brief": {
125
+ "main": [
126
+ [
127
+ {
128
+ "node": "Create saved campaign",
129
+ "type": "main",
130
+ "index": 0
131
+ }
132
+ ]
133
+ ]
134
+ },
135
+ "Create saved campaign": {
136
+ "main": [
137
+ [
138
+ {
139
+ "node": "CSV rows",
140
+ "type": "main",
141
+ "index": 0
142
+ }
143
+ ]
144
+ ]
145
+ },
146
+ "CSV rows": {
147
+ "main": [
148
+ [
149
+ {
150
+ "node": "Download CSV",
151
+ "type": "main",
152
+ "index": 0
153
+ }
154
+ ]
155
+ ]
156
+ }
157
+ },
158
+ "active": false,
159
+ "settings": {
160
+ "executionOrder": "v1"
161
+ },
162
+ "pinData": {},
163
+ "tags": []
164
+ }