@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
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService-method-action-types.d.cts","sourceRoot":"","sources":["../src/OnRampService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,4BAAwB;AAErD;;;;;GAKG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;CAC1C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,iCAAiC,CAAC"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
-
* Do not edit manually.
|
|
4
|
-
*/
|
|
5
|
-
import type { OnRampService } from "./OnRampService.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 OnRampServiceGetGeolocationAction = {
|
|
13
|
-
type: `OnRampService:getGeolocation`;
|
|
14
|
-
handler: OnRampService['getGeolocation'];
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Union of all OnRampService action types.
|
|
18
|
-
*/
|
|
19
|
-
export type OnRampServiceMethodActions = OnRampServiceGetGeolocationAction;
|
|
20
|
-
//# sourceMappingURL=OnRampService-method-action-types.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService-method-action-types.d.mts","sourceRoot":"","sources":["../src/OnRampService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,4BAAwB;AAErD;;;;;GAKG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;CAC1C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,iCAAiC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService-method-action-types.mjs","sourceRoot":"","sources":["../src/OnRampService-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 { OnRampService } from './OnRampService';\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 OnRampServiceGetGeolocationAction = {\n type: `OnRampService:getGeolocation`;\n handler: OnRampService['getGeolocation'];\n};\n\n/**\n * Union of all OnRampService action types.\n */\nexport type OnRampServiceMethodActions = OnRampServiceGetGeolocationAction;\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OnRampService.cjs","sourceRoot":"","sources":["../src/OnRampService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,iEAA4E;AAK5E,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,eAAe,CAAC;AAE3C;;GAEG;AACH,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IAC3B,8CAAyB,CAAA;IACzB,wCAAmB,CAAA;IACnB,gDAA2B,CAAA;AAC7B,CAAC,EAJW,iBAAiB,iCAAjB,iBAAiB,QAI5B;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CA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;;;;;;;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,4BAAS,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;AApJD,sCAoJC","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"]}
|
|
@@ -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;;;;;;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AAC;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"]}
|