Skip to content

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

fix(ahrefs cli): default the report date and columns instead of requiring them

Corey Haines566b9310d84d
diff --git a/tests/ahrefs-reports.test.cjs b/tests/clis/ahrefs-reports.test.cjs
similarity index 75%
rename from tests/ahrefs-reports.test.cjs
rename to tests/clis/ahrefs-reports.test.cjs
index b79ae87..194288d 100644
--- a/tests/ahrefs-reports.test.cjs
+++ b/tests/clis/ahrefs-reports.test.cjs
@@ -2,7 +2,7 @@ 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/ahrefs.js')
+const cli = path.resolve(__dirname, '../../tools/clis/ahrefs.js')
function run(args, oracle='') {
const code = `global.fetch = async (url, options) => {
const assert = require('node:assert/strict'); const parsed = new URL(url);
@@ -20,15 +20,17 @@ for (const command of commands) test(`${command[0]} forwards an explicit report
const oracle = "assert.equal(parsed.searchParams.get('date'),'2024-02-29');" + (command[0] === 'domain-rating' ? "assert.equal(parsed.searchParams.has('select'),false);" : "assert.equal(parsed.searchParams.get('select'),'keyword,url'); assert.equal(parsed.searchParams.get('country'),'us'); assert.equal(parsed.searchParams.get('limit'),'10');")
assert.equal(output(run(args, oracle)).accepted,true)
})
-for (const command of commands) test(`${command[0]} rejects missing report date before requesting paid data`, () => {
- localError(run([...command,'--target','example.com','--select','url'], "throw new Error('unexpected paid request')"), /date/)
+for (const command of commands) test(`${command[0]} defaults a missing report date to today (UTC)`, () => {
+ const oracle = "assert.match(parsed.searchParams.get('date'), /^\\d{4}-\\d{2}-\\d{2}$/); assert.equal(parsed.searchParams.get('date'), new Date().toISOString().slice(0, 10));"
+ assert.equal(output(run([...command,'--target','example.com','--select','url'], oracle)).accepted,true)
})
test('invalid calendar dates and date flag without a value are rejected', () => {
for (const date of ['2023-02-29','2026-04-31','2026-13-01','2026-1-01','not-a-date']) localError(run(['domain-rating','get','--target','example.com','--date',date], "throw new Error('unexpected paid request')"),/date/)
localError(run(['domain-rating','get','--target','example.com','--date'], "throw new Error('unexpected paid request')"),/date/)
})
-for (const command of commands.slice(1)) test(`${command[0]} requires explicit nonempty selected fields`, () => {
- for (const suffix of [[],['--select'],['--select',''],['--select',' , ']]) localError(run([...command,'--target','example.com','--date','2026-09-30',...suffix], "throw new Error('unexpected paid request')"),/select/)
+for (const command of commands.slice(1)) test(`${command[0]} rejects empty selected fields and defaults when omitted`, () => {
+ for (const suffix of [['--select'],['--select',''],['--select',' , ']]) localError(run([...command,'--target','example.com','--date','2026-09-30',...suffix], "throw new Error('unexpected paid request')"),/select/)
+ assert.equal(output(run([...command,'--target','example.com','--date','2026-09-30'], "assert.ok(parsed.searchParams.get('select').split(',').length >= 3)")).accepted,true)
})
test('domain rating does not require selected fields', () => {
assert.equal(output(run(['domain-rating','get','--target','example.com','--date','2026-09-30'], "assert.equal(parsed.searchParams.get('date'),'2026-09-30');")).accepted,true)
diff --git a/tools/clis/ahrefs.js b/tools/clis/ahrefs.js
index 2c86ba4..5f49106 100755
--- a/tools/clis/ahrefs.js
+++ b/tools/clis/ahrefs.js
@@ -52,9 +52,9 @@ const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
function reportDate() {
- const date = args.date
+ const date = args.date === undefined ? new Date().toISOString().slice(0, 10) : args.date
if (typeof date !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
- throw new Error('--date required as a valid YYYY-MM-DD calendar date')
+ throw new Error('--date must be a valid YYYY-MM-DD calendar date')
}
const parsed = new Date(`${date}T00:00:00Z`)
if (!Number.isFinite(parsed.getTime()) || parsed.toISOString().slice(0, 10) !== date) {
@@ -63,9 +63,10 @@ function reportDate() {
return date
}
-function reportSelect() {
+function reportSelect(defaultColumns) {
+ if (args.select === undefined) return defaultColumns
if (typeof args.select !== 'string' || !args.select.split(',').every(column => column.trim())) {
- throw new Error('--select required as a comma-separated list of nonempty columns')
+ throw new Error('--select must be a comma-separated list of nonempty columns')
}
return args.select
}
@@ -120,7 +121,7 @@ async function main() {
switch (sub) {
case 'organic': {
if (!args.target) { result = { error: '--target required (domain or URL)' }; break }
- const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect() })
+ const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect('keyword,volume,best_position,sum_traffic,best_position_url') })
if (args.country) params.set('country', args.country)
if (args.limit) params.set('limit', args.limit)
result = await api('GET', `/site-explorer/organic-keywords?${params}`)
@@ -135,7 +136,7 @@ async function main() {
switch (sub) {
case 'list': {
if (!args.target) { result = { error: '--target required (domain or URL)' }; break }
- const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect() })
+ const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect('url,sum_traffic,keywords,top_keyword') })
if (args.country) params.set('country', args.country)
if (args.limit) params.set('limit', args.limit)
result = await api('GET', `/site-explorer/top-pages?${params}`)
@@ -190,11 +191,11 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- 'domain-rating': 'domain-rating get --target <domain> --date <YYYY-MM-DD>',
+ 'domain-rating': 'domain-rating get --target <domain> [--date <YYYY-MM-DD>]',
'backlinks': 'backlinks list --target <domain> [--mode <mode>] [--limit <n>]',
'refdomains': 'refdomains list --target <domain> [--mode <mode>] [--limit <n>]',
- 'keywords': 'keywords organic --target <domain> --date <YYYY-MM-DD> --select <columns> [--country <cc>] [--limit <n>]',
- 'top-pages': 'top-pages list --target <domain> --date <YYYY-MM-DD> --select <columns> [--country <cc>] [--limit <n>]',
+ 'keywords': 'keywords organic --target <domain> [--date <YYYY-MM-DD>] [--select <columns>] [--country <cc>] [--limit <n>]',
+ 'top-pages': 'top-pages list --target <domain> [--date <YYYY-MM-DD>] [--select <columns>] [--country <cc>] [--limit <n>]',
'keyword-overview': 'keyword-overview get --keywords <kw1,kw2> [--country <cc>]',
'keyword-suggestions': 'keyword-suggestions get --keyword <keyword> [--country <cc>] [--limit <n>]',
'serp': 'serp get --keyword <keyword> [--country <cc>]',