@metamask/ramps-controller 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -1
- package/dist/RampsController.cjs +324 -9
- package/dist/RampsController.cjs.map +1 -1
- package/dist/RampsController.d.cts +117 -12
- package/dist/RampsController.d.cts.map +1 -1
- package/dist/RampsController.d.mts +117 -12
- package/dist/RampsController.d.mts.map +1 -1
- package/dist/RampsController.mjs +324 -9
- package/dist/RampsController.mjs.map +1 -1
- package/dist/RampsService-method-action-types.cjs.map +1 -1
- package/dist/RampsService-method-action-types.d.cts +33 -1
- package/dist/RampsService-method-action-types.d.cts.map +1 -1
- package/dist/RampsService-method-action-types.d.mts +33 -1
- package/dist/RampsService-method-action-types.d.mts.map +1 -1
- package/dist/RampsService-method-action-types.mjs.map +1 -1
- package/dist/RampsService.cjs +126 -24
- package/dist/RampsService.cjs.map +1 -1
- package/dist/RampsService.d.cts +178 -1
- package/dist/RampsService.d.cts.map +1 -1
- package/dist/RampsService.d.mts +178 -1
- package/dist/RampsService.d.mts.map +1 -1
- package/dist/RampsService.mjs +122 -23
- package/dist/RampsService.mjs.map +1 -1
- package/dist/RequestCache.cjs +98 -0
- package/dist/RequestCache.cjs.map +1 -0
- package/dist/RequestCache.d.cts +93 -0
- package/dist/RequestCache.d.cts.map +1 -0
- package/dist/RequestCache.d.mts +93 -0
- package/dist/RequestCache.d.mts.map +1 -0
- package/dist/RequestCache.mjs +90 -0
- package/dist/RequestCache.mjs.map +1 -0
- package/dist/index.cjs +14 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +8 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +81 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/selectors.d.cts +75 -0
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +75 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/selectors.mjs +77 -0
- package/dist/selectors.mjs.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selectors.d.mts","sourceRoot":"","sources":["../src/selectors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA0B;AAI9D;;;;;;GAMG;AACH,MAAM,MAAM,qBAAqB,CAAC,KAAK,IAAI;IACzC,+EAA+E;IAC/E,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC;IACnB,oDAAoD;IACpD,UAAU,EAAE,OAAO,CAAC;IACpB,uFAAuF;IACvF,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,KAAK,EACrD,QAAQ,EAAE,CAAC,SAAS,EAAE,UAAU,KAAK,oBAAoB,GAAG,SAAS,EACrE,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,EAAE,GAChB,CAAC,KAAK,EAAE,UAAU,KAAK,qBAAqB,CAAC,KAAK,CAAC,CAsBrD"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { RequestStatus, createCacheKey } from "./RequestCache.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a memoized selector for a controller method's request state.
|
|
4
|
+
*
|
|
5
|
+
* This selector tracks the loading, error, and data state for a specific
|
|
6
|
+
* controller method call. It's optimized for use with React Redux's `useSelector`
|
|
7
|
+
* hook - the selector returns the same object reference when the underlying
|
|
8
|
+
* request state hasn't changed, so no `shallowEqual` is needed.
|
|
9
|
+
*
|
|
10
|
+
* The selector uses reference equality on the request object itself, so it
|
|
11
|
+
* works correctly with arrays and objects without expensive deep equality checks.
|
|
12
|
+
*
|
|
13
|
+
* @param getState - Function that extracts RampsControllerState from the root state.
|
|
14
|
+
* Typically a reselect selector like `selectRampsControllerState`.
|
|
15
|
+
* @param method - The controller method name (e.g., 'updateUserRegion').
|
|
16
|
+
* @param params - The parameters passed to the method, used to generate the cache key.
|
|
17
|
+
* Must match the params used when calling the controller method.
|
|
18
|
+
* @returns A selector function that returns `{ data, isFetching, error }`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // In selectors file - create once at module level
|
|
23
|
+
* import { createRequestSelector } from '@metamask/ramps-controller';
|
|
24
|
+
* import { createSelector } from 'reselect';
|
|
25
|
+
*
|
|
26
|
+
* const selectRampsControllerState = createSelector(
|
|
27
|
+
* (state: RootState) => state.engine.backgroundState.RampsController,
|
|
28
|
+
* (rampsControllerState) => rampsControllerState,
|
|
29
|
+
* );
|
|
30
|
+
*
|
|
31
|
+
* export const selectUserRegionRequest = createRequestSelector<
|
|
32
|
+
* RootState,
|
|
33
|
+
* string
|
|
34
|
+
* >(selectRampsControllerState, 'updateUserRegion', []);
|
|
35
|
+
*
|
|
36
|
+
* // In hook - use directly with useSelector, no shallowEqual needed
|
|
37
|
+
* export function useRampsUserRegion() {
|
|
38
|
+
* const { isFetching, error } = useSelector(selectUserRegionRequest);
|
|
39
|
+
* // ... rest of hook
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* // For methods with parameters
|
|
46
|
+
* export const selectCryptoCurrenciesRequest = (region: string) =>
|
|
47
|
+
* createRequestSelector<RootState, CryptoCurrency[]>(
|
|
48
|
+
* selectRampsControllerState,
|
|
49
|
+
* 'getCryptoCurrencies',
|
|
50
|
+
* [region],
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* // In component
|
|
54
|
+
* const { data, isFetching, error } = useSelector(
|
|
55
|
+
* selectCryptoCurrenciesRequest('US')
|
|
56
|
+
* );
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function createRequestSelector(getState, method, params) {
|
|
60
|
+
const cacheKey = createCacheKey(method, params);
|
|
61
|
+
let lastRequest;
|
|
62
|
+
let lastResult = null;
|
|
63
|
+
return (state) => {
|
|
64
|
+
const request = getState(state)?.requests?.[cacheKey];
|
|
65
|
+
if (request === lastRequest && lastResult !== null) {
|
|
66
|
+
return lastResult;
|
|
67
|
+
}
|
|
68
|
+
lastRequest = request;
|
|
69
|
+
lastResult = {
|
|
70
|
+
data: request?.data ?? null,
|
|
71
|
+
isFetching: request?.status === RequestStatus.LOADING,
|
|
72
|
+
error: request?.error ?? null,
|
|
73
|
+
};
|
|
74
|
+
return lastResult;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=selectors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selectors.mjs","sourceRoot":"","sources":["../src/selectors.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,2BAAuB;AAkB/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,MAAM,UAAU,qBAAqB,CACnC,QAAqE,EACrE,MAAc,EACd,MAAiB;IAEjB,MAAM,QAAQ,GAAG,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhD,IAAI,WAAqC,CAAC;IAC1C,IAAI,UAAU,GAAwC,IAAI,CAAC;IAE3D,OAAO,CAAC,KAAiB,EAAgC,EAAE;QACzD,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC;QAEtD,IAAI,OAAO,KAAK,WAAW,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACnD,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,WAAW,GAAG,OAAO,CAAC;QACtB,UAAU,GAAG;YACX,IAAI,EAAG,OAAO,EAAE,IAAc,IAAI,IAAI;YACtC,UAAU,EAAE,OAAO,EAAE,MAAM,KAAK,aAAa,CAAC,OAAO;YACrD,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,IAAI;SAC9B,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { RampsControllerState } from './RampsController';\nimport type { RequestState } from './RequestCache';\nimport { RequestStatus, createCacheKey } from './RequestCache';\n\n/**\n * Result shape returned by request selectors.\n *\n * This object is memoized - the same reference is returned when the underlying\n * request state hasn't changed, making it safe to use with React Redux's\n * `useSelector` without `shallowEqual`.\n */\nexport type RequestSelectorResult<TData> = {\n /** The data returned by the request, or null if not yet loaded or on error. */\n data: TData | null;\n /** Whether the request is currently in progress. */\n isFetching: boolean;\n /** Error message if the request failed, or null if successful or not yet attempted. */\n error: string | null;\n};\n\n/**\n * Creates a memoized selector for a controller method's request state.\n *\n * This selector tracks the loading, error, and data state for a specific\n * controller method call. It's optimized for use with React Redux's `useSelector`\n * hook - the selector returns the same object reference when the underlying\n * request state hasn't changed, so no `shallowEqual` is needed.\n *\n * The selector uses reference equality on the request object itself, so it\n * works correctly with arrays and objects without expensive deep equality checks.\n *\n * @param getState - Function that extracts RampsControllerState from the root state.\n * Typically a reselect selector like `selectRampsControllerState`.\n * @param method - The controller method name (e.g., 'updateUserRegion').\n * @param params - The parameters passed to the method, used to generate the cache key.\n * Must match the params used when calling the controller method.\n * @returns A selector function that returns `{ data, isFetching, error }`.\n *\n * @example\n * ```ts\n * // In selectors file - create once at module level\n * import { createRequestSelector } from '@metamask/ramps-controller';\n * import { createSelector } from 'reselect';\n *\n * const selectRampsControllerState = createSelector(\n * (state: RootState) => state.engine.backgroundState.RampsController,\n * (rampsControllerState) => rampsControllerState,\n * );\n *\n * export const selectUserRegionRequest = createRequestSelector<\n * RootState,\n * string\n * >(selectRampsControllerState, 'updateUserRegion', []);\n *\n * // In hook - use directly with useSelector, no shallowEqual needed\n * export function useRampsUserRegion() {\n * const { isFetching, error } = useSelector(selectUserRegionRequest);\n * // ... rest of hook\n * }\n * ```\n *\n * @example\n * ```ts\n * // For methods with parameters\n * export const selectCryptoCurrenciesRequest = (region: string) =>\n * createRequestSelector<RootState, CryptoCurrency[]>(\n * selectRampsControllerState,\n * 'getCryptoCurrencies',\n * [region],\n * );\n *\n * // In component\n * const { data, isFetching, error } = useSelector(\n * selectCryptoCurrenciesRequest('US')\n * );\n * ```\n */\nexport function createRequestSelector<TRootState, TData>(\n getState: (rootState: TRootState) => RampsControllerState | undefined,\n method: string,\n params: unknown[],\n): (state: TRootState) => RequestSelectorResult<TData> {\n const cacheKey = createCacheKey(method, params);\n\n let lastRequest: RequestState | undefined;\n let lastResult: RequestSelectorResult<TData> | null = null;\n\n return (state: TRootState): RequestSelectorResult<TData> => {\n const request = getState(state)?.requests?.[cacheKey];\n\n if (request === lastRequest && lastResult !== null) {\n return lastResult;\n }\n\n lastRequest = request;\n lastResult = {\n data: (request?.data as TData) ?? null,\n isFetching: request?.status === RequestStatus.LOADING,\n error: request?.error ?? null,\n };\n\n return lastResult;\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/ramps-controller",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A controller for managing cryptocurrency on/off ramps functionality",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@metamask/base-controller": "^9.0.0",
|
|
53
|
-
"@metamask/controller-utils": "^11.
|
|
53
|
+
"@metamask/controller-utils": "^11.18.0",
|
|
54
54
|
"@metamask/messenger": "^0.3.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|