UNPKG

@unchainedshop/plugins

Version:

Because of a Typescript issue with upstream "postfinancecheckout", the Postfinance plugin has been disabled from transpilation, import the source ts files from src and enable node_module tsc or copy over the src/payment/postfinance-checkout to your projec

70 lines 2.3 kB
import { WorkerAdapter, WorkerDirector } from '@unchainedshop/core'; import webPush from 'web-push'; const { PUSH_NOTIFICATION_PUBLIC_KEY, PUSH_NOTIFICATION_PRIVATE_KEY } = process.env; const PushNotificationWorkerPlugin = { ...WorkerAdapter, key: 'shop.unchained.worker-plugin.push-notification', label: 'Push Notification', version: '1.0', type: 'PUSH', doWork: async ({ subscription, subject, payload, urgency = null, topic = null }) => { if (!PUSH_NOTIFICATION_PUBLIC_KEY) return { success: false, error: { name: 'VAPID_PUBLIC_KEY_REQUIRED', message: 'vapidPublicKey key required to send push notifications', }, }; if (!PUSH_NOTIFICATION_PRIVATE_KEY) return { success: false, error: { name: 'VAPID_PRIVATE_KEY_REQUIRED', message: 'vapidPrivateKey key are required to send push notifications', }, }; if (!subscription) { return { success: false, error: { name: 'USER_SUBSCRIPTION_OBJECT_REQUIRED', message: 'PUSH service subscription required', }, }; } const options = { vapidDetails: { subject, publicKey: PUSH_NOTIFICATION_PUBLIC_KEY, privateKey: PUSH_NOTIFICATION_PRIVATE_KEY, }, TTL: 60, }; if (urgency) { options.urgency = urgency; } if (topic) { options.topic = topic; } try { await webPush.sendNotification(subscription, payload, options); return { success: true, error: null, }; } catch (err) { return { success: false, error: { name: err.name, message: err.message, stack: err.stack, }, }; } }, }; WorkerDirector.registerAdapter(PushNotificationWorkerPlugin); //# sourceMappingURL=push-notification.js.map