Skip to content
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

feat: Add accepted and rejected events to ApprovalController #5127

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/approval-controller/src/ApprovalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ export type ApprovalStateChange = ControllerStateChangeEvent<
ApprovalControllerState
>;

export type ApprovalControllerEvents = ApprovalStateChange;
export type ApprovalControllerEvents =
| ApprovalAcceptedEvent
| ApprovalRejectedEvent
| ApprovalStateChange;

// Action Types

Expand Down Expand Up @@ -325,6 +328,25 @@ export type ShowError = {
handler: ApprovalController['error'];
};

export type ApprovalRejectedEvent = {
type: `${typeof controllerName}:rejected`;
payload: [
{
approval: ApprovalRequest<ApprovalRequestData>;
error: Error;
},
];
};

export type ApprovalAcceptedEvent = {
type: `${typeof controllerName}:accepted`;
payload: [
{
approval: ApprovalRequest<ApprovalRequestData>;
},
];
};

export type ApprovalControllerActions =
| GetApprovalsState
| ClearApprovalRequests
Expand Down Expand Up @@ -729,6 +751,9 @@ export class ApprovalController extends BaseController<
if (!requestDeleted) {
this.#delete(id);
}
this.messagingSystem.publish(`${controllerName}:accepted`, {
approval,
});
});
}

Expand All @@ -740,9 +765,16 @@ export class ApprovalController extends BaseController<
* @param error - The error to reject the approval promise with.
*/
reject(id: string, error: unknown): void {
const rejectedApproval = this.get(
id,
) as ApprovalRequest<ApprovalRequestData>;
const callbacks = this.#getCallbacks(id);
this.#delete(id);
callbacks.reject(error);
this.messagingSystem.publish(`${controllerName}:rejected`, {
approval: rejectedApproval,
error: error as Error,
});
}

/**
Expand Down
Loading