@metamask-previews/notification-services-controller 20.0.0-preview-dabf9bd8 → 20.0.0-preview-89bb4b56
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 +5 -0
- package/dist/NotificationServicesController/NotificationServicesController.cjs +14 -11
- package/dist/NotificationServicesController/NotificationServicesController.cjs.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.cts +3 -0
- package/dist/NotificationServicesController/NotificationServicesController.d.cts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.d.mts +3 -0
- package/dist/NotificationServicesController/NotificationServicesController.d.mts.map +1 -1
- package/dist/NotificationServicesController/NotificationServicesController.mjs +14 -11
- package/dist/NotificationServicesController/NotificationServicesController.mjs.map +1 -1
- package/dist/NotificationServicesController/mocks/mockResponses.cjs +4 -4
- package/dist/NotificationServicesController/mocks/mockResponses.cjs.map +1 -1
- package/dist/NotificationServicesController/mocks/mockResponses.mjs +4 -4
- package/dist/NotificationServicesController/mocks/mockResponses.mjs.map +1 -1
- package/dist/NotificationServicesController/services/api-notifications.cjs +34 -14
- package/dist/NotificationServicesController/services/api-notifications.cjs.map +1 -1
- package/dist/NotificationServicesController/services/api-notifications.d.cts +15 -10
- package/dist/NotificationServicesController/services/api-notifications.d.cts.map +1 -1
- package/dist/NotificationServicesController/services/api-notifications.d.mts +15 -10
- package/dist/NotificationServicesController/services/api-notifications.d.mts.map +1 -1
- package/dist/NotificationServicesController/services/api-notifications.mjs +28 -14
- package/dist/NotificationServicesController/services/api-notifications.mjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs +2 -0
- package/dist/NotificationServicesPushController/NotificationServicesPushController.cjs.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts +2 -0
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts +2 -0
- package/dist/NotificationServicesPushController/NotificationServicesPushController.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +2 -0
- package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs.map +1 -1
- package/dist/NotificationServicesPushController/mocks/mockResponse.cjs +1 -1
- package/dist/NotificationServicesPushController/mocks/mockResponse.cjs.map +1 -1
- package/dist/NotificationServicesPushController/mocks/mockResponse.mjs +1 -1
- package/dist/NotificationServicesPushController/mocks/mockResponse.mjs.map +1 -1
- package/dist/NotificationServicesPushController/services/endpoints.cjs +10 -3
- package/dist/NotificationServicesPushController/services/endpoints.cjs.map +1 -1
- package/dist/NotificationServicesPushController/services/endpoints.d.cts +3 -1
- package/dist/NotificationServicesPushController/services/endpoints.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/services/endpoints.d.mts +3 -1
- package/dist/NotificationServicesPushController/services/endpoints.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/services/endpoints.mjs +7 -2
- package/dist/NotificationServicesPushController/services/endpoints.mjs.map +1 -1
- package/dist/NotificationServicesPushController/services/services.cjs +2 -1
- package/dist/NotificationServicesPushController/services/services.cjs.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.cts +3 -0
- package/dist/NotificationServicesPushController/services/services.d.cts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.d.mts +3 -0
- package/dist/NotificationServicesPushController/services/services.d.mts.map +1 -1
- package/dist/NotificationServicesPushController/services/services.mjs +2 -1
- package/dist/NotificationServicesPushController/services/services.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -8,26 +8,43 @@ const loglevel_1 = __importDefault(require("loglevel"));
|
|
|
8
8
|
const notification_config_cache_1 = require("./notification-config-cache.cjs");
|
|
9
9
|
const to_raw_notification_1 = require("../../shared/to-raw-notification.cjs");
|
|
10
10
|
const utils_1 = require("../utils/utils.cjs");
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const TRIGGER_API_ENV = {
|
|
12
|
+
dev: 'https://trigger.dev-api.cx.metamask.io',
|
|
13
|
+
uat: 'https://trigger.uat-api.cx.metamask.io',
|
|
14
|
+
prd: 'https://trigger.api.cx.metamask.io',
|
|
15
|
+
};
|
|
16
|
+
const TRIGGER_API = (env = 'prd') => TRIGGER_API_ENV[env] ?? TRIGGER_API_ENV.prd;
|
|
17
|
+
exports.TRIGGER_API = TRIGGER_API;
|
|
18
|
+
const NOTIFICATION_API_ENV = {
|
|
19
|
+
dev: 'https://notification.dev-api.cx.metamask.io',
|
|
20
|
+
uat: 'https://notification.uat-api.cx.metamask.io',
|
|
21
|
+
prd: 'https://notification.api.cx.metamask.io',
|
|
22
|
+
};
|
|
23
|
+
const NOTIFICATION_API = (env = 'prd') => NOTIFICATION_API_ENV[env] ?? NOTIFICATION_API_ENV.prd;
|
|
24
|
+
exports.NOTIFICATION_API = NOTIFICATION_API;
|
|
13
25
|
// Gets notification settings for each account provided
|
|
14
|
-
|
|
26
|
+
const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = (env = 'prd') => `${(0, exports.TRIGGER_API)(env)}/api/v2/notifications/query`;
|
|
27
|
+
exports.TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT;
|
|
15
28
|
// Used to create/update account notifications for each account provided
|
|
16
|
-
|
|
29
|
+
const TRIGGER_API_NOTIFICATIONS_ENDPOINT = (env = 'prd') => `${(0, exports.TRIGGER_API)(env)}/api/v2/notifications`;
|
|
30
|
+
exports.TRIGGER_API_NOTIFICATIONS_ENDPOINT = TRIGGER_API_NOTIFICATIONS_ENDPOINT;
|
|
17
31
|
// Lists notifications for each account provided
|
|
18
|
-
|
|
32
|
+
const NOTIFICATION_API_LIST_ENDPOINT = (env = 'prd') => `${(0, exports.NOTIFICATION_API)(env)}/api/v3/notifications`;
|
|
33
|
+
exports.NOTIFICATION_API_LIST_ENDPOINT = NOTIFICATION_API_LIST_ENDPOINT;
|
|
19
34
|
// Makrs notifications as read
|
|
20
|
-
|
|
35
|
+
const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = (env = 'prd') => `${(0, exports.NOTIFICATION_API)(env)}/api/v3/notifications/mark-as-read`;
|
|
36
|
+
exports.NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT;
|
|
21
37
|
/**
|
|
22
38
|
* fetches notification config (accounts enabled vs disabled)
|
|
23
39
|
*
|
|
24
40
|
* @param bearerToken - jwt
|
|
25
41
|
* @param addresses - list of addresses to check
|
|
42
|
+
* @param env - the environment to use for the API call
|
|
26
43
|
* NOTE the API will return addresses config with false if they have not been created before.
|
|
27
44
|
* NOTE this is cached for 1s to prevent multiple update calls
|
|
28
45
|
* @returns object of notification config, or null if missing
|
|
29
46
|
*/
|
|
30
|
-
async function getNotificationsApiConfigCached(bearerToken, addresses) {
|
|
47
|
+
async function getNotificationsApiConfigCached(bearerToken, addresses, env = 'prd') {
|
|
31
48
|
if (addresses.length === 0) {
|
|
32
49
|
return [];
|
|
33
50
|
}
|
|
@@ -37,7 +54,7 @@ async function getNotificationsApiConfigCached(bearerToken, addresses) {
|
|
|
37
54
|
return cached;
|
|
38
55
|
}
|
|
39
56
|
const body = addresses.map((address) => ({ address }));
|
|
40
|
-
const data = await (0, utils_1.makeApiCall)(bearerToken, exports.TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT, 'POST', body)
|
|
57
|
+
const data = await (0, utils_1.makeApiCall)(bearerToken, (0, exports.TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT)(env), 'POST', body)
|
|
41
58
|
.then((r) => (r.ok ? r.json() : null))
|
|
42
59
|
.catch(() => null);
|
|
43
60
|
const result = data ?? [];
|
|
@@ -52,9 +69,10 @@ exports.getNotificationsApiConfigCached = getNotificationsApiConfigCached;
|
|
|
52
69
|
*
|
|
53
70
|
* @param bearerToken - jwt
|
|
54
71
|
* @param addresses - list of addresses to check
|
|
72
|
+
* @param env - the environment to use for the API call
|
|
55
73
|
* @returns void
|
|
56
74
|
*/
|
|
57
|
-
async function updateOnChainNotifications(bearerToken, addresses) {
|
|
75
|
+
async function updateOnChainNotifications(bearerToken, addresses, env = 'prd') {
|
|
58
76
|
if (addresses.length === 0) {
|
|
59
77
|
return;
|
|
60
78
|
}
|
|
@@ -63,7 +81,7 @@ async function updateOnChainNotifications(bearerToken, addresses) {
|
|
|
63
81
|
return a;
|
|
64
82
|
});
|
|
65
83
|
const body = addresses;
|
|
66
|
-
await (0, utils_1.makeApiCall)(bearerToken, exports.TRIGGER_API_NOTIFICATIONS_ENDPOINT, 'POST', body)
|
|
84
|
+
await (0, utils_1.makeApiCall)(bearerToken, (0, exports.TRIGGER_API_NOTIFICATIONS_ENDPOINT)(env), 'POST', body)
|
|
67
85
|
.then(() => notification_config_cache_1.notificationsConfigCache.set(addresses))
|
|
68
86
|
.catch(() => null);
|
|
69
87
|
}
|
|
@@ -75,9 +93,10 @@ exports.updateOnChainNotifications = updateOnChainNotifications;
|
|
|
75
93
|
* @param addresses - List of addresses
|
|
76
94
|
* @param locale - to generate translated notifications
|
|
77
95
|
* @param platform - filter notifications for specific platforms ('extension' | 'mobile')
|
|
96
|
+
* @param env - the environment to use for the API call
|
|
78
97
|
* @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.
|
|
79
98
|
*/
|
|
80
|
-
async function getAPINotifications(bearerToken, addresses, locale, platform) {
|
|
99
|
+
async function getAPINotifications(bearerToken, addresses, locale, platform, env = 'prd') {
|
|
81
100
|
if (addresses.length === 0) {
|
|
82
101
|
return [];
|
|
83
102
|
}
|
|
@@ -86,7 +105,7 @@ async function getAPINotifications(bearerToken, addresses, locale, platform) {
|
|
|
86
105
|
locale,
|
|
87
106
|
platform,
|
|
88
107
|
};
|
|
89
|
-
const notifications = await (0, utils_1.makeApiCall)(bearerToken, exports.NOTIFICATION_API_LIST_ENDPOINT, 'POST', body)
|
|
108
|
+
const notifications = await (0, utils_1.makeApiCall)(bearerToken, (0, exports.NOTIFICATION_API_LIST_ENDPOINT)(env), 'POST', body)
|
|
90
109
|
.then((r) => (r.ok ? r.json() : null))
|
|
91
110
|
.catch(() => null);
|
|
92
111
|
// Transform and sort notifications
|
|
@@ -113,9 +132,10 @@ exports.getAPINotifications = getAPINotifications;
|
|
|
113
132
|
*
|
|
114
133
|
* @param bearerToken - The JSON Web Token used for authentication in the API call.
|
|
115
134
|
* @param notificationIds - An array of notification IDs to be marked as read.
|
|
135
|
+
* @param env - the environment to use for the API call
|
|
116
136
|
* @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.
|
|
117
137
|
*/
|
|
118
|
-
async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
138
|
+
async function markNotificationsAsRead(bearerToken, notificationIds, env = 'prd') {
|
|
119
139
|
if (notificationIds.length === 0) {
|
|
120
140
|
return;
|
|
121
141
|
}
|
|
@@ -123,7 +143,7 @@ async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
|
123
143
|
ids: notificationIds,
|
|
124
144
|
};
|
|
125
145
|
try {
|
|
126
|
-
await (0, utils_1.makeApiCall)(bearerToken, exports.NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT, 'POST', body);
|
|
146
|
+
await (0, utils_1.makeApiCall)(bearerToken, (0, exports.NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT)(env), 'POST', body);
|
|
127
147
|
}
|
|
128
148
|
catch (err) {
|
|
129
149
|
loglevel_1.default.error('Error marking notifications as read:', err);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-notifications.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA2B;AAE3B,+EAAuE;AACvE,8EAAwE;AAMxE,8CAA6C;AAShC,QAAA,WAAW,GAAG,oCAAoC,CAAC;AACnD,QAAA,gBAAgB,GAAG,yCAAyC,CAAC;AAE1E,uDAAuD;AAC1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IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SACzD,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;IAErB,mCAAmC;IACnC,MAAM,wBAAwB,GAAG,aAAa;QAC5C,EAAE,GAAG,CAAC,CAAC,CAAC,EAA0C,EAAE;QAClD,IAAI,CAAC,CAAC,CAAC,iBAAiB,EAAE,CAAC;YACzB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,IAAI,CAAC;YACH,OAAO,IAAA,0CAAoB,EAAC,CAAC,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,CAAC,EAAkC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAE7D,OAAO,wBAAwB,IAAI,EAAE,CAAC;AACxC,CAAC;AA7CD,kDA6CC;AAED;;;;;;;;GAQG;AACI,KAAK,UAAU,uBAAuB,CAC3C,WAAmB,EACnB,eAAyB;IAEzB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,OAAO;IACT,CAAC;IAID,MAAM,IAAI,GAAiB;QACzB,GAAG,EAAE,eAAe;KACrB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,IAAA,mBAAW,EACf,WAAW,EACX,kDAA0C,EAC1C,MAAM,EACN,IAAI,CACL,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,kBAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;AACH,CAAC;AAxBD,0DAwBC","sourcesContent":["import log from 'loglevel';\n\nimport { notificationsConfigCache } from './notification-config-cache';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type {\n NormalisedAPINotification,\n Schema,\n UnprocessedRawNotification,\n} from '../types/notification-api';\nimport { makeApiCall } from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\n\n// Gets notification settings for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = `${TRIGGER_API}/api/v2/notifications/query`;\n\n// Used to create/update account notifications for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_ENDPOINT = `${TRIGGER_API}/api/v2/notifications`;\n\n// Lists notifications for each account provided\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications`;\n\n// Makrs notifications as read\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications/mark-as-read`;\n\n/**\n * fetches notification config (accounts enabled vs disabled)\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * NOTE the API will return addresses config with false if they have not been created before.\n * NOTE this is cached for 1s to prevent multiple update calls\n * @returns object of notification config, or null if missing\n */\nexport async function getNotificationsApiConfigCached(\n bearerToken: string,\n addresses: string[],\n) {\n if (addresses.length === 0) {\n return [];\n }\n\n addresses = addresses.map((a) => a.toLowerCase());\n\n const cached = notificationsConfigCache.get(addresses);\n if (cached) {\n return cached;\n }\n\n type RequestBody = { address: string }[];\n type Response = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses.map((address) => ({ address }));\n const data = await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT,\n 'POST',\n body,\n )\n .then<Response | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n const result = data ?? [];\n\n if (result.length > 0) {\n notificationsConfigCache.set(result);\n }\n\n return result;\n}\n\n/**\n * updates notifications for a given addresses\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @returns void\n */\nexport async function updateOnChainNotifications(\n bearerToken: string,\n addresses: { address: string; enabled: boolean }[],\n) {\n if (addresses.length === 0) {\n return;\n }\n\n addresses = addresses.map((a) => {\n a.address = a.address.toLowerCase();\n return a;\n });\n\n type RequestBody = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses;\n await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_ENDPOINT,\n 'POST',\n body,\n )\n .then(() => notificationsConfigCache.set(addresses))\n .catch(() => null);\n}\n\n/**\n * Fetches on-chain notifications for the given addresses\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param addresses - List of addresses\n * @param locale - to generate translated notifications\n * @param platform - filter notifications for specific platforms ('extension' | 'mobile')\n * @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.\n */\nexport async function getAPINotifications(\n bearerToken: string,\n addresses: string[],\n locale: string,\n platform: 'extension' | 'mobile',\n): Promise<NormalisedAPINotification[]> {\n if (addresses.length === 0) {\n return [];\n }\n\n type RequestBody =\n Schema.paths['/api/v3/notifications']['post']['requestBody']['content']['application/json'];\n type APIResponse =\n Schema.paths['/api/v3/notifications']['post']['responses']['200']['content']['application/json'];\n\n const body: RequestBody = {\n addresses: addresses.map((a) => a.toLowerCase()),\n locale,\n platform,\n };\n const notifications = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT,\n 'POST',\n body,\n )\n .then<APIResponse | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n ?.map((n): UnprocessedRawNotification | undefined => {\n if (!n.notification_type) {\n return undefined;\n }\n\n try {\n return toRawAPINotification(n);\n } catch {\n return undefined;\n }\n })\n .filter((n): n is NormalisedAPINotification => Boolean(n));\n\n return transformedNotifications ?? [];\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n type ResponseBody =\n Schema.paths['/api/v3/notifications/mark-as-read']['post']['requestBody']['content']['application/json'];\n const body: ResponseBody = {\n ids: notificationIds,\n };\n\n try {\n await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n body,\n );\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"api-notifications.cjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA2B;AAE3B,+EAAuE;AACvE,8EAAwE;AAMxE,8CAA6C;AAW7C,MAAM,eAAe,GAAG;IACtB,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,oCAAoC;CACZ,CAAC;AAEzB,MAAM,WAAW,GAAG,CAAC,MAAW,KAAK,EAAE,EAAE,CAC9C,eAAe,CAAC,GAAG,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC;AADjC,QAAA,WAAW,eACsB;AAE9C,MAAM,oBAAoB,GAAG;IAC3B,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,yCAAyC;CAC/C,CAAC;AAEK,MAAM,gBAAgB,GAAG,CAAC,MAAW,KAAK,EAAE,EAAE,CACnD,oBAAoB,CAAC,GAAG,CAAC,IAAI,oBAAoB,CAAC,GAAG,CAAC;AAD3C,QAAA,gBAAgB,oBAC2B;AAExD,uDAAuD;AAChD,MAAM,wCAAwC,GAAG,CAAC,MAAW,KAAK,EAAE,EAAE,CAC3E,GAAG,IAAA,mBAAW,EAAC,GAAG,CAAC,6BAA6B,CAAC;AADtC,QAAA,wCAAwC,4CACF;AA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AAG,CAAC,EAC/C,MAAM,EACN,IAAI,CACL,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,kBAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;AACH,CAAC;AAzBD,0DAyBC","sourcesContent":["import log from 'loglevel';\n\nimport { notificationsConfigCache } from './notification-config-cache';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type {\n NormalisedAPINotification,\n Schema,\n UnprocessedRawNotification,\n} from '../types/notification-api';\nimport { makeApiCall } from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport type ENV = 'prd' | 'uat' | 'dev';\n\nconst TRIGGER_API_ENV = {\n dev: 'https://trigger.dev-api.cx.metamask.io',\n uat: 'https://trigger.uat-api.cx.metamask.io',\n prd: 'https://trigger.api.cx.metamask.io',\n} satisfies Record<ENV, string>;\n\nexport const TRIGGER_API = (env: ENV = 'prd') =>\n TRIGGER_API_ENV[env] ?? TRIGGER_API_ENV.prd;\n\nconst NOTIFICATION_API_ENV = {\n dev: 'https://notification.dev-api.cx.metamask.io',\n uat: 'https://notification.uat-api.cx.metamask.io',\n prd: 'https://notification.api.cx.metamask.io',\n};\n\nexport const NOTIFICATION_API = (env: ENV = 'prd') =>\n NOTIFICATION_API_ENV[env] ?? NOTIFICATION_API_ENV.prd;\n\n// Gets notification settings for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = (env: ENV = 'prd') =>\n `${TRIGGER_API(env)}/api/v2/notifications/query`;\n\n// Used to create/update account notifications for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_ENDPOINT = (env: ENV = 'prd') =>\n `${TRIGGER_API(env)}/api/v2/notifications`;\n\n// Lists notifications for each account provided\nexport const NOTIFICATION_API_LIST_ENDPOINT = (env: ENV = 'prd') =>\n `${NOTIFICATION_API(env)}/api/v3/notifications`;\n\n// Makrs notifications as read\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = (env: ENV = 'prd') =>\n `${NOTIFICATION_API(env)}/api/v3/notifications/mark-as-read`;\n\n/**\n * fetches notification config (accounts enabled vs disabled)\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * NOTE the API will return addresses config with false if they have not been created before.\n * NOTE this is cached for 1s to prevent multiple update calls\n * @returns object of notification config, or null if missing\n */\nexport async function getNotificationsApiConfigCached(\n bearerToken: string,\n addresses: string[],\n env: ENV = 'prd',\n) {\n if (addresses.length === 0) {\n return [];\n }\n\n addresses = addresses.map((a) => a.toLowerCase());\n\n const cached = notificationsConfigCache.get(addresses);\n if (cached) {\n return cached;\n }\n\n type RequestBody = { address: string }[];\n type Response = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses.map((address) => ({ address }));\n const data = await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT(env),\n 'POST',\n body,\n )\n .then<Response | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n const result = data ?? [];\n\n if (result.length > 0) {\n notificationsConfigCache.set(result);\n }\n\n return result;\n}\n\n/**\n * updates notifications for a given addresses\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * @returns void\n */\nexport async function updateOnChainNotifications(\n bearerToken: string,\n addresses: { address: string; enabled: boolean }[],\n env: ENV = 'prd',\n) {\n if (addresses.length === 0) {\n return;\n }\n\n addresses = addresses.map((a) => {\n a.address = a.address.toLowerCase();\n return a;\n });\n\n type RequestBody = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses;\n await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_ENDPOINT(env),\n 'POST',\n body,\n )\n .then(() => notificationsConfigCache.set(addresses))\n .catch(() => null);\n}\n\n/**\n * Fetches on-chain notifications for the given addresses\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param addresses - List of addresses\n * @param locale - to generate translated notifications\n * @param platform - filter notifications for specific platforms ('extension' | 'mobile')\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.\n */\nexport async function getAPINotifications(\n bearerToken: string,\n addresses: string[],\n locale: string,\n platform: 'extension' | 'mobile',\n env: ENV = 'prd',\n): Promise<NormalisedAPINotification[]> {\n if (addresses.length === 0) {\n return [];\n }\n\n type RequestBody =\n Schema.paths['/api/v3/notifications']['post']['requestBody']['content']['application/json'];\n type APIResponse =\n Schema.paths['/api/v3/notifications']['post']['responses']['200']['content']['application/json'];\n\n const body: RequestBody = {\n addresses: addresses.map((a) => a.toLowerCase()),\n locale,\n platform,\n };\n const notifications = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT(env),\n 'POST',\n body,\n )\n .then<APIResponse | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n ?.map((n): UnprocessedRawNotification | undefined => {\n if (!n.notification_type) {\n return undefined;\n }\n\n try {\n return toRawAPINotification(n);\n } catch {\n return undefined;\n }\n })\n .filter((n): n is NormalisedAPINotification => Boolean(n));\n\n return transformedNotifications ?? [];\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n env: ENV = 'prd',\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n type ResponseBody =\n Schema.paths['/api/v3/notifications/mark-as-read']['post']['requestBody']['content']['application/json'];\n const body: ResponseBody = {\n ids: notificationIds,\n };\n\n try {\n await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT(env),\n 'POST',\n body,\n );\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n }\n}\n"]}
|
|
@@ -5,22 +5,24 @@ export type NotificationTrigger = {
|
|
|
5
5
|
kind: string;
|
|
6
6
|
address: string;
|
|
7
7
|
};
|
|
8
|
-
export
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
8
|
+
export type ENV = 'prd' | 'uat' | 'dev';
|
|
9
|
+
export declare const TRIGGER_API: (env?: ENV) => string;
|
|
10
|
+
export declare const NOTIFICATION_API: (env?: ENV) => string;
|
|
11
|
+
export declare const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT: (env?: ENV) => string;
|
|
12
|
+
export declare const TRIGGER_API_NOTIFICATIONS_ENDPOINT: (env?: ENV) => string;
|
|
13
|
+
export declare const NOTIFICATION_API_LIST_ENDPOINT: (env?: ENV) => string;
|
|
14
|
+
export declare const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT: (env?: ENV) => string;
|
|
14
15
|
/**
|
|
15
16
|
* fetches notification config (accounts enabled vs disabled)
|
|
16
17
|
*
|
|
17
18
|
* @param bearerToken - jwt
|
|
18
19
|
* @param addresses - list of addresses to check
|
|
20
|
+
* @param env - the environment to use for the API call
|
|
19
21
|
* NOTE the API will return addresses config with false if they have not been created before.
|
|
20
22
|
* NOTE this is cached for 1s to prevent multiple update calls
|
|
21
23
|
* @returns object of notification config, or null if missing
|
|
22
24
|
*/
|
|
23
|
-
export declare function getNotificationsApiConfigCached(bearerToken: string, addresses: string[]): Promise<{
|
|
25
|
+
export declare function getNotificationsApiConfigCached(bearerToken: string, addresses: string[], env?: ENV): Promise<{
|
|
24
26
|
address: string;
|
|
25
27
|
enabled: boolean;
|
|
26
28
|
}[]>;
|
|
@@ -29,12 +31,13 @@ export declare function getNotificationsApiConfigCached(bearerToken: string, add
|
|
|
29
31
|
*
|
|
30
32
|
* @param bearerToken - jwt
|
|
31
33
|
* @param addresses - list of addresses to check
|
|
34
|
+
* @param env - the environment to use for the API call
|
|
32
35
|
* @returns void
|
|
33
36
|
*/
|
|
34
37
|
export declare function updateOnChainNotifications(bearerToken: string, addresses: {
|
|
35
38
|
address: string;
|
|
36
39
|
enabled: boolean;
|
|
37
|
-
}[]): Promise<void>;
|
|
40
|
+
}[], env?: ENV): Promise<void>;
|
|
38
41
|
/**
|
|
39
42
|
* Fetches on-chain notifications for the given addresses
|
|
40
43
|
*
|
|
@@ -42,9 +45,10 @@ export declare function updateOnChainNotifications(bearerToken: string, addresse
|
|
|
42
45
|
* @param addresses - List of addresses
|
|
43
46
|
* @param locale - to generate translated notifications
|
|
44
47
|
* @param platform - filter notifications for specific platforms ('extension' | 'mobile')
|
|
48
|
+
* @param env - the environment to use for the API call
|
|
45
49
|
* @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.
|
|
46
50
|
*/
|
|
47
|
-
export declare function getAPINotifications(bearerToken: string, addresses: string[], locale: string, platform: 'extension' | 'mobile'): Promise<NormalisedAPINotification[]>;
|
|
51
|
+
export declare function getAPINotifications(bearerToken: string, addresses: string[], locale: string, platform: 'extension' | 'mobile', env?: ENV): Promise<NormalisedAPINotification[]>;
|
|
48
52
|
/**
|
|
49
53
|
* Marks the specified notifications as read.
|
|
50
54
|
* This method sends a POST request to the notifications service to mark the provided notification IDs as read.
|
|
@@ -52,7 +56,8 @@ export declare function getAPINotifications(bearerToken: string, addresses: stri
|
|
|
52
56
|
*
|
|
53
57
|
* @param bearerToken - The JSON Web Token used for authentication in the API call.
|
|
54
58
|
* @param notificationIds - An array of notification IDs to be marked as read.
|
|
59
|
+
* @param env - the environment to use for the API call
|
|
55
60
|
* @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.
|
|
56
61
|
*/
|
|
57
|
-
export declare function markNotificationsAsRead(bearerToken: string, notificationIds: string[]): Promise<void>;
|
|
62
|
+
export declare function markNotificationsAsRead(bearerToken: string, notificationIds: string[], env?: ENV): Promise<void>;
|
|
58
63
|
//# sourceMappingURL=api-notifications.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-notifications.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,yBAAyB,EAG1B,4CAAkC;AAGnC,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"api-notifications.d.cts","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,yBAAyB,EAG1B,4CAAkC;AAGnC,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,GAAG,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAQxC,eAAO,MAAM,WAAW,SAAS,GAAG,WACS,CAAC;AAQ9C,eAAO,MAAM,gBAAgB,SAAS,GAAG,WACc,CAAC;AAGxD,eAAO,MAAM,wCAAwC,SAAS,GAAG,WACf,CAAC;AAGnD,eAAO,MAAM,kCAAkC,SAAS,GAAG,WACf,CAAC;AAG7C,eAAO,MAAM,8BAA8B,SAAS,GAAG,WACN,CAAC;AAGlD,eAAO,MAAM,0CAA0C,SAAS,GAAG,WACL,CAAC;AAE/D;;;;;;;;;GASG;AACH,wBAAsB,+BAA+B,CACnD,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EAAE,EACnB,GAAG,GAAE,GAAW;;;KAgCjB;AAED;;;;;;;GAOG;AACH,wBAAsB,0BAA0B,CAC9C,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,EAAE,EAClD,GAAG,GAAE,GAAW,iBAqBjB;AAED;;;;;;;;;GASG;AACH,wBAAsB,mBAAmB,CACvC,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EAAE,EACnB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,WAAW,GAAG,QAAQ,EAChC,GAAG,GAAE,GAAW,GACf,OAAO,CAAC,yBAAyB,EAAE,CAAC,CAwCtC;AAED;;;;;;;;;GASG;AACH,wBAAsB,uBAAuB,CAC3C,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,MAAM,EAAE,EACzB,GAAG,GAAE,GAAW,GACf,OAAO,CAAC,IAAI,CAAC,CAqBf"}
|
|
@@ -5,22 +5,24 @@ export type NotificationTrigger = {
|
|
|
5
5
|
kind: string;
|
|
6
6
|
address: string;
|
|
7
7
|
};
|
|
8
|
-
export
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
8
|
+
export type ENV = 'prd' | 'uat' | 'dev';
|
|
9
|
+
export declare const TRIGGER_API: (env?: ENV) => string;
|
|
10
|
+
export declare const NOTIFICATION_API: (env?: ENV) => string;
|
|
11
|
+
export declare const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT: (env?: ENV) => string;
|
|
12
|
+
export declare const TRIGGER_API_NOTIFICATIONS_ENDPOINT: (env?: ENV) => string;
|
|
13
|
+
export declare const NOTIFICATION_API_LIST_ENDPOINT: (env?: ENV) => string;
|
|
14
|
+
export declare const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT: (env?: ENV) => string;
|
|
14
15
|
/**
|
|
15
16
|
* fetches notification config (accounts enabled vs disabled)
|
|
16
17
|
*
|
|
17
18
|
* @param bearerToken - jwt
|
|
18
19
|
* @param addresses - list of addresses to check
|
|
20
|
+
* @param env - the environment to use for the API call
|
|
19
21
|
* NOTE the API will return addresses config with false if they have not been created before.
|
|
20
22
|
* NOTE this is cached for 1s to prevent multiple update calls
|
|
21
23
|
* @returns object of notification config, or null if missing
|
|
22
24
|
*/
|
|
23
|
-
export declare function getNotificationsApiConfigCached(bearerToken: string, addresses: string[]): Promise<{
|
|
25
|
+
export declare function getNotificationsApiConfigCached(bearerToken: string, addresses: string[], env?: ENV): Promise<{
|
|
24
26
|
address: string;
|
|
25
27
|
enabled: boolean;
|
|
26
28
|
}[]>;
|
|
@@ -29,12 +31,13 @@ export declare function getNotificationsApiConfigCached(bearerToken: string, add
|
|
|
29
31
|
*
|
|
30
32
|
* @param bearerToken - jwt
|
|
31
33
|
* @param addresses - list of addresses to check
|
|
34
|
+
* @param env - the environment to use for the API call
|
|
32
35
|
* @returns void
|
|
33
36
|
*/
|
|
34
37
|
export declare function updateOnChainNotifications(bearerToken: string, addresses: {
|
|
35
38
|
address: string;
|
|
36
39
|
enabled: boolean;
|
|
37
|
-
}[]): Promise<void>;
|
|
40
|
+
}[], env?: ENV): Promise<void>;
|
|
38
41
|
/**
|
|
39
42
|
* Fetches on-chain notifications for the given addresses
|
|
40
43
|
*
|
|
@@ -42,9 +45,10 @@ export declare function updateOnChainNotifications(bearerToken: string, addresse
|
|
|
42
45
|
* @param addresses - List of addresses
|
|
43
46
|
* @param locale - to generate translated notifications
|
|
44
47
|
* @param platform - filter notifications for specific platforms ('extension' | 'mobile')
|
|
48
|
+
* @param env - the environment to use for the API call
|
|
45
49
|
* @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.
|
|
46
50
|
*/
|
|
47
|
-
export declare function getAPINotifications(bearerToken: string, addresses: string[], locale: string, platform: 'extension' | 'mobile'): Promise<NormalisedAPINotification[]>;
|
|
51
|
+
export declare function getAPINotifications(bearerToken: string, addresses: string[], locale: string, platform: 'extension' | 'mobile', env?: ENV): Promise<NormalisedAPINotification[]>;
|
|
48
52
|
/**
|
|
49
53
|
* Marks the specified notifications as read.
|
|
50
54
|
* This method sends a POST request to the notifications service to mark the provided notification IDs as read.
|
|
@@ -52,7 +56,8 @@ export declare function getAPINotifications(bearerToken: string, addresses: stri
|
|
|
52
56
|
*
|
|
53
57
|
* @param bearerToken - The JSON Web Token used for authentication in the API call.
|
|
54
58
|
* @param notificationIds - An array of notification IDs to be marked as read.
|
|
59
|
+
* @param env - the environment to use for the API call
|
|
55
60
|
* @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.
|
|
56
61
|
*/
|
|
57
|
-
export declare function markNotificationsAsRead(bearerToken: string, notificationIds: string[]): Promise<void>;
|
|
62
|
+
export declare function markNotificationsAsRead(bearerToken: string, notificationIds: string[], env?: ENV): Promise<void>;
|
|
58
63
|
//# sourceMappingURL=api-notifications.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-notifications.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,yBAAyB,EAG1B,4CAAkC;AAGnC,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"api-notifications.d.mts","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,yBAAyB,EAG1B,4CAAkC;AAGnC,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,E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|
|
9
9
|
import { notificationsConfigCache } from "./notification-config-cache.mjs";
|
|
10
10
|
import { toRawAPINotification } from "../../shared/to-raw-notification.mjs";
|
|
11
11
|
import { makeApiCall } from "../utils/utils.mjs";
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const TRIGGER_API_ENV = {
|
|
13
|
+
dev: 'https://trigger.dev-api.cx.metamask.io',
|
|
14
|
+
uat: 'https://trigger.uat-api.cx.metamask.io',
|
|
15
|
+
prd: 'https://trigger.api.cx.metamask.io',
|
|
16
|
+
};
|
|
17
|
+
export const TRIGGER_API = (env = 'prd') => TRIGGER_API_ENV[env] ?? TRIGGER_API_ENV.prd;
|
|
18
|
+
const NOTIFICATION_API_ENV = {
|
|
19
|
+
dev: 'https://notification.dev-api.cx.metamask.io',
|
|
20
|
+
uat: 'https://notification.uat-api.cx.metamask.io',
|
|
21
|
+
prd: 'https://notification.api.cx.metamask.io',
|
|
22
|
+
};
|
|
23
|
+
export const NOTIFICATION_API = (env = 'prd') => NOTIFICATION_API_ENV[env] ?? NOTIFICATION_API_ENV.prd;
|
|
14
24
|
// Gets notification settings for each account provided
|
|
15
|
-
export const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = `${TRIGGER_API}/api/v2/notifications/query`;
|
|
25
|
+
export const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = (env = 'prd') => `${TRIGGER_API(env)}/api/v2/notifications/query`;
|
|
16
26
|
// Used to create/update account notifications for each account provided
|
|
17
|
-
export const TRIGGER_API_NOTIFICATIONS_ENDPOINT = `${TRIGGER_API}/api/v2/notifications`;
|
|
27
|
+
export const TRIGGER_API_NOTIFICATIONS_ENDPOINT = (env = 'prd') => `${TRIGGER_API(env)}/api/v2/notifications`;
|
|
18
28
|
// Lists notifications for each account provided
|
|
19
|
-
export const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications`;
|
|
29
|
+
export const NOTIFICATION_API_LIST_ENDPOINT = (env = 'prd') => `${NOTIFICATION_API(env)}/api/v3/notifications`;
|
|
20
30
|
// Makrs notifications as read
|
|
21
|
-
export const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications/mark-as-read`;
|
|
31
|
+
export const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = (env = 'prd') => `${NOTIFICATION_API(env)}/api/v3/notifications/mark-as-read`;
|
|
22
32
|
/**
|
|
23
33
|
* fetches notification config (accounts enabled vs disabled)
|
|
24
34
|
*
|
|
25
35
|
* @param bearerToken - jwt
|
|
26
36
|
* @param addresses - list of addresses to check
|
|
37
|
+
* @param env - the environment to use for the API call
|
|
27
38
|
* NOTE the API will return addresses config with false if they have not been created before.
|
|
28
39
|
* NOTE this is cached for 1s to prevent multiple update calls
|
|
29
40
|
* @returns object of notification config, or null if missing
|
|
30
41
|
*/
|
|
31
|
-
export async function getNotificationsApiConfigCached(bearerToken, addresses) {
|
|
42
|
+
export async function getNotificationsApiConfigCached(bearerToken, addresses, env = 'prd') {
|
|
32
43
|
if (addresses.length === 0) {
|
|
33
44
|
return [];
|
|
34
45
|
}
|
|
@@ -38,7 +49,7 @@ export async function getNotificationsApiConfigCached(bearerToken, addresses) {
|
|
|
38
49
|
return cached;
|
|
39
50
|
}
|
|
40
51
|
const body = addresses.map((address) => ({ address }));
|
|
41
|
-
const data = await makeApiCall(bearerToken, TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT, 'POST', body)
|
|
52
|
+
const data = await makeApiCall(bearerToken, TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT(env), 'POST', body)
|
|
42
53
|
.then((r) => (r.ok ? r.json() : null))
|
|
43
54
|
.catch(() => null);
|
|
44
55
|
const result = data ?? [];
|
|
@@ -52,9 +63,10 @@ export async function getNotificationsApiConfigCached(bearerToken, addresses) {
|
|
|
52
63
|
*
|
|
53
64
|
* @param bearerToken - jwt
|
|
54
65
|
* @param addresses - list of addresses to check
|
|
66
|
+
* @param env - the environment to use for the API call
|
|
55
67
|
* @returns void
|
|
56
68
|
*/
|
|
57
|
-
export async function updateOnChainNotifications(bearerToken, addresses) {
|
|
69
|
+
export async function updateOnChainNotifications(bearerToken, addresses, env = 'prd') {
|
|
58
70
|
if (addresses.length === 0) {
|
|
59
71
|
return;
|
|
60
72
|
}
|
|
@@ -63,7 +75,7 @@ export async function updateOnChainNotifications(bearerToken, addresses) {
|
|
|
63
75
|
return a;
|
|
64
76
|
});
|
|
65
77
|
const body = addresses;
|
|
66
|
-
await makeApiCall(bearerToken, TRIGGER_API_NOTIFICATIONS_ENDPOINT, 'POST', body)
|
|
78
|
+
await makeApiCall(bearerToken, TRIGGER_API_NOTIFICATIONS_ENDPOINT(env), 'POST', body)
|
|
67
79
|
.then(() => notificationsConfigCache.set(addresses))
|
|
68
80
|
.catch(() => null);
|
|
69
81
|
}
|
|
@@ -74,9 +86,10 @@ export async function updateOnChainNotifications(bearerToken, addresses) {
|
|
|
74
86
|
* @param addresses - List of addresses
|
|
75
87
|
* @param locale - to generate translated notifications
|
|
76
88
|
* @param platform - filter notifications for specific platforms ('extension' | 'mobile')
|
|
89
|
+
* @param env - the environment to use for the API call
|
|
77
90
|
* @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.
|
|
78
91
|
*/
|
|
79
|
-
export async function getAPINotifications(bearerToken, addresses, locale, platform) {
|
|
92
|
+
export async function getAPINotifications(bearerToken, addresses, locale, platform, env = 'prd') {
|
|
80
93
|
if (addresses.length === 0) {
|
|
81
94
|
return [];
|
|
82
95
|
}
|
|
@@ -85,7 +98,7 @@ export async function getAPINotifications(bearerToken, addresses, locale, platfo
|
|
|
85
98
|
locale,
|
|
86
99
|
platform,
|
|
87
100
|
};
|
|
88
|
-
const notifications = await makeApiCall(bearerToken, NOTIFICATION_API_LIST_ENDPOINT, 'POST', body)
|
|
101
|
+
const notifications = await makeApiCall(bearerToken, NOTIFICATION_API_LIST_ENDPOINT(env), 'POST', body)
|
|
89
102
|
.then((r) => (r.ok ? r.json() : null))
|
|
90
103
|
.catch(() => null);
|
|
91
104
|
// Transform and sort notifications
|
|
@@ -111,9 +124,10 @@ export async function getAPINotifications(bearerToken, addresses, locale, platfo
|
|
|
111
124
|
*
|
|
112
125
|
* @param bearerToken - The JSON Web Token used for authentication in the API call.
|
|
113
126
|
* @param notificationIds - An array of notification IDs to be marked as read.
|
|
127
|
+
* @param env - the environment to use for the API call
|
|
114
128
|
* @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.
|
|
115
129
|
*/
|
|
116
|
-
export async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
130
|
+
export async function markNotificationsAsRead(bearerToken, notificationIds, env = 'prd') {
|
|
117
131
|
if (notificationIds.length === 0) {
|
|
118
132
|
return;
|
|
119
133
|
}
|
|
@@ -121,7 +135,7 @@ export async function markNotificationsAsRead(bearerToken, notificationIds) {
|
|
|
121
135
|
ids: notificationIds,
|
|
122
136
|
};
|
|
123
137
|
try {
|
|
124
|
-
await makeApiCall(bearerToken, NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT, 'POST', body);
|
|
138
|
+
await makeApiCall(bearerToken, NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT(env), 'POST', body);
|
|
125
139
|
}
|
|
126
140
|
catch (err) {
|
|
127
141
|
log.error('Error marking notifications as read:', err);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-notifications.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,oBAAoB,EAAE,6CAAyC;AAMxE,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAS7C,MAAM,CAAC,MAAM,WAAW,GAAG,oCAAoC,CAAC;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAAG,yCAAyC,CA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AC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SACzD,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;IAErB,mCAAmC;IACnC,MAAM,wBAAwB,GAAG,aAAa;QAC5C,EAAE,GAAG,CAAC,CAAC,CAAC,EAA0C,EAAE;QAClD,IAAI,CAAC,CAAC,CAAC,iBAAiB,EAAE,CAAC;YACzB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,IAAI,CAAC;YACH,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,CAAC,EAAkC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAE7D,OAAO,wBAAwB,IAAI,EAAE,CAAC;AACxC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,WAAmB,EACnB,eAAyB;IAEzB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,OAAO;IACT,CAAC;IAID,MAAM,IAAI,GAAiB;QACzB,GAAG,EAAE,eAAe;KACrB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,WAAW,CACf,WAAW,EACX,0CAA0C,EAC1C,MAAM,EACN,IAAI,CACL,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,GAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport { notificationsConfigCache } from './notification-config-cache';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type {\n NormalisedAPINotification,\n Schema,\n UnprocessedRawNotification,\n} from '../types/notification-api';\nimport { makeApiCall } from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport const TRIGGER_API = 'https://trigger.api.cx.metamask.io';\nexport const NOTIFICATION_API = 'https://notification.api.cx.metamask.io';\n\n// Gets notification settings for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = `${TRIGGER_API}/api/v2/notifications/query`;\n\n// Used to create/update account notifications for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_ENDPOINT = `${TRIGGER_API}/api/v2/notifications`;\n\n// Lists notifications for each account provided\nexport const NOTIFICATION_API_LIST_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications`;\n\n// Makrs notifications as read\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = `${NOTIFICATION_API}/api/v3/notifications/mark-as-read`;\n\n/**\n * fetches notification config (accounts enabled vs disabled)\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * NOTE the API will return addresses config with false if they have not been created before.\n * NOTE this is cached for 1s to prevent multiple update calls\n * @returns object of notification config, or null if missing\n */\nexport async function getNotificationsApiConfigCached(\n bearerToken: string,\n addresses: string[],\n) {\n if (addresses.length === 0) {\n return [];\n }\n\n addresses = addresses.map((a) => a.toLowerCase());\n\n const cached = notificationsConfigCache.get(addresses);\n if (cached) {\n return cached;\n }\n\n type RequestBody = { address: string }[];\n type Response = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses.map((address) => ({ address }));\n const data = await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT,\n 'POST',\n body,\n )\n .then<Response | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n const result = data ?? [];\n\n if (result.length > 0) {\n notificationsConfigCache.set(result);\n }\n\n return result;\n}\n\n/**\n * updates notifications for a given addresses\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @returns void\n */\nexport async function updateOnChainNotifications(\n bearerToken: string,\n addresses: { address: string; enabled: boolean }[],\n) {\n if (addresses.length === 0) {\n return;\n }\n\n addresses = addresses.map((a) => {\n a.address = a.address.toLowerCase();\n return a;\n });\n\n type RequestBody = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses;\n await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_ENDPOINT,\n 'POST',\n body,\n )\n .then(() => notificationsConfigCache.set(addresses))\n .catch(() => null);\n}\n\n/**\n * Fetches on-chain notifications for the given addresses\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param addresses - List of addresses\n * @param locale - to generate translated notifications\n * @param platform - filter notifications for specific platforms ('extension' | 'mobile')\n * @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.\n */\nexport async function getAPINotifications(\n bearerToken: string,\n addresses: string[],\n locale: string,\n platform: 'extension' | 'mobile',\n): Promise<NormalisedAPINotification[]> {\n if (addresses.length === 0) {\n return [];\n }\n\n type RequestBody =\n Schema.paths['/api/v3/notifications']['post']['requestBody']['content']['application/json'];\n type APIResponse =\n Schema.paths['/api/v3/notifications']['post']['responses']['200']['content']['application/json'];\n\n const body: RequestBody = {\n addresses: addresses.map((a) => a.toLowerCase()),\n locale,\n platform,\n };\n const notifications = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT,\n 'POST',\n body,\n )\n .then<APIResponse | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n ?.map((n): UnprocessedRawNotification | undefined => {\n if (!n.notification_type) {\n return undefined;\n }\n\n try {\n return toRawAPINotification(n);\n } catch {\n return undefined;\n }\n })\n .filter((n): n is NormalisedAPINotification => Boolean(n));\n\n return transformedNotifications ?? [];\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n type ResponseBody =\n Schema.paths['/api/v3/notifications/mark-as-read']['post']['requestBody']['content']['application/json'];\n const body: ResponseBody = {\n ids: notificationIds,\n };\n\n try {\n await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT,\n 'POST',\n body,\n );\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"api-notifications.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,oBAAoB,EAAE,6CAAyC;AAMxE,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAW7C,MAAM,eAAe,GAAG;IACtB,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,oCAAoC;CACZ,CAAC;AAEhC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAW,KAAK,EAAE,EAAE,CAC9C,eAAe,CAAC,GAAG,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC;AAE9C,MAAM,oBAAoB,GAAG;IAC3B,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,yCAAyC;CAC/C,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,MAAW,KAAK,EAAE,EAAE,CACnD,oBAAoB,CAAC,GAAG,CAAC,IAAI,oBAAoB,CAAC,GAAG,CAAC;AAExD,uDAAuD;AACvD,MAAM,CAAC,MAAM,wCAAwC,GAAG,CAAC,MAAW,KAAK,EAAE,EAA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OAAO,GAAG,EAAE,CAAC;QACb,GAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport { notificationsConfigCache } from './notification-config-cache';\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type {\n NormalisedAPINotification,\n Schema,\n UnprocessedRawNotification,\n} from '../types/notification-api';\nimport { makeApiCall } from '../utils/utils';\n\nexport type NotificationTrigger = {\n id: string;\n chainId: string;\n kind: string;\n address: string;\n};\n\nexport type ENV = 'prd' | 'uat' | 'dev';\n\nconst TRIGGER_API_ENV = {\n dev: 'https://trigger.dev-api.cx.metamask.io',\n uat: 'https://trigger.uat-api.cx.metamask.io',\n prd: 'https://trigger.api.cx.metamask.io',\n} satisfies Record<ENV, string>;\n\nexport const TRIGGER_API = (env: ENV = 'prd') =>\n TRIGGER_API_ENV[env] ?? TRIGGER_API_ENV.prd;\n\nconst NOTIFICATION_API_ENV = {\n dev: 'https://notification.dev-api.cx.metamask.io',\n uat: 'https://notification.uat-api.cx.metamask.io',\n prd: 'https://notification.api.cx.metamask.io',\n};\n\nexport const NOTIFICATION_API = (env: ENV = 'prd') =>\n NOTIFICATION_API_ENV[env] ?? NOTIFICATION_API_ENV.prd;\n\n// Gets notification settings for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = (env: ENV = 'prd') =>\n `${TRIGGER_API(env)}/api/v2/notifications/query`;\n\n// Used to create/update account notifications for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_ENDPOINT = (env: ENV = 'prd') =>\n `${TRIGGER_API(env)}/api/v2/notifications`;\n\n// Lists notifications for each account provided\nexport const NOTIFICATION_API_LIST_ENDPOINT = (env: ENV = 'prd') =>\n `${NOTIFICATION_API(env)}/api/v3/notifications`;\n\n// Makrs notifications as read\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = (env: ENV = 'prd') =>\n `${NOTIFICATION_API(env)}/api/v3/notifications/mark-as-read`;\n\n/**\n * fetches notification config (accounts enabled vs disabled)\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * NOTE the API will return addresses config with false if they have not been created before.\n * NOTE this is cached for 1s to prevent multiple update calls\n * @returns object of notification config, or null if missing\n */\nexport async function getNotificationsApiConfigCached(\n bearerToken: string,\n addresses: string[],\n env: ENV = 'prd',\n) {\n if (addresses.length === 0) {\n return [];\n }\n\n addresses = addresses.map((a) => a.toLowerCase());\n\n const cached = notificationsConfigCache.get(addresses);\n if (cached) {\n return cached;\n }\n\n type RequestBody = { address: string }[];\n type Response = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses.map((address) => ({ address }));\n const data = await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT(env),\n 'POST',\n body,\n )\n .then<Response | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n const result = data ?? [];\n\n if (result.length > 0) {\n notificationsConfigCache.set(result);\n }\n\n return result;\n}\n\n/**\n * updates notifications for a given addresses\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * @returns void\n */\nexport async function updateOnChainNotifications(\n bearerToken: string,\n addresses: { address: string; enabled: boolean }[],\n env: ENV = 'prd',\n) {\n if (addresses.length === 0) {\n return;\n }\n\n addresses = addresses.map((a) => {\n a.address = a.address.toLowerCase();\n return a;\n });\n\n type RequestBody = { address: string; enabled: boolean }[];\n const body: RequestBody = addresses;\n await makeApiCall(\n bearerToken,\n TRIGGER_API_NOTIFICATIONS_ENDPOINT(env),\n 'POST',\n body,\n )\n .then(() => notificationsConfigCache.set(addresses))\n .catch(() => null);\n}\n\n/**\n * Fetches on-chain notifications for the given addresses\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param addresses - List of addresses\n * @param locale - to generate translated notifications\n * @param platform - filter notifications for specific platforms ('extension' | 'mobile')\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.\n */\nexport async function getAPINotifications(\n bearerToken: string,\n addresses: string[],\n locale: string,\n platform: 'extension' | 'mobile',\n env: ENV = 'prd',\n): Promise<NormalisedAPINotification[]> {\n if (addresses.length === 0) {\n return [];\n }\n\n type RequestBody =\n Schema.paths['/api/v3/notifications']['post']['requestBody']['content']['application/json'];\n type APIResponse =\n Schema.paths['/api/v3/notifications']['post']['responses']['200']['content']['application/json'];\n\n const body: RequestBody = {\n addresses: addresses.map((a) => a.toLowerCase()),\n locale,\n platform,\n };\n const notifications = await makeApiCall(\n bearerToken,\n NOTIFICATION_API_LIST_ENDPOINT(env),\n 'POST',\n body,\n )\n .then<APIResponse | null>((r) => (r.ok ? r.json() : null))\n .catch(() => null);\n\n // Transform and sort notifications\n const transformedNotifications = notifications\n ?.map((n): UnprocessedRawNotification | undefined => {\n if (!n.notification_type) {\n return undefined;\n }\n\n try {\n return toRawAPINotification(n);\n } catch {\n return undefined;\n }\n })\n .filter((n): n is NormalisedAPINotification => Boolean(n));\n\n return transformedNotifications ?? [];\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n bearerToken: string,\n notificationIds: string[],\n env: ENV = 'prd',\n): Promise<void> {\n if (notificationIds.length === 0) {\n return;\n }\n\n type ResponseBody =\n Schema.paths['/api/v3/notifications/mark-as-read']['post']['requestBody']['content']['application/json'];\n const body: ResponseBody = {\n ids: notificationIds,\n };\n\n try {\n await makeApiCall(\n bearerToken,\n NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT(env),\n 'POST',\n body,\n );\n } catch (err) {\n log.error('Error marking notifications as read:', err);\n }\n}\n"]}
|
|
@@ -127,6 +127,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
127
127
|
locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
|
|
128
128
|
oldToken: this.state.fcmToken,
|
|
129
129
|
},
|
|
130
|
+
controllerEnv: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").env ?? 'prd',
|
|
130
131
|
});
|
|
131
132
|
if (fcmToken) {
|
|
132
133
|
__classPrivateFieldGet(this, _NotificationServicesPushController_instances, "m", _NotificationServicesPushController_updatePushState).call(this, { type: 'enable', fcmToken });
|
|
@@ -208,6 +209,7 @@ class NotificationServicesPushController extends base_controller_1.BaseControlle
|
|
|
208
209
|
locale: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").getLocale?.() ?? 'en',
|
|
209
210
|
oldToken: this.state.fcmToken,
|
|
210
211
|
},
|
|
212
|
+
controllerEnv: __classPrivateFieldGet(this, _NotificationServicesPushController_config, "f").env ?? 'prd',
|
|
211
213
|
});
|
|
212
214
|
// update the state with the new FCM token
|
|
213
215
|
if (fcmToken) {
|