@metamask/rate-limit-controller
Advanced tools
Comparing version
@@ -9,2 +9,8 @@ # Changelog | ||
## [4.0.0] | ||
### Changed | ||
- **BREAKING:** Enforce that `RateLimitedApi['method']` matches action handler type instead of using `any` ([#1890](https://github.com/MetaMask/core/pull/1890)) | ||
- **BREAKING:** Bump `@metamask/base-controller` to ^4.0.0 ([#2063](https://github.com/MetaMask/core/pull/2063)) | ||
- This is breaking because the type of the `messenger` has backward-incompatible changes. See the changelog for this package for more. | ||
## [3.0.3] | ||
@@ -55,3 +61,4 @@ ### Changed | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@3.0.3...HEAD | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@4.0.0...HEAD | ||
[4.0.0]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@3.0.3...@metamask/rate-limit-controller@4.0.0 | ||
[3.0.3]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@3.0.2...@metamask/rate-limit-controller@3.0.3 | ||
@@ -58,0 +65,0 @@ [3.0.2]: https://github.com/MetaMask/core/compare/@metamask/rate-limit-controller@3.0.1...@metamask/rate-limit-controller@3.0.2 |
@@ -1,4 +0,3 @@ | ||
import type { RestrictedControllerMessenger } from '@metamask/base-controller'; | ||
import { BaseControllerV2 as BaseController } from '@metamask/base-controller'; | ||
import type { Patch } from 'immer'; | ||
import type { ActionConstraint, RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from '@metamask/base-controller'; | ||
import { BaseController } from '@metamask/base-controller'; | ||
/** | ||
@@ -11,3 +10,3 @@ * @type RateLimitedApi | ||
export declare type RateLimitedApi = { | ||
method: (...args: any[]) => any; | ||
method: ActionConstraint['handler']; | ||
rateLimitTimeout?: number; | ||
@@ -24,10 +23,4 @@ rateLimitCount?: number; | ||
declare const name = "RateLimitController"; | ||
export declare type RateLimitStateChange<RateLimitedApis extends Record<string, RateLimitedApi>> = { | ||
type: `${typeof name}:stateChange`; | ||
payload: [RateLimitState<RateLimitedApis>, Patch[]]; | ||
}; | ||
export declare type GetRateLimitState<RateLimitedApis extends Record<string, RateLimitedApi>> = { | ||
type: `${typeof name}:getState`; | ||
handler: () => RateLimitState<RateLimitedApis>; | ||
}; | ||
export declare type RateLimitStateChange<RateLimitedApis extends Record<string, RateLimitedApi>> = ControllerStateChangeEvent<typeof name, RateLimitedApis>; | ||
export declare type GetRateLimitState<RateLimitedApis extends Record<string, RateLimitedApi>> = ControllerGetStateAction<typeof name, RateLimitedApis>; | ||
export declare type CallApi<RateLimitedApis extends Record<string, RateLimitedApi>> = { | ||
@@ -38,3 +31,4 @@ type: `${typeof name}:call`; | ||
export declare type RateLimitControllerActions<RateLimitedApis extends Record<string, RateLimitedApi>> = GetRateLimitState<RateLimitedApis> | CallApi<RateLimitedApis>; | ||
export declare type RateLimitMessenger<RateLimitedApis extends Record<string, RateLimitedApi>> = RestrictedControllerMessenger<typeof name, RateLimitControllerActions<RateLimitedApis>, RateLimitStateChange<RateLimitedApis>, never, never>; | ||
export declare type RateLimitControllerEvents<RateLimitedApis extends Record<string, RateLimitedApi>> = RateLimitStateChange<RateLimitedApis>; | ||
export declare type RateLimitMessenger<RateLimitedApis extends Record<string, RateLimitedApi>> = RestrictedControllerMessenger<typeof name, RateLimitControllerActions<RateLimitedApis>, RateLimitControllerEvents<RateLimitedApis>, never, never>; | ||
/** | ||
@@ -70,3 +64,2 @@ * Controller with logic for rate-limiting API endpoints per requesting origin. | ||
* @param args - Arguments for the API call. | ||
* @returns `false` if rate-limited, and `true` otherwise. | ||
*/ | ||
@@ -73,0 +66,0 @@ call<ApiType extends keyof RateLimitedApis>(origin: string, type: ApiType, ...args: Parameters<RateLimitedApis[ApiType]['method']>): Promise<ReturnType<RateLimitedApis[ApiType]['method']>>; |
@@ -22,3 +22,3 @@ "use strict"; | ||
*/ | ||
class RateLimitController extends base_controller_1.BaseControllerV2 { | ||
class RateLimitController extends base_controller_1.BaseController { | ||
/** | ||
@@ -47,3 +47,3 @@ * Creates a RateLimitController instance. | ||
this.rateLimitCount = rateLimitCount; | ||
this.messagingSystem.registerActionHandler(`${name}:call`, ((origin, type, ...args) => this.call(origin, type, ...args))); | ||
this.messagingSystem.registerActionHandler(`${name}:call`, (origin, type, ...args) => this.call(origin, type, ...args)); | ||
} | ||
@@ -56,3 +56,2 @@ /** | ||
* @param args - Arguments for the API call. | ||
* @returns `false` if rate-limited, and `true` otherwise. | ||
*/ | ||
@@ -59,0 +58,0 @@ call(origin, type, ...args) { |
{ | ||
"name": "@metamask/rate-limit-controller", | ||
"version": "3.0.3", | ||
"version": "4.0.0", | ||
"description": "Contains logic for rate-limiting API endpoints by requesting origin", | ||
@@ -27,13 +27,13 @@ "keywords": [ | ||
"publish:preview": "yarn npm publish --tag preview", | ||
"test": "jest", | ||
"test": "jest --reporters=jest-silent-reporter", | ||
"test:clean": "jest --clearCache", | ||
"test:verbose": "jest --verbose", | ||
"test:watch": "jest --watch" | ||
}, | ||
"dependencies": { | ||
"@metamask/base-controller": "^3.2.3", | ||
"@metamask/rpc-errors": "^6.1.0", | ||
"immer": "^9.0.6" | ||
"@metamask/base-controller": "^4.0.0", | ||
"@metamask/rpc-errors": "^6.1.0" | ||
}, | ||
"devDependencies": { | ||
"@metamask/auto-changelog": "^3.1.0", | ||
"@metamask/auto-changelog": "^3.4.3", | ||
"@types/jest": "^27.4.1", | ||
@@ -40,0 +40,0 @@ "deepmerge": "^4.2.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29690
2.01%2
-33.33%216
-3.57%+ Added
- Removed
- Removed