Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.
JavaScript agent-skillsai-agentsai-seoclaudeclaude-codeclaude-code-pluginclaude-skillscodexcontent-marketingconversion-rate-optimizationcopywritingcursoremail-marketinggenerative-engine-optimizationgrowth-marketingmarketingmarketing-automationsaas-marketingseoskill-md
fix: supply required Ahrefs report dates and selected columns
Rudy Celekli73aea3a80b4a
diff --git a/tests/ahrefs-reports.test.cjs b/tests/ahrefs-reports.test.cjsnew file mode 100644index 0000000..b79ae87--- /dev/null+++ b/tests/ahrefs-reports.test.cjs@@ -0,0 +1,41 @@+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')+function run(args, oracle='') {+ const code = `global.fetch = async (url, options) => {+ const assert = require('node:assert/strict'); const parsed = new URL(url);+ const body = options.body ? JSON.parse(options.body) : null;+ ${oracle}+ return new Response(JSON.stringify({accepted:true,url,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, AHREFS_API_KEY:'test-only-key'}})+}+function output(r) { assert.equal(r.status,0,r.stderr); return JSON.parse(r.stdout) }+function localError(r, pattern) { const text = r.status === 0 ? r.stdout : r.stderr; assert.match(JSON.parse(text).error,pattern) }+const commands = [['domain-rating','get'], ['keywords','organic'], ['top-pages','list']]+for (const command of commands) test(`${command[0]} forwards an explicit report date and chosen columns`, () => {+ const args = [...command,'--target','example.com','--date','2024-02-29','--select','keyword,url','--country','us','--limit','10']+ 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/)+})+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/)+})+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)+})+test('other commands retain their existing request inputs', () => {+ const p=output(run(['backlinks','list','--target','example.com','--limit','5'], "assert.equal(parsed.searchParams.has('date'),false); assert.equal(parsed.searchParams.get('limit'),'5');")); assert.equal(p.accepted,true)+})+test('dated report preview is complete and masks authorization', () => {+ const p=output(run(['top-pages','list','--target','example.com','--date','2026-09-30','--select','url','--dry-run'], "throw new Error('unexpected paid request')")); assert.equal(new URL(p.url).searchParams.get('select'),'url'); assert.equal(p.headers.Authorization,'***')+})diff --git a/tools/clis/ahrefs.js b/tools/clis/ahrefs.jsindex 63ec6dd..e44f610 100755--- a/tools/clis/ahrefs.js+++ b/tools/clis/ahrefs.js@@ -50,6 +50,25 @@ function parseArgs(args) {const args = parseArgs(process.argv.slice(2))const [cmd, sub, ...rest] = args._+function reportDate() {+ const date = 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')+ }+ const parsed = new Date(`${date}T00:00:00Z`)+ if (!Number.isFinite(parsed.getTime()) || parsed.toISOString().slice(0, 10) !== date) {+ throw new Error('--date must be a valid YYYY-MM-DD calendar date')+ }+ return date+}++function reportSelect() {+ 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')+ }+ return args.select+}+async function main() {let resultconst mode = args.mode || 'domain'@@ -59,7 +78,7 @@ async function main() {switch (sub) {case 'get': {if (!args.target) { result = { error: '--target required (domain)' }; break }- const params = new URLSearchParams({ target: args.target })+ const params = new URLSearchParams({ target: args.target, date: reportDate() })result = await api('GET', `/site-explorer/domain-rating?${params}`)break}@@ -100,7 +119,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 })+ const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect() })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}`)@@ -115,7 +134,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 })+ const params = new URLSearchParams({ target: args.target, mode, date: reportDate(), select: reportSelect() })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}`)@@ -170,11 +189,11 @@ async function main() {result = {error: 'Unknown command',usage: {- 'domain-rating': 'domain-rating get --target <domain>',+ '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> [--country <cc>] [--limit <n>]',- 'top-pages': 'top-pages list --target <domain> [--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>]',diff --git a/tools/integrations/ahrefs.md b/tools/integrations/ahrefs.mdindex 352cffd..5db523c 100644--- a/tools/integrations/ahrefs.md+++ b/tools/integrations/ahrefs.md@@ -22,7 +22,7 @@ SEO toolset for backlink analysis, keyword research, and competitive research.### Domain rating```bash-GET https://api.ahrefs.com/v3/site-explorer/domain-rating?target=example.com+GET https://api.ahrefs.com/v3/site-explorer/domain-rating?target=example.com&date=2026-09-30Authorization: Bearer {api_token}```@@ -54,7 +54,7 @@ Authorization: Bearer {api_token}### Organic keywords```bash-GET https://api.ahrefs.com/v3/site-explorer/organic-keywords?target=example.com&mode=domain&country=us&limit=100+GET https://api.ahrefs.com/v3/site-explorer/organic-keywords?target=example.com&mode=domain&country=us&limit=100&date=2026-09-30&select=keywordAuthorization: Bearer {api_token}```@@ -62,7 +62,7 @@ Authorization: Bearer {api_token}### Top pages```bash-GET https://api.ahrefs.com/v3/site-explorer/top-pages?target=example.com&mode=domain&country=us&limit=50+GET https://api.ahrefs.com/v3/site-explorer/top-pages?target=example.com&mode=domain&country=us&limit=50&date=2026-09-30&select=urlAuthorization: Bearer {api_token}```@@ -140,3 +140,15 @@ Authorization: Bearer {api_token}- seo-audit- content-strategy- competitors++## Dated reports in the CLI++`domain-rating get`, `keywords organic`, and `top-pages list` require an explicit+`--date YYYY-MM-DD`. Organic keywords and top pages also require `--select` with+the columns you want to request. The CLI validates the calendar date and rejects+missing or empty column lists before making a request; it does not guess paid+columns. Choose fields from the provider response schema:++- [Domain rating](https://docs.ahrefs.com/en/api/reference/site-explorer/get-domain-rating)+- [Organic keywords](https://docs.ahrefs.com/en/api/reference/site-explorer/get-organic-keywords)+- [Top pages](https://docs.ahrefs.com/en/api/reference/site-explorer/get-top-pages)