|
| 1 | +--- |
| 2 | +title: Service |
| 3 | +--- |
| 4 | + |
| 5 | +# Service |
| 6 | + |
| 7 | +## Service |
| 8 | + |
| 9 | +### Signature |
| 10 | + |
| 11 | +```js |
| 12 | +new Service(options) |
| 13 | +``` |
| 14 | + |
| 15 | +### Description |
| 16 | + |
| 17 | +A Feathers service for sending web push notifications to subscribed users. |
| 18 | + |
| 19 | +This service retrieves subscriptions from a specified Feathers service, |
| 20 | +validates them, and sends notifications using the `web-push` library. |
| 21 | +It handles successful and failed deliveries and logs relevant debug information. |
| 22 | + |
| 23 | +### Constructor Parameters |
| 24 | + |
| 25 | +| Name | Type | Required | Description | |
| 26 | +|------|------|----------|-------------| |
| 27 | +| `options` | object | yes | Configuration object for the service. | |
| 28 | +| `options.app` | object | yes | The Feathers application instance. | |
| 29 | +| `options.vapidDetails` | object | yes | VAPID key details for sending push notifications. Must contain `subject`, `publicKey`, and `privateKey`. | |
| 30 | + |
| 31 | +### Throws (constructor) |
| 32 | + |
| 33 | +| Type | Description | |
| 34 | +|------|-------------| |
| 35 | +| `Error` | If `options`, `options.app`, or `options.vapidDetails` are missing. | |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## create |
| 40 | + |
| 41 | +### Signature |
| 42 | + |
| 43 | +```js |
| 44 | +service.create(data, params) |
| 45 | +``` |
| 46 | + |
| 47 | +### Description |
| 48 | + |
| 49 | +Sends a web push notification to all subscriptions retrieved from the specified service. |
| 50 | + |
| 51 | +Steps: |
| 52 | + |
| 53 | +1. Validates the required parameters in `data`: |
| 54 | + - `notification` |
| 55 | + - `subscriptionService` |
| 56 | + - `subscriptionProperty` |
| 57 | +2. Retrieves subscriptions from the specified service using an optional `subscriptionFilter`. |
| 58 | +3. Iterates over each subscription, validates the subscription keys (`endpoint`, `keys.auth`, `keys.p256dh`). |
| 59 | +4. Sends the notification using `web-push.sendNotification`. |
| 60 | +5. Returns an object containing arrays of successful and failed deliveries. |
| 61 | + |
| 62 | +### Parameters |
| 63 | + |
| 64 | +| Name | Type | Required | Description | |
| 65 | +|------|------|----------|-------------| |
| 66 | +| `data` | object | yes | Data for sending the notification. | |
| 67 | +| `data.notification` | object | yes | The notification payload. | |
| 68 | +| `data.subscriptionService` | string | yes | Name of the Feathers service storing subscriptions. | |
| 69 | +| `data.subscriptionProperty` | string | yes | Property containing subscriptions in the service. | |
| 70 | +| `data.subscriptionFilter` | object | no | Optional filter/query for retrieving subscriptions. | |
| 71 | +| `params` | object | no | FeathersJS service call params. | |
| 72 | + |
| 73 | +### Returns |
| 74 | + |
| 75 | +| Type | Description | |
| 76 | +|------|-------------| |
| 77 | +| `Promise<object>` | An object containing: <br> - `succesful`: array of successful notifications <br> - `failed`: array of errors <br> - `subscriptionService` and `subscriptionProperty` used. | |
| 78 | + |
| 79 | +### Throws (create) |
| 80 | + |
| 81 | +| Type | Description | |
| 82 | +|------|-------------| |
| 83 | +| `BadRequest` | If required `data` parameters are missing or if subscription object is invalid. | |
| 84 | + |
| 85 | +### Examples |
| 86 | + |
| 87 | +```js |
| 88 | +import { Service } from '@kalisio/feathers-webpush/server' |
| 89 | + |
| 90 | +const pushService = new Service({ |
| 91 | + app, |
| 92 | + vapidDetails: { |
| 93 | + subject: 'mailto:admin@example.com', |
| 94 | + publicKey: process.env.VAPID_PUBLIC_KEY, |
| 95 | + privateKey: process.env.VAPID_PRIVATE_KEY |
| 96 | + } |
| 97 | +}) |
| 98 | + |
| 99 | +const result = await pushService.create({ |
| 100 | + notification: { title: 'Hello!', body: 'Test notification' }, |
| 101 | + subscriptionService: 'users', |
| 102 | + subscriptionProperty: 'subscriptions' |
| 103 | +}) |
| 104 | + |
| 105 | +console.log(result.succesful, result.failed) |
| 106 | +``` |
0 commit comments