@metamask/ramps-controller 2.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/dist/RampsController.cjs +226 -6
  3. package/dist/RampsController.cjs.map +1 -1
  4. package/dist/RampsController.d.cts +80 -10
  5. package/dist/RampsController.d.cts.map +1 -1
  6. package/dist/RampsController.d.mts +80 -10
  7. package/dist/RampsController.d.mts.map +1 -1
  8. package/dist/RampsController.mjs +226 -6
  9. package/dist/RampsController.mjs.map +1 -1
  10. package/dist/RampsService-method-action-types.cjs.map +1 -1
  11. package/dist/RampsService-method-action-types.d.cts +22 -1
  12. package/dist/RampsService-method-action-types.d.cts.map +1 -1
  13. package/dist/RampsService-method-action-types.d.mts +22 -1
  14. package/dist/RampsService-method-action-types.d.mts.map +1 -1
  15. package/dist/RampsService-method-action-types.mjs.map +1 -1
  16. package/dist/RampsService.cjs +106 -24
  17. package/dist/RampsService.cjs.map +1 -1
  18. package/dist/RampsService.d.cts +124 -1
  19. package/dist/RampsService.d.cts.map +1 -1
  20. package/dist/RampsService.d.mts +124 -1
  21. package/dist/RampsService.d.mts.map +1 -1
  22. package/dist/RampsService.mjs +102 -23
  23. package/dist/RampsService.mjs.map +1 -1
  24. package/dist/RequestCache.cjs +98 -0
  25. package/dist/RequestCache.cjs.map +1 -0
  26. package/dist/RequestCache.d.cts +93 -0
  27. package/dist/RequestCache.d.cts.map +1 -0
  28. package/dist/RequestCache.d.mts +93 -0
  29. package/dist/RequestCache.d.mts.map +1 -0
  30. package/dist/RequestCache.mjs +90 -0
  31. package/dist/RequestCache.mjs.map +1 -0
  32. package/dist/index.cjs +14 -1
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +8 -4
  35. package/dist/index.d.cts.map +1 -1
  36. package/dist/index.d.mts +8 -4
  37. package/dist/index.d.mts.map +1 -1
  38. package/dist/index.mjs +3 -1
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/selectors.cjs +81 -0
  41. package/dist/selectors.cjs.map +1 -0
  42. package/dist/selectors.d.cts +75 -0
  43. package/dist/selectors.d.cts.map +1 -0
  44. package/dist/selectors.d.mts +75 -0
  45. package/dist/selectors.d.mts.map +1 -0
  46. package/dist/selectors.mjs +77 -0
  47. package/dist/selectors.mjs.map +1 -0
  48. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"RampsService.cjs","sourceRoot":"","sources":["../src/RampsService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,iEAA4E;AAK5E,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,cAAc,CAAC;AAE1C;;GAEG;AACH,IAAY,gBAIX;AAJD,WAAY,gBAAgB;IAC1B,6CAAyB,CAAA;IACzB,uCAAmB,CAAA;IACnB,+CAA2B,CAAA;AAC7B,CAAC,EAJW,gBAAgB,gCAAhB,gBAAgB,QAI3B;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AAgC9D,6BAA6B;AAE7B;;;;;GAKG;AACH,SAAS,UAAU,CAAC,WAA6B;IAC/C,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,gBAAgB,CAAC,UAAU;YAC9B,OAAO,oCAAoC,CAAC;QAC9C,KAAK,gBAAgB,CAAC,OAAO;YAC3B,OAAO,wCAAwC,CAAC;QAClD,KAAK,gBAAgB,CAAC,WAAW;YAC/B,OAAO,uBAAuB,CAAC;QACjC;YACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAa,YAAY;IA8BvB;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,SAAS,EACT,WAAW,GAAG,gBAAgB,CAAC,OAAO,EACtC,KAAK,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,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,MAAM,YAAY,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC;YAChD,mEAAmE;YACnE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC;QACzD,CAAC,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,YAAY,CAAC,IAAI,CAAC;QACvC,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;AAtJD,oCAsJC","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"]}
1
+ {"version":3,"file":"RampsService.cjs","sourceRoot":"","sources":["../src/RampsService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAIA,iEAA4E;AAI5E,mEAA0C;AAmG1C;;GAEG;AACU,QAAA,iBAAiB,GAAG,OAAO,CAAC;AAEzC,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,cAAc,CAAC;AAE1C;;GAEG;AACH,IAAY,gBAIX;AAJD,WAAY,gBAAgB;IAC1B,6CAAyB,CAAA;IACzB,uCAAmB,CAAA;IACnB,+CAA2B,CAAA;AAC7B,CAAC,EAJW,gBAAgB,gCAAhB,gBAAgB,QAI3B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AAC,GAAG,CAAC,SAAS,EAAE,uBAAA,IAAI,6BAAS,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;GASG;AACH,KAAK,gCACH,OAAwB,EACxB,IAAY,EACZ,OAGC;IAED,OAAO,uBAAA,IAAI,4BAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;QACrC,MAAM,OAAO,GAAG,UAAU,CAAC,uBAAA,IAAI,iCAAa,EAAE,OAAO,CAAC,CAAC;QACvD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACnC,uBAAA,IAAI,8DAAiB,MAArB,IAAI,EAAkB,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAE3C,MAAM,QAAQ,GAAG,MAAM,uBAAA,IAAI,2BAAO,MAAX,IAAI,EAAQ,GAAG,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,4BAAS,CACjB,QAAQ,CAAC,MAAM,EACf,aAAa,GAAG,CAAC,QAAQ,EAAE,yBAAyB,QAAQ,CAAC,MAAM,GAAG,CACvE,CAAC;QACJ,CAAC;QAED,OAAO,OAAO,CAAC,YAAY,KAAK,MAAM;YACpC,CAAC,CAAE,QAAQ,CAAC,IAAI,EAAyB;YACzC,CAAC,CAAE,QAAQ,CAAC,IAAI,EAAyB,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC","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';\nimport packageJson from '../package.json';\n\n/**\n * Represents phone number information for a country.\n */\nexport type CountryPhone = {\n prefix: string;\n placeholder: string;\n template: string;\n};\n\n/**\n * Represents a state/province within a country.\n */\nexport type State = {\n /**\n * State identifier. Can be in path format (e.g., \"/regions/us-ut\") or ISO code format (e.g., \"us-ut\").\n */\n id?: string;\n /**\n * State name.\n */\n name?: string;\n /**\n * ISO state code (e.g., \"UT\", \"NY\").\n */\n stateId?: string;\n /**\n * Whether this state is supported for ramps.\n */\n supported?: boolean;\n /**\n * Whether this state is recommended.\n */\n recommended?: boolean;\n};\n\n/**\n * Represents eligibility information for a region.\n * Returned from the /regions/countries/{isoCode} endpoint.\n */\nexport type Eligibility = {\n /**\n * Whether aggregator providers are available.\n */\n aggregator?: boolean;\n /**\n * Whether deposit (buy) is available.\n */\n deposit?: boolean;\n /**\n * Whether global providers are available.\n */\n global?: boolean;\n};\n\n/**\n * Represents a country returned from the regions/countries API.\n */\nexport type Country = {\n /**\n * ISO-2 country code (e.g., \"US\", \"GB\").\n */\n isoCode: string;\n /**\n * Country identifier. Can be in path format (e.g., \"/regions/us\") or ISO code format.\n * If not provided, defaults to isoCode.\n */\n id?: string;\n /**\n * Country flag emoji or code.\n */\n flag: string;\n /**\n * Country name.\n */\n name: string;\n /**\n * Phone number information.\n */\n phone: CountryPhone;\n /**\n * Default currency code.\n */\n currency: string;\n /**\n * Whether this country is supported for ramps.\n */\n supported: boolean;\n /**\n * Whether this country is recommended.\n */\n recommended?: boolean;\n /**\n * Array of state objects.\n */\n states?: State[];\n};\n\n/**\n * The SDK version to send with API requests. (backwards-compatibility)\n */\nexport const RAMPS_SDK_VERSION = '2.1.6';\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/**\n * The type of ramps API service.\n * Determines which base URL to use (cache vs standard).\n */\nexport enum RampsApiService {\n Regions = 'regions',\n Orders = 'orders',\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getGeolocation',\n 'getCountries',\n 'getEligibility',\n] 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 and service type.\n * The Regions service uses a cache URL, while other services use the standard URL.\n *\n * @param environment - The environment to use.\n * @param service - The API service type (determines if cache URL is used).\n * @returns The base URL for API requests.\n */\nfunction getBaseUrl(\n environment: RampsEnvironment,\n service: RampsApiService,\n): string {\n const cache = service === RampsApiService.Regions ? '-cache' : '';\n\n switch (environment) {\n case RampsEnvironment.Production:\n return `https://on-ramp${cache}.api.cx.metamask.io`;\n case RampsEnvironment.Staging:\n case RampsEnvironment.Development:\n return `https://on-ramp${cache}.uat-api.cx.metamask.io`;\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 * context: 'mobile-ios',\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 environment used for API requests.\n */\n readonly #environment: RampsEnvironment;\n\n /**\n * The context for API requests (e.g., 'mobile-ios', 'mobile-android').\n */\n readonly #context: 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.context - The context for API requests (e.g., 'mobile-ios', 'mobile-android').\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 context,\n fetch: fetchFunction,\n policyOptions = {},\n }: {\n messenger: RampsServiceMessenger;\n environment?: RampsEnvironment;\n context: string;\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.#environment = environment;\n this.#context = context;\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 * Adds common request parameters to a URL.\n *\n * @param url - The URL to add parameters to.\n * @param action - The ramp action type (optional, not all endpoints require it).\n */\n #addCommonParams(url: URL, action?: 'buy' | 'sell'): void {\n if (action) {\n url.searchParams.set('action', action);\n }\n url.searchParams.set('sdk', RAMPS_SDK_VERSION);\n url.searchParams.set('controller', packageJson.version);\n url.searchParams.set('context', this.#context);\n }\n\n /**\n * Makes an API request with retry policy and error handling.\n *\n * @param service - The API service type (determines base URL).\n * @param path - The endpoint path.\n * @param options - Request options.\n * @param options.action - The ramp action type (optional).\n * @param options.responseType - How to parse the response ('json' or 'text').\n * @returns The parsed response data.\n */\n async #request<TResponse>(\n service: RampsApiService,\n path: string,\n options: {\n action?: 'buy' | 'sell';\n responseType: 'json' | 'text';\n },\n ): Promise<TResponse> {\n return this.#policy.execute(async () => {\n const baseUrl = getBaseUrl(this.#environment, service);\n const url = new URL(path, baseUrl);\n this.#addCommonParams(url, options.action);\n\n const response = await this.#fetch(url);\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Fetching '${url.toString()}' failed with status '${response.status}'`,\n );\n }\n\n return options.responseType === 'json'\n ? (response.json() as Promise<TResponse>)\n : (response.text() as Promise<TResponse>);\n });\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 textResponse = await this.#request<string>(\n RampsApiService.Orders,\n 'geolocation',\n { responseType: 'text' },\n );\n\n const trimmedResponse = textResponse.trim();\n if (trimmedResponse.length > 0) {\n return trimmedResponse;\n }\n\n throw new Error('Malformed response received from geolocation API');\n }\n\n /**\n * Makes a request to the cached API to retrieve the list of supported countries.\n * Filters countries based on aggregator support (preserves OnRampSDK logic).\n *\n * @param action - The ramp action type ('buy' or 'sell').\n * @returns An array of countries filtered by aggregator support.\n */\n async getCountries(action: 'buy' | 'sell' = 'buy'): Promise<Country[]> {\n const countries = await this.#request<Country[]>(\n RampsApiService.Regions,\n 'regions/countries',\n { action, responseType: 'json' },\n );\n\n if (!Array.isArray(countries)) {\n throw new Error('Malformed response received from countries API');\n }\n\n return countries.filter((country) => {\n if (country.states && country.states.length > 0) {\n const hasSupportedState = country.states.some(\n (state) => state.supported !== false,\n );\n return country.supported || hasSupportedState;\n }\n\n return country.supported;\n });\n }\n\n /**\n * Fetches eligibility information for a specific region.\n *\n * @param isoCode - The ISO code for the region (e.g., \"us\", \"fr\", \"us-ny\").\n * @returns Eligibility information for the region.\n */\n async getEligibility(isoCode: string): Promise<Eligibility> {\n const normalizedIsoCode = isoCode.toLowerCase().trim();\n return this.#request<Eligibility>(\n RampsApiService.Regions,\n `regions/countries/${normalizedIsoCode}`,\n { responseType: 'json' },\n );\n }\n}\n"]}
@@ -1,6 +1,103 @@
1
1
  import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
2
  import type { Messenger } from "@metamask/messenger";
3
3
  import type { RampsServiceMethodActions } from "./RampsService-method-action-types.cjs";
4
+ /**
5
+ * Represents phone number information for a country.
6
+ */
7
+ export type CountryPhone = {
8
+ prefix: string;
9
+ placeholder: string;
10
+ template: string;
11
+ };
12
+ /**
13
+ * Represents a state/province within a country.
14
+ */
15
+ export type State = {
16
+ /**
17
+ * State identifier. Can be in path format (e.g., "/regions/us-ut") or ISO code format (e.g., "us-ut").
18
+ */
19
+ id?: string;
20
+ /**
21
+ * State name.
22
+ */
23
+ name?: string;
24
+ /**
25
+ * ISO state code (e.g., "UT", "NY").
26
+ */
27
+ stateId?: string;
28
+ /**
29
+ * Whether this state is supported for ramps.
30
+ */
31
+ supported?: boolean;
32
+ /**
33
+ * Whether this state is recommended.
34
+ */
35
+ recommended?: boolean;
36
+ };
37
+ /**
38
+ * Represents eligibility information for a region.
39
+ * Returned from the /regions/countries/{isoCode} endpoint.
40
+ */
41
+ export type Eligibility = {
42
+ /**
43
+ * Whether aggregator providers are available.
44
+ */
45
+ aggregator?: boolean;
46
+ /**
47
+ * Whether deposit (buy) is available.
48
+ */
49
+ deposit?: boolean;
50
+ /**
51
+ * Whether global providers are available.
52
+ */
53
+ global?: boolean;
54
+ };
55
+ /**
56
+ * Represents a country returned from the regions/countries API.
57
+ */
58
+ export type Country = {
59
+ /**
60
+ * ISO-2 country code (e.g., "US", "GB").
61
+ */
62
+ isoCode: string;
63
+ /**
64
+ * Country identifier. Can be in path format (e.g., "/regions/us") or ISO code format.
65
+ * If not provided, defaults to isoCode.
66
+ */
67
+ id?: string;
68
+ /**
69
+ * Country flag emoji or code.
70
+ */
71
+ flag: string;
72
+ /**
73
+ * Country name.
74
+ */
75
+ name: string;
76
+ /**
77
+ * Phone number information.
78
+ */
79
+ phone: CountryPhone;
80
+ /**
81
+ * Default currency code.
82
+ */
83
+ currency: string;
84
+ /**
85
+ * Whether this country is supported for ramps.
86
+ */
87
+ supported: boolean;
88
+ /**
89
+ * Whether this country is recommended.
90
+ */
91
+ recommended?: boolean;
92
+ /**
93
+ * Array of state objects.
94
+ */
95
+ states?: State[];
96
+ };
97
+ /**
98
+ * The SDK version to send with API requests. (backwards-compatibility)
99
+ */
100
+ export declare const RAMPS_SDK_VERSION = "2.1.6";
4
101
  /**
5
102
  * The name of the {@link RampsService}, used to namespace the
6
103
  * service's actions and events.
@@ -14,6 +111,14 @@ export declare enum RampsEnvironment {
14
111
  Staging = "staging",
15
112
  Development = "development"
16
113
  }
114
+ /**
115
+ * The type of ramps API service.
116
+ * Determines which base URL to use (cache vs standard).
117
+ */
118
+ export declare enum RampsApiService {
119
+ Regions = "regions",
120
+ Orders = "orders"
121
+ }
17
122
  /**
18
123
  * Actions that {@link RampsService} exposes to other consumers.
19
124
  */
@@ -65,6 +170,7 @@ export type RampsServiceMessenger = Messenger<typeof serviceName, RampsServiceAc
65
170
  * new RampsService({
66
171
  * messenger: rampsServiceMessenger,
67
172
  * environment: RampsEnvironment.Production,
173
+ * context: 'mobile-ios',
68
174
  * fetch,
69
175
  * });
70
176
  *
@@ -88,6 +194,7 @@ export declare class RampsService {
88
194
  * @param args - The constructor arguments.
89
195
  * @param args.messenger - The messenger suited for this service.
90
196
  * @param args.environment - The environment to use for API requests.
197
+ * @param args.context - The context for API requests (e.g., 'mobile-ios', 'mobile-android').
91
198
  * @param args.fetch - A function that can be used to make an HTTP request. If
92
199
  * your JavaScript environment supports `fetch` natively, you'll probably want
93
200
  * to pass that; otherwise you can pass an equivalent (such as `fetch` via
@@ -95,9 +202,10 @@ export declare class RampsService {
95
202
  * @param args.policyOptions - Options to pass to `createServicePolicy`, which
96
203
  * is used to wrap each request. See {@link CreateServicePolicyOptions}.
97
204
  */
98
- constructor({ messenger, environment, fetch: fetchFunction, policyOptions, }: {
205
+ constructor({ messenger, environment, context, fetch: fetchFunction, policyOptions, }: {
99
206
  messenger: RampsServiceMessenger;
100
207
  environment?: RampsEnvironment;
208
+ context: string;
101
209
  fetch: typeof fetch;
102
210
  policyOptions?: CreateServicePolicyOptions;
103
211
  });
@@ -147,6 +255,21 @@ export declare class RampsService {
147
255
  * @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
148
256
  */
149
257
  getGeolocation(): Promise<string>;
258
+ /**
259
+ * Makes a request to the cached API to retrieve the list of supported countries.
260
+ * Filters countries based on aggregator support (preserves OnRampSDK logic).
261
+ *
262
+ * @param action - The ramp action type ('buy' or 'sell').
263
+ * @returns An array of countries filtered by aggregator support.
264
+ */
265
+ getCountries(action?: 'buy' | 'sell'): Promise<Country[]>;
266
+ /**
267
+ * Fetches eligibility information for a specific region.
268
+ *
269
+ * @param isoCode - The ISO code for the region (e.g., "us", "fr", "us-ny").
270
+ * @returns Eligibility information for the region.
271
+ */
272
+ getEligibility(isoCode: string): Promise<Eligibility>;
150
273
  }
151
274
  export {};
152
275
  //# sourceMappingURL=RampsService.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"RampsService.d.cts","sourceRoot":"","sources":["../src/RampsService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAEpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,yBAAyB,EAAE,+CAA2C;AAIpF;;;GAGG;AACH,eAAO,MAAM,WAAW,iBAAiB,CAAC;AAE1C;;GAEG;AACH,oBAAY,gBAAgB;IAC1B,UAAU,eAAe;IACzB,OAAO,YAAY;IACnB,WAAW,gBAAgB;CAC5B;AAMD;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;AAE5D;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,kBA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,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;IAwD1C;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAevC;;;;;;OAMG;IACG,YAAY,CAAC,MAAM,GAAE,KAAK,GAAG,MAAc,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAuBtE;;;;;OAKG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAQ5D"}
@@ -1,6 +1,103 @@
1
1
  import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
2
  import type { Messenger } from "@metamask/messenger";
3
3
  import type { RampsServiceMethodActions } from "./RampsService-method-action-types.mjs";
4
+ /**
5
+ * Represents phone number information for a country.
6
+ */
7
+ export type CountryPhone = {
8
+ prefix: string;
9
+ placeholder: string;
10
+ template: string;
11
+ };
12
+ /**
13
+ * Represents a state/province within a country.
14
+ */
15
+ export type State = {
16
+ /**
17
+ * State identifier. Can be in path format (e.g., "/regions/us-ut") or ISO code format (e.g., "us-ut").
18
+ */
19
+ id?: string;
20
+ /**
21
+ * State name.
22
+ */
23
+ name?: string;
24
+ /**
25
+ * ISO state code (e.g., "UT", "NY").
26
+ */
27
+ stateId?: string;
28
+ /**
29
+ * Whether this state is supported for ramps.
30
+ */
31
+ supported?: boolean;
32
+ /**
33
+ * Whether this state is recommended.
34
+ */
35
+ recommended?: boolean;
36
+ };
37
+ /**
38
+ * Represents eligibility information for a region.
39
+ * Returned from the /regions/countries/{isoCode} endpoint.
40
+ */
41
+ export type Eligibility = {
42
+ /**
43
+ * Whether aggregator providers are available.
44
+ */
45
+ aggregator?: boolean;
46
+ /**
47
+ * Whether deposit (buy) is available.
48
+ */
49
+ deposit?: boolean;
50
+ /**
51
+ * Whether global providers are available.
52
+ */
53
+ global?: boolean;
54
+ };
55
+ /**
56
+ * Represents a country returned from the regions/countries API.
57
+ */
58
+ export type Country = {
59
+ /**
60
+ * ISO-2 country code (e.g., "US", "GB").
61
+ */
62
+ isoCode: string;
63
+ /**
64
+ * Country identifier. Can be in path format (e.g., "/regions/us") or ISO code format.
65
+ * If not provided, defaults to isoCode.
66
+ */
67
+ id?: string;
68
+ /**
69
+ * Country flag emoji or code.
70
+ */
71
+ flag: string;
72
+ /**
73
+ * Country name.
74
+ */
75
+ name: string;
76
+ /**
77
+ * Phone number information.
78
+ */
79
+ phone: CountryPhone;
80
+ /**
81
+ * Default currency code.
82
+ */
83
+ currency: string;
84
+ /**
85
+ * Whether this country is supported for ramps.
86
+ */
87
+ supported: boolean;
88
+ /**
89
+ * Whether this country is recommended.
90
+ */
91
+ recommended?: boolean;
92
+ /**
93
+ * Array of state objects.
94
+ */
95
+ states?: State[];
96
+ };
97
+ /**
98
+ * The SDK version to send with API requests. (backwards-compatibility)
99
+ */
100
+ export declare const RAMPS_SDK_VERSION = "2.1.6";
4
101
  /**
5
102
  * The name of the {@link RampsService}, used to namespace the
6
103
  * service's actions and events.
@@ -14,6 +111,14 @@ export declare enum RampsEnvironment {
14
111
  Staging = "staging",
15
112
  Development = "development"
16
113
  }
114
+ /**
115
+ * The type of ramps API service.
116
+ * Determines which base URL to use (cache vs standard).
117
+ */
118
+ export declare enum RampsApiService {
119
+ Regions = "regions",
120
+ Orders = "orders"
121
+ }
17
122
  /**
18
123
  * Actions that {@link RampsService} exposes to other consumers.
19
124
  */
@@ -65,6 +170,7 @@ export type RampsServiceMessenger = Messenger<typeof serviceName, RampsServiceAc
65
170
  * new RampsService({
66
171
  * messenger: rampsServiceMessenger,
67
172
  * environment: RampsEnvironment.Production,
173
+ * context: 'mobile-ios',
68
174
  * fetch,
69
175
  * });
70
176
  *
@@ -88,6 +194,7 @@ export declare class RampsService {
88
194
  * @param args - The constructor arguments.
89
195
  * @param args.messenger - The messenger suited for this service.
90
196
  * @param args.environment - The environment to use for API requests.
197
+ * @param args.context - The context for API requests (e.g., 'mobile-ios', 'mobile-android').
91
198
  * @param args.fetch - A function that can be used to make an HTTP request. If
92
199
  * your JavaScript environment supports `fetch` natively, you'll probably want
93
200
  * to pass that; otherwise you can pass an equivalent (such as `fetch` via
@@ -95,9 +202,10 @@ export declare class RampsService {
95
202
  * @param args.policyOptions - Options to pass to `createServicePolicy`, which
96
203
  * is used to wrap each request. See {@link CreateServicePolicyOptions}.
97
204
  */
98
- constructor({ messenger, environment, fetch: fetchFunction, policyOptions, }: {
205
+ constructor({ messenger, environment, context, fetch: fetchFunction, policyOptions, }: {
99
206
  messenger: RampsServiceMessenger;
100
207
  environment?: RampsEnvironment;
208
+ context: string;
101
209
  fetch: typeof fetch;
102
210
  policyOptions?: CreateServicePolicyOptions;
103
211
  });
@@ -147,6 +255,21 @@ export declare class RampsService {
147
255
  * @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
148
256
  */
149
257
  getGeolocation(): Promise<string>;
258
+ /**
259
+ * Makes a request to the cached API to retrieve the list of supported countries.
260
+ * Filters countries based on aggregator support (preserves OnRampSDK logic).
261
+ *
262
+ * @param action - The ramp action type ('buy' or 'sell').
263
+ * @returns An array of countries filtered by aggregator support.
264
+ */
265
+ getCountries(action?: 'buy' | 'sell'): Promise<Country[]>;
266
+ /**
267
+ * Fetches eligibility information for a specific region.
268
+ *
269
+ * @param isoCode - The ISO code for the region (e.g., "us", "fr", "us-ny").
270
+ * @returns Eligibility information for the region.
271
+ */
272
+ getEligibility(isoCode: string): Promise<Eligibility>;
150
273
  }
151
274
  export {};
152
275
  //# sourceMappingURL=RampsService.d.mts.map
@@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;;GAyCG;AACH,qBAAa,YAAY;;IACvB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IA+BlC;;;;;;;;;;;;;OAaG;gBACS,EACV,SAAS,EACT,WAAsC,EACtC,OAAO,EACP,KAAK,EAAE,aAAa,EACpB,aAAkB,GACnB,EAAE;QACD,SAAS,EAAE,qBAAqB,CAAC;QACjC,WAAW,CAAC,EAAE,gBAAgB,CAAC;QAC/B,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,aAAa,CAAC,EAAE,0BAA0B,CAAC;KAC5C;IAcD;;;;;;;;;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;IAwD1C;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAevC;;;;;;OAMG;IACG,YAAY,CAAC,MAAM,GAAE,KAAK,GAAG,MAAc,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAuBtE;;;;;OAKG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAQ5D"}
@@ -9,8 +9,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _a, _RampsService_messenger, _RampsService_fetch, _RampsService_policy, _RampsService_baseUrl;
12
+ var _RampsService_instances, _a, _RampsService_messenger, _RampsService_fetch, _RampsService_policy, _RampsService_environment, _RampsService_context, _RampsService_addCommonParams, _RampsService_request;
13
13
  import { createServicePolicy, HttpError } from "@metamask/controller-utils";
14
+ import packageJson from "../package.json" with { type: "json" };
15
+ /**
16
+ * The SDK version to send with API requests. (backwards-compatibility)
17
+ */
18
+ export const RAMPS_SDK_VERSION = '2.1.6';
14
19
  // === GENERAL ===
15
20
  /**
16
21
  * The name of the {@link RampsService}, used to namespace the
@@ -26,23 +31,38 @@ export var RampsEnvironment;
26
31
  RampsEnvironment["Staging"] = "staging";
27
32
  RampsEnvironment["Development"] = "development";
28
33
  })(RampsEnvironment || (RampsEnvironment = {}));
34
+ /**
35
+ * The type of ramps API service.
36
+ * Determines which base URL to use (cache vs standard).
37
+ */
38
+ export var RampsApiService;
39
+ (function (RampsApiService) {
40
+ RampsApiService["Regions"] = "regions";
41
+ RampsApiService["Orders"] = "orders";
42
+ })(RampsApiService || (RampsApiService = {}));
29
43
  // === MESSENGER ===
30
- const MESSENGER_EXPOSED_METHODS = ['getGeolocation'];
44
+ const MESSENGER_EXPOSED_METHODS = [
45
+ 'getGeolocation',
46
+ 'getCountries',
47
+ 'getEligibility',
48
+ ];
31
49
  // === SERVICE DEFINITION ===
32
50
  /**
33
- * Gets the base URL for API requests based on the environment.
51
+ * Gets the base URL for API requests based on the environment and service type.
52
+ * The Regions service uses a cache URL, while other services use the standard URL.
34
53
  *
35
54
  * @param environment - The environment to use.
55
+ * @param service - The API service type (determines if cache URL is used).
36
56
  * @returns The base URL for API requests.
37
57
  */
38
- function getBaseUrl(environment) {
58
+ function getBaseUrl(environment, service) {
59
+ const cache = service === RampsApiService.Regions ? '-cache' : '';
39
60
  switch (environment) {
40
61
  case RampsEnvironment.Production:
41
- return 'https://on-ramp.api.cx.metamask.io';
62
+ return `https://on-ramp${cache}.api.cx.metamask.io`;
42
63
  case RampsEnvironment.Staging:
43
- return 'https://on-ramp.uat-api.cx.metamask.io';
44
64
  case RampsEnvironment.Development:
45
- return 'http://localhost:3000';
65
+ return `https://on-ramp${cache}.uat-api.cx.metamask.io`;
46
66
  default:
47
67
  throw new Error(`Invalid environment: ${String(environment)}`);
48
68
  }
@@ -77,6 +97,7 @@ function getBaseUrl(environment) {
77
97
  * new RampsService({
78
98
  * messenger: rampsServiceMessenger,
79
99
  * environment: RampsEnvironment.Production,
100
+ * context: 'mobile-ios',
80
101
  * fetch,
81
102
  * });
82
103
  *
@@ -95,6 +116,7 @@ export class RampsService {
95
116
  * @param args - The constructor arguments.
96
117
  * @param args.messenger - The messenger suited for this service.
97
118
  * @param args.environment - The environment to use for API requests.
119
+ * @param args.context - The context for API requests (e.g., 'mobile-ios', 'mobile-android').
98
120
  * @param args.fetch - A function that can be used to make an HTTP request. If
99
121
  * your JavaScript environment supports `fetch` natively, you'll probably want
100
122
  * to pass that; otherwise you can pass an equivalent (such as `fetch` via
@@ -102,7 +124,8 @@ export class RampsService {
102
124
  * @param args.policyOptions - Options to pass to `createServicePolicy`, which
103
125
  * is used to wrap each request. See {@link CreateServicePolicyOptions}.
104
126
  */
105
- constructor({ messenger, environment = RampsEnvironment.Staging, fetch: fetchFunction, policyOptions = {}, }) {
127
+ constructor({ messenger, environment = RampsEnvironment.Staging, context, fetch: fetchFunction, policyOptions = {}, }) {
128
+ _RampsService_instances.add(this);
106
129
  /**
107
130
  * The messenger suited for this service.
108
131
  */
@@ -118,14 +141,19 @@ export class RampsService {
118
141
  */
119
142
  _RampsService_policy.set(this, void 0);
120
143
  /**
121
- * The base URL for API requests.
144
+ * The environment used for API requests.
122
145
  */
123
- _RampsService_baseUrl.set(this, void 0);
146
+ _RampsService_environment.set(this, void 0);
147
+ /**
148
+ * The context for API requests (e.g., 'mobile-ios', 'mobile-android').
149
+ */
150
+ _RampsService_context.set(this, void 0);
124
151
  this.name = serviceName;
125
152
  __classPrivateFieldSet(this, _RampsService_messenger, messenger, "f");
126
153
  __classPrivateFieldSet(this, _RampsService_fetch, fetchFunction, "f");
127
154
  __classPrivateFieldSet(this, _RampsService_policy, createServicePolicy(policyOptions), "f");
128
- __classPrivateFieldSet(this, _RampsService_baseUrl, getBaseUrl(environment), "f");
155
+ __classPrivateFieldSet(this, _RampsService_environment, environment, "f");
156
+ __classPrivateFieldSet(this, _RampsService_context, context, "f");
129
157
  __classPrivateFieldGet(this, _RampsService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
130
158
  }
131
159
  /**
@@ -180,23 +208,74 @@ export class RampsService {
180
208
  * @returns The user's country/region code (e.g., "US-UT" for Utah, USA).
181
209
  */
182
210
  async getGeolocation() {
183
- const responseData = await __classPrivateFieldGet(this, _RampsService_policy, "f").execute(async () => {
184
- const url = new URL('geolocation', __classPrivateFieldGet(this, _RampsService_baseUrl, "f"));
185
- const localResponse = await __classPrivateFieldGet(this, _RampsService_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
- const textResponse = await localResponse.text();
190
- // Return both response and text content since we consumed the body
191
- return { response: localResponse, text: textResponse };
192
- });
193
- const textResponse = responseData.text;
211
+ const textResponse = await __classPrivateFieldGet(this, _RampsService_instances, "m", _RampsService_request).call(this, RampsApiService.Orders, 'geolocation', { responseType: 'text' });
194
212
  const trimmedResponse = textResponse.trim();
195
213
  if (trimmedResponse.length > 0) {
196
214
  return trimmedResponse;
197
215
  }
198
216
  throw new Error('Malformed response received from geolocation API');
199
217
  }
218
+ /**
219
+ * Makes a request to the cached API to retrieve the list of supported countries.
220
+ * Filters countries based on aggregator support (preserves OnRampSDK logic).
221
+ *
222
+ * @param action - The ramp action type ('buy' or 'sell').
223
+ * @returns An array of countries filtered by aggregator support.
224
+ */
225
+ async getCountries(action = 'buy') {
226
+ const countries = await __classPrivateFieldGet(this, _RampsService_instances, "m", _RampsService_request).call(this, RampsApiService.Regions, 'regions/countries', { action, responseType: 'json' });
227
+ if (!Array.isArray(countries)) {
228
+ throw new Error('Malformed response received from countries API');
229
+ }
230
+ return countries.filter((country) => {
231
+ if (country.states && country.states.length > 0) {
232
+ const hasSupportedState = country.states.some((state) => state.supported !== false);
233
+ return country.supported || hasSupportedState;
234
+ }
235
+ return country.supported;
236
+ });
237
+ }
238
+ /**
239
+ * Fetches eligibility information for a specific region.
240
+ *
241
+ * @param isoCode - The ISO code for the region (e.g., "us", "fr", "us-ny").
242
+ * @returns Eligibility information for the region.
243
+ */
244
+ async getEligibility(isoCode) {
245
+ const normalizedIsoCode = isoCode.toLowerCase().trim();
246
+ return __classPrivateFieldGet(this, _RampsService_instances, "m", _RampsService_request).call(this, RampsApiService.Regions, `regions/countries/${normalizedIsoCode}`, { responseType: 'json' });
247
+ }
200
248
  }
201
- _a = RampsService, _RampsService_messenger = new WeakMap(), _RampsService_fetch = new WeakMap(), _RampsService_policy = new WeakMap(), _RampsService_baseUrl = new WeakMap();
249
+ _a = RampsService, _RampsService_messenger = new WeakMap(), _RampsService_fetch = new WeakMap(), _RampsService_policy = new WeakMap(), _RampsService_environment = new WeakMap(), _RampsService_context = new WeakMap(), _RampsService_instances = new WeakSet(), _RampsService_addCommonParams = function _RampsService_addCommonParams(url, action) {
250
+ if (action) {
251
+ url.searchParams.set('action', action);
252
+ }
253
+ url.searchParams.set('sdk', RAMPS_SDK_VERSION);
254
+ url.searchParams.set('controller', packageJson.version);
255
+ url.searchParams.set('context', __classPrivateFieldGet(this, _RampsService_context, "f"));
256
+ }, _RampsService_request =
257
+ /**
258
+ * Makes an API request with retry policy and error handling.
259
+ *
260
+ * @param service - The API service type (determines base URL).
261
+ * @param path - The endpoint path.
262
+ * @param options - Request options.
263
+ * @param options.action - The ramp action type (optional).
264
+ * @param options.responseType - How to parse the response ('json' or 'text').
265
+ * @returns The parsed response data.
266
+ */
267
+ async function _RampsService_request(service, path, options) {
268
+ return __classPrivateFieldGet(this, _RampsService_policy, "f").execute(async () => {
269
+ const baseUrl = getBaseUrl(__classPrivateFieldGet(this, _RampsService_environment, "f"), service);
270
+ const url = new URL(path, baseUrl);
271
+ __classPrivateFieldGet(this, _RampsService_instances, "m", _RampsService_addCommonParams).call(this, url, options.action);
272
+ const response = await __classPrivateFieldGet(this, _RampsService_fetch, "f").call(this, url);
273
+ if (!response.ok) {
274
+ throw new HttpError(response.status, `Fetching '${url.toString()}' failed with status '${response.status}'`);
275
+ }
276
+ return options.responseType === 'json'
277
+ ? response.json()
278
+ : response.text();
279
+ });
280
+ };
202
281
  //# sourceMappingURL=RampsService.mjs.map