Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(dub): send link tag names in supported request field

Rudy Celeklibeffe9e9a24a
diff --git a/tests/clis/dub-link-tags.test.cjs b/tests/clis/dub-link-tags.test.cjs
new file mode 100644
index 0000000..e2485c2
--- /dev/null
+++ b/tests/clis/dub-link-tags.test.cjs
@@ -0,0 +1,65 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/dub.js')
+
+function run(args, oracle = '') {
+ const code = `global.fetch = async (url, options) => {
+ const assert = require('node:assert/strict');
+ const body = options.body ? JSON.parse(options.body) : null;
+ ${oracle}
+ return new Response(JSON.stringify({ url, method: options.method, body }), { status: 200 });
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], {
+ encoding: 'utf8', timeout: 10000,
+ env: { ...process.env, DUB_API_KEY: 'fixture-token' },
+ })
+}
+
+function result(r) { assert.equal(r.status, 0, r.stderr); return JSON.parse(r.stdout) }
+
+for (const [command, flags, method, endpoint] of [
+ ['create', ['--url', 'https://example.com/summer', '--domain', 'links.example.com', '--key', 'summer'], 'POST', '/links'],
+ ['update', ['--id', 'cl_owned_link', '--url', 'https://example.com/new-summer'], 'PATCH', '/links/cl_owned_link'],
+]) {
+ test(`${command} sends tag names in the supported request field`, () => {
+ const p = result(run(['links', command, ...flags, '--tags', 'campaign:Summer,channel:email'], `
+ assert.equal(new URL(url).pathname, ${JSON.stringify(endpoint)});
+ assert.equal(options.method, ${JSON.stringify(method)});
+ assert.deepEqual(body.tagNames, ['campaign:Summer', 'channel:email']);
+ assert.equal(Object.hasOwn(body, 'tags'), false);
+ assert.equal(Object.hasOwn(body, 'tagIds'), false);
+ `))
+ assert.equal(p.body.url, command === 'create' ? 'https://example.com/summer' : 'https://example.com/new-summer')
+ if (command === 'create') {
+ assert.equal(p.body.domain, 'links.example.com')
+ assert.equal(p.body.key, 'summer')
+ }
+ })
+
+ test(`${command} previews a single tag name without making a request`, () => {
+ const p = result(run(['links', command, ...flags, '--tags', 'channel:email', '--dry-run'], "throw new Error('unexpected fetch')"))
+ assert.deepEqual(p.body.tagNames, ['channel:email'])
+ assert.equal(Object.hasOwn(p.body, 'tags'), false)
+ assert.equal(p.headers.Authorization, '***')
+ assert.equal(p.method, method)
+ })
+
+ test(`${command} without tags omits tag mutations`, () => {
+ result(run(['links', command, ...flags], `
+ assert.equal(Object.hasOwn(body, 'tagNames'), false);
+ assert.equal(Object.hasOwn(body, 'tags'), false);
+ assert.equal(Object.hasOwn(body, 'tagIds'), false);
+ `))
+ })
+}
+
+test('bulk create forwards caller-supplied tag IDs without interpreting them as names', () => {
+ const links = [{ url: 'https://example.com', tagIds: ['cl_owned_tag'] }]
+ const p = result(run(['links', 'bulk-create', '--links', JSON.stringify(links)], `
+ assert.equal(new URL(url).pathname, '/links/bulk');
+ assert.equal(options.method, 'POST');
+ `))
+ assert.deepEqual(p.body, links)
+})
diff --git a/tools/clis/dub.js b/tools/clis/dub.js
index c69404f..1d8f432 100755
--- a/tools/clis/dub.js
+++ b/tools/clis/dub.js
@@ -64,7 +64,7 @@ async function main() {
if (args.url) body.url = args.url
if (args.domain) body.domain = args.domain
if (args.key) body.key = args.key
- if (args.tags) body.tags = args.tags.split(',')
+ if (args.tags) body.tagNames = args.tags.split(',')
result = await api('POST', '/links', body)
break
}
@@ -88,7 +88,7 @@ async function main() {
if (!args.id) { result = { error: '--id required (link ID)' }; break }
const body = {}
if (args.url) body.url = args.url
- if (args.tags) body.tags = args.tags.split(',')
+ if (args.tags) body.tagNames = args.tags.split(',')
result = await api('PATCH', `/links/${args.id}`, body)
break
}
@@ -144,7 +144,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- links: 'links [create|list|get|update|delete|bulk-create] [--url <url>] [--domain <domain>] [--key <key>] [--tags <tags>] [--id <id>] [--page <page>] [--links <json>]',
+ links: 'links [create|list|get|update|delete|bulk-create] [--url <url>] [--domain <domain>] [--key <key>] [--tags <comma-separated names>] [--id <id>] [--page <page>] [--links <json>]',
analytics: 'analytics [get|country|device] [--domain <domain>] [--key <key>] [--interval <interval>]',
}
}
diff --git a/tools/integrations/dub-co.md b/tools/integrations/dub-co.md
index 409f4ec..6857461 100644
--- a/tools/integrations/dub-co.md
+++ b/tools/integrations/dub-co.md
@@ -28,7 +28,7 @@ POST https://api.dub.co/links
"url": "https://example.com/landing-page",
"domain": "link.example.com",
"key": "summer-sale",
- "tags": ["campaign:summer", "channel:email"]
+ "tagNames": ["campaign:summer", "channel:email"]
}
```
@@ -69,10 +69,15 @@ PATCH https://api.dub.co/links/{link_id}
{
"url": "https://example.com/new-landing-page",
- "tags": ["campaign:summer", "channel:social"]
+ "tagNames": ["campaign:summer", "channel:social"]
}
```
+For [link creation](https://dub.co/docs/api-reference/links/create) and
+[updates](https://dub.co/docs/api-reference/links/update), use `tagNames` for names
+or `tagIds` for existing tag IDs. The CLI's `--tags` flag takes comma-separated
+names and sends `tagNames`. The response's `tags` field contains tag objects.
+
### Delete link
```bash