@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
package/dist/OnRampService.d.cts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
|
|
2
|
-
import type { Messenger } from "@metamask/messenger";
|
|
3
|
-
import type { OnRampServiceMethodActions } from "./OnRampService-method-action-types.cjs";
|
|
4
|
-
/**
|
|
5
|
-
* The name of the {@link OnRampService}, used to namespace the
|
|
6
|
-
* service's actions and events.
|
|
7
|
-
*/
|
|
8
|
-
export declare const serviceName = "OnRampService";
|
|
9
|
-
/**
|
|
10
|
-
* The environment to use for API requests.
|
|
11
|
-
*/
|
|
12
|
-
export declare enum OnRampEnvironment {
|
|
13
|
-
Production = "production",
|
|
14
|
-
Staging = "staging",
|
|
15
|
-
Development = "development"
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Actions that {@link OnRampService} exposes to other consumers.
|
|
19
|
-
*/
|
|
20
|
-
export type OnRampServiceActions = OnRampServiceMethodActions;
|
|
21
|
-
/**
|
|
22
|
-
* Actions from other messengers that {@link OnRampService} calls.
|
|
23
|
-
*/
|
|
24
|
-
type AllowedActions = never;
|
|
25
|
-
/**
|
|
26
|
-
* Events that {@link OnRampService} exposes to other consumers.
|
|
27
|
-
*/
|
|
28
|
-
export type OnRampServiceEvents = never;
|
|
29
|
-
/**
|
|
30
|
-
* Events from other messengers that {@link OnRampService} subscribes to.
|
|
31
|
-
*/
|
|
32
|
-
type AllowedEvents = never;
|
|
33
|
-
/**
|
|
34
|
-
* The messenger which is restricted to actions and events accessed by
|
|
35
|
-
* {@link OnRampService}.
|
|
36
|
-
*/
|
|
37
|
-
export type OnRampServiceMessenger = Messenger<typeof serviceName, OnRampServiceActions | AllowedActions, OnRampServiceEvents | AllowedEvents>;
|
|
38
|
-
/**
|
|
39
|
-
* This service object is responsible for interacting with the OnRamp API.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
*
|
|
43
|
-
* ``` ts
|
|
44
|
-
* import { Messenger } from '@metamask/messenger';
|
|
45
|
-
* import type {
|
|
46
|
-
* OnRampServiceActions,
|
|
47
|
-
* OnRampServiceEvents,
|
|
48
|
-
* } from '@metamask/ramps-controller';
|
|
49
|
-
*
|
|
50
|
-
* const rootMessenger = new Messenger<
|
|
51
|
-
* 'Root',
|
|
52
|
-
* OnRampServiceActions
|
|
53
|
-
* OnRampServiceEvents
|
|
54
|
-
* >({ namespace: 'Root' });
|
|
55
|
-
* const onRampServiceMessenger = new Messenger<
|
|
56
|
-
* 'OnRampService',
|
|
57
|
-
* OnRampServiceActions,
|
|
58
|
-
* OnRampServiceEvents,
|
|
59
|
-
* typeof rootMessenger,
|
|
60
|
-
* >({
|
|
61
|
-
* namespace: 'OnRampService',
|
|
62
|
-
* parent: rootMessenger,
|
|
63
|
-
* });
|
|
64
|
-
* // Instantiate the service to register its actions on the messenger
|
|
65
|
-
* new OnRampService({
|
|
66
|
-
* messenger: onRampServiceMessenger,
|
|
67
|
-
* environment: OnRampEnvironment.Production,
|
|
68
|
-
* fetch,
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* // Later...
|
|
72
|
-
* // Get the user's geolocation
|
|
73
|
-
* const geolocation = await rootMessenger.call(
|
|
74
|
-
* 'OnRampService:getGeolocation',
|
|
75
|
-
* );
|
|
76
|
-
* // ... Do something with the geolocation ...
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export declare class OnRampService {
|
|
80
|
-
#private;
|
|
81
|
-
/**
|
|
82
|
-
* The name of the service.
|
|
83
|
-
*/
|
|
84
|
-
readonly name: typeof serviceName;
|
|
85
|
-
/**
|
|
86
|
-
* Constructs a new OnRampService object.
|
|
87
|
-
*
|
|
88
|
-
* @param args - The constructor arguments.
|
|
89
|
-
* @param args.messenger - The messenger suited for this service.
|
|
90
|
-
* @param args.environment - The environment to use for API requests.
|
|
91
|
-
* @param args.fetch - A function that can be used to make an HTTP request. If
|
|
92
|
-
* your JavaScript environment supports `fetch` natively, you'll probably want
|
|
93
|
-
* to pass that; otherwise you can pass an equivalent (such as `fetch` via
|
|
94
|
-
* `node-fetch`).
|
|
95
|
-
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
96
|
-
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
97
|
-
*/
|
|
98
|
-
constructor({ messenger, environment, fetch: fetchFunction, policyOptions, }: {
|
|
99
|
-
messenger: OnRampServiceMessenger;
|
|
100
|
-
environment?: OnRampEnvironment;
|
|
101
|
-
fetch: typeof fetch;
|
|
102
|
-
policyOptions?: CreateServicePolicyOptions;
|
|
103
|
-
});
|
|
104
|
-
/**
|
|
105
|
-
* Registers a handler that will be called after a request returns a non-500
|
|
106
|
-
* response, causing a retry. Primarily useful in tests where timers are being
|
|
107
|
-
* mocked.
|
|
108
|
-
*
|
|
109
|
-
* @param listener - The handler to be called.
|
|
110
|
-
* @returns An object that can be used to unregister the handler. See
|
|
111
|
-
* {@link CockatielEvent}.
|
|
112
|
-
* @see {@link createServicePolicy}
|
|
113
|
-
*/
|
|
114
|
-
onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]): ReturnType<ServicePolicy['onRetry']>;
|
|
115
|
-
/**
|
|
116
|
-
* Registers a handler that will be called after a set number of retry rounds
|
|
117
|
-
* prove that requests to the API endpoint consistently return a 5xx response.
|
|
118
|
-
*
|
|
119
|
-
* @param listener - The handler to be called.
|
|
120
|
-
* @returns An object that can be used to unregister the handler. See
|
|
121
|
-
* {@link CockatielEvent}.
|
|
122
|
-
* @see {@link createServicePolicy}
|
|
123
|
-
*/
|
|
124
|
-
onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]): ReturnType<ServicePolicy['onBreak']>;
|
|
125
|
-
/**
|
|
126
|
-
* Registers a handler that will be called under one of two circumstances:
|
|
127
|
-
*
|
|
128
|
-
* 1. After a set number of retries prove that requests to the API
|
|
129
|
-
* consistently result in one of the following failures:
|
|
130
|
-
* 1. A connection initiation error
|
|
131
|
-
* 2. A connection reset error
|
|
132
|
-
* 3. A timeout error
|
|
133
|
-
* 4. A non-JSON response
|
|
134
|
-
* 5. A 502, 503, or 504 response
|
|
135
|
-
* 2. After a successful request is made to the API, but the response takes
|
|
136
|
-
* longer than a set duration to return.
|
|
137
|
-
*
|
|
138
|
-
* @param listener - The handler to be called.
|
|
139
|
-
* @returns An object that can be used to unregister the handler. See
|
|
140
|
-
* {@link CockatielEvent}.
|
|
141
|
-
*/
|
|
142
|
-
onDegraded(listener: Parameters<ServicePolicy['onDegraded']>[0]): ReturnType<ServicePolicy['onDegraded']>;
|
|
143
|
-
/**
|
|
144
|
-
* Makes a request to the API in order to retrieve the user's geolocation
|
|
145
|
-
* based on their IP address.
|
|
146
|
-
*
|
|
147
|
-
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
148
|
-
*/
|
|
149
|
-
getGeolocation(): Promise<string>;
|
|
150
|
-
}
|
|
151
|
-
export {};
|
|
152
|
-
//# sourceMappingURL=OnRampService.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService.d.cts","sourceRoot":"","sources":["../src/OnRampService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAEpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,0BAA0B,EAAE,gDAA4C;AAItF;;;GAGG;AACH,eAAO,MAAM,WAAW,kBAAkB,CAAC;AAE3C;;GAEG;AACH,oBAAY,iBAAiB;IAC3B,UAAU,eAAe;IACzB,OAAO,YAAY;IACnB,WAAW,gBAAgB;CAC5B;AAMD;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE9D;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAExC;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,SAAS,CAC5C,OAAO,WAAW,EAClB,oBAAoB,GAAG,cAAc,EACrC,mBAAmB,GAAG,aAAa,CACpC,CAAC;AAuBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,qBAAa,aAAa;;IACxB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IA0BlC;;;;;;;;;;;;OAYG;gBACS,EACV,SAAS,EACT,WAAuC,EACvC,KAAK,EAAE,aAAa,EACpB,aAAkB,GACnB,EAAE;QACD,SAAS,EAAE,sBAAsB,CAAC;QAClC,WAAW,CAAC,EAAE,iBAAiB,CAAC;QAChC,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,aAAa,CAAC,EAAE,0BAA0B,CAAC;KAC5C;IAaD;;;;;;;;;OASG;IACH,OAAO,CACL,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAChD,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;OAQG;IACH,OAAO,CACL,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAChD,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;;;;;;;;;OAgBG;IACH,UAAU,CACR,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAI1C;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;CAsBxC"}
|
package/dist/OnRampService.d.mts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
|
|
2
|
-
import type { Messenger } from "@metamask/messenger";
|
|
3
|
-
import type { OnRampServiceMethodActions } from "./OnRampService-method-action-types.mjs";
|
|
4
|
-
/**
|
|
5
|
-
* The name of the {@link OnRampService}, used to namespace the
|
|
6
|
-
* service's actions and events.
|
|
7
|
-
*/
|
|
8
|
-
export declare const serviceName = "OnRampService";
|
|
9
|
-
/**
|
|
10
|
-
* The environment to use for API requests.
|
|
11
|
-
*/
|
|
12
|
-
export declare enum OnRampEnvironment {
|
|
13
|
-
Production = "production",
|
|
14
|
-
Staging = "staging",
|
|
15
|
-
Development = "development"
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Actions that {@link OnRampService} exposes to other consumers.
|
|
19
|
-
*/
|
|
20
|
-
export type OnRampServiceActions = OnRampServiceMethodActions;
|
|
21
|
-
/**
|
|
22
|
-
* Actions from other messengers that {@link OnRampService} calls.
|
|
23
|
-
*/
|
|
24
|
-
type AllowedActions = never;
|
|
25
|
-
/**
|
|
26
|
-
* Events that {@link OnRampService} exposes to other consumers.
|
|
27
|
-
*/
|
|
28
|
-
export type OnRampServiceEvents = never;
|
|
29
|
-
/**
|
|
30
|
-
* Events from other messengers that {@link OnRampService} subscribes to.
|
|
31
|
-
*/
|
|
32
|
-
type AllowedEvents = never;
|
|
33
|
-
/**
|
|
34
|
-
* The messenger which is restricted to actions and events accessed by
|
|
35
|
-
* {@link OnRampService}.
|
|
36
|
-
*/
|
|
37
|
-
export type OnRampServiceMessenger = Messenger<typeof serviceName, OnRampServiceActions | AllowedActions, OnRampServiceEvents | AllowedEvents>;
|
|
38
|
-
/**
|
|
39
|
-
* This service object is responsible for interacting with the OnRamp API.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
*
|
|
43
|
-
* ``` ts
|
|
44
|
-
* import { Messenger } from '@metamask/messenger';
|
|
45
|
-
* import type {
|
|
46
|
-
* OnRampServiceActions,
|
|
47
|
-
* OnRampServiceEvents,
|
|
48
|
-
* } from '@metamask/ramps-controller';
|
|
49
|
-
*
|
|
50
|
-
* const rootMessenger = new Messenger<
|
|
51
|
-
* 'Root',
|
|
52
|
-
* OnRampServiceActions
|
|
53
|
-
* OnRampServiceEvents
|
|
54
|
-
* >({ namespace: 'Root' });
|
|
55
|
-
* const onRampServiceMessenger = new Messenger<
|
|
56
|
-
* 'OnRampService',
|
|
57
|
-
* OnRampServiceActions,
|
|
58
|
-
* OnRampServiceEvents,
|
|
59
|
-
* typeof rootMessenger,
|
|
60
|
-
* >({
|
|
61
|
-
* namespace: 'OnRampService',
|
|
62
|
-
* parent: rootMessenger,
|
|
63
|
-
* });
|
|
64
|
-
* // Instantiate the service to register its actions on the messenger
|
|
65
|
-
* new OnRampService({
|
|
66
|
-
* messenger: onRampServiceMessenger,
|
|
67
|
-
* environment: OnRampEnvironment.Production,
|
|
68
|
-
* fetch,
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* // Later...
|
|
72
|
-
* // Get the user's geolocation
|
|
73
|
-
* const geolocation = await rootMessenger.call(
|
|
74
|
-
* 'OnRampService:getGeolocation',
|
|
75
|
-
* );
|
|
76
|
-
* // ... Do something with the geolocation ...
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export declare class OnRampService {
|
|
80
|
-
#private;
|
|
81
|
-
/**
|
|
82
|
-
* The name of the service.
|
|
83
|
-
*/
|
|
84
|
-
readonly name: typeof serviceName;
|
|
85
|
-
/**
|
|
86
|
-
* Constructs a new OnRampService object.
|
|
87
|
-
*
|
|
88
|
-
* @param args - The constructor arguments.
|
|
89
|
-
* @param args.messenger - The messenger suited for this service.
|
|
90
|
-
* @param args.environment - The environment to use for API requests.
|
|
91
|
-
* @param args.fetch - A function that can be used to make an HTTP request. If
|
|
92
|
-
* your JavaScript environment supports `fetch` natively, you'll probably want
|
|
93
|
-
* to pass that; otherwise you can pass an equivalent (such as `fetch` via
|
|
94
|
-
* `node-fetch`).
|
|
95
|
-
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
96
|
-
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
97
|
-
*/
|
|
98
|
-
constructor({ messenger, environment, fetch: fetchFunction, policyOptions, }: {
|
|
99
|
-
messenger: OnRampServiceMessenger;
|
|
100
|
-
environment?: OnRampEnvironment;
|
|
101
|
-
fetch: typeof fetch;
|
|
102
|
-
policyOptions?: CreateServicePolicyOptions;
|
|
103
|
-
});
|
|
104
|
-
/**
|
|
105
|
-
* Registers a handler that will be called after a request returns a non-500
|
|
106
|
-
* response, causing a retry. Primarily useful in tests where timers are being
|
|
107
|
-
* mocked.
|
|
108
|
-
*
|
|
109
|
-
* @param listener - The handler to be called.
|
|
110
|
-
* @returns An object that can be used to unregister the handler. See
|
|
111
|
-
* {@link CockatielEvent}.
|
|
112
|
-
* @see {@link createServicePolicy}
|
|
113
|
-
*/
|
|
114
|
-
onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]): ReturnType<ServicePolicy['onRetry']>;
|
|
115
|
-
/**
|
|
116
|
-
* Registers a handler that will be called after a set number of retry rounds
|
|
117
|
-
* prove that requests to the API endpoint consistently return a 5xx response.
|
|
118
|
-
*
|
|
119
|
-
* @param listener - The handler to be called.
|
|
120
|
-
* @returns An object that can be used to unregister the handler. See
|
|
121
|
-
* {@link CockatielEvent}.
|
|
122
|
-
* @see {@link createServicePolicy}
|
|
123
|
-
*/
|
|
124
|
-
onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]): ReturnType<ServicePolicy['onBreak']>;
|
|
125
|
-
/**
|
|
126
|
-
* Registers a handler that will be called under one of two circumstances:
|
|
127
|
-
*
|
|
128
|
-
* 1. After a set number of retries prove that requests to the API
|
|
129
|
-
* consistently result in one of the following failures:
|
|
130
|
-
* 1. A connection initiation error
|
|
131
|
-
* 2. A connection reset error
|
|
132
|
-
* 3. A timeout error
|
|
133
|
-
* 4. A non-JSON response
|
|
134
|
-
* 5. A 502, 503, or 504 response
|
|
135
|
-
* 2. After a successful request is made to the API, but the response takes
|
|
136
|
-
* longer than a set duration to return.
|
|
137
|
-
*
|
|
138
|
-
* @param listener - The handler to be called.
|
|
139
|
-
* @returns An object that can be used to unregister the handler. See
|
|
140
|
-
* {@link CockatielEvent}.
|
|
141
|
-
*/
|
|
142
|
-
onDegraded(listener: Parameters<ServicePolicy['onDegraded']>[0]): ReturnType<ServicePolicy['onDegraded']>;
|
|
143
|
-
/**
|
|
144
|
-
* Makes a request to the API in order to retrieve the user's geolocation
|
|
145
|
-
* based on their IP address.
|
|
146
|
-
*
|
|
147
|
-
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
148
|
-
*/
|
|
149
|
-
getGeolocation(): Promise<string>;
|
|
150
|
-
}
|
|
151
|
-
export {};
|
|
152
|
-
//# sourceMappingURL=OnRampService.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService.d.mts","sourceRoot":"","sources":["../src/OnRampService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAEpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,0BAA0B,EAAE,gDAA4C;AAItF;;;GAGG;AACH,eAAO,MAAM,WAAW,kBAAkB,CAAC;AAE3C;;GAEG;AACH,oBAAY,iBAAiB;IAC3B,UAAU,eAAe;IACzB,OAAO,YAAY;IACnB,WAAW,gBAAgB;CAC5B;AAMD;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE9D;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAExC;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,SAAS,CAC5C,OAAO,WAAW,EAClB,oBAAoB,GAAG,cAAc,EACrC,mBAAmB,GAAG,aAAa,CACpC,CAAC;AAuBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,qBAAa,aAAa;;IACxB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IA0BlC;;;;;;;;;;;;OAYG;gBACS,EACV,SAAS,EACT,WAAuC,EACvC,KAAK,EAAE,aAAa,EACpB,aAAkB,GACnB,EAAE;QACD,SAAS,EAAE,sBAAsB,CAAC;QAClC,WAAW,CAAC,EAAE,iBAAiB,CAAC;QAChC,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,aAAa,CAAC,EAAE,0BAA0B,CAAC;KAC5C;IAaD;;;;;;;;;OASG;IACH,OAAO,CACL,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAChD,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;OAQG;IACH,OAAO,CACL,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAChD,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;;;;;;;;;OAgBG;IACH,UAAU,CACR,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAI1C;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;CAsBxC"}
|
package/dist/OnRampService.mjs
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
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");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _a, _OnRampService_messenger, _OnRampService_fetch, _OnRampService_policy, _OnRampService_baseUrl;
|
|
13
|
-
import { createServicePolicy, HttpError } from "@metamask/controller-utils";
|
|
14
|
-
// === GENERAL ===
|
|
15
|
-
/**
|
|
16
|
-
* The name of the {@link OnRampService}, used to namespace the
|
|
17
|
-
* service's actions and events.
|
|
18
|
-
*/
|
|
19
|
-
export const serviceName = 'OnRampService';
|
|
20
|
-
/**
|
|
21
|
-
* The environment to use for API requests.
|
|
22
|
-
*/
|
|
23
|
-
export var OnRampEnvironment;
|
|
24
|
-
(function (OnRampEnvironment) {
|
|
25
|
-
OnRampEnvironment["Production"] = "production";
|
|
26
|
-
OnRampEnvironment["Staging"] = "staging";
|
|
27
|
-
OnRampEnvironment["Development"] = "development";
|
|
28
|
-
})(OnRampEnvironment || (OnRampEnvironment = {}));
|
|
29
|
-
// === MESSENGER ===
|
|
30
|
-
const MESSENGER_EXPOSED_METHODS = ['getGeolocation'];
|
|
31
|
-
// === SERVICE DEFINITION ===
|
|
32
|
-
/**
|
|
33
|
-
* Gets the base URL for API requests based on the environment.
|
|
34
|
-
*
|
|
35
|
-
* @param environment - The environment to use.
|
|
36
|
-
* @returns The base URL for API requests.
|
|
37
|
-
*/
|
|
38
|
-
function getBaseUrl(environment) {
|
|
39
|
-
switch (environment) {
|
|
40
|
-
case OnRampEnvironment.Production:
|
|
41
|
-
return 'https://on-ramp.api.cx.metamask.io';
|
|
42
|
-
case OnRampEnvironment.Staging:
|
|
43
|
-
return 'https://on-ramp.uat-api.cx.metamask.io';
|
|
44
|
-
case OnRampEnvironment.Development:
|
|
45
|
-
return 'http://localhost:3000';
|
|
46
|
-
default:
|
|
47
|
-
throw new Error(`Invalid environment: ${String(environment)}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* This service object is responsible for interacting with the OnRamp API.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
*
|
|
55
|
-
* ``` ts
|
|
56
|
-
* import { Messenger } from '@metamask/messenger';
|
|
57
|
-
* import type {
|
|
58
|
-
* OnRampServiceActions,
|
|
59
|
-
* OnRampServiceEvents,
|
|
60
|
-
* } from '@metamask/ramps-controller';
|
|
61
|
-
*
|
|
62
|
-
* const rootMessenger = new Messenger<
|
|
63
|
-
* 'Root',
|
|
64
|
-
* OnRampServiceActions
|
|
65
|
-
* OnRampServiceEvents
|
|
66
|
-
* >({ namespace: 'Root' });
|
|
67
|
-
* const onRampServiceMessenger = new Messenger<
|
|
68
|
-
* 'OnRampService',
|
|
69
|
-
* OnRampServiceActions,
|
|
70
|
-
* OnRampServiceEvents,
|
|
71
|
-
* typeof rootMessenger,
|
|
72
|
-
* >({
|
|
73
|
-
* namespace: 'OnRampService',
|
|
74
|
-
* parent: rootMessenger,
|
|
75
|
-
* });
|
|
76
|
-
* // Instantiate the service to register its actions on the messenger
|
|
77
|
-
* new OnRampService({
|
|
78
|
-
* messenger: onRampServiceMessenger,
|
|
79
|
-
* environment: OnRampEnvironment.Production,
|
|
80
|
-
* fetch,
|
|
81
|
-
* });
|
|
82
|
-
*
|
|
83
|
-
* // Later...
|
|
84
|
-
* // Get the user's geolocation
|
|
85
|
-
* const geolocation = await rootMessenger.call(
|
|
86
|
-
* 'OnRampService:getGeolocation',
|
|
87
|
-
* );
|
|
88
|
-
* // ... Do something with the geolocation ...
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
export class OnRampService {
|
|
92
|
-
/**
|
|
93
|
-
* Constructs a new OnRampService object.
|
|
94
|
-
*
|
|
95
|
-
* @param args - The constructor arguments.
|
|
96
|
-
* @param args.messenger - The messenger suited for this service.
|
|
97
|
-
* @param args.environment - The environment to use for API requests.
|
|
98
|
-
* @param args.fetch - A function that can be used to make an HTTP request. If
|
|
99
|
-
* your JavaScript environment supports `fetch` natively, you'll probably want
|
|
100
|
-
* to pass that; otherwise you can pass an equivalent (such as `fetch` via
|
|
101
|
-
* `node-fetch`).
|
|
102
|
-
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
103
|
-
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
104
|
-
*/
|
|
105
|
-
constructor({ messenger, environment = OnRampEnvironment.Staging, fetch: fetchFunction, policyOptions = {}, }) {
|
|
106
|
-
/**
|
|
107
|
-
* The messenger suited for this service.
|
|
108
|
-
*/
|
|
109
|
-
_OnRampService_messenger.set(this, void 0);
|
|
110
|
-
/**
|
|
111
|
-
* A function that can be used to make an HTTP request.
|
|
112
|
-
*/
|
|
113
|
-
_OnRampService_fetch.set(this, void 0);
|
|
114
|
-
/**
|
|
115
|
-
* The policy that wraps the request.
|
|
116
|
-
*
|
|
117
|
-
* @see {@link createServicePolicy}
|
|
118
|
-
*/
|
|
119
|
-
_OnRampService_policy.set(this, void 0);
|
|
120
|
-
/**
|
|
121
|
-
* The base URL for API requests.
|
|
122
|
-
*/
|
|
123
|
-
_OnRampService_baseUrl.set(this, void 0);
|
|
124
|
-
this.name = serviceName;
|
|
125
|
-
__classPrivateFieldSet(this, _OnRampService_messenger, messenger, "f");
|
|
126
|
-
__classPrivateFieldSet(this, _OnRampService_fetch, fetchFunction, "f");
|
|
127
|
-
__classPrivateFieldSet(this, _OnRampService_policy, createServicePolicy(policyOptions), "f");
|
|
128
|
-
__classPrivateFieldSet(this, _OnRampService_baseUrl, getBaseUrl(environment), "f");
|
|
129
|
-
__classPrivateFieldGet(this, _OnRampService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Registers a handler that will be called after a request returns a non-500
|
|
133
|
-
* response, causing a retry. Primarily useful in tests where timers are being
|
|
134
|
-
* mocked.
|
|
135
|
-
*
|
|
136
|
-
* @param listener - The handler to be called.
|
|
137
|
-
* @returns An object that can be used to unregister the handler. See
|
|
138
|
-
* {@link CockatielEvent}.
|
|
139
|
-
* @see {@link createServicePolicy}
|
|
140
|
-
*/
|
|
141
|
-
onRetry(listener) {
|
|
142
|
-
return __classPrivateFieldGet(this, _OnRampService_policy, "f").onRetry(listener);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Registers a handler that will be called after a set number of retry rounds
|
|
146
|
-
* prove that requests to the API endpoint consistently return a 5xx response.
|
|
147
|
-
*
|
|
148
|
-
* @param listener - The handler to be called.
|
|
149
|
-
* @returns An object that can be used to unregister the handler. See
|
|
150
|
-
* {@link CockatielEvent}.
|
|
151
|
-
* @see {@link createServicePolicy}
|
|
152
|
-
*/
|
|
153
|
-
onBreak(listener) {
|
|
154
|
-
return __classPrivateFieldGet(this, _OnRampService_policy, "f").onBreak(listener);
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Registers a handler that will be called under one of two circumstances:
|
|
158
|
-
*
|
|
159
|
-
* 1. After a set number of retries prove that requests to the API
|
|
160
|
-
* consistently result in one of the following failures:
|
|
161
|
-
* 1. A connection initiation error
|
|
162
|
-
* 2. A connection reset error
|
|
163
|
-
* 3. A timeout error
|
|
164
|
-
* 4. A non-JSON response
|
|
165
|
-
* 5. A 502, 503, or 504 response
|
|
166
|
-
* 2. After a successful request is made to the API, but the response takes
|
|
167
|
-
* longer than a set duration to return.
|
|
168
|
-
*
|
|
169
|
-
* @param listener - The handler to be called.
|
|
170
|
-
* @returns An object that can be used to unregister the handler. See
|
|
171
|
-
* {@link CockatielEvent}.
|
|
172
|
-
*/
|
|
173
|
-
onDegraded(listener) {
|
|
174
|
-
return __classPrivateFieldGet(this, _OnRampService_policy, "f").onDegraded(listener);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Makes a request to the API in order to retrieve the user's geolocation
|
|
178
|
-
* based on their IP address.
|
|
179
|
-
*
|
|
180
|
-
* @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
|
|
181
|
-
*/
|
|
182
|
-
async getGeolocation() {
|
|
183
|
-
const response = await __classPrivateFieldGet(this, _OnRampService_policy, "f").execute(async () => {
|
|
184
|
-
const url = new URL('geolocation', __classPrivateFieldGet(this, _OnRampService_baseUrl, "f"));
|
|
185
|
-
const localResponse = await __classPrivateFieldGet(this, _OnRampService_fetch, "f").call(this, url);
|
|
186
|
-
if (!localResponse.ok) {
|
|
187
|
-
throw new HttpError(localResponse.status, `Fetching '${url.toString()}' failed with status '${localResponse.status}'`);
|
|
188
|
-
}
|
|
189
|
-
return localResponse;
|
|
190
|
-
});
|
|
191
|
-
const textResponse = await response.text();
|
|
192
|
-
const trimmedResponse = textResponse.trim();
|
|
193
|
-
if (trimmedResponse.length > 0) {
|
|
194
|
-
return trimmedResponse;
|
|
195
|
-
}
|
|
196
|
-
throw new Error('Malformed response received from geolocation API');
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
_a = OnRampService, _OnRampService_messenger = new WeakMap(), _OnRampService_fetch = new WeakMap(), _OnRampService_policy = new WeakMap(), _OnRampService_baseUrl = new WeakMap();
|
|
200
|
-
//# sourceMappingURL=OnRampService.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService.mjs","sourceRoot":"","sources":["../src/OnRampService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,mCAAmC;AAK5E,kBAAkB;AAElB;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,eAAe,CAAC;AAE3C;;GAEG;AACH,MAAM,CAAN,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IAC3B,8CAAyB,CAAA;IACzB,wCAAmB,CAAA;IACnB,gDAA2B,CAAA;AAC7B,CAAC,EAJW,iBAAiB,KAAjB,iBAAiB,QAI5B;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AAgC9D,6BAA6B;AAE7B;;;;;GAKG;AACH,SAAS,UAAU,CAAC,WAA8B;IAChD,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,iBAAiB,CAAC,UAAU;YAC/B,OAAO,oCAAoC,CAAC;QAC9C,KAAK,iBAAiB,CAAC,OAAO;YAC5B,OAAO,wCAAwC,CAAC;QAClD,KAAK,iBAAiB,CAAC,WAAW;YAChC,OAAO,uBAAuB,CAAC;QACjC;YACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,OAAO,aAAa;IA8BxB;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,SAAS,EACT,WAAW,GAAG,iBAAiB,CAAC,OAAO,EACvC,KAAK,EAAE,aAAa,EACpB,aAAa,GAAG,EAAE,GAMnB;QA/CD;;WAEG;QACM,2CAES;QAElB;;WAEG;QACM,uCAAgE;QAEzE;;;;WAIG;QACM,wCAAuB;QAEhC;;WAEG;QACM,yCAAiB;QA0BxB,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,uBAAA,IAAI,4BAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wBAAU,aAAa,MAAA,CAAC;QAC5B,uBAAA,IAAI,yBAAW,mBAAmB,CAAC,aAAa,CAAC,MAAA,CAAC;QAClD,uBAAA,IAAI,0BAAY,UAAU,CAAC,WAAW,CAAC,MAAA,CAAC;QAExC,uBAAA,IAAI,gCAAW,CAAC,4BAA4B,CAC1C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;;;OASG;IACH,OAAO,CACL,QAAiD;QAEjD,OAAO,uBAAA,IAAI,6BAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CACL,QAAiD;QAEjD,OAAO,uBAAA,IAAI,6BAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,UAAU,CACR,QAAoD;QAEpD,OAAO,uBAAA,IAAI,6BAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,cAAc;QAClB,MAAM,QAAQ,GAAG,MAAM,uBAAA,IAAI,6BAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;YACrD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,aAAa,EAAE,uBAAA,IAAI,8BAAS,CAAC,CAAC;YAClD,MAAM,aAAa,GAAG,MAAM,uBAAA,IAAI,4BAAO,MAAX,IAAI,EAAQ,GAAG,CAAC,CAAC;YAC7C,IAAI,CAAC,aAAa,CAAC,EAAE,EAAE,CAAC;gBACtB,MAAM,IAAI,SAAS,CACjB,aAAa,CAAC,MAAM,EACpB,aAAa,GAAG,CAAC,QAAQ,EAAE,yBAAyB,aAAa,CAAC,MAAM,GAAG,CAC5E,CAAC;YACJ,CAAC;YACD,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC3C,MAAM,eAAe,GAAG,YAAY,CAAC,IAAI,EAAE,CAAC;QAE5C,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC/B,OAAO,eAAe,CAAC;QACzB,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACtE,CAAC;CACF","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 { OnRampServiceMethodActions } from './OnRampService-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link OnRampService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'OnRampService';\n\n/**\n * The environment to use for API requests.\n */\nexport enum OnRampEnvironment {\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 OnRampService} exposes to other consumers.\n */\nexport type OnRampServiceActions = OnRampServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link OnRampService} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link OnRampService} exposes to other consumers.\n */\nexport type OnRampServiceEvents = never;\n\n/**\n * Events from other messengers that {@link OnRampService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link OnRampService}.\n */\nexport type OnRampServiceMessenger = Messenger<\n typeof serviceName,\n OnRampServiceActions | AllowedActions,\n OnRampServiceEvents | 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: OnRampEnvironment): string {\n switch (environment) {\n case OnRampEnvironment.Production:\n return 'https://on-ramp.api.cx.metamask.io';\n case OnRampEnvironment.Staging:\n return 'https://on-ramp.uat-api.cx.metamask.io';\n case OnRampEnvironment.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 OnRamp API.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * OnRampServiceActions,\n * OnRampServiceEvents,\n * } from '@metamask/ramps-controller';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * OnRampServiceActions\n * OnRampServiceEvents\n * >({ namespace: 'Root' });\n * const onRampServiceMessenger = new Messenger<\n * 'OnRampService',\n * OnRampServiceActions,\n * OnRampServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'OnRampService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new OnRampService({\n * messenger: onRampServiceMessenger,\n * environment: OnRampEnvironment.Production,\n * fetch,\n * });\n *\n * // Later...\n * // Get the user's geolocation\n * const geolocation = await rootMessenger.call(\n * 'OnRampService:getGeolocation',\n * );\n * // ... Do something with the geolocation ...\n * ```\n */\nexport class OnRampService {\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 OnRampService\n >[0]['messenger'];\n\n /**\n * A function that can be used to make an HTTP request.\n */\n readonly #fetch: ConstructorParameters<typeof OnRampService>[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 OnRampService 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 = OnRampEnvironment.Staging,\n fetch: fetchFunction,\n policyOptions = {},\n }: {\n messenger: OnRampServiceMessenger;\n environment?: OnRampEnvironment;\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 response = 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 return localResponse;\n });\n\n const textResponse = await response.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"]}
|