@metamask-previews/geolocation-controller 1.0.0-preview-8bfa290fb → 2.0.0-preview-0a30e47

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 (64) hide show
  1. package/CHANGELOG.md +15 -1
  2. package/dist/{GeolocationController-method-action-types.d.mts → GeolocationController-method-action-types.d.ts} +2 -2
  3. package/dist/GeolocationController-method-action-types.d.ts.map +1 -0
  4. package/dist/{GeolocationController-method-action-types.mjs → GeolocationController-method-action-types.js} +1 -1
  5. package/dist/GeolocationController-method-action-types.js.map +1 -0
  6. package/dist/{GeolocationController.d.cts → GeolocationController.d.ts} +8 -8
  7. package/dist/GeolocationController.d.ts.map +1 -0
  8. package/dist/{GeolocationController.mjs → GeolocationController.js} +40 -48
  9. package/dist/GeolocationController.js.map +1 -0
  10. package/dist/geolocation-api-service/{geolocation-api-service-method-action-types.d.cts → geolocation-api-service-method-action-types.d.ts} +2 -2
  11. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.ts.map +1 -0
  12. package/dist/geolocation-api-service/{geolocation-api-service-method-action-types.mjs → geolocation-api-service-method-action-types.js} +1 -1
  13. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.js.map +1 -0
  14. package/dist/geolocation-api-service/{geolocation-api-service.d.cts → geolocation-api-service.d.ts} +6 -6
  15. package/dist/geolocation-api-service/geolocation-api-service.d.ts.map +1 -0
  16. package/dist/geolocation-api-service/{geolocation-api-service.mjs → geolocation-api-service.js} +73 -77
  17. package/dist/geolocation-api-service/geolocation-api-service.js.map +1 -0
  18. package/dist/index.d.ts +9 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +4 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/{types.d.mts → types.d.ts} +1 -1
  23. package/dist/types.d.ts.map +1 -0
  24. package/dist/{types.mjs → types.js} +1 -1
  25. package/dist/types.js.map +1 -0
  26. package/package.json +15 -19
  27. package/dist/GeolocationController-method-action-types.cjs +0 -7
  28. package/dist/GeolocationController-method-action-types.cjs.map +0 -1
  29. package/dist/GeolocationController-method-action-types.d.cts +0 -54
  30. package/dist/GeolocationController-method-action-types.d.cts.map +0 -1
  31. package/dist/GeolocationController-method-action-types.d.mts.map +0 -1
  32. package/dist/GeolocationController-method-action-types.mjs.map +0 -1
  33. package/dist/GeolocationController.cjs +0 -214
  34. package/dist/GeolocationController.cjs.map +0 -1
  35. package/dist/GeolocationController.d.cts.map +0 -1
  36. package/dist/GeolocationController.d.mts +0 -140
  37. package/dist/GeolocationController.d.mts.map +0 -1
  38. package/dist/GeolocationController.mjs.map +0 -1
  39. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs +0 -7
  40. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs.map +0 -1
  41. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts.map +0 -1
  42. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts +0 -42
  43. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts.map +0 -1
  44. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs.map +0 -1
  45. package/dist/geolocation-api-service/geolocation-api-service.cjs +0 -305
  46. package/dist/geolocation-api-service/geolocation-api-service.cjs.map +0 -1
  47. package/dist/geolocation-api-service/geolocation-api-service.d.cts.map +0 -1
  48. package/dist/geolocation-api-service/geolocation-api-service.d.mts +0 -175
  49. package/dist/geolocation-api-service/geolocation-api-service.d.mts.map +0 -1
  50. package/dist/geolocation-api-service/geolocation-api-service.mjs.map +0 -1
  51. package/dist/index.cjs +0 -14
  52. package/dist/index.cjs.map +0 -1
  53. package/dist/index.d.cts +0 -9
  54. package/dist/index.d.cts.map +0 -1
  55. package/dist/index.d.mts +0 -9
  56. package/dist/index.d.mts.map +0 -1
  57. package/dist/index.mjs +0 -4
  58. package/dist/index.mjs.map +0 -1
  59. package/dist/types.cjs +0 -13
  60. package/dist/types.cjs.map +0 -1
  61. package/dist/types.d.cts +0 -13
  62. package/dist/types.d.cts.map +0 -1
  63. package/dist/types.d.mts.map +0 -1
  64. package/dist/types.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAChC,MAAM,GACN,SAAS,GACT,UAAU,GACV,OAAO,CAAC;AAEZ;;GAEG;AACH,oBAAY,GAAG;IACb,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ"}
@@ -7,4 +7,4 @@ export var Env;
7
7
  Env["UAT"] = "uat";
8
8
  Env["PRD"] = "prd";
9
9
  })(Env || (Env = {}));
10
- //# sourceMappingURL=types.mjs.map
10
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,MAAM,CAAN,IAAY,GAIX;AAJD,WAAY,GAAG;IACb,kBAAW,CAAA;IACX,kBAAW,CAAA;IACX,kBAAW,CAAA;AACb,CAAC,EAJW,GAAG,KAAH,GAAG,QAId","sourcesContent":["/**\n * The status of a geolocation fetch operation.\n */\nexport type GeolocationRequestStatus =\n | 'idle'\n | 'loading'\n | 'complete'\n | 'error';\n\n/**\n * Deployment environment for API endpoint selection.\n */\nexport enum Env {\n DEV = 'dev',\n UAT = 'uat',\n PRD = 'prd',\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/geolocation-controller",
3
- "version": "1.0.0-preview-8bfa290fb",
3
+ "version": "2.0.0-preview-0a30e47",
4
4
  "description": "Centralised geolocation controller with TTL caching and request deduplication",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -18,19 +18,12 @@
18
18
  "files": [
19
19
  "dist/"
20
20
  ],
21
+ "type": "module",
21
22
  "sideEffects": false,
22
- "main": "./dist/index.cjs",
23
- "types": "./dist/index.d.cts",
24
23
  "exports": {
25
24
  ".": {
26
- "import": {
27
- "types": "./dist/index.d.mts",
28
- "default": "./dist/index.mjs"
29
- },
30
- "require": {
31
- "types": "./dist/index.d.cts",
32
- "default": "./dist/index.cjs"
33
- }
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
34
27
  },
35
28
  "./package.json": "./package.json"
36
29
  },
@@ -39,9 +32,11 @@
39
32
  "registry": "https://registry.npmjs.org/"
40
33
  },
41
34
  "scripts": {
42
- "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
43
- "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
35
+ "build": "tsc --project tsconfig.build.json",
36
+ "build:all": "tsc --build tsconfig.build.json --verbose",
37
+ "build:clean": "yarn build:only-clean && yarn build",
44
38
  "build:docs": "typedoc",
39
+ "build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
45
40
  "changelog:update": "../../scripts/update-changelog.sh @metamask/geolocation-controller",
46
41
  "changelog:validate": "../../scripts/validate-changelog.sh @metamask/geolocation-controller",
47
42
  "lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
@@ -55,23 +50,24 @@
55
50
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
56
51
  },
57
52
  "dependencies": {
58
- "@metamask/base-controller": "^9.1.0",
59
- "@metamask/controller-utils": "^12.3.0",
60
- "@metamask/messenger": "^2.0.0"
53
+ "@metamask/base-controller": "^10.0.0",
54
+ "@metamask/controller-utils": "^13.0.0",
55
+ "@metamask/messenger": "^3.0.0"
61
56
  },
62
57
  "devDependencies": {
63
58
  "@metamask/auto-changelog": "^6.1.0",
64
- "@ts-bridge/cli": "^0.6.4",
65
59
  "@types/jest": "^30.0.0",
60
+ "@typescript/native": "npm:typescript@^7.0.2",
66
61
  "deepmerge": "^4.2.2",
67
62
  "jest": "^30.4.2",
63
+ "rimraf": "^5.0.5",
68
64
  "ts-jest": "^29.4.11",
69
65
  "tsx": "^4.20.5",
70
66
  "typedoc": "^0.25.13",
71
67
  "typedoc-plugin-missing-exports": "^2.0.0",
72
- "typescript": "~5.3.3"
68
+ "typescript": "npm:@typescript/typescript6@^6.0.2"
73
69
  },
74
70
  "engines": {
75
- "node": "^18.18 || >=20"
71
+ "node": "^22.14.0 || ^24"
76
72
  }
77
73
  }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is auto generated.
4
- * Do not edit manually.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- //# sourceMappingURL=GeolocationController-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController-method-action-types.cjs","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { GeolocationController } from './GeolocationController.js';\n\n/**\n * Returns the geolocation code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Returns the country, region, and timezone for the current client.\n * Delegates to the {@link GeolocationApiService} for network fetching and\n * caching, then updates controller state with the result.\n *\n * Unlike {@link getGeolocation}, this rejects when resolution fails instead\n * of returning a stale value, so callers can distinguish a fresh result from\n * a failed lookup (and, for example, omit location rather than enrich with a\n * previous session's data).\n *\n * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\n * @throws When the geolocation service fails to resolve.\n */\nexport type GeolocationControllerGetGeolocationDataAction = {\n type: `GeolocationController:getGeolocationData`;\n handler: GeolocationController['getGeolocationData'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerRefreshGeolocationAction = {\n type: `GeolocationController:refreshGeolocation`;\n handler: GeolocationController['refreshGeolocation'];\n};\n\n/**\n * Union of all GeolocationController action types.\n */\nexport type GeolocationControllerMethodActions =\n | GeolocationControllerGetGeolocationAction\n | GeolocationControllerGetGeolocationDataAction\n | GeolocationControllerRefreshGeolocationAction;\n"]}
@@ -1,54 +0,0 @@
1
- /**
2
- * This file is auto generated.
3
- * Do not edit manually.
4
- */
5
- import type { GeolocationController } from "./GeolocationController.cjs";
6
- /**
7
- * Returns the geolocation code. Delegates to the
8
- * {@link GeolocationApiService} for network fetching and caching, then
9
- * updates controller state with the result.
10
- *
11
- * Best-effort: if the fetch fails, the last known location code (or
12
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
13
- *
14
- * @returns The ISO 3166-2 location code string.
15
- */
16
- export type GeolocationControllerGetGeolocationAction = {
17
- type: `GeolocationController:getGeolocation`;
18
- handler: GeolocationController['getGeolocation'];
19
- };
20
- /**
21
- * Returns the country, region, and timezone for the current client.
22
- * Delegates to the {@link GeolocationApiService} for network fetching and
23
- * caching, then updates controller state with the result.
24
- *
25
- * Unlike {@link getGeolocation}, this rejects when resolution fails instead
26
- * of returning a stale value, so callers can distinguish a fresh result from
27
- * a failed lookup (and, for example, omit location rather than enrich with a
28
- * previous session's data).
29
- *
30
- * @returns The geolocation data, where each field is `null` when it could
31
- * not be determined.
32
- * @throws When the geolocation service fails to resolve.
33
- */
34
- export type GeolocationControllerGetGeolocationDataAction = {
35
- type: `GeolocationController:getGeolocationData`;
36
- handler: GeolocationController['getGeolocationData'];
37
- };
38
- /**
39
- * Forces a fresh geolocation fetch, bypassing the service's cache.
40
- *
41
- * Best-effort: if the fetch fails, the last known location code (or
42
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
43
- *
44
- * @returns The ISO 3166-2 location code string.
45
- */
46
- export type GeolocationControllerRefreshGeolocationAction = {
47
- type: `GeolocationController:refreshGeolocation`;
48
- handler: GeolocationController['refreshGeolocation'];
49
- };
50
- /**
51
- * Union of all GeolocationController action types.
52
- */
53
- export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerGetGeolocationDataAction | GeolocationControllerRefreshGeolocationAction;
54
- //# sourceMappingURL=GeolocationController-method-action-types.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController-method-action-types.d.cts","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,oCAAmC;AAExE;;;;;;;;;GASG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAC1C,yCAAyC,GACzC,6CAA6C,GAC7C,6CAA6C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController-method-action-types.d.mts","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,oCAAmC;AAExE;;;;;;;;;GASG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAC1C,yCAAyC,GACzC,6CAA6C,GAC7C,6CAA6C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController-method-action-types.mjs","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { GeolocationController } from './GeolocationController.js';\n\n/**\n * Returns the geolocation code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Returns the country, region, and timezone for the current client.\n * Delegates to the {@link GeolocationApiService} for network fetching and\n * caching, then updates controller state with the result.\n *\n * Unlike {@link getGeolocation}, this rejects when resolution fails instead\n * of returning a stale value, so callers can distinguish a fresh result from\n * a failed lookup (and, for example, omit location rather than enrich with a\n * previous session's data).\n *\n * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\n * @throws When the geolocation service fails to resolve.\n */\nexport type GeolocationControllerGetGeolocationDataAction = {\n type: `GeolocationController:getGeolocationData`;\n handler: GeolocationController['getGeolocationData'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerRefreshGeolocationAction = {\n type: `GeolocationController:refreshGeolocation`;\n handler: GeolocationController['refreshGeolocation'];\n};\n\n/**\n * Union of all GeolocationController action types.\n */\nexport type GeolocationControllerMethodActions =\n | GeolocationControllerGetGeolocationAction\n | GeolocationControllerGetGeolocationDataAction\n | GeolocationControllerRefreshGeolocationAction;\n"]}
@@ -1,214 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- 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");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var _GeolocationController_instances, _GeolocationController_fetchAndUpdate;
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.GeolocationController = exports.getDefaultGeolocationControllerState = exports.controllerName = void 0;
10
- const base_controller_1 = require("@metamask/base-controller");
11
- const geolocation_api_service_js_1 = require("./geolocation-api-service/geolocation-api-service.cjs");
12
- /**
13
- * The name of the {@link GeolocationController}, used to namespace the
14
- * controller's actions and events and to namespace the controller's state data
15
- * when composed with other controllers.
16
- */
17
- exports.controllerName = 'GeolocationController';
18
- /**
19
- * The metadata for each property in {@link GeolocationControllerState}.
20
- */
21
- const geolocationControllerMetadata = {
22
- location: {
23
- persist: false,
24
- includeInDebugSnapshot: true,
25
- includeInStateLogs: true,
26
- usedInUi: true,
27
- },
28
- country: {
29
- persist: false,
30
- includeInDebugSnapshot: true,
31
- includeInStateLogs: true,
32
- usedInUi: true,
33
- },
34
- region: {
35
- persist: false,
36
- includeInDebugSnapshot: true,
37
- includeInStateLogs: true,
38
- usedInUi: true,
39
- },
40
- timezone: {
41
- persist: false,
42
- includeInDebugSnapshot: true,
43
- includeInStateLogs: true,
44
- usedInUi: true,
45
- },
46
- status: {
47
- persist: false,
48
- includeInDebugSnapshot: true,
49
- includeInStateLogs: true,
50
- usedInUi: true,
51
- },
52
- lastFetchedAt: {
53
- persist: false,
54
- includeInDebugSnapshot: true,
55
- includeInStateLogs: true,
56
- usedInUi: false,
57
- },
58
- error: {
59
- persist: false,
60
- includeInDebugSnapshot: true,
61
- includeInStateLogs: true,
62
- usedInUi: false,
63
- },
64
- };
65
- /**
66
- * Constructs the default {@link GeolocationController} state. This allows
67
- * consumers to provide a partial state object when initializing the controller
68
- * and also helps in constructing complete state objects for this controller in
69
- * tests.
70
- *
71
- * @returns The default {@link GeolocationController} state.
72
- */
73
- function getDefaultGeolocationControllerState() {
74
- return {
75
- location: geolocation_api_service_js_1.UNKNOWN_LOCATION,
76
- ...(0, geolocation_api_service_js_1.getUnknownGeolocationData)(),
77
- status: 'idle',
78
- lastFetchedAt: null,
79
- error: null,
80
- };
81
- }
82
- exports.getDefaultGeolocationControllerState = getDefaultGeolocationControllerState;
83
- const MESSENGER_EXPOSED_METHODS = [
84
- 'getGeolocation',
85
- 'getGeolocationData',
86
- 'refreshGeolocation',
87
- ];
88
- /**
89
- * GeolocationController manages UI-facing geolocation state by delegating
90
- * the actual API interaction to {@link GeolocationApiService} via the
91
- * messenger.
92
- *
93
- * The service (registered externally as
94
- * `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response
95
- * validation, TTL caching, and promise deduplication. This controller focuses
96
- * on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and
97
- * exposes `getGeolocation` / `refreshGeolocation` as messenger actions.
98
- */
99
- class GeolocationController extends base_controller_1.BaseController {
100
- /**
101
- * Constructs a new {@link GeolocationController}.
102
- *
103
- * @param args - The arguments to this controller.
104
- * @param args.messenger - The messenger suited for this controller. Must
105
- * have a `GeolocationApiService:fetchGeolocation` action handler registered.
106
- * @param args.state - Optional partial initial state.
107
- */
108
- constructor({ messenger, state }) {
109
- super({
110
- messenger,
111
- metadata: geolocationControllerMetadata,
112
- name: exports.controllerName,
113
- state: { ...getDefaultGeolocationControllerState(), ...state },
114
- });
115
- _GeolocationController_instances.add(this);
116
- this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
117
- }
118
- /**
119
- * Returns the geolocation code. Delegates to the
120
- * {@link GeolocationApiService} for network fetching and caching, then
121
- * updates controller state with the result.
122
- *
123
- * Best-effort: if the fetch fails, the last known location code (or
124
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
125
- *
126
- * @returns The ISO 3166-2 location code string.
127
- */
128
- async getGeolocation() {
129
- try {
130
- await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
131
- }
132
- catch {
133
- // Best-effort: fall back to the last known location code below.
134
- }
135
- return this.state.location;
136
- }
137
- /**
138
- * Returns the country, region, and timezone for the current client.
139
- * Delegates to the {@link GeolocationApiService} for network fetching and
140
- * caching, then updates controller state with the result.
141
- *
142
- * Unlike {@link getGeolocation}, this rejects when resolution fails instead
143
- * of returning a stale value, so callers can distinguish a fresh result from
144
- * a failed lookup (and, for example, omit location rather than enrich with a
145
- * previous session's data).
146
- *
147
- * @returns The geolocation data, where each field is `null` when it could
148
- * not be determined.
149
- * @throws When the geolocation service fails to resolve.
150
- */
151
- async getGeolocationData() {
152
- return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
153
- }
154
- /**
155
- * Forces a fresh geolocation fetch, bypassing the service's cache.
156
- *
157
- * Best-effort: if the fetch fails, the last known location code (or
158
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
159
- *
160
- * @returns The ISO 3166-2 location code string.
161
- */
162
- async refreshGeolocation() {
163
- this.update((draft) => {
164
- draft.lastFetchedAt = null;
165
- });
166
- try {
167
- await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this, { bypassCache: true });
168
- }
169
- catch {
170
- // Best-effort: fall back to the last known location code below.
171
- }
172
- return this.state.location;
173
- }
174
- }
175
- exports.GeolocationController = GeolocationController;
176
- _GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAndUpdate =
177
- /**
178
- * Calls the geolocation service and updates controller state with the
179
- * result.
180
- *
181
- * @param options - Options forwarded to the service.
182
- * @param options.bypassCache - When true, the service skips its TTL cache.
183
- * @returns The resolved geolocation data.
184
- * @throws Re-throws the service error after recording it in state, so
185
- * callers can react to a failed lookup instead of receiving a stale value.
186
- */
187
- async function _GeolocationController_fetchAndUpdate(options) {
188
- this.update((draft) => {
189
- draft.status = 'loading';
190
- draft.error = null;
191
- });
192
- try {
193
- const geolocation = await this.messenger.call('GeolocationApiService:fetchGeolocationData', options);
194
- this.update((draft) => {
195
- draft.location = (0, geolocation_api_service_js_1.toLocationCode)(geolocation);
196
- draft.country = geolocation.country;
197
- draft.region = geolocation.region;
198
- draft.timezone = geolocation.timezone;
199
- draft.status = 'complete';
200
- draft.lastFetchedAt = Date.now();
201
- draft.error = null;
202
- });
203
- return geolocation;
204
- }
205
- catch (error) {
206
- const message = error instanceof Error ? error.message : String(error);
207
- this.update((draft) => {
208
- draft.status = 'error';
209
- draft.error = message;
210
- });
211
- throw error;
212
- }
213
- };
214
- //# sourceMappingURL=GeolocationController.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController.cjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAK3D,sGAI8D;AAI9D;;;;GAIG;AACU,QAAA,cAAc,GAAG,uBAAuB,CAAC;AAsBtD;;GAEG;AACH,MAAM,6BAA6B,GAAG;IACpC,QAAQ,EAAE;QACR,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,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IAAI,CAAC;QACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,4CAA4C,EAC5C,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,QAAQ,GAAG,IAAA,2CAAc,EAAC,WAAW,CAAC,CAAC;YAC7C,KAAK,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC;YACpC,KAAK,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC;YAClC,KAAK,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;YACtC,KAAK,CAAC,MAAM,GAAG,UAAU,CAAC;YAC1B,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACjC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;QACrB,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEvE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC;YACvB,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC;QACxB,CAAC,CAAC,CAAC;QAEH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { GeolocationApiServiceFetchGeolocationDataAction } from './geolocation-api-service/geolocation-api-service-method-action-types.js';\nimport type { GeolocationData } from './geolocation-api-service/geolocation-api-service.js';\nimport {\n getUnknownGeolocationData,\n toLocationCode,\n UNKNOWN_LOCATION,\n} from './geolocation-api-service/geolocation-api-service.js';\nimport type { GeolocationControllerMethodActions } from './GeolocationController-method-action-types.js';\nimport type { GeolocationRequestStatus } from './types.js';\n\n/**\n * The name of the {@link GeolocationController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'GeolocationController';\n\n/**\n * State for the {@link GeolocationController}.\n */\nexport type GeolocationControllerState = {\n /** ISO 3166-2 location code (e.g. \"US\", \"US-NY\", \"CA-ON\"), or \"UNKNOWN\" if not yet determined. */\n location: string;\n /** ISO 3166-1 alpha-2 country code (e.g. \"US\"), or null if not yet determined. */\n country: string | null;\n /** ISO 3166-2 subdivision code without the country prefix (e.g. \"NY\"), or null if not yet determined. */\n region: string | null;\n /** IANA time zone name (e.g. \"America/Los_Angeles\"), or null if not yet determined. */\n timezone: string | null;\n /** Current status of the geolocation fetch lifecycle. */\n status: GeolocationRequestStatus;\n /** Epoch milliseconds of the last successful fetch, or null if never fetched. */\n lastFetchedAt: number | null;\n /** Last error message, or null if no error has occurred. */\n error: string | null;\n};\n\n/**\n * The metadata for each property in {@link GeolocationControllerState}.\n */\nconst geolocationControllerMetadata = {\n location: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n country: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n region: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n timezone: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n status: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n lastFetchedAt: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n error: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<GeolocationControllerState>;\n\n/**\n * Constructs the default {@link GeolocationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link GeolocationController} state.\n */\nexport function getDefaultGeolocationControllerState(): GeolocationControllerState {\n return {\n location: UNKNOWN_LOCATION,\n ...getUnknownGeolocationData(),\n status: 'idle',\n lastFetchedAt: null,\n error: null,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getGeolocation',\n 'getGeolocationData',\n 'refreshGeolocation',\n] as const;\n\n/**\n * Retrieves the state of the {@link GeolocationController}.\n */\nexport type GeolocationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Actions that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerActions =\n | GeolocationControllerGetStateAction\n | GeolocationControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link GeolocationControllerMessenger} calls.\n */\ntype AllowedActions = GeolocationApiServiceFetchGeolocationDataAction;\n\n/**\n * Published when the state of {@link GeolocationController} changes.\n */\nexport type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Events that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link GeolocationControllerMessenger}\n * subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link GeolocationController}.\n */\nexport type GeolocationControllerMessenger = Messenger<\n typeof controllerName,\n GeolocationControllerActions | AllowedActions,\n GeolocationControllerEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing the {@link GeolocationController}.\n */\nexport type GeolocationControllerOptions = {\n /** The messenger for inter-controller communication. */\n messenger: GeolocationControllerMessenger;\n /** Optional partial initial state. */\n state?: Partial<GeolocationControllerState>;\n};\n\n/**\n * GeolocationController manages UI-facing geolocation state by delegating\n * the actual API interaction to {@link GeolocationApiService} via the\n * messenger.\n *\n * The service (registered externally as\n * `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response\n * validation, TTL caching, and promise deduplication. This controller focuses\n * on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and\n * exposes `getGeolocation` / `refreshGeolocation` as messenger actions.\n */\nexport class GeolocationController extends BaseController<\n typeof controllerName,\n GeolocationControllerState,\n GeolocationControllerMessenger\n> {\n /**\n * Constructs a new {@link GeolocationController}.\n *\n * @param args - The arguments to this controller.\n * @param args.messenger - The messenger suited for this controller. Must\n * have a `GeolocationApiService:fetchGeolocation` action handler registered.\n * @param args.state - Optional partial initial state.\n */\n constructor({ messenger, state }: GeolocationControllerOptions) {\n super({\n messenger,\n metadata: geolocationControllerMetadata,\n name: controllerName,\n state: { ...getDefaultGeolocationControllerState(), ...state },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Returns the geolocation code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\n async getGeolocation(): Promise<string> {\n try {\n await this.#fetchAndUpdate();\n } catch {\n // Best-effort: fall back to the last known location code below.\n }\n return this.state.location;\n }\n\n /**\n * Returns the country, region, and timezone for the current client.\n * Delegates to the {@link GeolocationApiService} for network fetching and\n * caching, then updates controller state with the result.\n *\n * Unlike {@link getGeolocation}, this rejects when resolution fails instead\n * of returning a stale value, so callers can distinguish a fresh result from\n * a failed lookup (and, for example, omit location rather than enrich with a\n * previous session's data).\n *\n * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\n * @throws When the geolocation service fails to resolve.\n */\n async getGeolocationData(): Promise<GeolocationData> {\n return this.#fetchAndUpdate();\n }\n\n /**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\n async refreshGeolocation(): Promise<string> {\n this.update((draft) => {\n draft.lastFetchedAt = null;\n });\n try {\n await this.#fetchAndUpdate({ bypassCache: true });\n } catch {\n // Best-effort: fall back to the last known location code below.\n }\n return this.state.location;\n }\n\n /**\n * Calls the geolocation service and updates controller state with the\n * result.\n *\n * @param options - Options forwarded to the service.\n * @param options.bypassCache - When true, the service skips its TTL cache.\n * @returns The resolved geolocation data.\n * @throws Re-throws the service error after recording it in state, so\n * callers can react to a failed lookup instead of receiving a stale value.\n */\n async #fetchAndUpdate(options?: {\n bypassCache?: boolean;\n }): Promise<GeolocationData> {\n this.update((draft) => {\n draft.status = 'loading';\n draft.error = null;\n });\n\n try {\n const geolocation = await this.messenger.call(\n 'GeolocationApiService:fetchGeolocationData',\n options,\n );\n\n this.update((draft) => {\n draft.location = toLocationCode(geolocation);\n draft.country = geolocation.country;\n draft.region = geolocation.region;\n draft.timezone = geolocation.timezone;\n draft.status = 'complete';\n draft.lastFetchedAt = Date.now();\n draft.error = null;\n });\n\n return geolocation;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n\n this.update((draft) => {\n draft.status = 'error';\n draft.error = message;\n });\n\n throw error;\n }\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController.d.cts","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,+CAA+C,EAAE,kFAAiF;AAChJ,OAAO,KAAK,EAAE,eAAe,EAAE,8DAA6D;AAM5F,OAAO,KAAK,EAAE,kCAAkC,EAAE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C,wDAAwD;IACxD,SAAS,EAAE,8BAA8B,CAAC;IAC1C,sCAAsC;IACtC,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;CAC7C,CAAC;AAEF;;;;;;;;;;GAUG;AACH,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IACC;;;;;;;OAOG;gBACS,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,4BAA4B;IAc9D;;;;;;;;;OASG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IASvC;;;;;;;;;;;;;OAaG;IACG,kBAAkB,IAAI,OAAO,CAAC,eAAe,CAAC;IAIpD;;;;;;;OAOG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;CA0D5C"}
@@ -1,140 +0,0 @@
1
- import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
2
- import { BaseController } from "@metamask/base-controller";
3
- import type { Messenger } from "@metamask/messenger";
4
- import type { GeolocationApiServiceFetchGeolocationDataAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.mjs";
5
- import type { GeolocationData } from "./geolocation-api-service/geolocation-api-service.mjs";
6
- import type { GeolocationControllerMethodActions } from "./GeolocationController-method-action-types.mjs";
7
- import type { GeolocationRequestStatus } from "./types.mjs";
8
- /**
9
- * The name of the {@link GeolocationController}, used to namespace the
10
- * controller's actions and events and to namespace the controller's state data
11
- * when composed with other controllers.
12
- */
13
- export declare const controllerName = "GeolocationController";
14
- /**
15
- * State for the {@link GeolocationController}.
16
- */
17
- export type GeolocationControllerState = {
18
- /** ISO 3166-2 location code (e.g. "US", "US-NY", "CA-ON"), or "UNKNOWN" if not yet determined. */
19
- location: string;
20
- /** ISO 3166-1 alpha-2 country code (e.g. "US"), or null if not yet determined. */
21
- country: string | null;
22
- /** ISO 3166-2 subdivision code without the country prefix (e.g. "NY"), or null if not yet determined. */
23
- region: string | null;
24
- /** IANA time zone name (e.g. "America/Los_Angeles"), or null if not yet determined. */
25
- timezone: string | null;
26
- /** Current status of the geolocation fetch lifecycle. */
27
- status: GeolocationRequestStatus;
28
- /** Epoch milliseconds of the last successful fetch, or null if never fetched. */
29
- lastFetchedAt: number | null;
30
- /** Last error message, or null if no error has occurred. */
31
- error: string | null;
32
- };
33
- /**
34
- * Constructs the default {@link GeolocationController} state. This allows
35
- * consumers to provide a partial state object when initializing the controller
36
- * and also helps in constructing complete state objects for this controller in
37
- * tests.
38
- *
39
- * @returns The default {@link GeolocationController} state.
40
- */
41
- export declare function getDefaultGeolocationControllerState(): GeolocationControllerState;
42
- /**
43
- * Retrieves the state of the {@link GeolocationController}.
44
- */
45
- export type GeolocationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, GeolocationControllerState>;
46
- /**
47
- * Actions that {@link GeolocationControllerMessenger} exposes to other consumers.
48
- */
49
- export type GeolocationControllerActions = GeolocationControllerGetStateAction | GeolocationControllerMethodActions;
50
- /**
51
- * Actions from other messengers that {@link GeolocationControllerMessenger} calls.
52
- */
53
- type AllowedActions = GeolocationApiServiceFetchGeolocationDataAction;
54
- /**
55
- * Published when the state of {@link GeolocationController} changes.
56
- */
57
- export type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, GeolocationControllerState>;
58
- /**
59
- * Events that {@link GeolocationControllerMessenger} exposes to other consumers.
60
- */
61
- export type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;
62
- /**
63
- * Events from other messengers that {@link GeolocationControllerMessenger}
64
- * subscribes to.
65
- */
66
- type AllowedEvents = never;
67
- /**
68
- * The messenger restricted to actions and events accessed by
69
- * {@link GeolocationController}.
70
- */
71
- export type GeolocationControllerMessenger = Messenger<typeof controllerName, GeolocationControllerActions | AllowedActions, GeolocationControllerEvents | AllowedEvents>;
72
- /**
73
- * Options for constructing the {@link GeolocationController}.
74
- */
75
- export type GeolocationControllerOptions = {
76
- /** The messenger for inter-controller communication. */
77
- messenger: GeolocationControllerMessenger;
78
- /** Optional partial initial state. */
79
- state?: Partial<GeolocationControllerState>;
80
- };
81
- /**
82
- * GeolocationController manages UI-facing geolocation state by delegating
83
- * the actual API interaction to {@link GeolocationApiService} via the
84
- * messenger.
85
- *
86
- * The service (registered externally as
87
- * `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response
88
- * validation, TTL caching, and promise deduplication. This controller focuses
89
- * on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and
90
- * exposes `getGeolocation` / `refreshGeolocation` as messenger actions.
91
- */
92
- export declare class GeolocationController extends BaseController<typeof controllerName, GeolocationControllerState, GeolocationControllerMessenger> {
93
- #private;
94
- /**
95
- * Constructs a new {@link GeolocationController}.
96
- *
97
- * @param args - The arguments to this controller.
98
- * @param args.messenger - The messenger suited for this controller. Must
99
- * have a `GeolocationApiService:fetchGeolocation` action handler registered.
100
- * @param args.state - Optional partial initial state.
101
- */
102
- constructor({ messenger, state }: GeolocationControllerOptions);
103
- /**
104
- * Returns the geolocation code. Delegates to the
105
- * {@link GeolocationApiService} for network fetching and caching, then
106
- * updates controller state with the result.
107
- *
108
- * Best-effort: if the fetch fails, the last known location code (or
109
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
110
- *
111
- * @returns The ISO 3166-2 location code string.
112
- */
113
- getGeolocation(): Promise<string>;
114
- /**
115
- * Returns the country, region, and timezone for the current client.
116
- * Delegates to the {@link GeolocationApiService} for network fetching and
117
- * caching, then updates controller state with the result.
118
- *
119
- * Unlike {@link getGeolocation}, this rejects when resolution fails instead
120
- * of returning a stale value, so callers can distinguish a fresh result from
121
- * a failed lookup (and, for example, omit location rather than enrich with a
122
- * previous session's data).
123
- *
124
- * @returns The geolocation data, where each field is `null` when it could
125
- * not be determined.
126
- * @throws When the geolocation service fails to resolve.
127
- */
128
- getGeolocationData(): Promise<GeolocationData>;
129
- /**
130
- * Forces a fresh geolocation fetch, bypassing the service's cache.
131
- *
132
- * Best-effort: if the fetch fails, the last known location code (or
133
- * {@link UNKNOWN_LOCATION}) is returned rather than throwing.
134
- *
135
- * @returns The ISO 3166-2 location code string.
136
- */
137
- refreshGeolocation(): Promise<string>;
138
- }
139
- export {};
140
- //# sourceMappingURL=GeolocationController.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController.d.mts","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,+CAA+C,EAAE,kFAAiF;AAChJ,OAAO,KAAK,EAAE,eAAe,EAAE,8DAA6D;AAM5F,OAAO,KAAK,EAAE,kCAAkC,EAAE,wDAAuD;AACzG,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAmB;AAE3D;;;;GAIG;AACH,eAAO,MAAM,cAAc,0BAA0B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,kGAAkG;IAClG,QAAQ,EAAE,MAAM,CAAC;IACjB,kFAAkF;IAClF,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,yGAAyG;IACzG,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,uFAAuF;IACvF,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,yDAAyD;IACzD,MAAM,EAAE,wBAAwB,CAAC;IACjC,iFAAiF;IACjF,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,4DAA4D;IAC5D,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAkDF;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,IAAI,0BAA0B,CAQjF;AAQD;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG,wBAAwB,CACxE,OAAO,cAAc,EACrB,0BAA0B,CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,4BAA4B,GACpC,mCAAmC,GACnC,kCAAkC,CAAC;AAEvC;;GAEG;AACH,KAAK,cAAc,GAAG,+CAA+C,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,qCAAqC,GAAG,0BAA0B,CAC5E,OAAO,cAAc,EACrB,0BAA0B,CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG,qCAAqC,CAAC;AAEhF;;;GAGG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,8BAA8B,GAAG,SAAS,CACpD,OAAO,cAAc,EACrB,4BAA4B,GAAG,cAAc,EAC7C,2BAA2B,GAAG,aAAa,CAC5C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,4BAA4B,GAAG;IACzC,wDAAwD;IACxD,SAAS,EAAE,8BAA8B,CAAC;IAC1C,sCAAsC;IACtC,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;CAC7C,CAAC;AAEF;;;;;;;;;;GAUG;AACH,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IACC;;;;;;;OAOG;gBACS,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,4BAA4B;IAc9D;;;;;;;;;OASG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IASvC;;;;;;;;;;;;;OAaG;IACG,kBAAkB,IAAI,OAAO,CAAC,eAAe,CAAC;IAIpD;;;;;;;OAOG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;CA0D5C"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"GeolocationController.mjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,EACL,yBAAyB,EACzB,cAAc,EACd,gBAAgB,EACjB,8DAA6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;IACC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC;QACzB,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAC3C,4CAA4C,EAC5C,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,QAAQ,GAAG,cAAc,CAAC,WAAW,CAAC,CAAC;YAC7C,KAAK,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC;YACpC,KAAK,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC;YAClC,KAAK,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;YACtC,KAAK,CAAC,MAAM,GAAG,UAAU,CAAC;YAC1B,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACjC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;QACrB,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEvE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC;YACvB,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC;QACxB,CAAC,CAAC,CAAC;QAEH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { GeolocationApiServiceFetchGeolocationDataAction } from './geolocation-api-service/geolocation-api-service-method-action-types.js';\nimport type { GeolocationData } from './geolocation-api-service/geolocation-api-service.js';\nimport {\n getUnknownGeolocationData,\n toLocationCode,\n UNKNOWN_LOCATION,\n} from './geolocation-api-service/geolocation-api-service.js';\nimport type { GeolocationControllerMethodActions } from './GeolocationController-method-action-types.js';\nimport type { GeolocationRequestStatus } from './types.js';\n\n/**\n * The name of the {@link GeolocationController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'GeolocationController';\n\n/**\n * State for the {@link GeolocationController}.\n */\nexport type GeolocationControllerState = {\n /** ISO 3166-2 location code (e.g. \"US\", \"US-NY\", \"CA-ON\"), or \"UNKNOWN\" if not yet determined. */\n location: string;\n /** ISO 3166-1 alpha-2 country code (e.g. \"US\"), or null if not yet determined. */\n country: string | null;\n /** ISO 3166-2 subdivision code without the country prefix (e.g. \"NY\"), or null if not yet determined. */\n region: string | null;\n /** IANA time zone name (e.g. \"America/Los_Angeles\"), or null if not yet determined. */\n timezone: string | null;\n /** Current status of the geolocation fetch lifecycle. */\n status: GeolocationRequestStatus;\n /** Epoch milliseconds of the last successful fetch, or null if never fetched. */\n lastFetchedAt: number | null;\n /** Last error message, or null if no error has occurred. */\n error: string | null;\n};\n\n/**\n * The metadata for each property in {@link GeolocationControllerState}.\n */\nconst geolocationControllerMetadata = {\n location: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n country: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n region: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n timezone: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n status: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n lastFetchedAt: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n error: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<GeolocationControllerState>;\n\n/**\n * Constructs the default {@link GeolocationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link GeolocationController} state.\n */\nexport function getDefaultGeolocationControllerState(): GeolocationControllerState {\n return {\n location: UNKNOWN_LOCATION,\n ...getUnknownGeolocationData(),\n status: 'idle',\n lastFetchedAt: null,\n error: null,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getGeolocation',\n 'getGeolocationData',\n 'refreshGeolocation',\n] as const;\n\n/**\n * Retrieves the state of the {@link GeolocationController}.\n */\nexport type GeolocationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Actions that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerActions =\n | GeolocationControllerGetStateAction\n | GeolocationControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link GeolocationControllerMessenger} calls.\n */\ntype AllowedActions = GeolocationApiServiceFetchGeolocationDataAction;\n\n/**\n * Published when the state of {@link GeolocationController} changes.\n */\nexport type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Events that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link GeolocationControllerMessenger}\n * subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link GeolocationController}.\n */\nexport type GeolocationControllerMessenger = Messenger<\n typeof controllerName,\n GeolocationControllerActions | AllowedActions,\n GeolocationControllerEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing the {@link GeolocationController}.\n */\nexport type GeolocationControllerOptions = {\n /** The messenger for inter-controller communication. */\n messenger: GeolocationControllerMessenger;\n /** Optional partial initial state. */\n state?: Partial<GeolocationControllerState>;\n};\n\n/**\n * GeolocationController manages UI-facing geolocation state by delegating\n * the actual API interaction to {@link GeolocationApiService} via the\n * messenger.\n *\n * The service (registered externally as\n * `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response\n * validation, TTL caching, and promise deduplication. This controller focuses\n * on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and\n * exposes `getGeolocation` / `refreshGeolocation` as messenger actions.\n */\nexport class GeolocationController extends BaseController<\n typeof controllerName,\n GeolocationControllerState,\n GeolocationControllerMessenger\n> {\n /**\n * Constructs a new {@link GeolocationController}.\n *\n * @param args - The arguments to this controller.\n * @param args.messenger - The messenger suited for this controller. Must\n * have a `GeolocationApiService:fetchGeolocation` action handler registered.\n * @param args.state - Optional partial initial state.\n */\n constructor({ messenger, state }: GeolocationControllerOptions) {\n super({\n messenger,\n metadata: geolocationControllerMetadata,\n name: controllerName,\n state: { ...getDefaultGeolocationControllerState(), ...state },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Returns the geolocation code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\n async getGeolocation(): Promise<string> {\n try {\n await this.#fetchAndUpdate();\n } catch {\n // Best-effort: fall back to the last known location code below.\n }\n return this.state.location;\n }\n\n /**\n * Returns the country, region, and timezone for the current client.\n * Delegates to the {@link GeolocationApiService} for network fetching and\n * caching, then updates controller state with the result.\n *\n * Unlike {@link getGeolocation}, this rejects when resolution fails instead\n * of returning a stale value, so callers can distinguish a fresh result from\n * a failed lookup (and, for example, omit location rather than enrich with a\n * previous session's data).\n *\n * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\n * @throws When the geolocation service fails to resolve.\n */\n async getGeolocationData(): Promise<GeolocationData> {\n return this.#fetchAndUpdate();\n }\n\n /**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * Best-effort: if the fetch fails, the last known location code (or\n * {@link UNKNOWN_LOCATION}) is returned rather than throwing.\n *\n * @returns The ISO 3166-2 location code string.\n */\n async refreshGeolocation(): Promise<string> {\n this.update((draft) => {\n draft.lastFetchedAt = null;\n });\n try {\n await this.#fetchAndUpdate({ bypassCache: true });\n } catch {\n // Best-effort: fall back to the last known location code below.\n }\n return this.state.location;\n }\n\n /**\n * Calls the geolocation service and updates controller state with the\n * result.\n *\n * @param options - Options forwarded to the service.\n * @param options.bypassCache - When true, the service skips its TTL cache.\n * @returns The resolved geolocation data.\n * @throws Re-throws the service error after recording it in state, so\n * callers can react to a failed lookup instead of receiving a stale value.\n */\n async #fetchAndUpdate(options?: {\n bypassCache?: boolean;\n }): Promise<GeolocationData> {\n this.update((draft) => {\n draft.status = 'loading';\n draft.error = null;\n });\n\n try {\n const geolocation = await this.messenger.call(\n 'GeolocationApiService:fetchGeolocationData',\n options,\n );\n\n this.update((draft) => {\n draft.location = toLocationCode(geolocation);\n draft.country = geolocation.country;\n draft.region = geolocation.region;\n draft.timezone = geolocation.timezone;\n draft.status = 'complete';\n draft.lastFetchedAt = Date.now();\n draft.error = null;\n });\n\n return geolocation;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n\n this.update((draft) => {\n draft.status = 'error';\n draft.error = message;\n });\n\n throw error;\n }\n }\n}\n"]}
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is auto generated.
4
- * Do not edit manually.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- //# sourceMappingURL=geolocation-api-service-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"geolocation-api-service-method-action-types.cjs","sourceRoot":"","sources":["../../src/geolocation-api-service/geolocation-api-service-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { GeolocationApiService } from './geolocation-api-service.js';\n\n/**\n * Returns the geolocation code. Serves from cache when the TTL has not\n * expired, otherwise performs a network fetch. Concurrent callers are\n * deduplicated to a single in-flight request.\n *\n * @param options - Optional fetch options.\n * @param options.bypassCache - When true, invalidates the TTL cache. If a\n * request is already in-flight it will be reused (deduplication always\n * applies).\n * @returns An ISO 3166-2 location code (e.g. `US`, `US-NY`, `CA-ON`), or\n * {@link UNKNOWN_LOCATION} when the API returns an empty or invalid body.\n */\nexport type GeolocationApiServiceFetchGeolocationAction = {\n type: `GeolocationApiService:fetchGeolocation`;\n handler: GeolocationApiService['fetchGeolocation'];\n};\n\n/**\n * Returns the country, region, and timezone for the current client. Serves\n * from cache when the TTL has not expired, otherwise performs a network\n * fetch. Concurrent callers are deduplicated to a single in-flight request.\n *\n * @param options - Optional fetch options.\n * @param options.bypassCache - When true, invalidates the TTL cache. If a\n * request is already in-flight it will be reused (deduplication always\n * applies).\n * @returns The geolocation data, where each field is `null` when the API\n * omits it or returns a value that fails validation.\n */\nexport type GeolocationApiServiceFetchGeolocationDataAction = {\n type: `GeolocationApiService:fetchGeolocationData`;\n handler: GeolocationApiService['fetchGeolocationData'];\n};\n\n/**\n * Union of all GeolocationApiService action types.\n */\nexport type GeolocationApiServiceMethodActions =\n | GeolocationApiServiceFetchGeolocationAction\n | GeolocationApiServiceFetchGeolocationDataAction;\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"geolocation-api-service-method-action-types.d.cts","sourceRoot":"","sources":["../../src/geolocation-api-service/geolocation-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAqC;AAE1E;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,2CAA2C,GAAG;IACxD,IAAI,EAAE,wCAAwC,CAAC;IAC/C,OAAO,EAAE,qBAAqB,CAAC,kBAAkB,CAAC,CAAC;CACpD,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,+CAA+C,GAAG;IAC5D,IAAI,EAAE,4CAA4C,CAAC;IACnD,OAAO,EAAE,qBAAqB,CAAC,sBAAsB,CAAC,CAAC;CACxD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAC1C,2CAA2C,GAC3C,+CAA+C,CAAC"}