( targetedNotifications: $ReadOnlyArray<TargetedWebNotification>, )
| 240 | +error?: WebPushError, |
| 241 | }; |
| 242 | async function webPush( |
| 243 | targetedNotifications: $ReadOnlyArray<TargetedWebNotification>, |
| 244 | ): Promise<WebPushResult> { |
| 245 | await ensureWebPushInitialized(); |
| 246 | |
| 247 | const pushResults: $ReadOnlyArray<WebPushAttempt> = await Promise.all( |
| 248 | targetedNotifications.map( |
| 249 | async ({ notification, deliveryID: deviceTokenString }) => { |
| 250 | const deviceToken: PushSubscriptionJSON = JSON.parse(deviceTokenString); |
| 251 | const notificationString = JSON.stringify(notification); |
| 252 | try { |
| 253 | await webpush.sendNotification(deviceToken, notificationString); |
| 254 | } catch (error) { |
| 255 | return ({ error }: WebPushAttempt); |
| 256 | } |
| 257 | return {}; |
| 258 | }, |
| 259 | ), |
| 260 | ); |
| 261 | |
| 262 | const errors = []; |
| 263 | const invalidTokens = []; |
| 264 | const deviceTokens = targetedNotifications.map( |
| 265 | ({ deliveryID }) => deliveryID, |
| 266 | ); |
| 267 | for (let i = 0; i < pushResults.length; i++) { |
| 268 | const pushResult = pushResults[i]; |
| 269 | const { error } = pushResult; |
| 270 | if (error) { |
| 271 | errors.push(error); |
| 272 | if (webInvalidTokenErrorCodes.includes(error.statusCode)) { |
| 273 | invalidTokens.push(deviceTokens[i]); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | const result: WritableWebPushResult = {}; |
| 279 | if (errors.length > 0) { |
| 280 | result.errors = errors; |
| 281 | } else { |
| 282 | result.success = true; |
| 283 | } |
| 284 | if (invalidTokens.length > 0) { |
| 285 | result.invalidTokens = invalidTokens; |
| 286 | } |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | export type WNSPushError = any | string | Response; |
| 291 | type WritableWNSPushResult = { |
no test coverage detected