-
Notifications
You must be signed in to change notification settings - Fork 34
feat(webhooks): add event replay command #381
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
+190
−13
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
885da64
feat(webhooks): add event replay command
gabrielmfern aaafe8e
fix(webhooks): document that replay requires an enabled webhook
gabrielmfern c040701
docs(webhooks): stop repeating the disabled-webhook caveat in the README
gabrielmfern 4db8fb8
fix(webhooks): describe replay's disabled-webhook error accurately
gabrielmfern 6bdf179
test(webhooks): assert the disabled-webhook message reaches the user
gabrielmfern 04f68a1
fix(webhooks): stop quoting unverified API error text, fix stale even…
gabrielmfern 0a37894
fix(webhooks): trim replay help text to match sibling mutating commands
gabrielmfern 1cdcaca
update lockfile
gabrielmfern 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
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
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,49 @@ | ||
| import { Command } from '@commander-js/extra-typings'; | ||
| import { runWrite } from '../../../lib/actions'; | ||
| import type { GlobalOpts } from '../../../lib/client'; | ||
| import { buildHelpText } from '../../../lib/help-text'; | ||
| import { pickId } from '../../../lib/prompts'; | ||
| import { webhookPickerConfig } from '../utils'; | ||
| import { webhookEventPickerConfig } from './utils'; | ||
|
|
||
| export const replayWebhookEventCommand = new Command('replay') | ||
| .description('Queue another delivery attempt for a webhook event') | ||
| .argument('[webhookId]', 'Webhook ID') | ||
| .argument('[eventId]', 'Webhook event ID') | ||
| .addHelpText( | ||
| 'after', | ||
| buildHelpText({ | ||
| context: `Queues one more delivery of the event. Does not schedule automatic retries. | ||
| The webhook must be enabled — re-enable it first with: resend webhooks update <webhook-id> --status enabled`, | ||
| output: ` {"object":"webhook_event","id":"msg_..."}`, | ||
| errorCodes: ['auth_error', 'replay_error'], | ||
| examples: [ | ||
| 'resend webhooks events replay 4dd369bc-aa82-4ff3-97de-514ae3000ee0 msg_1srOrx2ZWZBpBUvZwXKQmoEYga2', | ||
| 'resend webhooks events replay 4dd369bc-aa82-4ff3-97de-514ae3000ee0 msg_1srOrx2ZWZBpBUvZwXKQmoEYga2 --json', | ||
| ], | ||
| }), | ||
| ) | ||
| .action(async (webhookIdArg, eventIdArg, _opts, cmd) => { | ||
| const globalOpts = cmd.optsWithGlobals() as GlobalOpts; | ||
| const webhookId = await pickId( | ||
| webhookIdArg, | ||
| webhookPickerConfig, | ||
| globalOpts, | ||
| ); | ||
| const eventId = await pickId( | ||
| eventIdArg, | ||
| webhookEventPickerConfig(webhookId), | ||
| globalOpts, | ||
| ); | ||
|
|
||
| await runWrite( | ||
| { | ||
| loading: 'Replaying webhook event...', | ||
| sdkCall: (resend) => | ||
| resend.webhooks.events.replay({ webhookId, eventId }), | ||
| errorCode: 'replay_error', | ||
| successMsg: `Webhook event replay queued: ${eventId}`, | ||
| }, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { | ||
| afterEach, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| type MockInstance, | ||
| vi, | ||
| } from 'vitest'; | ||
| import { replayWebhookEventCommand } from '../../../../src/commands/webhooks/events/replay'; | ||
| import { | ||
| captureTestEnv, | ||
| expectExit1, | ||
| mockExitThrow, | ||
| mockSdkError, | ||
| setNonInteractive, | ||
| setupOutputSpies, | ||
| } from '../../../helpers'; | ||
|
|
||
| const WEBHOOK_ID = '4dd369bc-aa82-4ff3-97de-514ae3000ee0'; | ||
| const EVENT_ID = 'msg_1srOrx2ZWZBpBUvZwXKQmoEYga2'; | ||
|
|
||
| const mockReplay = vi.fn(async () => ({ | ||
| data: { | ||
| object: 'webhook_event' as const, | ||
| id: EVENT_ID, | ||
| }, | ||
| error: null, | ||
| })); | ||
|
|
||
| vi.mock('resend', () => ({ | ||
| Resend: class MockResend { | ||
| constructor(public key: string) {} | ||
| webhooks = { events: { replay: mockReplay } }; | ||
| }, | ||
| })); | ||
|
|
||
| describe('webhooks events replay 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'; | ||
| mockReplay.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreEnv(); | ||
| errorSpy?.mockRestore(); | ||
| stderrSpy?.mockRestore(); | ||
| exitSpy?.mockRestore(); | ||
| spies = undefined; | ||
| errorSpy = undefined; | ||
| stderrSpy = undefined; | ||
| exitSpy = undefined; | ||
| }); | ||
|
|
||
| it('maps the two positional args to webhookId and eventId in that order', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| await replayWebhookEventCommand.parseAsync([WEBHOOK_ID, EVENT_ID], { | ||
| from: 'user', | ||
| }); | ||
|
|
||
| expect(mockReplay).toHaveBeenCalledTimes(1); | ||
| const opts = mockReplay.mock.calls[0][0] as Record<string, unknown>; | ||
| expect(opts.webhookId).toBe(WEBHOOK_ID); | ||
| expect(opts.eventId).toBe(EVENT_ID); | ||
| }); | ||
|
|
||
| it('outputs the replayed event as JSON when non-interactive', async () => { | ||
| spies = setupOutputSpies(); | ||
|
|
||
| await replayWebhookEventCommand.parseAsync([WEBHOOK_ID, EVENT_ID], { | ||
| from: 'user', | ||
| }); | ||
|
|
||
| const output = spies.logSpy.mock.calls[0][0] as string; | ||
| const parsed = JSON.parse(output); | ||
| expect(parsed.object).toBe('webhook_event'); | ||
| expect(parsed.id).toBe(EVENT_ID); | ||
| }); | ||
|
|
||
| it('errors with replay_error when the SDK returns an error', async () => { | ||
| setNonInteractive(); | ||
| mockReplay.mockResolvedValueOnce( | ||
| mockSdkError('Webhook is disabled', 'validation_error'), | ||
| ); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| stderrSpy = vi | ||
| .spyOn(process.stderr, 'write') | ||
| .mockImplementation(() => true); | ||
| exitSpy = mockExitThrow(); | ||
|
|
||
| await expectExit1(() => | ||
| replayWebhookEventCommand.parseAsync([WEBHOOK_ID, EVENT_ID], { | ||
| from: 'user', | ||
| }), | ||
| ); | ||
|
|
||
| const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); | ||
| expect(output).toContain('replay_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.