@metamask-previews/geolocation-controller 0.1.3-preview-0b2e3c9b4 → 0.1.3-preview-2296abeaf
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 +13 -1
- package/dist/GeolocationController-method-action-types.d.cts.map +1 -1
- package/dist/GeolocationController-method-action-types.d.mts +13 -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 +44 -6
- package/dist/GeolocationController.cjs.map +1 -1
- package/dist/GeolocationController.d.cts +18 -2
- package/dist/GeolocationController.d.cts.map +1 -1
- package/dist/GeolocationController.d.mts +18 -2
- package/dist/GeolocationController.d.mts.map +1 -1
- package/dist/GeolocationController.mjs +45 -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 +124 -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 +121 -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 * @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 * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\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 * @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"]}
|
|
@@ -14,6 +14,18 @@ export type GeolocationControllerGetGeolocationAction = {
|
|
|
14
14
|
type: `GeolocationController:getGeolocation`;
|
|
15
15
|
handler: GeolocationController['getGeolocation'];
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns the country, region, and timezone for the current client.
|
|
19
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
20
|
+
* caching, then updates controller state with the result.
|
|
21
|
+
*
|
|
22
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
23
|
+
* not be determined.
|
|
24
|
+
*/
|
|
25
|
+
export type GeolocationControllerGetGeolocationDataAction = {
|
|
26
|
+
type: `GeolocationController:getGeolocationData`;
|
|
27
|
+
handler: GeolocationController['getGeolocationData'];
|
|
28
|
+
};
|
|
17
29
|
/**
|
|
18
30
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
31
|
*
|
|
@@ -26,5 +38,5 @@ export type GeolocationControllerRefreshGeolocationAction = {
|
|
|
26
38
|
/**
|
|
27
39
|
* Union of all GeolocationController action types.
|
|
28
40
|
*/
|
|
29
|
-
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
41
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerGetGeolocationDataAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
42
|
//# 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;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;GAIG;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,CAAC"}
|
|
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;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;;;GAIG;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"}
|
|
@@ -14,6 +14,18 @@ export type GeolocationControllerGetGeolocationAction = {
|
|
|
14
14
|
type: `GeolocationController:getGeolocation`;
|
|
15
15
|
handler: GeolocationController['getGeolocation'];
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns the country, region, and timezone for the current client.
|
|
19
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
20
|
+
* caching, then updates controller state with the result.
|
|
21
|
+
*
|
|
22
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
23
|
+
* not be determined.
|
|
24
|
+
*/
|
|
25
|
+
export type GeolocationControllerGetGeolocationDataAction = {
|
|
26
|
+
type: `GeolocationController:getGeolocationData`;
|
|
27
|
+
handler: GeolocationController['getGeolocationData'];
|
|
28
|
+
};
|
|
17
29
|
/**
|
|
18
30
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
31
|
*
|
|
@@ -26,5 +38,5 @@ export type GeolocationControllerRefreshGeolocationAction = {
|
|
|
26
38
|
/**
|
|
27
39
|
* Union of all GeolocationController action types.
|
|
28
40
|
*/
|
|
29
|
-
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
41
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerGetGeolocationDataAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
42
|
//# 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;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;GAIG;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,CAAC"}
|
|
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;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;;;GAIG;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 * @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 * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\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 * @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
|
/**
|
|
@@ -103,6 +123,18 @@ class GeolocationController extends base_controller_1.BaseController {
|
|
|
103
123
|
* @returns The ISO 3166-2 location code string.
|
|
104
124
|
*/
|
|
105
125
|
async getGeolocation() {
|
|
126
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
127
|
+
return this.state.location;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns the country, region, and timezone for the current client.
|
|
131
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
132
|
+
* caching, then updates controller state with the result.
|
|
133
|
+
*
|
|
134
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
135
|
+
* not be determined.
|
|
136
|
+
*/
|
|
137
|
+
async getGeolocationData() {
|
|
106
138
|
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
107
139
|
}
|
|
108
140
|
/**
|
|
@@ -114,7 +146,8 @@ class GeolocationController extends base_controller_1.BaseController {
|
|
|
114
146
|
this.update((draft) => {
|
|
115
147
|
draft.lastFetchedAt = null;
|
|
116
148
|
});
|
|
117
|
-
|
|
149
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this, { bypassCache: true });
|
|
150
|
+
return this.state.location;
|
|
118
151
|
}
|
|
119
152
|
}
|
|
120
153
|
exports.GeolocationController = GeolocationController;
|
|
@@ -125,7 +158,8 @@ _GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAn
|
|
|
125
158
|
*
|
|
126
159
|
* @param options - Options forwarded to the service.
|
|
127
160
|
* @param options.bypassCache - When true, the service skips its TTL cache.
|
|
128
|
-
* @returns The
|
|
161
|
+
* @returns The geolocation data. When the service throws, the last known
|
|
162
|
+
* geolocation is returned instead.
|
|
129
163
|
*/
|
|
130
164
|
async function _GeolocationController_fetchAndUpdate(options) {
|
|
131
165
|
this.update((draft) => {
|
|
@@ -133,14 +167,17 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
133
167
|
draft.error = null;
|
|
134
168
|
});
|
|
135
169
|
try {
|
|
136
|
-
const
|
|
170
|
+
const geolocation = await this.messenger.call('GeolocationApiService:fetchGeolocationData', options);
|
|
137
171
|
this.update((draft) => {
|
|
138
|
-
draft.location =
|
|
172
|
+
draft.location = (0, geolocation_api_service_js_1.toLocationCode)(geolocation);
|
|
173
|
+
draft.country = geolocation.country;
|
|
174
|
+
draft.region = geolocation.region;
|
|
175
|
+
draft.timezone = geolocation.timezone;
|
|
139
176
|
draft.status = 'complete';
|
|
140
177
|
draft.lastFetchedAt = Date.now();
|
|
141
178
|
draft.error = null;
|
|
142
179
|
});
|
|
143
|
-
return
|
|
180
|
+
return geolocation;
|
|
144
181
|
}
|
|
145
182
|
catch (error) {
|
|
146
183
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -148,7 +185,8 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
148
185
|
draft.status = 'error';
|
|
149
186
|
draft.error = message;
|
|
150
187
|
});
|
|
151
|
-
|
|
188
|
+
const { country, region, timezone } = this.state;
|
|
189
|
+
return { country, region, timezone };
|
|
152
190
|
}
|
|
153
191
|
};
|
|
154
192
|
//# sourceMappingURL=GeolocationController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GeolocationController.cjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAI3D,sGAAwF;AAIxF;;;;GAIG;AACU,QAAA,cAAc,GAAG,uBAAuB,CAAC;AAgBtD;;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,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,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACkD,CAAC;AAEtD;;;;;;;GAOG;AACH,SAAgB,oCAAoC;IAClD,OAAO;QACL,QAAQ,EA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I,CAAC,SAAS,CAAC,IAAI,CACxC,wCAAwC,EACxC,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;YAC1B,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,QAAQ,CAAC;IAClB,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,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;QAC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AAQ,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,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACjD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;IACvC,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 * @returns The ISO 3166-2 location code string.\n */\n async getGeolocation(): Promise<string> {\n await this.#fetchAndUpdate();\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 * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\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 * @returns The ISO 3166-2 location code string.\n */\n async refreshGeolocation(): Promise<string> {\n this.update((draft) => {\n draft.lastFetchedAt = null;\n });\n await this.#fetchAndUpdate({ bypassCache: true });\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 geolocation data. When the service throws, the last known\n * geolocation is returned instead.\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 const { country, region, timezone } = this.state;\n return { country, region, timezone };\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
|
*/
|
|
@@ -101,6 +108,15 @@ export declare class GeolocationController extends BaseController<typeof control
|
|
|
101
108
|
* @returns The ISO 3166-2 location code string.
|
|
102
109
|
*/
|
|
103
110
|
getGeolocation(): Promise<string>;
|
|
111
|
+
/**
|
|
112
|
+
* Returns the country, region, and timezone for the current client.
|
|
113
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
114
|
+
* caching, then updates controller state with the result.
|
|
115
|
+
*
|
|
116
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
117
|
+
* not be determined.
|
|
118
|
+
*/
|
|
119
|
+
getGeolocationData(): Promise<GeolocationData>;
|
|
104
120
|
/**
|
|
105
121
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
122
|
*
|
|
@@ -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,KAA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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;;;;;;OAMG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAKvC;;;;;;;OAOG;IACG,kBAAkB,IAAI,OAAO,CAAC,eAAe,CAAC;IAIpD;;;;OAIG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;CAsD5C"}
|
|
@@ -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
|
*/
|
|
@@ -101,6 +108,15 @@ export declare class GeolocationController extends BaseController<typeof control
|
|
|
101
108
|
* @returns The ISO 3166-2 location code string.
|
|
102
109
|
*/
|
|
103
110
|
getGeolocation(): Promise<string>;
|
|
111
|
+
/**
|
|
112
|
+
* Returns the country, region, and timezone for the current client.
|
|
113
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
114
|
+
* caching, then updates controller state with the result.
|
|
115
|
+
*
|
|
116
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
117
|
+
* not be determined.
|
|
118
|
+
*/
|
|
119
|
+
getGeolocationData(): Promise<GeolocationData>;
|
|
104
120
|
/**
|
|
105
121
|
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
122
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GeolocationController.d.mts","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EAC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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;;;;;;OAMG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAKvC;;;;;;;OAOG;IACG,kBAAkB,IAAI,OAAO,CAAC,eAAe,CAAC;IAIpD;;;;OAIG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;CAsD5C"}
|
|
@@ -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
|
/**
|
|
@@ -99,6 +119,18 @@ export class GeolocationController extends BaseController {
|
|
|
99
119
|
* @returns The ISO 3166-2 location code string.
|
|
100
120
|
*/
|
|
101
121
|
async getGeolocation() {
|
|
122
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
123
|
+
return this.state.location;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Returns the country, region, and timezone for the current client.
|
|
127
|
+
* Delegates to the {@link GeolocationApiService} for network fetching and
|
|
128
|
+
* caching, then updates controller state with the result.
|
|
129
|
+
*
|
|
130
|
+
* @returns The geolocation data, where each field is `null` when it could
|
|
131
|
+
* not be determined.
|
|
132
|
+
*/
|
|
133
|
+
async getGeolocationData() {
|
|
102
134
|
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
103
135
|
}
|
|
104
136
|
/**
|
|
@@ -110,7 +142,8 @@ export class GeolocationController extends BaseController {
|
|
|
110
142
|
this.update((draft) => {
|
|
111
143
|
draft.lastFetchedAt = null;
|
|
112
144
|
});
|
|
113
|
-
|
|
145
|
+
await __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this, { bypassCache: true });
|
|
146
|
+
return this.state.location;
|
|
114
147
|
}
|
|
115
148
|
}
|
|
116
149
|
_GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAndUpdate =
|
|
@@ -120,7 +153,8 @@ _GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAn
|
|
|
120
153
|
*
|
|
121
154
|
* @param options - Options forwarded to the service.
|
|
122
155
|
* @param options.bypassCache - When true, the service skips its TTL cache.
|
|
123
|
-
* @returns The
|
|
156
|
+
* @returns The geolocation data. When the service throws, the last known
|
|
157
|
+
* geolocation is returned instead.
|
|
124
158
|
*/
|
|
125
159
|
async function _GeolocationController_fetchAndUpdate(options) {
|
|
126
160
|
this.update((draft) => {
|
|
@@ -128,14 +162,17 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
128
162
|
draft.error = null;
|
|
129
163
|
});
|
|
130
164
|
try {
|
|
131
|
-
const
|
|
165
|
+
const geolocation = await this.messenger.call('GeolocationApiService:fetchGeolocationData', options);
|
|
132
166
|
this.update((draft) => {
|
|
133
|
-
draft.location =
|
|
167
|
+
draft.location = toLocationCode(geolocation);
|
|
168
|
+
draft.country = geolocation.country;
|
|
169
|
+
draft.region = geolocation.region;
|
|
170
|
+
draft.timezone = geolocation.timezone;
|
|
134
171
|
draft.status = 'complete';
|
|
135
172
|
draft.lastFetchedAt = Date.now();
|
|
136
173
|
draft.error = null;
|
|
137
174
|
});
|
|
138
|
-
return
|
|
175
|
+
return geolocation;
|
|
139
176
|
}
|
|
140
177
|
catch (error) {
|
|
141
178
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -143,7 +180,8 @@ async function _GeolocationController_fetchAndUpdate(options) {
|
|
|
143
180
|
draft.status = 'error';
|
|
144
181
|
draft.error = message;
|
|
145
182
|
});
|
|
146
|
-
|
|
183
|
+
const { country, region, timezone } = this.state;
|
|
184
|
+
return { country, region, timezone };
|
|
147
185
|
}
|
|
148
186
|
};
|
|
149
187
|
//# sourceMappingURL=GeolocationController.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GeolocationController.mjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,EAAE,gBAAgB,EAAE,8DAA6D;AAIxF;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAgBtD;;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,MAAM,EAAE;QACN,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,I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AApB,IAAI,EAAiB,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC;CAyCF;;AAvCC;;;;;;;GAOG;AACH,KAAK,gDAAiB,OAAmC;IACvD,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,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACxC,wCAAwC,EACxC,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;YAC1B,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,QAAQ,CAAC;IAClB,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,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.mjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,EACL,yBAAyB,EACzB,cAAc,EACd,gBAAgB,EACjB,8DAA6D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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,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACjD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;IACvC,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 * @returns The ISO 3166-2 location code string.\n */\n async getGeolocation(): Promise<string> {\n await this.#fetchAndUpdate();\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 * @returns The geolocation data, where each field is `null` when it could\n * not be determined.\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 * @returns The ISO 3166-2 location code string.\n */\n async refreshGeolocation(): Promise<string> {\n this.update((draft) => {\n draft.lastFetchedAt = null;\n });\n await this.#fetchAndUpdate({ bypassCache: true });\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 geolocation data. When the service throws, the last known\n * geolocation is returned instead.\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 const { country, region, timezone } = this.state;\n return { country, region, timezone };\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
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 * Union of all GeolocationApiService action types.\n */\nexport type GeolocationApiServiceMethodActions =\n GeolocationApiServiceFetchGeolocationAction;\n"]}
|
|
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"]}
|
|
@@ -19,8 +19,24 @@ export type GeolocationApiServiceFetchGeolocationAction = {
|
|
|
19
19
|
type: `GeolocationApiService:fetchGeolocation`;
|
|
20
20
|
handler: GeolocationApiService['fetchGeolocation'];
|
|
21
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Returns the country, region, and timezone for the current client. Serves
|
|
24
|
+
* from cache when the TTL has not expired, otherwise performs a network
|
|
25
|
+
* fetch. Concurrent callers are deduplicated to a single in-flight request.
|
|
26
|
+
*
|
|
27
|
+
* @param options - Optional fetch options.
|
|
28
|
+
* @param options.bypassCache - When true, invalidates the TTL cache. If a
|
|
29
|
+
* request is already in-flight it will be reused (deduplication always
|
|
30
|
+
* applies).
|
|
31
|
+
* @returns The geolocation data, where each field is `null` when the API
|
|
32
|
+
* omits it or returns a value that fails validation.
|
|
33
|
+
*/
|
|
34
|
+
export type GeolocationApiServiceFetchGeolocationDataAction = {
|
|
35
|
+
type: `GeolocationApiService:fetchGeolocationData`;
|
|
36
|
+
handler: GeolocationApiService['fetchGeolocationData'];
|
|
37
|
+
};
|
|
22
38
|
/**
|
|
23
39
|
* Union of all GeolocationApiService action types.
|
|
24
40
|
*/
|
|
25
|
-
export type GeolocationApiServiceMethodActions = GeolocationApiServiceFetchGeolocationAction;
|
|
41
|
+
export type GeolocationApiServiceMethodActions = GeolocationApiServiceFetchGeolocationAction | GeolocationApiServiceFetchGeolocationDataAction;
|
|
26
42
|
//# sourceMappingURL=geolocation-api-service-method-action-types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
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;;GAEG;AACH,MAAM,MAAM,kCAAkC,
|
|
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"}
|