feat(base-controller): Allow using internal events/actions#2050
feat(base-controller): Allow using internal events/actions#2050
Conversation
|
Looks good! I added some updates building on this: #2051 |
| * @returns Whether the event type is allowed. | ||
| */ | ||
| #isAllowedEvent(eventType: Event['type']): eventType is AllowedEvent { | ||
| // Safely upcast to allow runtime check |
There was a problem hiding this comment.
Yeah I thought so! I was considering suggesting this strategy in our docs.
That is, if you want to upcast something (which is always safe to do), assign it to a new variable. This lets TypeScript validate that it's a safe upcast.
Whereas the as operator also allows downcasting, so it's not as safe to use.
packages/base-controller/src/RestrictedControllerMessenger.test.ts
Outdated
Show resolved
Hide resolved
The restricted controller messenger now allows using all internal events and actions. Previously internal events were treated the same as external events, they had to be explicitly allowed. This is a non-breaking change; internal actions/events are still permitted to be list as "allowed", it's just no longer necessary. Resolves #2047
4fd01b5 to
6542f62
Compare
|
Thanks for the feedback @MajorLift , I removed the unnecessary allowed action/event types from the tests and I've propagated the new helper function to those three spots you found. The rest of the changes in that PR seem better to handle separately, but let me know if I've missed something |
|
Agreed on the scope of the PR, but I think the |
Co-authored-by: Jongsun Suh <jongsun.suh@icloud.com>
…ot initialized with internal actions/events in allow lists (#2051) ## Explanation - `RestrictedControllerMessenger` constructor and `ControllerMessenger.getRestricted()` method now raise a type error if an internal action is passed into `allowedActions`, or an internal event into `allowedEvents`. - ` Type 'string' is not assignable to type 'never'.` - `Type '"SomeController:internalAction"' is not assignable to type '"OtherController:externalAction1" | "OtherController:externalAction2"'.` - Fixes breaking tests in downstream controllers in core repo. ## Impact - The `allowed{Actions,Events}` arrays in `ControllerMessenger.getRestricted()` now align with the `Allowed{Actions,Events}` generic params of `RestrictedControllerMessenger`: Both only enumerate external actions/events. - This commit disallows `allowed{Actions,Events}` arrays that contain an incomplete list of internal actions/events. - A partial list doesn't provide any useful information as all internal actions/events will be available to the messenger regardless of whether any of them are allowlisted. - A partial list also gives the confusing impression that some internal actions/events can be *disallowed* through omission, which is not true as of #2050. --------- Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
…ot initialized with internal actions/events in allow lists (#2051) ## Explanation - `RestrictedControllerMessenger` constructor and `ControllerMessenger.getRestricted()` method now raise a type error if an internal action is passed into `allowedActions`, or an internal event into `allowedEvents`. - ` Type 'string' is not assignable to type 'never'.` - `Type '"SomeController:internalAction"' is not assignable to type '"OtherController:externalAction1" | "OtherController:externalAction2"'.` - Fixes breaking tests in downstream controllers in core repo. ## Impact - The `allowed{Actions,Events}` arrays in `ControllerMessenger.getRestricted()` now align with the `Allowed{Actions,Events}` generic params of `RestrictedControllerMessenger`: Both only enumerate external actions/events. - This commit disallows `allowed{Actions,Events}` arrays that contain an incomplete list of internal actions/events. - A partial list doesn't provide any useful information as all internal actions/events will be available to the messenger regardless of whether any of them are allowlisted. - A partial list also gives the confusing impression that some internal actions/events can be *disallowed* through omission, which is not true as of #2050. --------- Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
Explanation
The restricted controller messenger now allows using all internal events and actions. Previously internal events were treated the same as external events, they had to be explicitly allowed.
This is a non-breaking change; internal actions/events are still permitted to be list as "allowed", it's just no longer necessary.
References
Resolves #2047
Changelog
@metamask/base-controllerChecklist