@metamask/ramps-controller 1.0.0 → 2.1.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 +28 -1
- package/dist/RampsController.cjs +226 -6
- package/dist/RampsController.cjs.map +1 -1
- package/dist/RampsController.d.cts +80 -10
- package/dist/RampsController.d.cts.map +1 -1
- package/dist/RampsController.d.mts +80 -10
- package/dist/RampsController.d.mts.map +1 -1
- package/dist/RampsController.mjs +226 -6
- package/dist/RampsController.mjs.map +1 -1
- package/dist/{OnRampService-method-action-types.cjs → RampsService-method-action-types.cjs} +1 -1
- package/dist/RampsService-method-action-types.cjs.map +1 -0
- package/dist/RampsService-method-action-types.d.cts +41 -0
- package/dist/RampsService-method-action-types.d.cts.map +1 -0
- package/dist/RampsService-method-action-types.d.mts +41 -0
- package/dist/RampsService-method-action-types.d.mts.map +1 -0
- package/dist/{OnRampService-method-action-types.mjs → RampsService-method-action-types.mjs} +1 -1
- package/dist/RampsService-method-action-types.mjs.map +1 -0
- package/dist/RampsService.cjs +288 -0
- package/dist/RampsService.cjs.map +1 -0
- package/dist/RampsService.d.cts +275 -0
- package/dist/RampsService.d.cts.map +1 -0
- package/dist/RampsService.d.mts +275 -0
- package/dist/RampsService.d.mts.map +1 -0
- package/dist/RampsService.mjs +281 -0
- package/dist/RampsService.mjs.map +1 -0
- 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 +17 -4
- 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 +3 -2
- package/dist/OnRampService-method-action-types.cjs.map +0 -1
- package/dist/OnRampService-method-action-types.d.cts +0 -20
- package/dist/OnRampService-method-action-types.d.cts.map +0 -1
- package/dist/OnRampService-method-action-types.d.mts +0 -20
- package/dist/OnRampService-method-action-types.d.mts.map +0 -1
- package/dist/OnRampService-method-action-types.mjs.map +0 -1
- package/dist/OnRampService.cjs +0 -204
- package/dist/OnRampService.cjs.map +0 -1
- package/dist/OnRampService.d.cts +0 -152
- package/dist/OnRampService.d.cts.map +0 -1
- package/dist/OnRampService.d.mts +0 -152
- package/dist/OnRampService.d.mts.map +0 -1
- package/dist/OnRampService.mjs +0 -200
- package/dist/OnRampService.mjs.map +0 -1
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createErrorState = exports.createSuccessState = exports.createLoadingState = exports.isCacheExpired = exports.createCacheKey = exports.DEFAULT_REQUEST_CACHE_MAX_SIZE = exports.DEFAULT_REQUEST_CACHE_TTL = exports.RequestStatus = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Status of a cached request.
|
|
6
|
+
*/
|
|
7
|
+
var RequestStatus;
|
|
8
|
+
(function (RequestStatus) {
|
|
9
|
+
RequestStatus["IDLE"] = "idle";
|
|
10
|
+
RequestStatus["LOADING"] = "loading";
|
|
11
|
+
RequestStatus["SUCCESS"] = "success";
|
|
12
|
+
RequestStatus["ERROR"] = "error";
|
|
13
|
+
})(RequestStatus || (exports.RequestStatus = RequestStatus = {}));
|
|
14
|
+
/**
|
|
15
|
+
* Default TTL for cached requests in milliseconds (15 minutes).
|
|
16
|
+
*/
|
|
17
|
+
exports.DEFAULT_REQUEST_CACHE_TTL = 15 * 60 * 1000;
|
|
18
|
+
/**
|
|
19
|
+
* Default maximum number of entries in the request cache.
|
|
20
|
+
*/
|
|
21
|
+
exports.DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a cache key from a method name and parameters.
|
|
24
|
+
*
|
|
25
|
+
* @param method - The method name.
|
|
26
|
+
* @param params - The parameters passed to the method.
|
|
27
|
+
* @returns A unique cache key string.
|
|
28
|
+
*/
|
|
29
|
+
function createCacheKey(method, params) {
|
|
30
|
+
return `${method}:${JSON.stringify(params)}`;
|
|
31
|
+
}
|
|
32
|
+
exports.createCacheKey = createCacheKey;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if a cached request has expired based on TTL.
|
|
35
|
+
*
|
|
36
|
+
* @param requestState - The cached request state.
|
|
37
|
+
* @param ttl - Time to live in milliseconds.
|
|
38
|
+
* @returns True if the cache entry has expired.
|
|
39
|
+
*/
|
|
40
|
+
function isCacheExpired(requestState, ttl = exports.DEFAULT_REQUEST_CACHE_TTL) {
|
|
41
|
+
if (requestState.status !== RequestStatus.SUCCESS) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
return now - requestState.timestamp > ttl;
|
|
46
|
+
}
|
|
47
|
+
exports.isCacheExpired = isCacheExpired;
|
|
48
|
+
/**
|
|
49
|
+
* Creates an initial loading state for a request.
|
|
50
|
+
*
|
|
51
|
+
* @returns A new RequestState in loading status.
|
|
52
|
+
*/
|
|
53
|
+
function createLoadingState() {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
return {
|
|
56
|
+
status: RequestStatus.LOADING,
|
|
57
|
+
data: null,
|
|
58
|
+
error: null,
|
|
59
|
+
timestamp: now,
|
|
60
|
+
lastFetchedAt: now,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.createLoadingState = createLoadingState;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a success state for a request.
|
|
66
|
+
*
|
|
67
|
+
* @param data - The data returned by the request.
|
|
68
|
+
* @param lastFetchedAt - When the fetch started.
|
|
69
|
+
* @returns A new RequestState in success status.
|
|
70
|
+
*/
|
|
71
|
+
function createSuccessState(data, lastFetchedAt) {
|
|
72
|
+
return {
|
|
73
|
+
status: RequestStatus.SUCCESS,
|
|
74
|
+
data,
|
|
75
|
+
error: null,
|
|
76
|
+
timestamp: Date.now(),
|
|
77
|
+
lastFetchedAt,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.createSuccessState = createSuccessState;
|
|
81
|
+
/**
|
|
82
|
+
* Creates an error state for a request.
|
|
83
|
+
*
|
|
84
|
+
* @param error - The error message.
|
|
85
|
+
* @param lastFetchedAt - When the fetch started.
|
|
86
|
+
* @returns A new RequestState in error status.
|
|
87
|
+
*/
|
|
88
|
+
function createErrorState(error, lastFetchedAt) {
|
|
89
|
+
return {
|
|
90
|
+
status: RequestStatus.ERROR,
|
|
91
|
+
data: null,
|
|
92
|
+
error,
|
|
93
|
+
timestamp: Date.now(),
|
|
94
|
+
lastFetchedAt,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
exports.createErrorState = createErrorState;
|
|
98
|
+
//# sourceMappingURL=RequestCache.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestCache.cjs","sourceRoot":"","sources":["../src/RequestCache.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,8BAAa,CAAA;IACb,oCAAmB,CAAA;IACnB,oCAAmB,CAAA;IACnB,gCAAe,CAAA;AACjB,CAAC,EALW,aAAa,6BAAb,aAAa,QAKxB;AAwBD;;GAEG;AACU,QAAA,yBAAyB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAExD;;GAEG;AACU,QAAA,8BAA8B,G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esContent":["import type { Json } from '@metamask/utils';\n\n/**\n * Status of a cached request.\n */\nexport enum RequestStatus {\n IDLE = 'idle',\n LOADING = 'loading',\n SUCCESS = 'success',\n ERROR = 'error',\n}\n\n/**\n * State of a single cached request.\n * All properties must be JSON-serializable to satisfy StateConstraint.\n */\nexport type RequestState = {\n /** Current status of the request */\n status: `${RequestStatus}`;\n /** The data returned by the request, if successful */\n data: Json | null;\n /** Error message if the request failed */\n error: string | null;\n /** Timestamp when the request completed (for TTL calculation) */\n timestamp: number;\n /** Timestamp when the fetch started */\n lastFetchedAt: number;\n};\n\n/**\n * Cache of request states, keyed by cache key.\n */\nexport type RequestCache = Record<string, RequestState>;\n\n/**\n * Default TTL for cached requests in milliseconds (15 minutes).\n */\nexport const DEFAULT_REQUEST_CACHE_TTL = 15 * 60 * 1000;\n\n/**\n * Default maximum number of entries in the request cache.\n */\nexport const DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;\n\n/**\n * Creates a cache key from a method name and parameters.\n *\n * @param method - The method name.\n * @param params - The parameters passed to the method.\n * @returns A unique cache key string.\n */\nexport function createCacheKey(method: string, params: unknown[]): string {\n return `${method}:${JSON.stringify(params)}`;\n}\n\n/**\n * Checks if a cached request has expired based on TTL.\n *\n * @param requestState - The cached request state.\n * @param ttl - Time to live in milliseconds.\n * @returns True if the cache entry has expired.\n */\nexport function isCacheExpired(\n requestState: RequestState,\n ttl: number = DEFAULT_REQUEST_CACHE_TTL,\n): boolean {\n if (requestState.status !== RequestStatus.SUCCESS) {\n return true;\n }\n const now = Date.now();\n return now - requestState.timestamp > ttl;\n}\n\n/**\n * Creates an initial loading state for a request.\n *\n * @returns A new RequestState in loading status.\n */\nexport function createLoadingState(): RequestState {\n const now = Date.now();\n return {\n status: RequestStatus.LOADING,\n data: null,\n error: null,\n timestamp: now,\n lastFetchedAt: now,\n };\n}\n\n/**\n * Creates a success state for a request.\n *\n * @param data - The data returned by the request.\n * @param lastFetchedAt - When the fetch started.\n * @returns A new RequestState in success status.\n */\nexport function createSuccessState(\n data: Json,\n lastFetchedAt: number,\n): RequestState {\n return {\n status: RequestStatus.SUCCESS,\n data,\n error: null,\n timestamp: Date.now(),\n lastFetchedAt,\n };\n}\n\n/**\n * Creates an error state for a request.\n *\n * @param error - The error message.\n * @param lastFetchedAt - When the fetch started.\n * @returns A new RequestState in error status.\n */\nexport function createErrorState(\n error: string,\n lastFetchedAt: number,\n): RequestState {\n return {\n status: RequestStatus.ERROR,\n data: null,\n error,\n timestamp: Date.now(),\n lastFetchedAt,\n };\n}\n\n/**\n * Options for executing a cached request.\n */\nexport type ExecuteRequestOptions = {\n /** Force a refresh even if cached data exists */\n forceRefresh?: boolean;\n /** Custom TTL for this request in milliseconds */\n ttl?: number;\n};\n\n/**\n * Represents a pending request with its promise and abort controller.\n */\nexport type PendingRequest<TResult = unknown> = {\n promise: Promise<TResult>;\n abortController: AbortController;\n};\n"]}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Json } from "@metamask/utils";
|
|
2
|
+
/**
|
|
3
|
+
* Status of a cached request.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum RequestStatus {
|
|
6
|
+
IDLE = "idle",
|
|
7
|
+
LOADING = "loading",
|
|
8
|
+
SUCCESS = "success",
|
|
9
|
+
ERROR = "error"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* State of a single cached request.
|
|
13
|
+
* All properties must be JSON-serializable to satisfy StateConstraint.
|
|
14
|
+
*/
|
|
15
|
+
export type RequestState = {
|
|
16
|
+
/** Current status of the request */
|
|
17
|
+
status: `${RequestStatus}`;
|
|
18
|
+
/** The data returned by the request, if successful */
|
|
19
|
+
data: Json | null;
|
|
20
|
+
/** Error message if the request failed */
|
|
21
|
+
error: string | null;
|
|
22
|
+
/** Timestamp when the request completed (for TTL calculation) */
|
|
23
|
+
timestamp: number;
|
|
24
|
+
/** Timestamp when the fetch started */
|
|
25
|
+
lastFetchedAt: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cache of request states, keyed by cache key.
|
|
29
|
+
*/
|
|
30
|
+
export type RequestCache = Record<string, RequestState>;
|
|
31
|
+
/**
|
|
32
|
+
* Default TTL for cached requests in milliseconds (15 minutes).
|
|
33
|
+
*/
|
|
34
|
+
export declare const DEFAULT_REQUEST_CACHE_TTL: number;
|
|
35
|
+
/**
|
|
36
|
+
* Default maximum number of entries in the request cache.
|
|
37
|
+
*/
|
|
38
|
+
export declare const DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a cache key from a method name and parameters.
|
|
41
|
+
*
|
|
42
|
+
* @param method - The method name.
|
|
43
|
+
* @param params - The parameters passed to the method.
|
|
44
|
+
* @returns A unique cache key string.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createCacheKey(method: string, params: unknown[]): string;
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a cached request has expired based on TTL.
|
|
49
|
+
*
|
|
50
|
+
* @param requestState - The cached request state.
|
|
51
|
+
* @param ttl - Time to live in milliseconds.
|
|
52
|
+
* @returns True if the cache entry has expired.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isCacheExpired(requestState: RequestState, ttl?: number): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Creates an initial loading state for a request.
|
|
57
|
+
*
|
|
58
|
+
* @returns A new RequestState in loading status.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createLoadingState(): RequestState;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a success state for a request.
|
|
63
|
+
*
|
|
64
|
+
* @param data - The data returned by the request.
|
|
65
|
+
* @param lastFetchedAt - When the fetch started.
|
|
66
|
+
* @returns A new RequestState in success status.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createSuccessState(data: Json, lastFetchedAt: number): RequestState;
|
|
69
|
+
/**
|
|
70
|
+
* Creates an error state for a request.
|
|
71
|
+
*
|
|
72
|
+
* @param error - The error message.
|
|
73
|
+
* @param lastFetchedAt - When the fetch started.
|
|
74
|
+
* @returns A new RequestState in error status.
|
|
75
|
+
*/
|
|
76
|
+
export declare function createErrorState(error: string, lastFetchedAt: number): RequestState;
|
|
77
|
+
/**
|
|
78
|
+
* Options for executing a cached request.
|
|
79
|
+
*/
|
|
80
|
+
export type ExecuteRequestOptions = {
|
|
81
|
+
/** Force a refresh even if cached data exists */
|
|
82
|
+
forceRefresh?: boolean;
|
|
83
|
+
/** Custom TTL for this request in milliseconds */
|
|
84
|
+
ttl?: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Represents a pending request with its promise and abort controller.
|
|
88
|
+
*/
|
|
89
|
+
export type PendingRequest<TResult = unknown> = {
|
|
90
|
+
promise: Promise<TResult>;
|
|
91
|
+
abortController: AbortController;
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=RequestCache.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestCache.d.cts","sourceRoot":"","sources":["../src/RequestCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C;;GAEG;AACH,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAED;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,oCAAoC;IACpC,MAAM,EAAE,GAAG,aAAa,EAAE,CAAC;IAC3B,sDAAsD;IACtD,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAClB,0CAA0C;IAC1C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,iEAAiE;IACjE,SAAS,EAAE,MAAM,CAAC;IAClB,uCAAuC;IACvC,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAExD;;GAEG;AACH,eAAO,MAAM,yBAAyB,QAAiB,CAAC;AAExD;;GAEG;AACH,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAElD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAExE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,YAAY,EAAE,YAAY,EAC1B,GAAG,GAAE,MAAkC,GACtC,OAAO,CAMT;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,YAAY,CASjD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,IAAI,EACV,aAAa,EAAE,MAAM,GACpB,YAAY,CAQd;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,aAAa,EAAE,MAAM,GACpB,YAAY,CAQd;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,iDAAiD;IACjD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,kDAAkD;IAClD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,OAAO,GAAG,OAAO,IAAI;IAC9C,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,eAAe,EAAE,eAAe,CAAC;CAClC,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Json } from "@metamask/utils";
|
|
2
|
+
/**
|
|
3
|
+
* Status of a cached request.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum RequestStatus {
|
|
6
|
+
IDLE = "idle",
|
|
7
|
+
LOADING = "loading",
|
|
8
|
+
SUCCESS = "success",
|
|
9
|
+
ERROR = "error"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* State of a single cached request.
|
|
13
|
+
* All properties must be JSON-serializable to satisfy StateConstraint.
|
|
14
|
+
*/
|
|
15
|
+
export type RequestState = {
|
|
16
|
+
/** Current status of the request */
|
|
17
|
+
status: `${RequestStatus}`;
|
|
18
|
+
/** The data returned by the request, if successful */
|
|
19
|
+
data: Json | null;
|
|
20
|
+
/** Error message if the request failed */
|
|
21
|
+
error: string | null;
|
|
22
|
+
/** Timestamp when the request completed (for TTL calculation) */
|
|
23
|
+
timestamp: number;
|
|
24
|
+
/** Timestamp when the fetch started */
|
|
25
|
+
lastFetchedAt: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Cache of request states, keyed by cache key.
|
|
29
|
+
*/
|
|
30
|
+
export type RequestCache = Record<string, RequestState>;
|
|
31
|
+
/**
|
|
32
|
+
* Default TTL for cached requests in milliseconds (15 minutes).
|
|
33
|
+
*/
|
|
34
|
+
export declare const DEFAULT_REQUEST_CACHE_TTL: number;
|
|
35
|
+
/**
|
|
36
|
+
* Default maximum number of entries in the request cache.
|
|
37
|
+
*/
|
|
38
|
+
export declare const DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a cache key from a method name and parameters.
|
|
41
|
+
*
|
|
42
|
+
* @param method - The method name.
|
|
43
|
+
* @param params - The parameters passed to the method.
|
|
44
|
+
* @returns A unique cache key string.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createCacheKey(method: string, params: unknown[]): string;
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a cached request has expired based on TTL.
|
|
49
|
+
*
|
|
50
|
+
* @param requestState - The cached request state.
|
|
51
|
+
* @param ttl - Time to live in milliseconds.
|
|
52
|
+
* @returns True if the cache entry has expired.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isCacheExpired(requestState: RequestState, ttl?: number): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Creates an initial loading state for a request.
|
|
57
|
+
*
|
|
58
|
+
* @returns A new RequestState in loading status.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createLoadingState(): RequestState;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a success state for a request.
|
|
63
|
+
*
|
|
64
|
+
* @param data - The data returned by the request.
|
|
65
|
+
* @param lastFetchedAt - When the fetch started.
|
|
66
|
+
* @returns A new RequestState in success status.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createSuccessState(data: Json, lastFetchedAt: number): RequestState;
|
|
69
|
+
/**
|
|
70
|
+
* Creates an error state for a request.
|
|
71
|
+
*
|
|
72
|
+
* @param error - The error message.
|
|
73
|
+
* @param lastFetchedAt - When the fetch started.
|
|
74
|
+
* @returns A new RequestState in error status.
|
|
75
|
+
*/
|
|
76
|
+
export declare function createErrorState(error: string, lastFetchedAt: number): RequestState;
|
|
77
|
+
/**
|
|
78
|
+
* Options for executing a cached request.
|
|
79
|
+
*/
|
|
80
|
+
export type ExecuteRequestOptions = {
|
|
81
|
+
/** Force a refresh even if cached data exists */
|
|
82
|
+
forceRefresh?: boolean;
|
|
83
|
+
/** Custom TTL for this request in milliseconds */
|
|
84
|
+
ttl?: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Represents a pending request with its promise and abort controller.
|
|
88
|
+
*/
|
|
89
|
+
export type PendingRequest<TResult = unknown> = {
|
|
90
|
+
promise: Promise<TResult>;
|
|
91
|
+
abortController: AbortController;
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=RequestCache.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestCache.d.mts","sourceRoot":"","sources":["../src/RequestCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C;;GAEG;AACH,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAED;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,oCAAoC;IACpC,MAAM,EAAE,GAAG,aAAa,EAAE,CAAC;IAC3B,sDAAsD;IACtD,IAAI,EAAE,IAAI,GAAG,IAAI,CAA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
+
export var RequestStatus;
|
|
5
|
+
(function (RequestStatus) {
|
|
6
|
+
RequestStatus["IDLE"] = "idle";
|
|
7
|
+
RequestStatus["LOADING"] = "loading";
|
|
8
|
+
RequestStatus["SUCCESS"] = "success";
|
|
9
|
+
RequestStatus["ERROR"] = "error";
|
|
10
|
+
})(RequestStatus || (RequestStatus = {}));
|
|
11
|
+
/**
|
|
12
|
+
* Default TTL for cached requests in milliseconds (15 minutes).
|
|
13
|
+
*/
|
|
14
|
+
export const DEFAULT_REQUEST_CACHE_TTL = 15 * 60 * 1000;
|
|
15
|
+
/**
|
|
16
|
+
* Default maximum number of entries in the request cache.
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a cache key from a method name and parameters.
|
|
21
|
+
*
|
|
22
|
+
* @param method - The method name.
|
|
23
|
+
* @param params - The parameters passed to the method.
|
|
24
|
+
* @returns A unique cache key string.
|
|
25
|
+
*/
|
|
26
|
+
export function createCacheKey(method, params) {
|
|
27
|
+
return `${method}:${JSON.stringify(params)}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a cached request has expired based on TTL.
|
|
31
|
+
*
|
|
32
|
+
* @param requestState - The cached request state.
|
|
33
|
+
* @param ttl - Time to live in milliseconds.
|
|
34
|
+
* @returns True if the cache entry has expired.
|
|
35
|
+
*/
|
|
36
|
+
export function isCacheExpired(requestState, ttl = DEFAULT_REQUEST_CACHE_TTL) {
|
|
37
|
+
if (requestState.status !== RequestStatus.SUCCESS) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
return now - requestState.timestamp > ttl;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates an initial loading state for a request.
|
|
45
|
+
*
|
|
46
|
+
* @returns A new RequestState in loading status.
|
|
47
|
+
*/
|
|
48
|
+
export function createLoadingState() {
|
|
49
|
+
const now = Date.now();
|
|
50
|
+
return {
|
|
51
|
+
status: RequestStatus.LOADING,
|
|
52
|
+
data: null,
|
|
53
|
+
error: null,
|
|
54
|
+
timestamp: now,
|
|
55
|
+
lastFetchedAt: now,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a success state for a request.
|
|
60
|
+
*
|
|
61
|
+
* @param data - The data returned by the request.
|
|
62
|
+
* @param lastFetchedAt - When the fetch started.
|
|
63
|
+
* @returns A new RequestState in success status.
|
|
64
|
+
*/
|
|
65
|
+
export function createSuccessState(data, lastFetchedAt) {
|
|
66
|
+
return {
|
|
67
|
+
status: RequestStatus.SUCCESS,
|
|
68
|
+
data,
|
|
69
|
+
error: null,
|
|
70
|
+
timestamp: Date.now(),
|
|
71
|
+
lastFetchedAt,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates an error state for a request.
|
|
76
|
+
*
|
|
77
|
+
* @param error - The error message.
|
|
78
|
+
* @param lastFetchedAt - When the fetch started.
|
|
79
|
+
* @returns A new RequestState in error status.
|
|
80
|
+
*/
|
|
81
|
+
export function createErrorState(error, lastFetchedAt) {
|
|
82
|
+
return {
|
|
83
|
+
status: RequestStatus.ERROR,
|
|
84
|
+
data: null,
|
|
85
|
+
error,
|
|
86
|
+
timestamp: Date.now(),
|
|
87
|
+
lastFetchedAt,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=RequestCache.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestCache.mjs","sourceRoot":"","sources":["../src/RequestCache.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAN,IAAY,aAKX;AALD,WAAY,aAAa;IACvB,8BAAa,CAAA;IACb,oCAAmB,CAAA;IACnB,oCAAmB,CAAA;IACnB,gCAAe,CAAA;AACjB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB;AAwBD;;GA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KAAK;QACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;QACrB,aAAa;KACd,CAAC;AACJ,CAAC","sourcesContent":["import type { Json } from '@metamask/utils';\n\n/**\n * Status of a cached request.\n */\nexport enum RequestStatus {\n IDLE = 'idle',\n LOADING = 'loading',\n SUCCESS = 'success',\n ERROR = 'error',\n}\n\n/**\n * State of a single cached request.\n * All properties must be JSON-serializable to satisfy StateConstraint.\n */\nexport type RequestState = {\n /** Current status of the request */\n status: `${RequestStatus}`;\n /** The data returned by the request, if successful */\n data: Json | null;\n /** Error message if the request failed */\n error: string | null;\n /** Timestamp when the request completed (for TTL calculation) */\n timestamp: number;\n /** Timestamp when the fetch started */\n lastFetchedAt: number;\n};\n\n/**\n * Cache of request states, keyed by cache key.\n */\nexport type RequestCache = Record<string, RequestState>;\n\n/**\n * Default TTL for cached requests in milliseconds (15 minutes).\n */\nexport const DEFAULT_REQUEST_CACHE_TTL = 15 * 60 * 1000;\n\n/**\n * Default maximum number of entries in the request cache.\n */\nexport const DEFAULT_REQUEST_CACHE_MAX_SIZE = 250;\n\n/**\n * Creates a cache key from a method name and parameters.\n *\n * @param method - The method name.\n * @param params - The parameters passed to the method.\n * @returns A unique cache key string.\n */\nexport function createCacheKey(method: string, params: unknown[]): string {\n return `${method}:${JSON.stringify(params)}`;\n}\n\n/**\n * Checks if a cached request has expired based on TTL.\n *\n * @param requestState - The cached request state.\n * @param ttl - Time to live in milliseconds.\n * @returns True if the cache entry has expired.\n */\nexport function isCacheExpired(\n requestState: RequestState,\n ttl: number = DEFAULT_REQUEST_CACHE_TTL,\n): boolean {\n if (requestState.status !== RequestStatus.SUCCESS) {\n return true;\n }\n const now = Date.now();\n return now - requestState.timestamp > ttl;\n}\n\n/**\n * Creates an initial loading state for a request.\n *\n * @returns A new RequestState in loading status.\n */\nexport function createLoadingState(): RequestState {\n const now = Date.now();\n return {\n status: RequestStatus.LOADING,\n data: null,\n error: null,\n timestamp: now,\n lastFetchedAt: now,\n };\n}\n\n/**\n * Creates a success state for a request.\n *\n * @param data - The data returned by the request.\n * @param lastFetchedAt - When the fetch started.\n * @returns A new RequestState in success status.\n */\nexport function createSuccessState(\n data: Json,\n lastFetchedAt: number,\n): RequestState {\n return {\n status: RequestStatus.SUCCESS,\n data,\n error: null,\n timestamp: Date.now(),\n lastFetchedAt,\n };\n}\n\n/**\n * Creates an error state for a request.\n *\n * @param error - The error message.\n * @param lastFetchedAt - When the fetch started.\n * @returns A new RequestState in error status.\n */\nexport function createErrorState(\n error: string,\n lastFetchedAt: number,\n): RequestState {\n return {\n status: RequestStatus.ERROR,\n data: null,\n error,\n timestamp: Date.now(),\n lastFetchedAt,\n };\n}\n\n/**\n * Options for executing a cached request.\n */\nexport type ExecuteRequestOptions = {\n /** Force a refresh even if cached data exists */\n forceRefresh?: boolean;\n /** Custom TTL for this request in milliseconds */\n ttl?: number;\n};\n\n/**\n * Represents a pending request with its promise and abort controller.\n */\nexport type PendingRequest<TResult = unknown> = {\n promise: Promise<TResult>;\n abortController: AbortController;\n};\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createRequestSelector = exports.createErrorState = exports.createSuccessState = exports.createLoadingState = exports.isCacheExpired = exports.createCacheKey = exports.DEFAULT_REQUEST_CACHE_MAX_SIZE = exports.DEFAULT_REQUEST_CACHE_TTL = exports.RequestStatus = exports.RAMPS_SDK_VERSION = exports.RampsApiService = exports.RampsEnvironment = exports.RampsService = exports.getDefaultRampsControllerState = exports.RampsController = void 0;
|
|
4
4
|
var RampsController_1 = require("./RampsController.cjs");
|
|
5
5
|
Object.defineProperty(exports, "RampsController", { enumerable: true, get: function () { return RampsController_1.RampsController; } });
|
|
6
6
|
Object.defineProperty(exports, "getDefaultRampsControllerState", { enumerable: true, get: function () { return RampsController_1.getDefaultRampsControllerState; } });
|
|
7
|
-
var
|
|
8
|
-
Object.defineProperty(exports, "
|
|
9
|
-
Object.defineProperty(exports, "
|
|
7
|
+
var RampsService_1 = require("./RampsService.cjs");
|
|
8
|
+
Object.defineProperty(exports, "RampsService", { enumerable: true, get: function () { return RampsService_1.RampsService; } });
|
|
9
|
+
Object.defineProperty(exports, "RampsEnvironment", { enumerable: true, get: function () { return RampsService_1.RampsEnvironment; } });
|
|
10
|
+
Object.defineProperty(exports, "RampsApiService", { enumerable: true, get: function () { return RampsService_1.RampsApiService; } });
|
|
11
|
+
Object.defineProperty(exports, "RAMPS_SDK_VERSION", { enumerable: true, get: function () { return RampsService_1.RAMPS_SDK_VERSION; } });
|
|
12
|
+
var RequestCache_1 = require("./RequestCache.cjs");
|
|
13
|
+
Object.defineProperty(exports, "RequestStatus", { enumerable: true, get: function () { return RequestCache_1.RequestStatus; } });
|
|
14
|
+
Object.defineProperty(exports, "DEFAULT_REQUEST_CACHE_TTL", { enumerable: true, get: function () { return RequestCache_1.DEFAULT_REQUEST_CACHE_TTL; } });
|
|
15
|
+
Object.defineProperty(exports, "DEFAULT_REQUEST_CACHE_MAX_SIZE", { enumerable: true, get: function () { return RequestCache_1.DEFAULT_REQUEST_CACHE_MAX_SIZE; } });
|
|
16
|
+
Object.defineProperty(exports, "createCacheKey", { enumerable: true, get: function () { return RequestCache_1.createCacheKey; } });
|
|
17
|
+
Object.defineProperty(exports, "isCacheExpired", { enumerable: true, get: function () { return RequestCache_1.isCacheExpired; } });
|
|
18
|
+
Object.defineProperty(exports, "createLoadingState", { enumerable: true, get: function () { return RequestCache_1.createLoadingState; } });
|
|
19
|
+
Object.defineProperty(exports, "createSuccessState", { enumerable: true, get: function () { return RequestCache_1.createSuccessState; } });
|
|
20
|
+
Object.defineProperty(exports, "createErrorState", { enumerable: true, get: function () { return RequestCache_1.createErrorState; } });
|
|
21
|
+
var selectors_1 = require("./selectors.cjs");
|
|
22
|
+
Object.defineProperty(exports, "createRequestSelector", { enumerable: true, get: function () { return selectors_1.createRequestSelector; } });
|
|
10
23
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,yDAG2B;AAFzB,kHAAA,eAAe,OAAA;AACf,iIAAA,8BAA8B,OAAA;AAWhC,mDAKwB;AAJtB,4GAAA,YAAY,OAAA;AACZ,gHAAA,gBAAgB,OAAA;AAChB,+GAAA,eAAe,OAAA;AACf,iHAAA,iBAAiB,OAAA;AAanB,mDASwB;AARtB,6GAAA,aAAa,OAAA;AACb,yHAAA,yBAAyB,OAAA;AACzB,8HAAA,8BAA8B,OAAA;AAC9B,8GAAA,cAAc,OAAA;AACd,8GAAA,cAAc,OAAA;AACd,kHAAA,kBAAkB,OAAA;AAClB,kHAAA,kBAAkB,OAAA;AAClB,gHAAA,gBAAgB,OAAA;AAGlB,6CAAoD;AAA3C,kHAAA,qBAAqB,OAAA","sourcesContent":["export type {\n RampsControllerActions,\n RampsControllerEvents,\n RampsControllerGetStateAction,\n RampsControllerMessenger,\n RampsControllerState,\n RampsControllerStateChangeEvent,\n RampsControllerOptions,\n} from './RampsController';\nexport {\n RampsController,\n getDefaultRampsControllerState,\n} from './RampsController';\nexport type {\n RampsServiceActions,\n RampsServiceEvents,\n RampsServiceMessenger,\n Country,\n State,\n Eligibility,\n CountryPhone,\n} from './RampsService';\nexport {\n RampsService,\n RampsEnvironment,\n RampsApiService,\n RAMPS_SDK_VERSION,\n} from './RampsService';\nexport type {\n RampsServiceGetGeolocationAction,\n RampsServiceGetCountriesAction,\n RampsServiceGetEligibilityAction,\n} from './RampsService-method-action-types';\nexport type {\n RequestCache,\n RequestState,\n ExecuteRequestOptions,\n PendingRequest,\n} from './RequestCache';\nexport {\n RequestStatus,\n DEFAULT_REQUEST_CACHE_TTL,\n DEFAULT_REQUEST_CACHE_MAX_SIZE,\n createCacheKey,\n isCacheExpired,\n createLoadingState,\n createSuccessState,\n createErrorState,\n} from './RequestCache';\nexport type { RequestSelectorResult } from './selectors';\nexport { createRequestSelector } from './selectors';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export type { RampsControllerActions, RampsControllerEvents, RampsControllerGetStateAction, RampsControllerMessenger, RampsControllerState, RampsControllerStateChangeEvent, } from "./RampsController.cjs";
|
|
1
|
+
export type { RampsControllerActions, RampsControllerEvents, RampsControllerGetStateAction, RampsControllerMessenger, RampsControllerState, RampsControllerStateChangeEvent, RampsControllerOptions, } from "./RampsController.cjs";
|
|
2
2
|
export { RampsController, getDefaultRampsControllerState, } from "./RampsController.cjs";
|
|
3
|
-
export type {
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
3
|
+
export type { RampsServiceActions, RampsServiceEvents, RampsServiceMessenger, Country, State, Eligibility, CountryPhone, } from "./RampsService.cjs";
|
|
4
|
+
export { RampsService, RampsEnvironment, RampsApiService, RAMPS_SDK_VERSION, } from "./RampsService.cjs";
|
|
5
|
+
export type { RampsServiceGetGeolocationAction, RampsServiceGetCountriesAction, RampsServiceGetEligibilityAction, } from "./RampsService-method-action-types.cjs";
|
|
6
|
+
export type { RequestCache, RequestState, ExecuteRequestOptions, PendingRequest, } from "./RequestCache.cjs";
|
|
7
|
+
export { RequestStatus, DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE, createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState, } from "./RequestCache.cjs";
|
|
8
|
+
export type { RequestSelectorResult } from "./selectors.cjs";
|
|
9
|
+
export { createRequestSelector } from "./selectors.cjs";
|
|
6
10
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,wBAAwB,EACxB,oBAAoB,EACpB,+BAA+B,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,wBAAwB,EACxB,oBAAoB,EACpB,+BAA+B,EAC/B,sBAAsB,GACvB,8BAA0B;AAC3B,OAAO,EACL,eAAe,EACf,8BAA8B,GAC/B,8BAA0B;AAC3B,YAAY,EACV,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,OAAO,EACP,KAAK,EACL,WAAW,EACX,YAAY,GACb,2BAAuB;AACxB,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,iBAAiB,GAClB,2BAAuB;AACxB,YAAY,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,gCAAgC,GACjC,+CAA2C;AAC5C,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,qBAAqB,EACrB,cAAc,GACf,2BAAuB;AACxB,OAAO,EACL,aAAa,EACb,yBAAyB,EACzB,8BAA8B,EAC9B,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,2BAAuB;AACxB,YAAY,EAAE,qBAAqB,EAAE,wBAAoB;AACzD,OAAO,EAAE,qBAAqB,EAAE,wBAAoB"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export type { RampsControllerActions, RampsControllerEvents, RampsControllerGetStateAction, RampsControllerMessenger, RampsControllerState, RampsControllerStateChangeEvent, } from "./RampsController.mjs";
|
|
1
|
+
export type { RampsControllerActions, RampsControllerEvents, RampsControllerGetStateAction, RampsControllerMessenger, RampsControllerState, RampsControllerStateChangeEvent, RampsControllerOptions, } from "./RampsController.mjs";
|
|
2
2
|
export { RampsController, getDefaultRampsControllerState, } from "./RampsController.mjs";
|
|
3
|
-
export type {
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
3
|
+
export type { RampsServiceActions, RampsServiceEvents, RampsServiceMessenger, Country, State, Eligibility, CountryPhone, } from "./RampsService.mjs";
|
|
4
|
+
export { RampsService, RampsEnvironment, RampsApiService, RAMPS_SDK_VERSION, } from "./RampsService.mjs";
|
|
5
|
+
export type { RampsServiceGetGeolocationAction, RampsServiceGetCountriesAction, RampsServiceGetEligibilityAction, } from "./RampsService-method-action-types.mjs";
|
|
6
|
+
export type { RequestCache, RequestState, ExecuteRequestOptions, PendingRequest, } from "./RequestCache.mjs";
|
|
7
|
+
export { RequestStatus, DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE, createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState, } from "./RequestCache.mjs";
|
|
8
|
+
export type { RequestSelectorResult } from "./selectors.mjs";
|
|
9
|
+
export { createRequestSelector } from "./selectors.mjs";
|
|
6
10
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,wBAAwB,EACxB,oBAAoB,EACpB,+BAA+B,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,wBAAwB,EACxB,oBAAoB,EACpB,+BAA+B,EAC/B,sBAAsB,GACvB,8BAA0B;AAC3B,OAAO,EACL,eAAe,EACf,8BAA8B,GAC/B,8BAA0B;AAC3B,YAAY,EACV,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,OAAO,EACP,KAAK,EACL,WAAW,EACX,YAAY,GACb,2BAAuB;AACxB,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,iBAAiB,GAClB,2BAAuB;AACxB,YAAY,EACV,gCAAgC,EAChC,8BAA8B,EAC9B,gCAAgC,GACjC,+CAA2C;AAC5C,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,qBAAqB,EACrB,cAAc,GACf,2BAAuB;AACxB,OAAO,EACL,aAAa,EACb,yBAAyB,EACzB,8BAA8B,EAC9B,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,GACjB,2BAAuB;AACxB,YAAY,EAAE,qBAAqB,EAAE,wBAAoB;AACzD,OAAO,EAAE,qBAAqB,EAAE,wBAAoB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { RampsController, getDefaultRampsControllerState } from "./RampsController.mjs";
|
|
2
|
-
export {
|
|
2
|
+
export { RampsService, RampsEnvironment, RampsApiService, RAMPS_SDK_VERSION } from "./RampsService.mjs";
|
|
3
|
+
export { RequestStatus, DEFAULT_REQUEST_CACHE_TTL, DEFAULT_REQUEST_CACHE_MAX_SIZE, createCacheKey, isCacheExpired, createLoadingState, createSuccessState, createErrorState } from "./RequestCache.mjs";
|
|
4
|
+
export { createRequestSelector } from "./selectors.mjs";
|
|
3
5
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,OAAO,EACL,eAAe,EACf,8BAA8B,EAC/B,8BAA0B;AAU3B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EAClB,2BAAuB;AAYxB,OAAO,EACL,aAAa,EACb,yBAAyB,EACzB,8BAA8B,EAC9B,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACjB,2BAAuB;AAExB,OAAO,EAAE,qBAAqB,EAAE,wBAAoB","sourcesContent":["export type {\n RampsControllerActions,\n RampsControllerEvents,\n RampsControllerGetStateAction,\n RampsControllerMessenger,\n RampsControllerState,\n RampsControllerStateChangeEvent,\n RampsControllerOptions,\n} from './RampsController';\nexport {\n RampsController,\n getDefaultRampsControllerState,\n} from './RampsController';\nexport type {\n RampsServiceActions,\n RampsServiceEvents,\n RampsServiceMessenger,\n Country,\n State,\n Eligibility,\n CountryPhone,\n} from './RampsService';\nexport {\n RampsService,\n RampsEnvironment,\n RampsApiService,\n RAMPS_SDK_VERSION,\n} from './RampsService';\nexport type {\n RampsServiceGetGeolocationAction,\n RampsServiceGetCountriesAction,\n RampsServiceGetEligibilityAction,\n} from './RampsService-method-action-types';\nexport type {\n RequestCache,\n RequestState,\n ExecuteRequestOptions,\n PendingRequest,\n} from './RequestCache';\nexport {\n RequestStatus,\n DEFAULT_REQUEST_CACHE_TTL,\n DEFAULT_REQUEST_CACHE_MAX_SIZE,\n createCacheKey,\n isCacheExpired,\n createLoadingState,\n createSuccessState,\n createErrorState,\n} from './RequestCache';\nexport type { RequestSelectorResult } from './selectors';\nexport { createRequestSelector } from './selectors';\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRequestSelector = void 0;
|
|
4
|
+
const RequestCache_1 = require("./RequestCache.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a memoized selector for a controller method's request state.
|
|
7
|
+
*
|
|
8
|
+
* This selector tracks the loading, error, and data state for a specific
|
|
9
|
+
* controller method call. It's optimized for use with React Redux's `useSelector`
|
|
10
|
+
* hook - the selector returns the same object reference when the underlying
|
|
11
|
+
* request state hasn't changed, so no `shallowEqual` is needed.
|
|
12
|
+
*
|
|
13
|
+
* The selector uses reference equality on the request object itself, so it
|
|
14
|
+
* works correctly with arrays and objects without expensive deep equality checks.
|
|
15
|
+
*
|
|
16
|
+
* @param getState - Function that extracts RampsControllerState from the root state.
|
|
17
|
+
* Typically a reselect selector like `selectRampsControllerState`.
|
|
18
|
+
* @param method - The controller method name (e.g., 'updateGeolocation').
|
|
19
|
+
* @param params - The parameters passed to the method, used to generate the cache key.
|
|
20
|
+
* Must match the params used when calling the controller method.
|
|
21
|
+
* @returns A selector function that returns `{ data, isFetching, error }`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // In selectors file - create once at module level
|
|
26
|
+
* import { createRequestSelector } from '@metamask/ramps-controller';
|
|
27
|
+
* import { createSelector } from 'reselect';
|
|
28
|
+
*
|
|
29
|
+
* const selectRampsControllerState = createSelector(
|
|
30
|
+
* (state: RootState) => state.engine.backgroundState.RampsController,
|
|
31
|
+
* (rampsControllerState) => rampsControllerState,
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* export const selectGeolocationRequest = createRequestSelector<
|
|
35
|
+
* RootState,
|
|
36
|
+
* string
|
|
37
|
+
* >(selectRampsControllerState, 'updateGeolocation', []);
|
|
38
|
+
*
|
|
39
|
+
* // In hook - use directly with useSelector, no shallowEqual needed
|
|
40
|
+
* export function useRampsGeolocation() {
|
|
41
|
+
* const { isFetching, error } = useSelector(selectGeolocationRequest);
|
|
42
|
+
* // ... rest of hook
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // For methods with parameters
|
|
49
|
+
* export const selectCryptoCurrenciesRequest = (region: string) =>
|
|
50
|
+
* createRequestSelector<RootState, CryptoCurrency[]>(
|
|
51
|
+
* selectRampsControllerState,
|
|
52
|
+
* 'getCryptoCurrencies',
|
|
53
|
+
* [region],
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* // In component
|
|
57
|
+
* const { data, isFetching, error } = useSelector(
|
|
58
|
+
* selectCryptoCurrenciesRequest('US')
|
|
59
|
+
* );
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function createRequestSelector(getState, method, params) {
|
|
63
|
+
const cacheKey = (0, RequestCache_1.createCacheKey)(method, params);
|
|
64
|
+
let lastRequest;
|
|
65
|
+
let lastResult = null;
|
|
66
|
+
return (state) => {
|
|
67
|
+
const request = getState(state)?.requests?.[cacheKey];
|
|
68
|
+
if (request === lastRequest && lastResult !== null) {
|
|
69
|
+
return lastResult;
|
|
70
|
+
}
|
|
71
|
+
lastRequest = request;
|
|
72
|
+
lastResult = {
|
|
73
|
+
data: request?.data ?? null,
|
|
74
|
+
isFetching: request?.status === RequestCache_1.RequestStatus.LOADING,
|
|
75
|
+
error: request?.error ?? null,
|
|
76
|
+
};
|
|
77
|
+
return lastResult;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.createRequestSelector = createRequestSelector;
|
|
81
|
+
//# sourceMappingURL=selectors.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selectors.cjs","sourceRoot":"","sources":["../src/selectors.ts"],"names":[],"mappings":";;;AAEA,qDAA+D;AAkB/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,SAAgB,qBAAqB,CACnC,QAAqE,EACrE,MAAc,EACd,MAAiB;IAEjB,MAAM,QAAQ,GAAG,IAAA,6BAAc,EAAC,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,4BAAa,CAAC,OAAO;YACrD,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,IAAI;SAC9B,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC;AA1BD,sDA0BC","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., 'updateGeolocation').\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 selectGeolocationRequest = createRequestSelector<\n * RootState,\n * string\n * >(selectRampsControllerState, 'updateGeolocation', []);\n *\n * // In hook - use directly with useSelector, no shallowEqual needed\n * export function useRampsGeolocation() {\n * const { isFetching, error } = useSelector(selectGeolocationRequest);\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"]}
|