Repository navigation
feat: add broadcasts clicked-links command #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { Command } from '@commander-js/extra-typings'; | ||
| import { runList } from '../../lib/actions'; | ||
| import type { GlobalOpts } from '../../lib/client'; | ||
| import { buildHelpText } from '../../lib/help-text'; | ||
| import { | ||
| buildPaginationOpts, | ||
| parseLimitOpt, | ||
| printPaginationHint, | ||
| } from '../../lib/pagination'; | ||
| import { pickId } from '../../lib/prompts'; | ||
| import { | ||
| broadcastPickerConfig, | ||
| renderBroadcastClickedLinksTable, | ||
| } from './utils'; | ||
|
|
||
| export const clickedLinksBroadcastCommand = new Command('clicked-links') | ||
| .description('List the links clicked in a broadcast, ranked by total clicks') | ||
| .argument('[id]', 'Broadcast ID') | ||
| .option('--limit <n>', 'Maximum number of results to return (1-100)', '10') | ||
| .option( | ||
| '--after <cursor>', | ||
| 'Cursor for forward pagination — list items after this ID', | ||
| ) | ||
| .option( | ||
| '--before <cursor>', | ||
| 'Cursor for backward pagination — list items before this ID', | ||
| ) | ||
| .addHelpText( | ||
| 'after', | ||
| buildHelpText({ | ||
| output: ` {"object":"list","has_more":false,"data":[{"id":"...","url":"...","clicks":42,"unique_clicks":30}]}`, | ||
| errorCodes: ['auth_error', 'invalid_limit', 'list_error'], | ||
| examples: [ | ||
| 'resend broadcasts clicked-links d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', | ||
| 'resend broadcasts clicked-links d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6 --limit 5', | ||
| 'resend broadcasts clicked-links d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6 --after b2Zmc2V0OjA', | ||
| 'resend broadcasts clicked-links d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6 --json', | ||
| ], | ||
| }), | ||
| ) | ||
| .action(async (idArg, opts, cmd) => { | ||
| const globalOpts = cmd.optsWithGlobals() as GlobalOpts; | ||
| const id = await pickId(idArg, broadcastPickerConfig, globalOpts); | ||
| const limit = parseLimitOpt(opts.limit, globalOpts); | ||
| const paginationOpts = buildPaginationOpts( | ||
| limit, | ||
| opts.after, | ||
| opts.before, | ||
| globalOpts, | ||
| ); | ||
| await runList( | ||
| { | ||
| loading: 'Fetching clicked links...', | ||
| sdkCall: (resend) => resend.broadcasts.clickedLinks(id, paginationOpts), | ||
| onInteractive: (list) => { | ||
| console.log(renderBroadcastClickedLinksTable(list.data)); | ||
| printPaginationHint(list, `broadcasts clicked-links ${id}`, { | ||
| limit, | ||
| before: opts.before, | ||
| apiKey: globalOpts.apiKey, | ||
| profile: globalOpts.profile, | ||
| }); | ||
| }, | ||
| }, | ||
| globalOpts, | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import { | ||
| afterEach, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| type MockInstance, | ||
| vi, | ||
| } from 'vitest'; | ||
| import { | ||
| captureTestEnv, | ||
| expectExit1, | ||
| mockExitThrow, | ||
| mockSdkError, | ||
| setNonInteractive, | ||
| setupOutputSpies, | ||
| } from '../../helpers'; | ||
|
|
||
| const mockClickedLinks = vi.fn(async () => ({ | ||
| data: { | ||
| object: 'list' as const, | ||
| has_more: false, | ||
| data: [ | ||
| { | ||
| id: 'b2Zmc2V0OjA', | ||
| url: 'https://resend.com/pricing', | ||
| clicks: 42, | ||
| unique_clicks: 30, | ||
| }, | ||
| ], | ||
| }, | ||
| error: null, | ||
| })); | ||
|
|
||
| vi.mock('resend', () => ({ | ||
| Resend: class MockResend { | ||
| constructor(public key: string) {} | ||
| broadcasts = { clickedLinks: mockClickedLinks }; | ||
| }, | ||
| })); | ||
|
|
||
| describe('broadcasts clicked-links command', () => { | ||
| const restoreEnv = captureTestEnv(); | ||
| let spies: ReturnType<typeof setupOutputSpies> | undefined; | ||
| let errorSpy: MockInstance | undefined; | ||
| let stderrSpy: MockInstance | undefined; | ||
| let exitSpy: MockInstance | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.RESEND_API_KEY = 're_test_key'; | ||
| mockClickedLinks.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreEnv(); | ||
| errorSpy?.mockRestore(); | ||
| stderrSpy?.mockRestore(); | ||
| exitSpy?.mockRestore(); | ||
| spies = undefined; | ||
| errorSpy = undefined; | ||
| stderrSpy = undefined; | ||
| exitSpy = undefined; | ||
| }); | ||
|
|
||
| it('lists clicked links for a broadcast id', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| expect(mockClickedLinks).toHaveBeenCalledTimes(1); | ||
| expect(mockClickedLinks.mock.calls[0][0]).toBe( | ||
| 'd1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', | ||
| ); | ||
| }); | ||
|
|
||
| it('outputs JSON list when non-interactive', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| const output = spies.logSpy.mock.calls[0][0] as string; | ||
| const parsed = JSON.parse(output); | ||
| expect(parsed.object).toBe('list'); | ||
| expect(parsed.data).toHaveLength(1); | ||
| expect(parsed.data[0].url).toBe('https://resend.com/pricing'); | ||
| }); | ||
|
|
||
| it('passes --limit to SDK', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', '--limit', '5'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| const opts = mockClickedLinks.mock.calls[0][1] as Record<string, unknown>; | ||
| expect(opts.limit).toBe(5); | ||
| }); | ||
|
|
||
| it('passes --after cursor to SDK', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', '--after', 'b2Zmc2V0OjA'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| const opts = mockClickedLinks.mock.calls[0][1] as Record<string, unknown>; | ||
| expect(opts.after).toBe('b2Zmc2V0OjA'); | ||
| }); | ||
|
|
||
| it('passes --before cursor to SDK', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', '--before', 'b2Zmc2V0OjA'], | ||
| { from: 'user' }, | ||
| ); | ||
|
|
||
| const opts = mockClickedLinks.mock.calls[0][1] as Record<string, unknown>; | ||
| expect(opts.before).toBe('b2Zmc2V0OjA'); | ||
| }); | ||
|
|
||
| it('errors with invalid_limit when --limit is out of range', async () => { | ||
| setNonInteractive(); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| stderrSpy = vi | ||
| .spyOn(process.stderr, 'write') | ||
| .mockImplementation(() => true); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await expectExit1(() => | ||
| clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6', '--limit', '999'], | ||
| { from: 'user' }, | ||
| ), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('invalid_limit'); | ||
| }); | ||
|
|
||
| it('errors with auth_error when no API key', async () => { | ||
| setNonInteractive(); | ||
| delete process.env.RESEND_API_KEY; | ||
| process.env.XDG_CONFIG_HOME = '/tmp/nonexistent-resend'; | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await expectExit1(() => | ||
| clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('auth_error'); | ||
| }); | ||
|
|
||
| it('errors with list_error when SDK returns an error', async () => { | ||
| setNonInteractive(); | ||
| mockClickedLinks.mockResolvedValueOnce( | ||
| mockSdkError('Broadcast not found', 'not_found'), | ||
| ); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| stderrSpy = vi | ||
| .spyOn(process.stderr, 'write') | ||
| .mockImplementation(() => true); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| const { clickedLinksBroadcastCommand } = await import( | ||
| '../../../src/commands/broadcasts/clicked-links' | ||
| ); | ||
| await expectExit1(() => | ||
| clickedLinksBroadcastCommand.parseAsync( | ||
| ['d1c2b3a4-5e6f-7a8b-9c0d-e1f2a3b4c5d6'], | ||
| { from: 'user' }, | ||
| ), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('list_error'); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.