@metamask/ramps-controller 1.0.0 → 2.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 +13 -1
- package/dist/RampsController.cjs +2 -2
- package/dist/RampsController.cjs.map +1 -1
- package/dist/RampsController.d.cts +3 -3
- package/dist/RampsController.d.cts.map +1 -1
- package/dist/RampsController.d.mts +3 -3
- package/dist/RampsController.d.mts.map +1 -1
- package/dist/RampsController.mjs +2 -2
- 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 +20 -0
- package/dist/RampsService-method-action-types.d.cts.map +1 -0
- package/dist/RampsService-method-action-types.d.mts +20 -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/{OnRampService.cjs → RampsService.cjs} +52 -50
- package/dist/RampsService.cjs.map +1 -0
- package/dist/{OnRampService.d.cts → RampsService.d.cts} +31 -31
- package/dist/RampsService.d.cts.map +1 -0
- package/dist/{OnRampService.d.mts → RampsService.d.mts} +31 -31
- package/dist/RampsService.d.mts.map +1 -0
- package/dist/{OnRampService.mjs → RampsService.mjs} +50 -48
- package/dist/RampsService.mjs.map +1 -0
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- 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.map +0 -1
- package/dist/OnRampService.d.cts.map +0 -1
- package/dist/OnRampService.d.mts.map +0 -1
- package/dist/OnRampService.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [2.0.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING:** Rename `OnRampService` to `RampsService` and `OnRampEnvironment` to `RampsEnvironment` ([#7502](https://github.com/MetaMask/core/pull/7502))
|
|
15
|
+
- **BREAKING:** Rename action types from `OnRampService:*` to `RampsService:*` (e.g., `OnRampService:getGeolocation` → `RampsService:getGeolocation`) ([#7502](https://github.com/MetaMask/core/pull/7502))
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- Fix `RampsService#getGeolocation` to read response text within the policy execution and return parsed text ([#7502](https://github.com/MetaMask/core/pull/7502))
|
|
20
|
+
|
|
10
21
|
## [1.0.0]
|
|
11
22
|
|
|
12
23
|
### Added
|
|
@@ -16,5 +27,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
16
27
|
- Add `OnRampService` for interacting with the OnRamp API
|
|
17
28
|
- Add geolocation detection via IP address lookup
|
|
18
29
|
|
|
19
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@
|
|
30
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@2.0.0...HEAD
|
|
31
|
+
[2.0.0]: https://github.com/MetaMask/core/compare/@metamask/ramps-controller@1.0.0...@metamask/ramps-controller@2.0.0
|
|
20
32
|
[1.0.0]: https://github.com/MetaMask/core/releases/tag/@metamask/ramps-controller@1.0.0
|
package/dist/RampsController.cjs
CHANGED
|
@@ -60,11 +60,11 @@ class RampsController extends base_controller_1.BaseController {
|
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* Updates the user's geolocation.
|
|
63
|
-
* This method calls the
|
|
63
|
+
* This method calls the RampsService to get the geolocation
|
|
64
64
|
* and stores the result in state.
|
|
65
65
|
*/
|
|
66
66
|
async updateGeolocation() {
|
|
67
|
-
const geolocation = await this.messenger.call('
|
|
67
|
+
const geolocation = await this.messenger.call('RampsService:getGeolocation');
|
|
68
68
|
this.update((state) => {
|
|
69
69
|
state.geolocation = geolocation;
|
|
70
70
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.cjs","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAK3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,iBAAiB,CAAC;AAchD;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CAC4C,CAAC;AAEhD;;;;;;;GAOG;AACH,SAAgB,8BAA8B;IAC5C,OAAO;QACL,WAAW,EAAE,IAAI;KAClB,CAAC;AACJ,CAAC;AAJD,wEAIC;AAkDD,gCAAgC;AAEhC;;GAEG;AACH,MAAa,eAAgB,SAAQ,gCAIpC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAAG,EAAE,GAIX;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,uBAAuB;YACjC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,8BAA8B,EAAE;gBACnC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,
|
|
1
|
+
{"version":3,"file":"RampsController.cjs","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAK3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,iBAAiB,CAAC;AAchD;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CAC4C,CAAC;AAEhD;;;;;;;GAOG;AACH,SAAgB,8BAA8B;IAC5C,OAAO;QACL,WAAW,EAAE,IAAI;KAClB,CAAC;AACJ,CAAC;AAJD,wEAIC;AAkDD,gCAAgC;AAEhC;;GAEG;AACH,MAAa,eAAgB,SAAQ,gCAIpC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAAG,EAAE,GAIX;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,uBAAuB;YACjC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,8BAA8B,EAAE;gBACnC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,6BAA6B,CAC9B,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,WAAW,GAAG,WAAW,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA7CD,0CA6CC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { RampsServiceGetGeolocationAction } from './RampsService-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link RampsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'RampsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link RampsController}.\n */\nexport type RampsControllerState = {\n /**\n * The user's country code determined by geolocation.\n */\n geolocation: string | null;\n};\n\n/**\n * The metadata for each property in {@link RampsControllerState}.\n */\nconst rampsControllerMetadata = {\n geolocation: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<RampsControllerState>;\n\n/**\n * Constructs the default {@link RampsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link RampsController} state.\n */\nexport function getDefaultRampsControllerState(): RampsControllerState {\n return {\n geolocation: null,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * Retrieves the state of the {@link RampsController}.\n */\nexport type RampsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n RampsControllerState\n>;\n\n/**\n * Actions that {@link RampsControllerMessenger} exposes to other consumers.\n */\nexport type RampsControllerActions = RampsControllerGetStateAction;\n\n/**\n * Actions from other messengers that {@link RampsController} calls.\n */\ntype AllowedActions = RampsServiceGetGeolocationAction;\n\n/**\n * Published when the state of {@link RampsController} changes.\n */\nexport type RampsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n RampsControllerState\n>;\n\n/**\n * Events that {@link RampsControllerMessenger} exposes to other consumers.\n */\nexport type RampsControllerEvents = RampsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link RampsController} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link RampsController}.\n */\nexport type RampsControllerMessenger = Messenger<\n typeof controllerName,\n RampsControllerActions | AllowedActions,\n RampsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * Manages cryptocurrency on/off ramps functionality.\n */\nexport class RampsController extends BaseController<\n typeof controllerName,\n RampsControllerState,\n RampsControllerMessenger\n> {\n /**\n * Constructs a new {@link RampsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state = {},\n }: {\n messenger: RampsControllerMessenger;\n state?: Partial<RampsControllerState>;\n }) {\n super({\n messenger,\n metadata: rampsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultRampsControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Updates the user's geolocation.\n * This method calls the RampsService to get the geolocation\n * and stores the result in state.\n */\n async updateGeolocation(): Promise<void> {\n const geolocation = await this.messenger.call(\n 'RampsService:getGeolocation',\n );\n\n this.update((state) => {\n state.geolocation = geolocation;\n });\n }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type {
|
|
4
|
+
import type { RampsServiceGetGeolocationAction } from "./RampsService-method-action-types.cjs";
|
|
5
5
|
/**
|
|
6
6
|
* The name of the {@link RampsController}, used to namespace the
|
|
7
7
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -37,7 +37,7 @@ export type RampsControllerActions = RampsControllerGetStateAction;
|
|
|
37
37
|
/**
|
|
38
38
|
* Actions from other messengers that {@link RampsController} calls.
|
|
39
39
|
*/
|
|
40
|
-
type AllowedActions =
|
|
40
|
+
type AllowedActions = RampsServiceGetGeolocationAction;
|
|
41
41
|
/**
|
|
42
42
|
* Published when the state of {@link RampsController} changes.
|
|
43
43
|
*/
|
|
@@ -73,7 +73,7 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
73
73
|
});
|
|
74
74
|
/**
|
|
75
75
|
* Updates the user's geolocation.
|
|
76
|
-
* This method calls the
|
|
76
|
+
* This method calls the RampsService to get the geolocation
|
|
77
77
|
* and stores the result in state.
|
|
78
78
|
*/
|
|
79
79
|
updateGeolocation(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.d.cts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"RampsController.d.cts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,gCAAgC,EAAE,+CAA2C;AAI3F;;;;GAIG;AACH,eAAO,MAAM,cAAc,oBAAoB,CAAC;AAIhD;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;OAEG;IACH,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,8BAA8B,IAAI,oBAAoB,CAIrE;AAID;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG,wBAAwB,CAClE,OAAO,cAAc,EACrB,oBAAoB,CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,6BAA6B,CAAC;AAEnE;;GAEG;AACH,KAAK,cAAc,GAAG,gCAAgC,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,+BAA+B,GAAG,0BAA0B,CACtE,OAAO,cAAc,EACrB,oBAAoB,CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,+BAA+B,CAAC;AAEpE;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,SAAS,CAC9C,OAAO,cAAc,EACrB,sBAAsB,GAAG,cAAc,EACvC,qBAAqB,GAAG,aAAa,CACtC,CAAC;AAIF;;GAEG;AACH,qBAAa,eAAgB,SAAQ,cAAc,CACjD,OAAO,cAAc,EACrB,oBAAoB,EACpB,wBAAwB,CACzB;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAU,GACX,EAAE;QACD,SAAS,EAAE,wBAAwB,CAAC;QACpC,KAAK,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;KACvC;IAYD;;;;OAIG;IACG,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;CASzC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type {
|
|
4
|
+
import type { RampsServiceGetGeolocationAction } from "./RampsService-method-action-types.mjs";
|
|
5
5
|
/**
|
|
6
6
|
* The name of the {@link RampsController}, used to namespace the
|
|
7
7
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -37,7 +37,7 @@ export type RampsControllerActions = RampsControllerGetStateAction;
|
|
|
37
37
|
/**
|
|
38
38
|
* Actions from other messengers that {@link RampsController} calls.
|
|
39
39
|
*/
|
|
40
|
-
type AllowedActions =
|
|
40
|
+
type AllowedActions = RampsServiceGetGeolocationAction;
|
|
41
41
|
/**
|
|
42
42
|
* Published when the state of {@link RampsController} changes.
|
|
43
43
|
*/
|
|
@@ -73,7 +73,7 @@ export declare class RampsController extends BaseController<typeof controllerNam
|
|
|
73
73
|
});
|
|
74
74
|
/**
|
|
75
75
|
* Updates the user's geolocation.
|
|
76
|
-
* This method calls the
|
|
76
|
+
* This method calls the RampsService to get the geolocation
|
|
77
77
|
* and stores the result in state.
|
|
78
78
|
*/
|
|
79
79
|
updateGeolocation(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.d.mts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"RampsController.d.mts","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,gCAAgC,EAAE,+CAA2C;AAI3F;;;;GAIG;AACH,eAAO,MAAM,cAAc,oBAAoB,CAAC;AAIhD;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;OAEG;IACH,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,8BAA8B,IAAI,oBAAoB,CAIrE;AAID;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG,wBAAwB,CAClE,OAAO,cAAc,EACrB,oBAAoB,CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,6BAA6B,CAAC;AAEnE;;GAEG;AACH,KAAK,cAAc,GAAG,gCAAgC,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,+BAA+B,GAAG,0BAA0B,CACtE,OAAO,cAAc,EACrB,oBAAoB,CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,+BAA+B,CAAC;AAEpE;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,SAAS,CAC9C,OAAO,cAAc,EACrB,sBAAsB,GAAG,cAAc,EACvC,qBAAqB,GAAG,aAAa,CACtC,CAAC;AAIF;;GAEG;AACH,qBAAa,eAAgB,SAAQ,cAAc,CACjD,OAAO,cAAc,EACrB,oBAAoB,EACpB,wBAAwB,CACzB;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAU,GACX,EAAE;QACD,SAAS,EAAE,wBAAwB,CAAC;QACpC,KAAK,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;KACvC;IAYD;;;;OAIG;IACG,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;CASzC"}
|
package/dist/RampsController.mjs
CHANGED
|
@@ -56,11 +56,11 @@ export class RampsController extends BaseController {
|
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Updates the user's geolocation.
|
|
59
|
-
* This method calls the
|
|
59
|
+
* This method calls the RampsService to get the geolocation
|
|
60
60
|
* and stores the result in state.
|
|
61
61
|
*/
|
|
62
62
|
async updateGeolocation() {
|
|
63
|
-
const geolocation = await this.messenger.call('
|
|
63
|
+
const geolocation = await this.messenger.call('RampsService:getGeolocation');
|
|
64
64
|
this.update((state) => {
|
|
65
65
|
state.geolocation = geolocation;
|
|
66
66
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RampsController.mjs","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,iBAAiB,CAAC;AAchD;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CAC4C,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,UAAU,8BAA8B;IAC5C,OAAO;QACL,WAAW,EAAE,IAAI;KAClB,CAAC;AACJ,CAAC;AAkDD,gCAAgC;AAEhC;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,cAIpC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAAG,EAAE,GAIX;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,uBAAuB;YACjC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,8BAA8B,EAAE;gBACnC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,
|
|
1
|
+
{"version":3,"file":"RampsController.mjs","sourceRoot":"","sources":["../src/RampsController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,iBAAiB,CAAC;AAchD;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;CAC4C,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,UAAU,8BAA8B;IAC5C,OAAO;QACL,WAAW,EAAE,IAAI;KAClB,CAAC;AACJ,CAAC;AAkDD,gCAAgC;AAEhC;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,cAIpC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAAG,EAAE,GAIX;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,uBAAuB;YACjC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,8BAA8B,EAAE;gBACnC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,iBAAiB;QACrB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,6BAA6B,CAC9B,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,WAAW,GAAG,WAAW,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { RampsServiceGetGeolocationAction } from './RampsService-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link RampsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'RampsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link RampsController}.\n */\nexport type RampsControllerState = {\n /**\n * The user's country code determined by geolocation.\n */\n geolocation: string | null;\n};\n\n/**\n * The metadata for each property in {@link RampsControllerState}.\n */\nconst rampsControllerMetadata = {\n geolocation: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<RampsControllerState>;\n\n/**\n * Constructs the default {@link RampsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link RampsController} state.\n */\nexport function getDefaultRampsControllerState(): RampsControllerState {\n return {\n geolocation: null,\n };\n}\n\n// === MESSENGER ===\n\n/**\n * Retrieves the state of the {@link RampsController}.\n */\nexport type RampsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n RampsControllerState\n>;\n\n/**\n * Actions that {@link RampsControllerMessenger} exposes to other consumers.\n */\nexport type RampsControllerActions = RampsControllerGetStateAction;\n\n/**\n * Actions from other messengers that {@link RampsController} calls.\n */\ntype AllowedActions = RampsServiceGetGeolocationAction;\n\n/**\n * Published when the state of {@link RampsController} changes.\n */\nexport type RampsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n RampsControllerState\n>;\n\n/**\n * Events that {@link RampsControllerMessenger} exposes to other consumers.\n */\nexport type RampsControllerEvents = RampsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link RampsController} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link RampsController}.\n */\nexport type RampsControllerMessenger = Messenger<\n typeof controllerName,\n RampsControllerActions | AllowedActions,\n RampsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * Manages cryptocurrency on/off ramps functionality.\n */\nexport class RampsController extends BaseController<\n typeof controllerName,\n RampsControllerState,\n RampsControllerMessenger\n> {\n /**\n * Constructs a new {@link RampsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state = {},\n }: {\n messenger: RampsControllerMessenger;\n state?: Partial<RampsControllerState>;\n }) {\n super({\n messenger,\n metadata: rampsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultRampsControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Updates the user's geolocation.\n * This method calls the RampsService to get the geolocation\n * and stores the result in state.\n */\n async updateGeolocation(): Promise<void> {\n const geolocation = await this.messenger.call(\n 'RampsService:getGeolocation',\n );\n\n this.update((state) => {\n state.geolocation = geolocation;\n });\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RampsService-method-action-types.cjs","sourceRoot":"","sources":["../src/RampsService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { RampsService } from './RampsService';\n\n/**\n * Makes a request to the API in order to retrieve the user's geolocation\n * based on their IP address.\n *\n * @returns The user's country/region code (e.g., \"US-UT\" for Utah, USA).\n */\nexport type RampsServiceGetGeolocationAction = {\n type: `RampsService:getGeolocation`;\n handler: RampsService['getGeolocation'];\n};\n\n/**\n * Union of all RampsService action types.\n */\nexport type RampsServiceMethodActions = RampsServiceGetGeolocationAction;\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { RampsService } from "./RampsService.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Makes a request to the API in order to retrieve the user's geolocation
|
|
8
|
+
* based on their IP address.
|
|
9
|
+
*
|
|
10
|
+
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
11
|
+
*/
|
|
12
|
+
export type RampsServiceGetGeolocationAction = {
|
|
13
|
+
type: `RampsService:getGeolocation`;
|
|
14
|
+
handler: RampsService['getGeolocation'];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Union of all RampsService action types.
|
|
18
|
+
*/
|
|
19
|
+
export type RampsServiceMethodActions = RampsServiceGetGeolocationAction;
|
|
20
|
+
//# sourceMappingURL=RampsService-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RampsService-method-action-types.d.cts","sourceRoot":"","sources":["../src/RampsService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,2BAAuB;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,gCAAgC,GAAG;IAC7C,IAAI,EAAE,6BAA6B,CAAC;IACpC,OAAO,EAAE,YAAY,CAAC,gBAAgB,CAAC,CAAC;CACzC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { RampsService } from "./RampsService.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Makes a request to the API in order to retrieve the user's geolocation
|
|
8
|
+
* based on their IP address.
|
|
9
|
+
*
|
|
10
|
+
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
11
|
+
*/
|
|
12
|
+
export type RampsServiceGetGeolocationAction = {
|
|
13
|
+
type: `RampsService:getGeolocation`;
|
|
14
|
+
handler: RampsService['getGeolocation'];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Union of all RampsService action types.
|
|
18
|
+
*/
|
|
19
|
+
export type RampsServiceMethodActions = RampsServiceGetGeolocationAction;
|
|
20
|
+
//# sourceMappingURL=RampsService-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RampsService-method-action-types.d.mts","sourceRoot":"","sources":["../src/RampsService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,2BAAuB;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,gCAAgC,GAAG;IAC7C,IAAI,EAAE,6BAA6B,CAAC;IACpC,OAAO,EAAE,YAAY,CAAC,gBAAgB,CAAC,CAAC;CACzC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,gCAAgC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RampsService-method-action-types.mjs","sourceRoot":"","sources":["../src/RampsService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { RampsService } from './RampsService';\n\n/**\n * Makes a request to the API in order to retrieve the user's geolocation\n * based on their IP address.\n *\n * @returns The user's country/region code (e.g., \"US-UT\" for Utah, USA).\n */\nexport type RampsServiceGetGeolocationAction = {\n type: `RampsService:getGeolocation`;\n handler: RampsService['getGeolocation'];\n};\n\n/**\n * Union of all RampsService action types.\n */\nexport type RampsServiceMethodActions = RampsServiceGetGeolocationAction;\n"]}
|
|
@@ -10,25 +10,25 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _a,
|
|
13
|
+
var _a, _RampsService_messenger, _RampsService_fetch, _RampsService_policy, _RampsService_baseUrl;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
15
|
+
exports.RampsService = exports.RampsEnvironment = exports.serviceName = void 0;
|
|
16
16
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
17
17
|
// === GENERAL ===
|
|
18
18
|
/**
|
|
19
|
-
* The name of the {@link
|
|
19
|
+
* The name of the {@link RampsService}, used to namespace the
|
|
20
20
|
* service's actions and events.
|
|
21
21
|
*/
|
|
22
|
-
exports.serviceName = '
|
|
22
|
+
exports.serviceName = 'RampsService';
|
|
23
23
|
/**
|
|
24
24
|
* The environment to use for API requests.
|
|
25
25
|
*/
|
|
26
|
-
var
|
|
27
|
-
(function (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})(
|
|
26
|
+
var RampsEnvironment;
|
|
27
|
+
(function (RampsEnvironment) {
|
|
28
|
+
RampsEnvironment["Production"] = "production";
|
|
29
|
+
RampsEnvironment["Staging"] = "staging";
|
|
30
|
+
RampsEnvironment["Development"] = "development";
|
|
31
|
+
})(RampsEnvironment || (exports.RampsEnvironment = RampsEnvironment = {}));
|
|
32
32
|
// === MESSENGER ===
|
|
33
33
|
const MESSENGER_EXPOSED_METHODS = ['getGeolocation'];
|
|
34
34
|
// === SERVICE DEFINITION ===
|
|
@@ -40,60 +40,60 @@ const MESSENGER_EXPOSED_METHODS = ['getGeolocation'];
|
|
|
40
40
|
*/
|
|
41
41
|
function getBaseUrl(environment) {
|
|
42
42
|
switch (environment) {
|
|
43
|
-
case
|
|
43
|
+
case RampsEnvironment.Production:
|
|
44
44
|
return 'https://on-ramp.api.cx.metamask.io';
|
|
45
|
-
case
|
|
45
|
+
case RampsEnvironment.Staging:
|
|
46
46
|
return 'https://on-ramp.uat-api.cx.metamask.io';
|
|
47
|
-
case
|
|
47
|
+
case RampsEnvironment.Development:
|
|
48
48
|
return 'http://localhost:3000';
|
|
49
49
|
default:
|
|
50
50
|
throw new Error(`Invalid environment: ${String(environment)}`);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* This service object is responsible for interacting with the
|
|
54
|
+
* This service object is responsible for interacting with the Ramps API.
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
57
57
|
*
|
|
58
58
|
* ``` ts
|
|
59
59
|
* import { Messenger } from '@metamask/messenger';
|
|
60
60
|
* import type {
|
|
61
|
-
*
|
|
62
|
-
*
|
|
61
|
+
* RampsServiceActions,
|
|
62
|
+
* RampsServiceEvents,
|
|
63
63
|
* } from '@metamask/ramps-controller';
|
|
64
64
|
*
|
|
65
65
|
* const rootMessenger = new Messenger<
|
|
66
66
|
* 'Root',
|
|
67
|
-
*
|
|
68
|
-
*
|
|
67
|
+
* RampsServiceActions
|
|
68
|
+
* RampsServiceEvents
|
|
69
69
|
* >({ namespace: 'Root' });
|
|
70
|
-
* const
|
|
71
|
-
* '
|
|
72
|
-
*
|
|
73
|
-
*
|
|
70
|
+
* const rampsServiceMessenger = new Messenger<
|
|
71
|
+
* 'RampsService',
|
|
72
|
+
* RampsServiceActions,
|
|
73
|
+
* RampsServiceEvents,
|
|
74
74
|
* typeof rootMessenger,
|
|
75
75
|
* >({
|
|
76
|
-
* namespace: '
|
|
76
|
+
* namespace: 'RampsService',
|
|
77
77
|
* parent: rootMessenger,
|
|
78
78
|
* });
|
|
79
79
|
* // Instantiate the service to register its actions on the messenger
|
|
80
|
-
* new
|
|
81
|
-
* messenger:
|
|
82
|
-
* environment:
|
|
80
|
+
* new RampsService({
|
|
81
|
+
* messenger: rampsServiceMessenger,
|
|
82
|
+
* environment: RampsEnvironment.Production,
|
|
83
83
|
* fetch,
|
|
84
84
|
* });
|
|
85
85
|
*
|
|
86
86
|
* // Later...
|
|
87
87
|
* // Get the user's geolocation
|
|
88
88
|
* const geolocation = await rootMessenger.call(
|
|
89
|
-
* '
|
|
89
|
+
* 'RampsService:getGeolocation',
|
|
90
90
|
* );
|
|
91
91
|
* // ... Do something with the geolocation ...
|
|
92
92
|
* ```
|
|
93
93
|
*/
|
|
94
|
-
class
|
|
94
|
+
class RampsService {
|
|
95
95
|
/**
|
|
96
|
-
* Constructs a new
|
|
96
|
+
* Constructs a new RampsService object.
|
|
97
97
|
*
|
|
98
98
|
* @param args - The constructor arguments.
|
|
99
99
|
* @param args.messenger - The messenger suited for this service.
|
|
@@ -105,31 +105,31 @@ class OnRampService {
|
|
|
105
105
|
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
106
106
|
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
107
107
|
*/
|
|
108
|
-
constructor({ messenger, environment =
|
|
108
|
+
constructor({ messenger, environment = RampsEnvironment.Staging, fetch: fetchFunction, policyOptions = {}, }) {
|
|
109
109
|
/**
|
|
110
110
|
* The messenger suited for this service.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
_RampsService_messenger.set(this, void 0);
|
|
113
113
|
/**
|
|
114
114
|
* A function that can be used to make an HTTP request.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
_RampsService_fetch.set(this, void 0);
|
|
117
117
|
/**
|
|
118
118
|
* The policy that wraps the request.
|
|
119
119
|
*
|
|
120
120
|
* @see {@link createServicePolicy}
|
|
121
121
|
*/
|
|
122
|
-
|
|
122
|
+
_RampsService_policy.set(this, void 0);
|
|
123
123
|
/**
|
|
124
124
|
* The base URL for API requests.
|
|
125
125
|
*/
|
|
126
|
-
|
|
126
|
+
_RampsService_baseUrl.set(this, void 0);
|
|
127
127
|
this.name = exports.serviceName;
|
|
128
|
-
__classPrivateFieldSet(this,
|
|
129
|
-
__classPrivateFieldSet(this,
|
|
130
|
-
__classPrivateFieldSet(this,
|
|
131
|
-
__classPrivateFieldSet(this,
|
|
132
|
-
__classPrivateFieldGet(this,
|
|
128
|
+
__classPrivateFieldSet(this, _RampsService_messenger, messenger, "f");
|
|
129
|
+
__classPrivateFieldSet(this, _RampsService_fetch, fetchFunction, "f");
|
|
130
|
+
__classPrivateFieldSet(this, _RampsService_policy, (0, controller_utils_1.createServicePolicy)(policyOptions), "f");
|
|
131
|
+
__classPrivateFieldSet(this, _RampsService_baseUrl, getBaseUrl(environment), "f");
|
|
132
|
+
__classPrivateFieldGet(this, _RampsService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
135
|
* Registers a handler that will be called after a request returns a non-500
|
|
@@ -142,7 +142,7 @@ class OnRampService {
|
|
|
142
142
|
* @see {@link createServicePolicy}
|
|
143
143
|
*/
|
|
144
144
|
onRetry(listener) {
|
|
145
|
-
return __classPrivateFieldGet(this,
|
|
145
|
+
return __classPrivateFieldGet(this, _RampsService_policy, "f").onRetry(listener);
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Registers a handler that will be called after a set number of retry rounds
|
|
@@ -154,7 +154,7 @@ class OnRampService {
|
|
|
154
154
|
* @see {@link createServicePolicy}
|
|
155
155
|
*/
|
|
156
156
|
onBreak(listener) {
|
|
157
|
-
return __classPrivateFieldGet(this,
|
|
157
|
+
return __classPrivateFieldGet(this, _RampsService_policy, "f").onBreak(listener);
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
160
|
* Registers a handler that will be called under one of two circumstances:
|
|
@@ -174,7 +174,7 @@ class OnRampService {
|
|
|
174
174
|
* {@link CockatielEvent}.
|
|
175
175
|
*/
|
|
176
176
|
onDegraded(listener) {
|
|
177
|
-
return __classPrivateFieldGet(this,
|
|
177
|
+
return __classPrivateFieldGet(this, _RampsService_policy, "f").onDegraded(listener);
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
180
|
* Makes a request to the API in order to retrieve the user's geolocation
|
|
@@ -183,15 +183,17 @@ class OnRampService {
|
|
|
183
183
|
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
184
184
|
*/
|
|
185
185
|
async getGeolocation() {
|
|
186
|
-
const
|
|
187
|
-
const url = new URL('geolocation', __classPrivateFieldGet(this,
|
|
188
|
-
const localResponse = await __classPrivateFieldGet(this,
|
|
186
|
+
const responseData = await __classPrivateFieldGet(this, _RampsService_policy, "f").execute(async () => {
|
|
187
|
+
const url = new URL('geolocation', __classPrivateFieldGet(this, _RampsService_baseUrl, "f"));
|
|
188
|
+
const localResponse = await __classPrivateFieldGet(this, _RampsService_fetch, "f").call(this, url);
|
|
189
189
|
if (!localResponse.ok) {
|
|
190
190
|
throw new controller_utils_1.HttpError(localResponse.status, `Fetching '${url.toString()}' failed with status '${localResponse.status}'`);
|
|
191
191
|
}
|
|
192
|
-
|
|
192
|
+
const textResponse = await localResponse.text();
|
|
193
|
+
// Return both response and text content since we consumed the body
|
|
194
|
+
return { response: localResponse, text: textResponse };
|
|
193
195
|
});
|
|
194
|
-
const textResponse =
|
|
196
|
+
const textResponse = responseData.text;
|
|
195
197
|
const trimmedResponse = textResponse.trim();
|
|
196
198
|
if (trimmedResponse.length > 0) {
|
|
197
199
|
return trimmedResponse;
|
|
@@ -199,6 +201,6 @@ class OnRampService {
|
|
|
199
201
|
throw new Error('Malformed response received from geolocation API');
|
|
200
202
|
}
|
|
201
203
|
}
|
|
202
|
-
exports.
|
|
203
|
-
_a =
|
|
204
|
-
//# sourceMappingURL=
|
|
204
|
+
exports.RampsService = RampsService;
|
|
205
|
+
_a = RampsService, _RampsService_messenger = new WeakMap(), _RampsService_fetch = new WeakMap(), _RampsService_policy = new WeakMap(), _RampsService_baseUrl = new WeakMap();
|
|
206
|
+
//# sourceMappingURL=RampsService.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RampsService.cjs","sourceRoot":"","sources":["../src/RampsService.ts"],"names":[],"mappings":"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","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { RampsServiceMethodActions } from './RampsService-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link RampsService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'RampsService';\n\n/**\n * The environment to use for API requests.\n */\nexport enum RampsEnvironment {\n Production = 'production',\n Staging = 'staging',\n Development = 'development',\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['getGeolocation'] as const;\n\n/**\n * Actions that {@link RampsService} exposes to other consumers.\n */\nexport type RampsServiceActions = RampsServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link RampsService} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link RampsService} exposes to other consumers.\n */\nexport type RampsServiceEvents = never;\n\n/**\n * Events from other messengers that {@link RampsService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link RampsService}.\n */\nexport type RampsServiceMessenger = Messenger<\n typeof serviceName,\n RampsServiceActions | AllowedActions,\n RampsServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Gets the base URL for API requests based on the environment.\n *\n * @param environment - The environment to use.\n * @returns The base URL for API requests.\n */\nfunction getBaseUrl(environment: RampsEnvironment): string {\n switch (environment) {\n case RampsEnvironment.Production:\n return 'https://on-ramp.api.cx.metamask.io';\n case RampsEnvironment.Staging:\n return 'https://on-ramp.uat-api.cx.metamask.io';\n case RampsEnvironment.Development:\n return 'http://localhost:3000';\n default:\n throw new Error(`Invalid environment: ${String(environment)}`);\n }\n}\n\n/**\n * This service object is responsible for interacting with the Ramps API.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * RampsServiceActions,\n * RampsServiceEvents,\n * } from '@metamask/ramps-controller';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * RampsServiceActions\n * RampsServiceEvents\n * >({ namespace: 'Root' });\n * const rampsServiceMessenger = new Messenger<\n * 'RampsService',\n * RampsServiceActions,\n * RampsServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'RampsService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new RampsService({\n * messenger: rampsServiceMessenger,\n * environment: RampsEnvironment.Production,\n * fetch,\n * });\n *\n * // Later...\n * // Get the user's geolocation\n * const geolocation = await rootMessenger.call(\n * 'RampsService:getGeolocation',\n * );\n * // ... Do something with the geolocation ...\n * ```\n */\nexport class RampsService {\n /**\n * The name of the service.\n */\n readonly name: typeof serviceName;\n\n /**\n * The messenger suited for this service.\n */\n readonly #messenger: ConstructorParameters<\n typeof RampsService\n >[0]['messenger'];\n\n /**\n * A function that can be used to make an HTTP request.\n */\n readonly #fetch: ConstructorParameters<typeof RampsService>[0]['fetch'];\n\n /**\n * The policy that wraps the request.\n *\n * @see {@link createServicePolicy}\n */\n readonly #policy: ServicePolicy;\n\n /**\n * The base URL for API requests.\n */\n readonly #baseUrl: string;\n\n /**\n * Constructs a new RampsService object.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this service.\n * @param args.environment - The environment to use for API requests.\n * @param args.fetch - A function that can be used to make an HTTP request. If\n * your JavaScript environment supports `fetch` natively, you'll probably want\n * to pass that; otherwise you can pass an equivalent (such as `fetch` via\n * `node-fetch`).\n * @param args.policyOptions - Options to pass to `createServicePolicy`, which\n * is used to wrap each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n environment = RampsEnvironment.Staging,\n fetch: fetchFunction,\n policyOptions = {},\n }: {\n messenger: RampsServiceMessenger;\n environment?: RampsEnvironment;\n fetch: typeof fetch;\n policyOptions?: CreateServicePolicyOptions;\n }) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n this.#policy = createServicePolicy(policyOptions);\n this.#baseUrl = getBaseUrl(environment);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers a handler that will be called after a request returns a non-500\n * response, causing a retry. Primarily useful in tests where timers are being\n * mocked.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n * @see {@link createServicePolicy}\n */\n onRetry(\n listener: Parameters<ServicePolicy['onRetry']>[0],\n ): ReturnType<ServicePolicy['onRetry']> {\n return this.#policy.onRetry(listener);\n }\n\n /**\n * Registers a handler that will be called after a set number of retry rounds\n * prove that requests to the API endpoint consistently return a 5xx response.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n * @see {@link createServicePolicy}\n */\n onBreak(\n listener: Parameters<ServicePolicy['onBreak']>[0],\n ): ReturnType<ServicePolicy['onBreak']> {\n return this.#policy.onBreak(listener);\n }\n\n /**\n * Registers a handler that will be called under one of two circumstances:\n *\n * 1. After a set number of retries prove that requests to the API\n * consistently result in one of the following failures:\n * 1. A connection initiation error\n * 2. A connection reset error\n * 3. A timeout error\n * 4. A non-JSON response\n * 5. A 502, 503, or 504 response\n * 2. After a successful request is made to the API, but the response takes\n * longer than a set duration to return.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n onDegraded(\n listener: Parameters<ServicePolicy['onDegraded']>[0],\n ): ReturnType<ServicePolicy['onDegraded']> {\n return this.#policy.onDegraded(listener);\n }\n\n /**\n * Makes a request to the API in order to retrieve the user's geolocation\n * based on their IP address.\n *\n * @returns The user's country/region code (e.g., \"US-UT\" for Utah, USA).\n */\n async getGeolocation(): Promise<string> {\n const responseData = await this.#policy.execute(async () => {\n const url = new URL('geolocation', this.#baseUrl);\n const localResponse = await this.#fetch(url);\n if (!localResponse.ok) {\n throw new HttpError(\n localResponse.status,\n `Fetching '${url.toString()}' failed with status '${localResponse.status}'`,\n );\n }\n const textResponse = await localResponse.text();\n // Return both response and text content since we consumed the body\n return { response: localResponse, text: textResponse };\n });\n\n const textResponse = responseData.text;\n const trimmedResponse = textResponse.trim();\n\n if (trimmedResponse.length > 0) {\n return trimmedResponse;\n }\n\n throw new Error('Malformed response received from geolocation API');\n }\n}\n"]}
|