@metamask-previews/geolocation-controller 0.1.3-preview-a1d0bbfa8 → 0.1.3-preview-d77c814bb
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 +2 -0
- package/dist/GeolocationController-method-action-types.cjs.map +1 -1
- package/dist/GeolocationController-method-action-types.d.cts +25 -1
- package/dist/GeolocationController-method-action-types.d.cts.map +1 -1
- package/dist/GeolocationController-method-action-types.d.mts +25 -1
- package/dist/GeolocationController-method-action-types.d.mts.map +1 -1
- package/dist/GeolocationController-method-action-types.mjs.map +1 -1
- package/dist/GeolocationController.cjs +66 -6
- package/dist/GeolocationController.cjs.map +1 -1
- package/dist/GeolocationController.d.cts +30 -2
- package/dist/GeolocationController.d.cts.map +1 -1
- package/dist/GeolocationController.d.mts +30 -2
- package/dist/GeolocationController.d.mts.map +1 -1
- package/dist/GeolocationController.mjs +67 -7
- package/dist/GeolocationController.mjs.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts +17 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts +17 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service.cjs +131 -20
- package/dist/geolocation-api-service/geolocation-api-service.cjs.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service.d.cts +52 -5
- package/dist/geolocation-api-service/geolocation-api-service.d.cts.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service.d.mts +52 -5
- package/dist/geolocation-api-service/geolocation-api-service.d.mts.map +1 -1
- package/dist/geolocation-api-service/geolocation-api-service.mjs +128 -19
- package/dist/geolocation-api-service/geolocation-api-service.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
+
- **BREAKING:** Resolve the user's country, region, and timezone via the `v2` geolocation endpoint through new `GeolocationController:getGeolocationData` and `GeolocationApiService:fetchGeolocationData` actions that return a `GeolocationData` object, along with matching `GeolocationControllerState` fields ([#9691](https://github.com/MetaMask/core/pull/9691))
|
|
13
|
+
- `GeolocationController` now delegates to `GeolocationApiService:fetchGeolocationData`; the location code returned by `getGeolocation`/`fetchGeolocation` preserves the legacy `v1` behavior of appending the region only for the US and Canada (e.g. `US-NY`, `CA-ON`) and returning the country alone elsewhere
|
|
12
14
|
- Point `GeolocationApiService` at API Platform's `geolocation-api` service instead of the legacy Ramps-owned `on-ramp` geolocation endpoint, which is slated for deprecation ([#9417](https://github.com/MetaMask/core/pull/9417))
|
|
13
15
|
- UAT temporarily resolves to the production URL since API Platform has not yet provisioned a dedicated UAT deployment for this service
|
|
14
16
|
- Bump `@metamask/controller-utils` from `^12.0.0` to `^12.3.0` ([#8774](https://github.com/MetaMask/core/pull/8774), [#9058](https://github.com/MetaMask/core/pull/9058), [#9083](https://github.com/MetaMask/core/pull/9083), [#9218](https://github.com/MetaMask/core/pull/9218))
|
|
@@ -1 +1 @@
|
|
|
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 * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\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 | GeolocationControllerRefreshGeolocationAction;\n"]}
|
|
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"]}
|
|
@@ -8,15 +8,39 @@ import type { GeolocationController } from "./GeolocationController.cjs";
|
|
|
8
8
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
9
9
|
* updates controller state with the result.
|
|
10
10
|
*
|
|
11
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
12
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
13
|
+
*
|
|
11
14
|
* @returns The ISO 3166-2 location code string.
|
|
12
15
|
*/
|
|
13
16
|
export type GeolocationControllerGetGeolocationAction = {
|
|
14
17
|
type: `GeolocationController:getGeolocation`;
|
|
15
18
|
handler: GeolocationController['getGeolocation'];
|
|
16
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
|
+
};
|
|
17
38
|
/**
|
|
18
39
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
40
|
*
|
|
41
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
42
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
43
|
+
*
|
|
20
44
|
* @returns The ISO 3166-2 location code string.
|
|
21
45
|
*/
|
|
22
46
|
export type GeolocationControllerRefreshGeolocationAction = {
|
|
@@ -26,5 +50,5 @@ export type GeolocationControllerRefreshGeolocationAction = {
|
|
|
26
50
|
/**
|
|
27
51
|
* Union of all GeolocationController action types.
|
|
28
52
|
*/
|
|
29
|
-
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
53
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerGetGeolocationDataAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
54
|
//# sourceMappingURL=GeolocationController-method-action-types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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"}
|
|
@@ -8,15 +8,39 @@ import type { GeolocationController } from "./GeolocationController.mjs";
|
|
|
8
8
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
9
9
|
* updates controller state with the result.
|
|
10
10
|
*
|
|
11
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
12
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
13
|
+
*
|
|
11
14
|
* @returns The ISO 3166-2 location code string.
|
|
12
15
|
*/
|
|
13
16
|
export type GeolocationControllerGetGeolocationAction = {
|
|
14
17
|
type: `GeolocationController:getGeolocation`;
|
|
15
18
|
handler: GeolocationController['getGeolocation'];
|
|
16
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
|
+
};
|
|
17
38
|
/**
|
|
18
39
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
40
|
*
|
|
41
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
42
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
43
|
+
*
|
|
20
44
|
* @returns The ISO 3166-2 location code string.
|
|
21
45
|
*/
|
|
22
46
|
export type GeolocationControllerRefreshGeolocationAction = {
|
|
@@ -26,5 +50,5 @@ export type GeolocationControllerRefreshGeolocationAction = {
|
|
|
26
50
|
/**
|
|
27
51
|
* Union of all GeolocationController action types.
|
|
28
52
|
*/
|
|
29
|
-
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
53
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerGetGeolocationDataAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
54
|
//# sourceMappingURL=GeolocationController-method-action-types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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 +1 @@
|
|
|
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 * @returns The ISO 3166-2 location code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\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 | GeolocationControllerRefreshGeolocationAction;\n"]}
|
|
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"]}
|
|
@@ -25,6 +25,24 @@ const geolocationControllerMetadata = {
|
|
|
25
25
|
includeInStateLogs: true,
|
|
26
26
|
usedInUi: true,
|
|
27
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
|
+
},
|
|
28
46
|
status: {
|
|
29
47
|
persist: false,
|
|
30
48
|
includeInDebugSnapshot: true,
|
|
@@ -55,6 +73,7 @@ const geolocationControllerMetadata = {
|
|
|
55
73
|
function getDefaultGeolocationControllerState() {
|
|
56
74
|
return {
|
|
57
75
|
location: geolocation_api_service_js_1.UNKNOWN_LOCATION,
|
|
76
|
+
...(0, geolocation_api_service_js_1.getUnknownGeolocationData)(),
|
|
58
77
|
status: 'idle',
|
|
59
78
|
lastFetchedAt: null,
|
|
60
79
|
error: null,
|
|
@@ -63,6 +82,7 @@ function getDefaultGeolocationControllerState() {
|
|
|
63
82
|
exports.getDefaultGeolocationControllerState = getDefaultGeolocationControllerState;
|
|
64
83
|
const MESSENGER_EXPOSED_METHODS = [
|
|
65
84
|
'getGeolocation',
|
|
85
|
+
'getGeolocationData',
|
|
66
86
|
'refreshGeolocation',
|
|
67
87
|
];
|
|
68
88
|
/**
|
|
@@ -100,21 +120,56 @@ class GeolocationController extends base_controller_1.BaseController {
|
|
|
100
120
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
101
121
|
* updates controller state with the result.
|
|
102
122
|
*
|
|
123
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
124
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
125
|
+
*
|
|
103
126
|
* @returns The ISO 3166-2 location code string.
|
|
104
127
|
*/
|
|
105
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() {
|
|
106
152
|
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
107
153
|
}
|
|
108
154
|
/**
|
|
109
155
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
110
156
|
*
|
|
157
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
158
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
159
|
+
*
|
|
111
160
|
* @returns The ISO 3166-2 location code string.
|
|
112
161
|
*/
|
|
113
162
|
async refreshGeolocation() {
|
|
114
163
|
this.update((draft) => {
|
|
115
164
|
draft.lastFetchedAt = null;
|
|
116
165
|
});
|
|
117
|
-
|
|
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;
|
|
118
173
|
}
|
|
119
174
|
}
|
|
120
175
|
exports.GeolocationController = GeolocationController;
|
|
@@ -125,7 +180,9 @@ _GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAn
|
|
|
125
180
|
*
|
|
126
181
|
* @param options - Options forwarded to the service.
|
|
127
182
|
* @param options.bypassCache - When true, the service skips its TTL cache.
|
|
128
|
-
* @returns The
|
|
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.
|
|
129
186
|
*/
|
|
130
187
|
async function _GeolocationController_fetchAndUpdate(options) {
|
|
131
188
|
this.update((draft) => {
|
|
@@ -133,14 +190,17 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
133
190
|
draft.error = null;
|
|
134
191
|
});
|
|
135
192
|
try {
|
|
136
|
-
const
|
|
193
|
+
const geolocation = await this.messenger.call('GeolocationApiService:fetchGeolocationData', options);
|
|
137
194
|
this.update((draft) => {
|
|
138
|
-
draft.location =
|
|
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;
|
|
139
199
|
draft.status = 'complete';
|
|
140
200
|
draft.lastFetchedAt = Date.now();
|
|
141
201
|
draft.error = null;
|
|
142
202
|
});
|
|
143
|
-
return
|
|
203
|
+
return geolocation;
|
|
144
204
|
}
|
|
145
205
|
catch (error) {
|
|
146
206
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -148,7 +208,7 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
148
208
|
draft.status = 'error';
|
|
149
209
|
draft.error = message;
|
|
150
210
|
});
|
|
151
|
-
|
|
211
|
+
throw error;
|
|
152
212
|
}
|
|
153
213
|
};
|
|
154
214
|
//# sourceMappingURL=GeolocationController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GeolocationController.cjs","sourceRoot":"","sources":["../src/GeolocationController.ts"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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,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC7B,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 { GeolocationApiServiceFetchGeolocationAction } from './geolocation-api-service/geolocation-api-service-method-action-types.js';\nimport { UNKNOWN_LOCATION } 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 /** 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 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 status: 'idle',\n lastFetchedAt: null,\n error: null,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getGeolocation',\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 = GeolocationApiServiceFetchGeolocationAction;\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 * @returns The ISO 3166-2 location code string.\n */\n async getGeolocation(): Promise<string> {\n return this.#fetchAndUpdate();\n }\n\n /**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\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 return this.#fetchAndUpdate({ bypassCache: true });\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 ISO 3166-2 location code string.\n */\n async #fetchAndUpdate(options?: { bypassCache?: boolean }): Promise<string> {\n this.update((draft) => {\n draft.status = 'loading';\n draft.error = null;\n });\n\n try {\n const location = await this.messenger.call(\n 'GeolocationApiService:fetchGeolocation',\n options,\n );\n\n this.update((draft) => {\n draft.location = location;\n draft.status = 'complete';\n draft.lastFetchedAt = Date.now();\n draft.error = null;\n });\n\n return location;\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 return this.state.location;\n }\n }\n}\n"]}
|
|
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,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACP,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;IACD,MAAM,EAAE;QACN,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;IACD,QAAQ,EAAE;QACR,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;IACD,MAAM,EAAE;QACN,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,IAAI;KACf;IACD,aAAa,EAAE;QACb,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,KAAK,EAAE;QACL,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EA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,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 +1,8 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type {
|
|
4
|
+
import type { GeolocationApiServiceFetchGeolocationDataAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.cjs";
|
|
5
|
+
import type { GeolocationData } from "./geolocation-api-service/geolocation-api-service.cjs";
|
|
5
6
|
import type { GeolocationControllerMethodActions } from "./GeolocationController-method-action-types.cjs";
|
|
6
7
|
import type { GeolocationRequestStatus } from "./types.cjs";
|
|
7
8
|
/**
|
|
@@ -16,6 +17,12 @@ export declare const controllerName = "GeolocationController";
|
|
|
16
17
|
export type GeolocationControllerState = {
|
|
17
18
|
/** ISO 3166-2 location code (e.g. "US", "US-NY", "CA-ON"), or "UNKNOWN" if not yet determined. */
|
|
18
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;
|
|
19
26
|
/** Current status of the geolocation fetch lifecycle. */
|
|
20
27
|
status: GeolocationRequestStatus;
|
|
21
28
|
/** Epoch milliseconds of the last successful fetch, or null if never fetched. */
|
|
@@ -43,7 +50,7 @@ export type GeolocationControllerActions = GeolocationControllerGetStateAction |
|
|
|
43
50
|
/**
|
|
44
51
|
* Actions from other messengers that {@link GeolocationControllerMessenger} calls.
|
|
45
52
|
*/
|
|
46
|
-
type AllowedActions =
|
|
53
|
+
type AllowedActions = GeolocationApiServiceFetchGeolocationDataAction;
|
|
47
54
|
/**
|
|
48
55
|
* Published when the state of {@link GeolocationController} changes.
|
|
49
56
|
*/
|
|
@@ -98,12 +105,33 @@ export declare class GeolocationController extends BaseController<typeof control
|
|
|
98
105
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
99
106
|
* updates controller state with the result.
|
|
100
107
|
*
|
|
108
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
109
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
110
|
+
*
|
|
101
111
|
* @returns The ISO 3166-2 location code string.
|
|
102
112
|
*/
|
|
103
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>;
|
|
104
129
|
/**
|
|
105
130
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
131
|
*
|
|
132
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
133
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
134
|
+
*
|
|
107
135
|
* @returns The ISO 3166-2 location code string.
|
|
108
136
|
*/
|
|
109
137
|
refreshGeolocation(): Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,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,7 +1,8 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
-
import type {
|
|
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";
|
|
5
6
|
import type { GeolocationControllerMethodActions } from "./GeolocationController-method-action-types.mjs";
|
|
6
7
|
import type { GeolocationRequestStatus } from "./types.mjs";
|
|
7
8
|
/**
|
|
@@ -16,6 +17,12 @@ export declare const controllerName = "GeolocationController";
|
|
|
16
17
|
export type GeolocationControllerState = {
|
|
17
18
|
/** ISO 3166-2 location code (e.g. "US", "US-NY", "CA-ON"), or "UNKNOWN" if not yet determined. */
|
|
18
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;
|
|
19
26
|
/** Current status of the geolocation fetch lifecycle. */
|
|
20
27
|
status: GeolocationRequestStatus;
|
|
21
28
|
/** Epoch milliseconds of the last successful fetch, or null if never fetched. */
|
|
@@ -43,7 +50,7 @@ export type GeolocationControllerActions = GeolocationControllerGetStateAction |
|
|
|
43
50
|
/**
|
|
44
51
|
* Actions from other messengers that {@link GeolocationControllerMessenger} calls.
|
|
45
52
|
*/
|
|
46
|
-
type AllowedActions =
|
|
53
|
+
type AllowedActions = GeolocationApiServiceFetchGeolocationDataAction;
|
|
47
54
|
/**
|
|
48
55
|
* Published when the state of {@link GeolocationController} changes.
|
|
49
56
|
*/
|
|
@@ -98,12 +105,33 @@ export declare class GeolocationController extends BaseController<typeof control
|
|
|
98
105
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
99
106
|
* updates controller state with the result.
|
|
100
107
|
*
|
|
108
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
109
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
110
|
+
*
|
|
101
111
|
* @returns The ISO 3166-2 location code string.
|
|
102
112
|
*/
|
|
103
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>;
|
|
104
129
|
/**
|
|
105
130
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
131
|
*
|
|
132
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
133
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
134
|
+
*
|
|
107
135
|
* @returns The ISO 3166-2 location code string.
|
|
108
136
|
*/
|
|
109
137
|
refreshGeolocation(): Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
1
|
+
{"version":3,"file":"GeolocationController.d.mts","sourceRoot":"","sources":["../src/G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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"}
|
|
@@ -5,7 +5,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
5
5
|
};
|
|
6
6
|
var _GeolocationController_instances, _GeolocationController_fetchAndUpdate;
|
|
7
7
|
import { BaseController } from "@metamask/base-controller";
|
|
8
|
-
import { UNKNOWN_LOCATION } from "./geolocation-api-service/geolocation-api-service.mjs";
|
|
8
|
+
import { getUnknownGeolocationData, toLocationCode, UNKNOWN_LOCATION } from "./geolocation-api-service/geolocation-api-service.mjs";
|
|
9
9
|
/**
|
|
10
10
|
* The name of the {@link GeolocationController}, used to namespace the
|
|
11
11
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -22,6 +22,24 @@ const geolocationControllerMetadata = {
|
|
|
22
22
|
includeInStateLogs: true,
|
|
23
23
|
usedInUi: true,
|
|
24
24
|
},
|
|
25
|
+
country: {
|
|
26
|
+
persist: false,
|
|
27
|
+
includeInDebugSnapshot: true,
|
|
28
|
+
includeInStateLogs: true,
|
|
29
|
+
usedInUi: true,
|
|
30
|
+
},
|
|
31
|
+
region: {
|
|
32
|
+
persist: false,
|
|
33
|
+
includeInDebugSnapshot: true,
|
|
34
|
+
includeInStateLogs: true,
|
|
35
|
+
usedInUi: true,
|
|
36
|
+
},
|
|
37
|
+
timezone: {
|
|
38
|
+
persist: false,
|
|
39
|
+
includeInDebugSnapshot: true,
|
|
40
|
+
includeInStateLogs: true,
|
|
41
|
+
usedInUi: true,
|
|
42
|
+
},
|
|
25
43
|
status: {
|
|
26
44
|
persist: false,
|
|
27
45
|
includeInDebugSnapshot: true,
|
|
@@ -52,6 +70,7 @@ const geolocationControllerMetadata = {
|
|
|
52
70
|
export function getDefaultGeolocationControllerState() {
|
|
53
71
|
return {
|
|
54
72
|
location: UNKNOWN_LOCATION,
|
|
73
|
+
...getUnknownGeolocationData(),
|
|
55
74
|
status: 'idle',
|
|
56
75
|
lastFetchedAt: null,
|
|
57
76
|
error: null,
|
|
@@ -59,6 +78,7 @@ export function getDefaultGeolocationControllerState() {
|
|
|
59
78
|
}
|
|
60
79
|
const MESSENGER_EXPOSED_METHODS = [
|
|
61
80
|
'getGeolocation',
|
|
81
|
+
'getGeolocationData',
|
|
62
82
|
'refreshGeolocation',
|
|
63
83
|
];
|
|
64
84
|
/**
|
|
@@ -96,21 +116,56 @@ export class GeolocationController extends BaseController {
|
|
|
96
116
|
* {@link GeolocationApiService} for network fetching and caching, then
|
|
97
117
|
* updates controller state with the result.
|
|
98
118
|
*
|
|
119
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
120
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
121
|
+
*
|
|
99
122
|
* @returns The ISO 3166-2 location code string.
|
|
100
123
|
*/
|
|
101
124
|
async getGeolocation() {
|
|
125
|
+
try {
|
|
126
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// Best-effort: fall back to the last known location code below.
|
|
130
|
+
}
|
|
131
|
+
return this.state.location;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns the country, region, and timezone for the current client.
|
|
135
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
136
|
+
* caching, then updates controller state with the result.
|
|
137
|
+
*
|
|
138
|
+
* Unlike {@link getGeolocation}, this rejects when resolution fails instead
|
|
139
|
+
* of returning a stale value, so callers can distinguish a fresh result from
|
|
140
|
+
* a failed lookup (and, for example, omit location rather than enrich with a
|
|
141
|
+
* previous session's data).
|
|
142
|
+
*
|
|
143
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
144
|
+
* not be determined.
|
|
145
|
+
* @throws When the geolocation service fails to resolve.
|
|
146
|
+
*/
|
|
147
|
+
async getGeolocationData() {
|
|
102
148
|
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
103
149
|
}
|
|
104
150
|
/**
|
|
105
151
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
152
|
*
|
|
153
|
+
* Best-effort: if the fetch fails, the last known location code (or
|
|
154
|
+
* {@link UNKNOWN_LOCATION}) is returned rather than throwing.
|
|
155
|
+
*
|
|
107
156
|
* @returns The ISO 3166-2 location code string.
|
|
108
157
|
*/
|
|
109
158
|
async refreshGeolocation() {
|
|
110
159
|
this.update((draft) => {
|
|
111
160
|
draft.lastFetchedAt = null;
|
|
112
161
|
});
|
|
113
|
-
|
|
162
|
+
try {
|
|
163
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this, { bypassCache: true });
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
// Best-effort: fall back to the last known location code below.
|
|
167
|
+
}
|
|
168
|
+
return this.state.location;
|
|
114
169
|
}
|
|
115
170
|
}
|
|
116
171
|
_GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAndUpdate =
|
|
@@ -120,7 +175,9 @@ _GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAn
|
|
|
120
175
|
*
|
|
121
176
|
* @param options - Options forwarded to the service.
|
|
122
177
|
* @param options.bypassCache - When true, the service skips its TTL cache.
|
|
123
|
-
* @returns The
|
|
178
|
+
* @returns The resolved geolocation data.
|
|
179
|
+
* @throws Re-throws the service error after recording it in state, so
|
|
180
|
+
* callers can react to a failed lookup instead of receiving a stale value.
|
|
124
181
|
*/
|
|
125
182
|
async function _GeolocationController_fetchAndUpdate(options) {
|
|
126
183
|
this.update((draft) => {
|
|
@@ -128,14 +185,17 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
128
185
|
draft.error = null;
|
|
129
186
|
});
|
|
130
187
|
try {
|
|
131
|
-
const
|
|
188
|
+
const geolocation = await this.messenger.call('GeolocationApiService:fetchGeolocationData', options);
|
|
132
189
|
this.update((draft) => {
|
|
133
|
-
draft.location =
|
|
190
|
+
draft.location = toLocationCode(geolocation);
|
|
191
|
+
draft.country = geolocation.country;
|
|
192
|
+
draft.region = geolocation.region;
|
|
193
|
+
draft.timezone = geolocation.timezone;
|
|
134
194
|
draft.status = 'complete';
|
|
135
195
|
draft.lastFetchedAt = Date.now();
|
|
136
196
|
draft.error = null;
|
|
137
197
|
});
|
|
138
|
-
return
|
|
198
|
+
return geolocation;
|
|
139
199
|
}
|
|
140
200
|
catch (error) {
|
|
141
201
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -143,7 +203,7 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
143
203
|
draft.status = 'error';
|
|
144
204
|
draft.error = message;
|
|
145
205
|
});
|
|
146
|
-
|
|
206
|
+
throw error;
|
|
147
207
|
}
|
|
148
208
|
};
|
|
149
209
|
//# sourceMappingURL=GeolocationController.mjs.map
|