@metamask-previews/config-registry-controller 2.0.1-preview-e81fe8853 → 2.0.1-preview-3c77cf4

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 CHANGED
@@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add `ConfigRegistryApiEnv` enum to select the API environment for the service ([#9918](https://github.com/MetaMask/core/pull/9918))
13
+
10
14
  ### Changed
11
15
 
16
+ - **BREAKING:** The `env` optional constructor option type is now `ConfigRegistryApiEnv` ([#9918](https://github.com/MetaMask/core/pull/9918))
17
+ - Previously, constructor options were reusing the `SDK.Env` enum from `@metamask/profile-sync-controller`.
12
18
  - Bump `@metamask/superstruct` from `^3.1.0` to `^3.4.1` ([#9754](https://github.com/MetaMask/core/pull/9754))
13
19
  - Bump `@metamask/keyring-controller` from `^27.1.0` to `^27.1.1` ([#9791](https://github.com/MetaMask/core/pull/9791))
14
20
 
@@ -12,11 +12,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
12
  };
13
13
  var _ConfigRegistryApiService_messenger, _ConfigRegistryApiService_policy, _ConfigRegistryApiService_url, _ConfigRegistryApiService_fetch, _ConfigRegistryApiService_cachedResponse;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ConfigRegistryApiService = exports.serviceName = void 0;
15
+ exports.ConfigRegistryApiService = exports.serviceName = exports.ConfigRegistryApiEnv = void 0;
16
16
  const controller_utils_1 = require("@metamask/controller-utils");
17
- const profile_sync_controller_1 = require("@metamask/profile-sync-controller");
18
17
  const types_js_1 = require("./types.cjs");
19
18
  const ENDPOINT_PATH = '/config/networks';
19
+ var ConfigRegistryApiEnv;
20
+ (function (ConfigRegistryApiEnv) {
21
+ ConfigRegistryApiEnv["DEV"] = "dev";
22
+ ConfigRegistryApiEnv["UAT"] = "uat";
23
+ ConfigRegistryApiEnv["PRD"] = "prod";
24
+ })(ConfigRegistryApiEnv || (exports.ConfigRegistryApiEnv = ConfigRegistryApiEnv = {}));
20
25
  /**
21
26
  * The name of the {@link ConfigRegistryApiService}, used to namespace the
22
27
  * service's actions and events.
@@ -32,7 +37,7 @@ const MESSENGER_EXPOSED_METHODS = ['fetchConfig'];
32
37
  * @returns The base URL for the environment.
33
38
  */
34
39
  function getConfigRegistryUrl(env) {
35
- const envPrefix = env === profile_sync_controller_1.SDK.Env.PRD ? '' : `${env}-`;
40
+ const envPrefix = env === ConfigRegistryApiEnv.PRD ? '' : `${env}-`;
36
41
  return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;
37
42
  }
38
43
  class ConfigRegistryApiService {
@@ -45,7 +50,7 @@ class ConfigRegistryApiService {
45
50
  * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.
46
51
  * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.
47
52
  */
48
- constructor({ messenger, env = profile_sync_controller_1.SDK.Env.UAT, fetch: customFetch = globalThis.fetch, policyOptions = {}, }) {
53
+ constructor({ messenger, env = ConfigRegistryApiEnv.UAT, fetch: customFetch = globalThis.fetch, policyOptions = {}, }) {
49
54
  _ConfigRegistryApiService_messenger.set(this, void 0);
50
55
  _ConfigRegistryApiService_policy.set(this, void 0);
51
56
  _ConfigRegistryApiService_url.set(this, void 0);
@@ -1 +1 @@
1
- {"version":3,"file":"config-registry-api-service.cjs","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iEAA4E;AAM5E,+EAAwD;AASxD,0CAA+D;AAE/D,MAAM,aAAa,GAAG,kBAAkB,CAAC;AAEzC;;;GAGG;AACU,QAAA,WAAW,GAAG,0BAA0B,CAAC;AAEtD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,aAAa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QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;QACvD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAEvC,IAAA,4CAAiC,EAAC,QAAQ,CAAC,CAAC;QAE5C,uBAAA,IAAI,4CAAmB,QAAQ,MAAA,CAAC;QAEhC,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,IAAI;YACJ,QAAQ,EAAE,IAAI;SACf,CAAC;IACJ,CAAC;CACF;AApJD,4DAoJC","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { SDK } from '@metamask/profile-sync-controller';\nimport type { IDisposable } from 'cockatiel';\n\nimport type { ConfigRegistryApiServiceMethodActions } from './config-registry-api-service-method-action-types.js';\nimport type {\n FetchConfigOptions,\n FetchConfigResult,\n RegistryConfigApiResponse,\n} from './types.js';\nimport { validateRegistryConfigApiResponse } from './types.js';\n\nconst ENDPOINT_PATH = '/config/networks';\n\n/**\n * The name of the {@link ConfigRegistryApiService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'ConfigRegistryApiService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchConfig'] as const;\n\n/**\n * Actions that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceActions =\n ConfigRegistryApiServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ConfigRegistryApiServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceEvents = never;\n\n/**\n * Events from other messengers that {@link ConfigRegistryApiService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link ConfigRegistryApiService}.\n */\nexport type ConfigRegistryApiServiceMessenger = Messenger<\n typeof serviceName,\n ConfigRegistryApiServiceActions | AllowedActions,\n ConfigRegistryApiServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Returns the base URL for the config registry API for the given environment.\n *\n * @param env - The environment to get the URL for.\n * @returns The base URL for the environment.\n */\nfunction getConfigRegistryUrl(env: SDK.Env): string {\n const envPrefix = env === SDK.Env.PRD ? '' : `${env}-`;\n return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;\n}\n\nexport type ConfigRegistryApiServiceOptions = {\n /**\n * The messenger suited for this service. Required so the service can be used\n * independently and register its actions.\n */\n messenger: ConfigRegistryApiServiceMessenger;\n env?: SDK.Env;\n fetch?: typeof fetch;\n /**\n * Options to pass to `createServicePolicy`, which wraps each request.\n * See {@link CreateServicePolicyOptions}.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\nexport class ConfigRegistryApiService {\n readonly name: typeof serviceName;\n\n readonly #messenger: ConfigRegistryApiServiceMessenger;\n\n readonly #policy: ServicePolicy;\n\n readonly #url: string;\n\n readonly #fetch: typeof fetch;\n\n /** Cached response from the last successful fetch. Used when server returns 304. */\n #cachedResponse: RegistryConfigApiResponse | null = null;\n\n /**\n * Construct a Config Registry API Service.\n *\n * @param options - The options for constructing the service.\n * @param options.messenger - The messenger suited for this service.\n * @param options.env - The environment to determine the correct API endpoints. Defaults to UAT.\n * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.\n * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n env = SDK.Env.UAT,\n fetch: customFetch = globalThis.fetch,\n policyOptions = {},\n }: ConfigRegistryApiServiceOptions) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#url = getConfigRegistryUrl(env);\n this.#fetch = customFetch;\n\n this.#policy = createServicePolicy(policyOptions);\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(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable {\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 args - The arguments passed to the underlying policy's onBreak method\n * (e.g. the listener 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 ...args: Parameters<ServicePolicy['onBreak']>\n ): ReturnType<ServicePolicy['onBreak']> {\n return this.#policy.onBreak(...args);\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 args - The arguments passed to the underlying policy's onDegraded\n * method (e.g. the listener to be called).\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n onDegraded(\n ...args: Parameters<ServicePolicy['onDegraded']>\n ): ReturnType<ServicePolicy['onDegraded']> {\n return this.#policy.onDegraded(...args);\n }\n\n async fetchConfig(\n options: FetchConfigOptions = {},\n ): Promise<FetchConfigResult> {\n const headers: HeadersInit = {\n 'Cache-Control': 'no-cache',\n };\n\n if (options.etag) {\n headers['If-None-Match'] = options.etag;\n }\n\n const response = await this.#policy.execute(async () => {\n const res = await this.#fetch(this.#url, {\n headers,\n });\n\n if (res.status === 304) {\n return res;\n }\n\n if (!res.ok) {\n throw new HttpError(\n res.status,\n `Failed to fetch config: ${res.status} ${res.statusText}`,\n );\n }\n\n return res;\n });\n\n if (response.status === 304) {\n const etag = response.headers.get('ETag') ?? undefined;\n return {\n modified: false,\n etag,\n ...(this.#cachedResponse !== null && { data: this.#cachedResponse }),\n };\n }\n\n const etag = response.headers.get('ETag') ?? undefined;\n const jsonData = await response.json();\n\n validateRegistryConfigApiResponse(jsonData);\n\n this.#cachedResponse = jsonData;\n\n return {\n data: jsonData,\n etag,\n modified: true,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"config-registry-api-service.cjs","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,iEAA4E;AAc5E,0CAA+D;AAE/D,MAAM,aAAa,GAAG,kBAAkB,CAAC;AAEzC,IAAY,oBAIX;AAJD,WAAY,oBAAoB;IAC9B,mCAAW,CAAA;IACX,mCAAW,CAAA;IACX,oCAAY,CAAA;AACd,CAAC,EAJW,oBAAoB,oCAApB,oBAAoB,QAI/B;AAED;;;GAGG;AACU,QAAA,WAAW,GAAG,0BAA0B,CAAC;AAEtD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,aAAa,CAAU,CAAC;AAiC3D,6BAA6B;AAE7B;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,GAAyB;IACrD,MAAM,SAAS,GAAG,GAAG,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;IACpE,OAAO,yBAAyB,SAAS,wBAAwB,aAAa,EAAE,CAAC;AACnF,CAAC;AAiBD,MAAa,wBAAwB;IAcnC;;;;;;;;OAQG;IACH,YAAY,EACV,SAAS,EACT,GAAG,GAAG,oBAAoB,CAAC,GAAG,EAC9B,KAAK,EAAE,WAAW,GAAG,UAAU,CAAC,KAAK,EACrC,aAAa,GAAG,EAAE,GACc;QAzBzB,sDAA8C;QAE9C,mDAAuB;QAEvB,gDAAa;QAEb,kDAAqB;QAE9B,oFAAoF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,CAAC,CAAC;YAEH,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;gBACvB,OAAO,GAAG,CAAC;YACb,CAAC;YAED,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,IAAI,4BAAS,CACjB,GAAG,CAAC,MAAM,EACV,2BAA2B,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,CAC1D,CAAC;YACJ,CAAC;YAED,OAAO,GAAG,CAAC;QACb,CAAC,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;YACvD,OAAO;gBACL,QAAQ,EAAE,KAAK;gBACf,IAAI;gBACJ,GAAG,CAAC,uBAAA,IAAI,gDAAgB,KAAK,IAAI,IAAI,EAAE,IAAI,EAAE,uBAAA,IAAI,gDAAgB,EAAE,CAAC;aACrE,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;QACvD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAEvC,IAAA,4CAAiC,EAAC,QAAQ,CAAC,CAAC;QAE5C,uBAAA,IAAI,4CAAmB,QAAQ,MAAA,CAAC;QAEhC,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,IAAI;YACJ,QAAQ,EAAE,IAAI;SACf,CAAC;IACJ,CAAC;CACF;AApJD,4DAoJC","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type { IDisposable } from 'cockatiel';\n\nimport type { ConfigRegistryApiServiceMethodActions } from './config-registry-api-service-method-action-types.js';\nimport type {\n FetchConfigOptions,\n FetchConfigResult,\n RegistryConfigApiResponse,\n} from './types.js';\nimport { validateRegistryConfigApiResponse } from './types.js';\n\nconst ENDPOINT_PATH = '/config/networks';\n\nexport enum ConfigRegistryApiEnv {\n DEV = 'dev',\n UAT = 'uat',\n PRD = 'prod',\n}\n\n/**\n * The name of the {@link ConfigRegistryApiService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'ConfigRegistryApiService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchConfig'] as const;\n\n/**\n * Actions that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceActions =\n ConfigRegistryApiServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ConfigRegistryApiServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceEvents = never;\n\n/**\n * Events from other messengers that {@link ConfigRegistryApiService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link ConfigRegistryApiService}.\n */\nexport type ConfigRegistryApiServiceMessenger = Messenger<\n typeof serviceName,\n ConfigRegistryApiServiceActions | AllowedActions,\n ConfigRegistryApiServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Returns the base URL for the config registry API for the given environment.\n *\n * @param env - The environment to get the URL for.\n * @returns The base URL for the environment.\n */\nfunction getConfigRegistryUrl(env: ConfigRegistryApiEnv): string {\n const envPrefix = env === ConfigRegistryApiEnv.PRD ? '' : `${env}-`;\n return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;\n}\n\nexport type ConfigRegistryApiServiceOptions = {\n /**\n * The messenger suited for this service. Required so the service can be used\n * independently and register its actions.\n */\n messenger: ConfigRegistryApiServiceMessenger;\n env?: ConfigRegistryApiEnv;\n fetch?: typeof fetch;\n /**\n * Options to pass to `createServicePolicy`, which wraps each request.\n * See {@link CreateServicePolicyOptions}.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\nexport class ConfigRegistryApiService {\n readonly name: typeof serviceName;\n\n readonly #messenger: ConfigRegistryApiServiceMessenger;\n\n readonly #policy: ServicePolicy;\n\n readonly #url: string;\n\n readonly #fetch: typeof fetch;\n\n /** Cached response from the last successful fetch. Used when server returns 304. */\n #cachedResponse: RegistryConfigApiResponse | null = null;\n\n /**\n * Construct a Config Registry API Service.\n *\n * @param options - The options for constructing the service.\n * @param options.messenger - The messenger suited for this service.\n * @param options.env - The environment to determine the correct API endpoints. Defaults to UAT.\n * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.\n * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n env = ConfigRegistryApiEnv.UAT,\n fetch: customFetch = globalThis.fetch,\n policyOptions = {},\n }: ConfigRegistryApiServiceOptions) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#url = getConfigRegistryUrl(env);\n this.#fetch = customFetch;\n\n this.#policy = createServicePolicy(policyOptions);\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(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable {\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 args - The arguments passed to the underlying policy's onBreak method\n * (e.g. the listener 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 ...args: Parameters<ServicePolicy['onBreak']>\n ): ReturnType<ServicePolicy['onBreak']> {\n return this.#policy.onBreak(...args);\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 args - The arguments passed to the underlying policy's onDegraded\n * method (e.g. the listener to be called).\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n onDegraded(\n ...args: Parameters<ServicePolicy['onDegraded']>\n ): ReturnType<ServicePolicy['onDegraded']> {\n return this.#policy.onDegraded(...args);\n }\n\n async fetchConfig(\n options: FetchConfigOptions = {},\n ): Promise<FetchConfigResult> {\n const headers: HeadersInit = {\n 'Cache-Control': 'no-cache',\n };\n\n if (options.etag) {\n headers['If-None-Match'] = options.etag;\n }\n\n const response = await this.#policy.execute(async () => {\n const res = await this.#fetch(this.#url, {\n headers,\n });\n\n if (res.status === 304) {\n return res;\n }\n\n if (!res.ok) {\n throw new HttpError(\n res.status,\n `Failed to fetch config: ${res.status} ${res.statusText}`,\n );\n }\n\n return res;\n });\n\n if (response.status === 304) {\n const etag = response.headers.get('ETag') ?? undefined;\n return {\n modified: false,\n etag,\n ...(this.#cachedResponse !== null && { data: this.#cachedResponse }),\n };\n }\n\n const etag = response.headers.get('ETag') ?? undefined;\n const jsonData = await response.json();\n\n validateRegistryConfigApiResponse(jsonData);\n\n this.#cachedResponse = jsonData;\n\n return {\n data: jsonData,\n etag,\n modified: true,\n };\n }\n}\n"]}
@@ -1,9 +1,13 @@
1
1
  import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
2
  import type { Messenger } from "@metamask/messenger";
3
- import { SDK } from "@metamask/profile-sync-controller";
4
3
  import type { IDisposable } from "cockatiel";
5
4
  import type { ConfigRegistryApiServiceMethodActions } from "./config-registry-api-service-method-action-types.cjs";
6
5
  import type { FetchConfigOptions, FetchConfigResult } from "./types.cjs";
6
+ export declare enum ConfigRegistryApiEnv {
7
+ DEV = "dev",
8
+ UAT = "uat",
9
+ PRD = "prod"
10
+ }
7
11
  /**
8
12
  * The name of the {@link ConfigRegistryApiService}, used to namespace the
9
13
  * service's actions and events.
@@ -36,7 +40,7 @@ export type ConfigRegistryApiServiceOptions = {
36
40
  * independently and register its actions.
37
41
  */
38
42
  messenger: ConfigRegistryApiServiceMessenger;
39
- env?: SDK.Env;
43
+ env?: ConfigRegistryApiEnv;
40
44
  fetch?: typeof fetch;
41
45
  /**
42
46
  * Options to pass to `createServicePolicy`, which wraps each request.
@@ -1 +1 @@
1
- {"version":3,"file":"config-registry-api-service.d.cts","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EAAE,GAAG,EAAE,0CAA0C;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB;AAE7C,OAAO,KAAK,EAAE,qCAAqC,EAAE,8DAA6D;AAClH,OAAO,KAAK,EACV,kBAAkB,EAClB,iBAAiB,EAElB,oBAAmB;AAKpB;;;GAGG;AACH,eAAO,MAAM,WAAW,6BAA6B,CAAC;AAMtD;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACzC,qCAAqC,CAAC;AAExC;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG,KAAK,CAAC;AAEnD;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,WAAW,EAClB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;AAeF,MAAM,MAAM,+BAA+B,GAAG;IAC5C;;;OAGG;IACH,SAAS,EAAE,iCAAiC,CAAC;IAC7C,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;;OAGG;IACH,aAAa,CAAC,EAAE,0BAA0B,CAAC;CAC5C,CAAC;AAEF,qBAAa,wBAAwB;;IACnC,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IAalC;;;;;;;;OAQG;gBACS,EACV,SAAS,EACT,GAAiB,EACjB,KAAK,EAAE,WAA8B,EACrC,aAAkB,GACnB,EAAE,+BAA+B;IAclC;;;;;;;;;OASG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW;IAIvE;;;;;;;;;OASG;IACH,OAAO,CACL,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,GAC5C,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;;;;;;;;;;OAiBG;IACH,UAAU,CACR,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,GAC/C,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAIpC,WAAW,CACf,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,iBAAiB,CAAC;CAkD9B"}
1
+ {"version":3,"file":"config-registry-api-service.d.cts","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB;AAE7C,OAAO,KAAK,EAAE,qCAAqC,EAAE,8DAA6D;AAClH,OA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;;;;;;;;;OAiBG;IACH,UAAU,CACR,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,GAC/C,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAIpC,WAAW,CACf,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,iBAAiB,CAAC;CAkD9B"}
@@ -1,9 +1,13 @@
1
1
  import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
2
  import type { Messenger } from "@metamask/messenger";
3
- import { SDK } from "@metamask/profile-sync-controller";
4
3
  import type { IDisposable } from "cockatiel";
5
4
  import type { ConfigRegistryApiServiceMethodActions } from "./config-registry-api-service-method-action-types.mjs";
6
5
  import type { FetchConfigOptions, FetchConfigResult } from "./types.mjs";
6
+ export declare enum ConfigRegistryApiEnv {
7
+ DEV = "dev",
8
+ UAT = "uat",
9
+ PRD = "prod"
10
+ }
7
11
  /**
8
12
  * The name of the {@link ConfigRegistryApiService}, used to namespace the
9
13
  * service's actions and events.
@@ -36,7 +40,7 @@ export type ConfigRegistryApiServiceOptions = {
36
40
  * independently and register its actions.
37
41
  */
38
42
  messenger: ConfigRegistryApiServiceMessenger;
39
- env?: SDK.Env;
43
+ env?: ConfigRegistryApiEnv;
40
44
  fetch?: typeof fetch;
41
45
  /**
42
46
  * Options to pass to `createServicePolicy`, which wraps each request.
@@ -1 +1 @@
1
- {"version":3,"file":"config-registry-api-service.d.mts","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EAAE,GAAG,EAAE,0CAA0C;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB;AAE7C,OAAO,KAAK,EAAE,qCAAqC,EAAE,8DAA6D;AAClH,OAAO,KAAK,EACV,kBAAkB,EAClB,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AU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAIpC,WAAW,CACf,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,iBAAiB,CAAC;CAkD9B"}
1
+ {"version":3,"file":"config-registry-api-service.d.mts","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AACpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB;AAE7C,OAAO,KAAK,EAAE,qCAAqC,EAAE,8DAA6D;AAClH,OAAO,KAAK,EACV,kBAAkB,EAClB,iBAAiB,EAElB,oBAAmB;AAKpB,oBAAY,oBAAoB;IAC9B,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,SAAS;CACb;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,6BAA6B,CAAC;AAMtD;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACzC,qCAAqC,CAAC;AAExC;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG,KAAK,CAAC;AAEnD;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,WAAW,EAClB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;AAeF,MAAM,MAAM,+BAA+B,GAAG;IAC5C;;;OAGG;IACH,SAAS,EAAE,iCAAiC,CAAC;IAC7C,GAAG,CAAC,EAAE,oBAAoB,CAAC;IAC3B,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;;OAGG;IACH,aAAa,CAAC,EAAE,0BAA0B,CAAC;CAC5C,CAAC;AAEF,qBAAa,wBAAwB;;IACnC,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IAalC;;;;;;;;OAQG;gBACS,EACV,SAAS,EACT,GAA8B,EAC9B,KAAK,EAAE,WAA8B,EACrC,aAAkB,GACnB,EAAE,+BAA+B;IAclC;;;;;;;;;OASG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW;IAIvE;;;;;;;;;OASG;IACH,OAAO,CACL,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,GAC5C,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAIvC;;;;;;;;;;;;;;;;;OAiBG;IACH,UAAU,CACR,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,GAC/C,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAIpC,WAAW,CACf,OAAO,GAAE,kBAAuB,GAC/B,OAAO,CAAC,iBAAiB,CAAC;CAkD9B"}
@@ -11,9 +11,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
11
  };
12
12
  var _ConfigRegistryApiService_messenger, _ConfigRegistryApiService_policy, _ConfigRegistryApiService_url, _ConfigRegistryApiService_fetch, _ConfigRegistryApiService_cachedResponse;
13
13
  import { createServicePolicy, HttpError } from "@metamask/controller-utils";
14
- import { SDK } from "@metamask/profile-sync-controller";
15
14
  import { validateRegistryConfigApiResponse } from "./types.mjs";
16
15
  const ENDPOINT_PATH = '/config/networks';
16
+ export var ConfigRegistryApiEnv;
17
+ (function (ConfigRegistryApiEnv) {
18
+ ConfigRegistryApiEnv["DEV"] = "dev";
19
+ ConfigRegistryApiEnv["UAT"] = "uat";
20
+ ConfigRegistryApiEnv["PRD"] = "prod";
21
+ })(ConfigRegistryApiEnv || (ConfigRegistryApiEnv = {}));
17
22
  /**
18
23
  * The name of the {@link ConfigRegistryApiService}, used to namespace the
19
24
  * service's actions and events.
@@ -29,7 +34,7 @@ const MESSENGER_EXPOSED_METHODS = ['fetchConfig'];
29
34
  * @returns The base URL for the environment.
30
35
  */
31
36
  function getConfigRegistryUrl(env) {
32
- const envPrefix = env === SDK.Env.PRD ? '' : `${env}-`;
37
+ const envPrefix = env === ConfigRegistryApiEnv.PRD ? '' : `${env}-`;
33
38
  return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;
34
39
  }
35
40
  export class ConfigRegistryApiService {
@@ -42,7 +47,7 @@ export class ConfigRegistryApiService {
42
47
  * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.
43
48
  * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.
44
49
  */
45
- constructor({ messenger, env = SDK.Env.UAT, fetch: customFetch = globalThis.fetch, policyOptions = {}, }) {
50
+ constructor({ messenger, env = ConfigRegistryApiEnv.UAT, fetch: customFetch = globalThis.fetch, policyOptions = {}, }) {
46
51
  _ConfigRegistryApiService_messenger.set(this, void 0);
47
52
  _ConfigRegistryApiService_policy.set(this, void 0);
48
53
  _ConfigRegistryApiService_url.set(this, void 0);
@@ -1 +1 @@
1
- {"version":3,"file":"config-registry-api-service.mjs","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,mCAAmC;AAM5E,OAAO,EAAE,GAAG,EAAE,0CAA0C;AASxD,OAAO,EAAE,iCAAiC,EAAE,oBAAmB;AAE/D,MAAM,aAAa,GAAG,kBAAkB,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,0BAA0B,CAAC;AAEtD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,aAAa,CAAU,CAAC;AAiC3D,6BAA6B;AAE7B;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,GAAY;IACxC,MAAM,SAAS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AQ,CAAC,IAAI,EAAE,CAAC;QAEvC,iCAAiC,CAAC,QAAQ,CAAC,CAAC;QAE5C,uBAAA,IAAI,4CAAmB,QAAQ,MAAA,CAAC;QAEhC,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,IAAI;YACJ,QAAQ,EAAE,IAAI;SACf,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { SDK } from '@metamask/profile-sync-controller';\nimport type { IDisposable } from 'cockatiel';\n\nimport type { ConfigRegistryApiServiceMethodActions } from './config-registry-api-service-method-action-types.js';\nimport type {\n FetchConfigOptions,\n FetchConfigResult,\n RegistryConfigApiResponse,\n} from './types.js';\nimport { validateRegistryConfigApiResponse } from './types.js';\n\nconst ENDPOINT_PATH = '/config/networks';\n\n/**\n * The name of the {@link ConfigRegistryApiService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'ConfigRegistryApiService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchConfig'] as const;\n\n/**\n * Actions that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceActions =\n ConfigRegistryApiServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ConfigRegistryApiServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceEvents = never;\n\n/**\n * Events from other messengers that {@link ConfigRegistryApiService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link ConfigRegistryApiService}.\n */\nexport type ConfigRegistryApiServiceMessenger = Messenger<\n typeof serviceName,\n ConfigRegistryApiServiceActions | AllowedActions,\n ConfigRegistryApiServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Returns the base URL for the config registry API for the given environment.\n *\n * @param env - The environment to get the URL for.\n * @returns The base URL for the environment.\n */\nfunction getConfigRegistryUrl(env: SDK.Env): string {\n const envPrefix = env === SDK.Env.PRD ? '' : `${env}-`;\n return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;\n}\n\nexport type ConfigRegistryApiServiceOptions = {\n /**\n * The messenger suited for this service. Required so the service can be used\n * independently and register its actions.\n */\n messenger: ConfigRegistryApiServiceMessenger;\n env?: SDK.Env;\n fetch?: typeof fetch;\n /**\n * Options to pass to `createServicePolicy`, which wraps each request.\n * See {@link CreateServicePolicyOptions}.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\nexport class ConfigRegistryApiService {\n readonly name: typeof serviceName;\n\n readonly #messenger: ConfigRegistryApiServiceMessenger;\n\n readonly #policy: ServicePolicy;\n\n readonly #url: string;\n\n readonly #fetch: typeof fetch;\n\n /** Cached response from the last successful fetch. Used when server returns 304. */\n #cachedResponse: RegistryConfigApiResponse | null = null;\n\n /**\n * Construct a Config Registry API Service.\n *\n * @param options - The options for constructing the service.\n * @param options.messenger - The messenger suited for this service.\n * @param options.env - The environment to determine the correct API endpoints. Defaults to UAT.\n * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.\n * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n env = SDK.Env.UAT,\n fetch: customFetch = globalThis.fetch,\n policyOptions = {},\n }: ConfigRegistryApiServiceOptions) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#url = getConfigRegistryUrl(env);\n this.#fetch = customFetch;\n\n this.#policy = createServicePolicy(policyOptions);\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(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable {\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 args - The arguments passed to the underlying policy's onBreak method\n * (e.g. the listener 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 ...args: Parameters<ServicePolicy['onBreak']>\n ): ReturnType<ServicePolicy['onBreak']> {\n return this.#policy.onBreak(...args);\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 args - The arguments passed to the underlying policy's onDegraded\n * method (e.g. the listener to be called).\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n onDegraded(\n ...args: Parameters<ServicePolicy['onDegraded']>\n ): ReturnType<ServicePolicy['onDegraded']> {\n return this.#policy.onDegraded(...args);\n }\n\n async fetchConfig(\n options: FetchConfigOptions = {},\n ): Promise<FetchConfigResult> {\n const headers: HeadersInit = {\n 'Cache-Control': 'no-cache',\n };\n\n if (options.etag) {\n headers['If-None-Match'] = options.etag;\n }\n\n const response = await this.#policy.execute(async () => {\n const res = await this.#fetch(this.#url, {\n headers,\n });\n\n if (res.status === 304) {\n return res;\n }\n\n if (!res.ok) {\n throw new HttpError(\n res.status,\n `Failed to fetch config: ${res.status} ${res.statusText}`,\n );\n }\n\n return res;\n });\n\n if (response.status === 304) {\n const etag = response.headers.get('ETag') ?? undefined;\n return {\n modified: false,\n etag,\n ...(this.#cachedResponse !== null && { data: this.#cachedResponse }),\n };\n }\n\n const etag = response.headers.get('ETag') ?? undefined;\n const jsonData = await response.json();\n\n validateRegistryConfigApiResponse(jsonData);\n\n this.#cachedResponse = jsonData;\n\n return {\n data: jsonData,\n etag,\n modified: true,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"config-registry-api-service.mjs","sourceRoot":"","sources":["../../src/config-registry-api-service/config-registry-api-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,mCAAmC;AAc5E,OAAO,EAAE,iCAAiC,EAAE,oBAAmB;AAE/D,MAAM,aAAa,GAAG,kBAAkB,CAAC;AAEzC,MAAM,CAAN,IAAY,oBAIX;AAJD,WAAY,oBAAoB;IAC9B,mCAAW,CAAA;IACX,mCAAW,CAAA;IACX,oCAAY,CAAA;AACd,CAAC,EAJW,oBAAoB,KAApB,oBAAoB,QAI/B;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,0BAA0B,CAAC;AAEtD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,aAAa,CAAU,CAAC;AAiC3D,6BAA6B;AAE7B;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,GAAyB;IACrD,MAAM,SAAS,GAAG,GAAG,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;IACpE,OAAO,yBAAyB,SAAS,wBAAwB,aAAa,EAAE,CAAC;AACnF,CAAC;AAiBD,MAAM,OAAO,wBAAwB;IAcnC;;;;;;;;OAQG;IACH,YAAY,EACV,SAAS,EACT,GAAG,GAAG,oBAAoB,CAAC,GAAG,EAC9B,KAAK,EAAE,WAAW,GAAG,UAAU,CAAC,KAAK,EACrC,aAAa,GAAG,EAAE,GACc;QAzBzB,sDAA8C;QAE9C,mDAAuB;QAEvB,gDAAa;QAEb,kDAAqB;QAE9B,oFAAoF;QACpF,mDAAoD,IAAI,EAAC;QAi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AAM,KAAK,GAAG,EAAE,CAAC;gBACvB,OAAO,GAAG,CAAC;YACb,CAAC;YAED,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,IAAI,SAAS,CACjB,GAAG,CAAC,MAAM,EACV,2BAA2B,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,CAC1D,CAAC;YACJ,CAAC;YAED,OAAO,GAAG,CAAC;QACb,CAAC,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;YACvD,OAAO;gBACL,QAAQ,EAAE,KAAK;gBACf,IAAI;gBACJ,GAAG,CAAC,uBAAA,IAAI,gDAAgB,KAAK,IAAI,IAAI,EAAE,IAAI,EAAE,uBAAA,IAAI,gDAAgB,EAAE,CAAC;aACrE,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC;QACvD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAEvC,iCAAiC,CAAC,QAAQ,CAAC,CAAC;QAE5C,uBAAA,IAAI,4CAAmB,QAAQ,MAAA,CAAC;QAEhC,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,IAAI;YACJ,QAAQ,EAAE,IAAI;SACf,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type { IDisposable } from 'cockatiel';\n\nimport type { ConfigRegistryApiServiceMethodActions } from './config-registry-api-service-method-action-types.js';\nimport type {\n FetchConfigOptions,\n FetchConfigResult,\n RegistryConfigApiResponse,\n} from './types.js';\nimport { validateRegistryConfigApiResponse } from './types.js';\n\nconst ENDPOINT_PATH = '/config/networks';\n\nexport enum ConfigRegistryApiEnv {\n DEV = 'dev',\n UAT = 'uat',\n PRD = 'prod',\n}\n\n/**\n * The name of the {@link ConfigRegistryApiService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'ConfigRegistryApiService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchConfig'] as const;\n\n/**\n * Actions that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceActions =\n ConfigRegistryApiServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ConfigRegistryApiServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link ConfigRegistryApiService} exposes to other consumers.\n */\nexport type ConfigRegistryApiServiceEvents = never;\n\n/**\n * Events from other messengers that {@link ConfigRegistryApiService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link ConfigRegistryApiService}.\n */\nexport type ConfigRegistryApiServiceMessenger = Messenger<\n typeof serviceName,\n ConfigRegistryApiServiceActions | AllowedActions,\n ConfigRegistryApiServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Returns the base URL for the config registry API for the given environment.\n *\n * @param env - The environment to get the URL for.\n * @returns The base URL for the environment.\n */\nfunction getConfigRegistryUrl(env: ConfigRegistryApiEnv): string {\n const envPrefix = env === ConfigRegistryApiEnv.PRD ? '' : `${env}-`;\n return `https://client-config.${envPrefix}api.cx.metamask.io/v1${ENDPOINT_PATH}`;\n}\n\nexport type ConfigRegistryApiServiceOptions = {\n /**\n * The messenger suited for this service. Required so the service can be used\n * independently and register its actions.\n */\n messenger: ConfigRegistryApiServiceMessenger;\n env?: ConfigRegistryApiEnv;\n fetch?: typeof fetch;\n /**\n * Options to pass to `createServicePolicy`, which wraps each request.\n * See {@link CreateServicePolicyOptions}.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\nexport class ConfigRegistryApiService {\n readonly name: typeof serviceName;\n\n readonly #messenger: ConfigRegistryApiServiceMessenger;\n\n readonly #policy: ServicePolicy;\n\n readonly #url: string;\n\n readonly #fetch: typeof fetch;\n\n /** Cached response from the last successful fetch. Used when server returns 304. */\n #cachedResponse: RegistryConfigApiResponse | null = null;\n\n /**\n * Construct a Config Registry API Service.\n *\n * @param options - The options for constructing the service.\n * @param options.messenger - The messenger suited for this service.\n * @param options.env - The environment to determine the correct API endpoints. Defaults to UAT.\n * @param options.fetch - Custom fetch function for testing or custom implementations. Defaults to the global fetch.\n * @param options.policyOptions - Options to pass to `createServicePolicy`, which wraps each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n env = ConfigRegistryApiEnv.UAT,\n fetch: customFetch = globalThis.fetch,\n policyOptions = {},\n }: ConfigRegistryApiServiceOptions) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#url = getConfigRegistryUrl(env);\n this.#fetch = customFetch;\n\n this.#policy = createServicePolicy(policyOptions);\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(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable {\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 args - The arguments passed to the underlying policy's onBreak method\n * (e.g. the listener 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 ...args: Parameters<ServicePolicy['onBreak']>\n ): ReturnType<ServicePolicy['onBreak']> {\n return this.#policy.onBreak(...args);\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 args - The arguments passed to the underlying policy's onDegraded\n * method (e.g. the listener to be called).\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n onDegraded(\n ...args: Parameters<ServicePolicy['onDegraded']>\n ): ReturnType<ServicePolicy['onDegraded']> {\n return this.#policy.onDegraded(...args);\n }\n\n async fetchConfig(\n options: FetchConfigOptions = {},\n ): Promise<FetchConfigResult> {\n const headers: HeadersInit = {\n 'Cache-Control': 'no-cache',\n };\n\n if (options.etag) {\n headers['If-None-Match'] = options.etag;\n }\n\n const response = await this.#policy.execute(async () => {\n const res = await this.#fetch(this.#url, {\n headers,\n });\n\n if (res.status === 304) {\n return res;\n }\n\n if (!res.ok) {\n throw new HttpError(\n res.status,\n `Failed to fetch config: ${res.status} ${res.statusText}`,\n );\n }\n\n return res;\n });\n\n if (response.status === 304) {\n const etag = response.headers.get('ETag') ?? undefined;\n return {\n modified: false,\n etag,\n ...(this.#cachedResponse !== null && { data: this.#cachedResponse }),\n };\n }\n\n const etag = response.headers.get('ETag') ?? undefined;\n const jsonData = await response.json();\n\n validateRegistryConfigApiResponse(jsonData);\n\n this.#cachedResponse = jsonData;\n\n return {\n data: jsonData,\n etag,\n modified: true,\n };\n }\n}\n"]}
@@ -4,33 +4,33 @@ import type { Infer } from "@metamask/superstruct";
4
4
  * chainId is in CAIP-2 format (e.g. "eip155:1", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp").
5
5
  */
6
6
  export declare const RegistryNetworkConfigSchema: import("@metamask/superstruct").Struct<{
7
- name: string;
8
7
  imageUrl: string;
8
+ name: string;
9
9
  chainId: `${string}:${string}`;
10
10
  coingeckoPlatformId: string;
11
11
  assets: {
12
12
  native: {
13
13
  symbol: string;
14
- name: string;
15
14
  assetId: string;
16
15
  imageUrl: string;
16
+ name: string;
17
17
  decimals: number;
18
18
  coingeckoCoinId?: string | undefined;
19
19
  };
20
20
  listUrl?: string | undefined;
21
21
  governance?: {
22
22
  symbol: string;
23
- name: string;
24
23
  assetId: string;
25
24
  imageUrl: string;
25
+ name: string;
26
26
  decimals: number;
27
27
  coingeckoCoinId?: string | undefined;
28
28
  } | undefined;
29
29
  };
30
30
  rpcProviders: {
31
31
  default: {
32
- type: string;
33
32
  url: string;
33
+ type: string;
34
34
  networkClientId: string;
35
35
  };
36
36
  fallbacks: string[];
@@ -61,18 +61,18 @@ export declare const RegistryNetworkConfigSchema: import("@metamask/superstruct"
61
61
  assets: import("@metamask/superstruct").Struct<{
62
62
  native: {
63
63
  symbol: string;
64
- name: string;
65
64
  assetId: string;
66
65
  imageUrl: string;
66
+ name: string;
67
67
  decimals: number;
68
68
  coingeckoCoinId?: string | undefined;
69
69
  };
70
70
  listUrl?: string | undefined;
71
71
  governance?: {
72
72
  symbol: string;
73
- name: string;
74
73
  assetId: string;
75
74
  imageUrl: string;
75
+ name: string;
76
76
  decimals: number;
77
77
  coingeckoCoinId?: string | undefined;
78
78
  } | undefined;
@@ -80,9 +80,9 @@ export declare const RegistryNetworkConfigSchema: import("@metamask/superstruct"
80
80
  listUrl: import("@metamask/superstruct").Struct<string | undefined, null>;
81
81
  native: import("@metamask/superstruct").Struct<{
82
82
  symbol: string;
83
- name: string;
84
83
  assetId: string;
85
84
  imageUrl: string;
85
+ name: string;
86
86
  decimals: number;
87
87
  coingeckoCoinId?: string | undefined;
88
88
  }, {
@@ -95,9 +95,9 @@ export declare const RegistryNetworkConfigSchema: import("@metamask/superstruct"
95
95
  }>;
96
96
  governance: import("@metamask/superstruct").Struct<{
97
97
  symbol: string;
98
- name: string;
99
98
  assetId: string;
100
99
  imageUrl: string;
100
+ name: string;
101
101
  decimals: number;
102
102
  coingeckoCoinId?: string | undefined;
103
103
  } | undefined, {
@@ -111,15 +111,15 @@ export declare const RegistryNetworkConfigSchema: import("@metamask/superstruct"
111
111
  }>;
112
112
  rpcProviders: import("@metamask/superstruct").Struct<{
113
113
  default: {
114
- type: string;
115
114
  url: string;
115
+ type: string;
116
116
  networkClientId: string;
117
117
  };
118
118
  fallbacks: string[];
119
119
  }, {
120
120
  default: import("@metamask/superstruct").Struct<{
121
- type: string;
122
121
  url: string;
122
+ type: string;
123
123
  networkClientId: string;
124
124
  }, {
125
125
  url: import("@metamask/superstruct").Struct<string, null>;
@@ -166,33 +166,33 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
166
166
  version: string;
167
167
  timestamp: number;
168
168
  chains: {
169
- name: string;
170
169
  imageUrl: string;
170
+ name: string;
171
171
  chainId: `${string}:${string}`;
172
172
  coingeckoPlatformId: string;
173
173
  assets: {
174
174
  native: {
175
175
  symbol: string;
176
- name: string;
177
176
  assetId: string;
178
177
  imageUrl: string;
178
+ name: string;
179
179
  decimals: number;
180
180
  coingeckoCoinId?: string | undefined;
181
181
  };
182
182
  listUrl?: string | undefined;
183
183
  governance?: {
184
184
  symbol: string;
185
- name: string;
186
185
  assetId: string;
187
186
  imageUrl: string;
187
+ name: string;
188
188
  decimals: number;
189
189
  coingeckoCoinId?: string | undefined;
190
190
  } | undefined;
191
191
  };
192
192
  rpcProviders: {
193
193
  default: {
194
- type: string;
195
194
  url: string;
195
+ type: string;
196
196
  networkClientId: string;
197
197
  };
198
198
  fallbacks: string[];
@@ -221,33 +221,33 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
221
221
  version: string;
222
222
  timestamp: number;
223
223
  chains: {
224
- name: string;
225
224
  imageUrl: string;
225
+ name: string;
226
226
  chainId: `${string}:${string}`;
227
227
  coingeckoPlatformId: string;
228
228
  assets: {
229
229
  native: {
230
230
  symbol: string;
231
- name: string;
232
231
  assetId: string;
233
232
  imageUrl: string;
233
+ name: string;
234
234
  decimals: number;
235
235
  coingeckoCoinId?: string | undefined;
236
236
  };
237
237
  listUrl?: string | undefined;
238
238
  governance?: {
239
239
  symbol: string;
240
- name: string;
241
240
  assetId: string;
242
241
  imageUrl: string;
242
+ name: string;
243
243
  decimals: number;
244
244
  coingeckoCoinId?: string | undefined;
245
245
  } | undefined;
246
246
  };
247
247
  rpcProviders: {
248
248
  default: {
249
- type: string;
250
249
  url: string;
250
+ type: string;
251
251
  networkClientId: string;
252
252
  };
253
253
  fallbacks: string[];
@@ -274,33 +274,33 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
274
274
  version: import("@metamask/superstruct").Struct<string, null>;
275
275
  timestamp: import("@metamask/superstruct").Struct<number, null>;
276
276
  chains: import("@metamask/superstruct").Struct<{
277
- name: string;
278
277
  imageUrl: string;
278
+ name: string;
279
279
  chainId: `${string}:${string}`;
280
280
  coingeckoPlatformId: string;
281
281
  assets: {
282
282
  native: {
283
283
  symbol: string;
284
- name: string;
285
284
  assetId: string;
286
285
  imageUrl: string;
286
+ name: string;
287
287
  decimals: number;
288
288
  coingeckoCoinId?: string | undefined;
289
289
  };
290
290
  listUrl?: string | undefined;
291
291
  governance?: {
292
292
  symbol: string;
293
- name: string;
294
293
  assetId: string;
295
294
  imageUrl: string;
295
+ name: string;
296
296
  decimals: number;
297
297
  coingeckoCoinId?: string | undefined;
298
298
  } | undefined;
299
299
  };
300
300
  rpcProviders: {
301
301
  default: {
302
- type: string;
303
302
  url: string;
303
+ type: string;
304
304
  networkClientId: string;
305
305
  };
306
306
  fallbacks: string[];
@@ -323,33 +323,33 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
323
323
  multicall3?: `0x${string}` | undefined;
324
324
  } | undefined;
325
325
  }[], import("@metamask/superstruct").Struct<{
326
- name: string;
327
326
  imageUrl: string;
327
+ name: string;
328
328
  chainId: `${string}:${string}`;
329
329
  coingeckoPlatformId: string;
330
330
  assets: {
331
331
  native: {
332
332
  symbol: string;
333
- name: string;
334
333
  assetId: string;
335
334
  imageUrl: string;
335
+ name: string;
336
336
  decimals: number;
337
337
  coingeckoCoinId?: string | undefined;
338
338
  };
339
339
  listUrl?: string | undefined;
340
340
  governance?: {
341
341
  symbol: string;
342
- name: string;
343
342
  assetId: string;
344
343
  imageUrl: string;
344
+ name: string;
345
345
  decimals: number;
346
346
  coingeckoCoinId?: string | undefined;
347
347
  } | undefined;
348
348
  };
349
349
  rpcProviders: {
350
350
  default: {
351
- type: string;
352
351
  url: string;
352
+ type: string;
353
353
  networkClientId: string;
354
354
  };
355
355
  fallbacks: string[];
@@ -380,18 +380,18 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
380
380
  assets: import("@metamask/superstruct").Struct<{
381
381
  native: {
382
382
  symbol: string;
383
- name: string;
384
383
  assetId: string;
385
384
  imageUrl: string;
385
+ name: string;
386
386
  decimals: number;
387
387
  coingeckoCoinId?: string | undefined;
388
388
  };
389
389
  listUrl?: string | undefined;
390
390
  governance?: {
391
391
  symbol: string;
392
- name: string;
393
392
  assetId: string;
394
393
  imageUrl: string;
394
+ name: string;
395
395
  decimals: number;
396
396
  coingeckoCoinId?: string | undefined;
397
397
  } | undefined;
@@ -399,9 +399,9 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
399
399
  listUrl: import("@metamask/superstruct").Struct<string | undefined, null>;
400
400
  native: import("@metamask/superstruct").Struct<{
401
401
  symbol: string;
402
- name: string;
403
402
  assetId: string;
404
403
  imageUrl: string;
404
+ name: string;
405
405
  decimals: number;
406
406
  coingeckoCoinId?: string | undefined;
407
407
  }, {
@@ -414,9 +414,9 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
414
414
  }>;
415
415
  governance: import("@metamask/superstruct").Struct<{
416
416
  symbol: string;
417
- name: string;
418
417
  assetId: string;
419
418
  imageUrl: string;
419
+ name: string;
420
420
  decimals: number;
421
421
  coingeckoCoinId?: string | undefined;
422
422
  } | undefined, {
@@ -430,15 +430,15 @@ export declare const RegistryConfigApiResponseSchema: import("@metamask/superstr
430
430
  }>;
431
431
  rpcProviders: import("@metamask/superstruct").Struct<{
432
432
  default: {
433
- type: string;
434
433
  url: string;
434
+ type: string;
435
435
  networkClientId: string;
436
436
  };
437
437
  fallbacks: string[];
438
438
  }, {
439
439
  default: import("@metamask/superstruct").Struct<{
440
- type: string;
441
440
  url: string;
441
+ type: string;
442
442
  networkClientId: string;
443
443
  }, {
444
444
  url: import("@metamask/superstruct").Struct<string, null>;