Skip to content

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

feat(rewardful): expose pagination across affiliate revenue collections

Rudy Celekli865903e4f331
diff --git a/tests/clis/rewardful-pagination.test.cjs b/tests/clis/rewardful-pagination.test.cjs
new file mode 100644
index 0000000..ff7b870
--- /dev/null
+++ b/tests/clis/rewardful-pagination.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/rewardful.js')
+
+function run(args) {
+ const code = `global.fetch = async (url, options) => ({ status: 200, text: async () => JSON.stringify({ url, method: options.method, page: new URL(url).searchParams.get('page') || '1' }) }); process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const result = spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, REWARDFUL_API_KEY: 'fixture-only' } })
+ assert.equal(result.status, 0, result.stderr)
+ return JSON.parse(result.stdout)
+}
+
+for (const command of [['affiliates', 'list'], ['affiliates', 'search', '--email', '[email protected]'], ['referrals', 'list', '--affiliate-id', 'aff-1'], ['referrals', 'get', '--stripe-customer-id', 'cus-1'], ['commissions', 'list', '--affiliate-id', 'aff-1']]) {
+ test(`${command.join(' ')} can request later collection pages`, () => {
+ const result = run([...command, '--page', '3', '--limit', '50'])
+ const query = new URL(result.url).searchParams
+ assert.equal(result.page, '3')
+ assert.equal(query.get('limit'), '50')
+ if (command.includes('--email')) assert.equal(query.get('email'), '[email protected]')
+ if (command.includes('--affiliate-id')) assert.equal(query.get('affiliate_id'), 'aff-1')
+ if (command.includes('--stripe-customer-id')) assert.equal(query.get('stripe_customer_id'), 'cus-1')
+ })
+}
+
+test('pagination preview remains credential-redacted without changing default list requests', () => {
+ const preview = run(['commissions', 'list', '--page', '2', '--limit', '25', '--dry-run'])
+ assert.equal(preview.headers.Authorization, '***')
+ assert.equal(new URL(preview.url).searchParams.get('page'), '2')
+ assert.equal(new URL(run(['referrals', 'list']).url).searchParams.has('page'), false)
+})
+
+for (const extra of [['--page'], ['--page', 'NaN'], ['--page', '0'], ['--page', '1.5'], ['--page', '9007199254740992'], ['--limit'], ['--limit', '101'], ['--limit', '0'], ['--limit', 'Infinity']]) {
+ test(`invalid pagination ${extra.join(' ')} fails before fetching`, () => {
+ const args = ['affiliates', 'list', ...extra]
+ const code = `global.fetch=async()=>{throw new Error('unexpected fetch')};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ const result = spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, REWARDFUL_API_KEY: 'fixture-only' } })
+ assert.equal(result.status, 1)
+ assert.match(JSON.parse(result.stderr).error, /must be an integer/)
+ })
+}
diff --git a/tools/clis/rewardful.js b/tools/clis/rewardful.js
index 78738b2..65ef798 100755
--- a/tools/clis/rewardful.js
+++ b/tools/clis/rewardful.js
@@ -53,6 +53,19 @@ function parseArgs(args) {
const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
+function collectionParams() {
+ const params = new URLSearchParams()
+ for (const [key, max] of [['page', Number.MAX_SAFE_INTEGER], ['limit', 100]]) {
+ if (args[key] === undefined) continue
+ const value = args[key]
+ if (typeof value !== 'string' || !/^[0-9]+$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) < 1 || Number(value) > max) {
+ throw new Error(`--${key} must be an integer between 1 and ${max}`)
+ }
+ params.set(key, value)
+ }
+ return params
+}
+
async function main() {
let result
@@ -60,8 +73,7 @@ async function main() {
case 'affiliates':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
- if (args.page) params.set('page', args.page)
+ const params = collectionParams()
result = await api('GET', `/affiliates?${params}`)
break
}
@@ -70,7 +82,7 @@ async function main() {
result = await api('GET', `/affiliates/${rest[0]}`)
break
case 'search': {
- const params = new URLSearchParams()
+ const params = collectionParams()
if (args.email) params.set('email', args.email)
result = await api('GET', `/affiliates?${params}`)
break
@@ -96,13 +108,13 @@ async function main() {
case 'referrals':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
+ const params = collectionParams()
if (args['affiliate-id']) params.set('affiliate_id', args['affiliate-id'])
result = await api('GET', `/referrals?${params}`)
break
}
case 'get': {
- const params = new URLSearchParams()
+ const params = collectionParams()
if (args['stripe-customer-id']) params.set('stripe_customer_id', args['stripe-customer-id'])
result = await api('GET', `/referrals?${params}`)
break
@@ -115,7 +127,7 @@ async function main() {
case 'commissions':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
+ const params = collectionParams()
if (args['affiliate-id']) params.set('affiliate_id', args['affiliate-id'])
result = await api('GET', `/commissions?${params}`)
break
@@ -151,6 +163,7 @@ async function main() {
affiliates: 'affiliates [list|get|search|update] [id] [--email <email>] [--id <id>] [--first-name <name>] [--last-name <name>] [--paypal-email <email>]',
referrals: 'referrals [list|get] [--affiliate-id <id>] [--stripe-customer-id <id>]',
commissions: 'commissions [list|get] [id] [--affiliate-id <id>]',
+ options: '--page <n> --limit <n> (collection requests)',
links: 'links [create] [--affiliate-id <id>] [--token <token>] [--url <url>]',
}
}
diff --git a/tools/integrations/rewardful.md b/tools/integrations/rewardful.md
index aed864f..fcfd1b4 100644
--- a/tools/integrations/rewardful.md
+++ b/tools/integrations/rewardful.md
@@ -145,3 +145,17 @@ Rewardful automatically:
- referrals
- pricing
+
+### Listing all collection pages with the CLI
+
+Rewardful returns paginated affiliate, referral and commission collections. Pass `--page` and `--limit` (provider maximum 100) to advance while preserving the selected filters:
+
+```bash
+node tools/clis/rewardful.js referrals list --affiliate-id aff_123 --page 3 --limit 50
+node tools/clis/rewardful.js commissions list --affiliate-id aff_123 --page 2 --limit 50
+node tools/clis/rewardful.js affiliates list --page 2 --limit 50
+```
+
+Read the response's pagination metadata before requesting another page. No flags retain the provider's default page and size; the CLI does not automatically fetch every page.
+
+Sources: [referrals](https://developers.rewardful.com/rest-api/referrals/list), [commissions](https://developers.rewardful.com/rest-api/commissions/list), [affiliates](https://developers.rewardful.com/rest-api/affiliates/list).