@markwharton/pwa-push 3.0.0 → 3.1.0
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/dist/server.d.ts +10 -26
- package/dist/server.js +20 -35
- package/package.json +1 -1
package/dist/server.d.ts
CHANGED
|
@@ -44,39 +44,17 @@ export declare function initPushFromEnv(): void;
|
|
|
44
44
|
export declare function getVapidPublicKey(): string;
|
|
45
45
|
/**
|
|
46
46
|
* Sends a push notification to a single subscription.
|
|
47
|
-
* @param subscription - The push subscription to send to
|
|
48
|
-
* @param payload - The notification payload (title, body, etc.)
|
|
49
|
-
* @returns Result with ok=true on success, or error message and statusCode on failure
|
|
50
|
-
* @example
|
|
51
|
-
* const result = await sendPushNotification(subscription, {
|
|
52
|
-
* title: 'New Message',
|
|
53
|
-
* body: 'You have a new message'
|
|
54
|
-
* });
|
|
55
|
-
*/
|
|
56
|
-
export declare function sendPushNotification(subscription: PushSubscription, payload: NotificationPayload): Promise<Result<void>>;
|
|
57
|
-
/**
|
|
58
|
-
* Sends a push notification to multiple subscriptions in parallel.
|
|
59
|
-
* @param subscriptions - Array of push subscriptions
|
|
60
|
-
* @param payload - The notification payload (title, body, etc.)
|
|
61
|
-
* @returns Array of Results, one per subscription (in same order)
|
|
62
|
-
* @example
|
|
63
|
-
* const results = await sendPushToAll(subscriptions, { title: 'Alert', body: 'System update' });
|
|
64
|
-
* const failed = results.filter(r => !r.ok);
|
|
65
|
-
*/
|
|
66
|
-
export declare function sendPushToAll(subscriptions: PushSubscription[], payload: NotificationPayload): Promise<Result<void>[]>;
|
|
67
|
-
/**
|
|
68
|
-
* Sends a push notification with detailed error information.
|
|
69
47
|
* Automatically injects basePath from subscription if not set in payload.
|
|
70
48
|
* @param subscription - The push subscription to send to
|
|
71
49
|
* @param payload - The notification payload
|
|
72
|
-
* @returns Result with ok=true on success, or error with
|
|
50
|
+
* @returns Result with ok=true on success, or error with status on failure
|
|
73
51
|
* @example
|
|
74
|
-
* const result = await
|
|
75
|
-
* if (!result.ok && isSubscriptionExpired(result.
|
|
52
|
+
* const result = await sendPush(subscription, { title: 'Alert', body: 'Update' });
|
|
53
|
+
* if (!result.ok && isSubscriptionExpired(result.status)) {
|
|
76
54
|
* await deleteSubscription(subscription);
|
|
77
55
|
* }
|
|
78
56
|
*/
|
|
79
|
-
export declare function
|
|
57
|
+
export declare function sendPush(subscription: PushSubscription, payload: NotificationPayload): Promise<Result<void>>;
|
|
80
58
|
/**
|
|
81
59
|
* Broadcasts a push notification to multiple subscriptions in parallel.
|
|
82
60
|
* Returns structured results with counts and invalid endpoint tracking.
|
|
@@ -106,3 +84,9 @@ export declare function broadcastPush<TSub extends PushSubscription>(subscriptio
|
|
|
106
84
|
* }
|
|
107
85
|
*/
|
|
108
86
|
export declare function isSubscriptionExpired(status: number | undefined): boolean;
|
|
87
|
+
/** @deprecated Use `sendPush` instead */
|
|
88
|
+
export declare const sendPushNotification: typeof sendPush;
|
|
89
|
+
/** @deprecated Use `sendPush` instead */
|
|
90
|
+
export declare const sendPushWithDetails: typeof sendPush;
|
|
91
|
+
/** @deprecated Use `broadcastPush` instead */
|
|
92
|
+
export declare function sendPushToAll(subscriptions: PushSubscription[], payload: NotificationPayload): Promise<Result<void>[]>;
|
package/dist/server.js
CHANGED
|
@@ -8,14 +8,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
8
8
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
9
|
};
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.sendPushWithDetails = exports.sendPushNotification = void 0;
|
|
11
12
|
exports.initPush = initPush;
|
|
12
13
|
exports.initPushFromEnv = initPushFromEnv;
|
|
13
14
|
exports.getVapidPublicKey = getVapidPublicKey;
|
|
14
|
-
exports.
|
|
15
|
-
exports.sendPushToAll = sendPushToAll;
|
|
16
|
-
exports.sendPushWithDetails = sendPushWithDetails;
|
|
15
|
+
exports.sendPush = sendPush;
|
|
17
16
|
exports.broadcastPush = broadcastPush;
|
|
18
17
|
exports.isSubscriptionExpired = isSubscriptionExpired;
|
|
18
|
+
exports.sendPushToAll = sendPushToAll;
|
|
19
19
|
const web_push_1 = __importDefault(require("web-push"));
|
|
20
20
|
const shared_1 = require("./shared");
|
|
21
21
|
let vapidConfig = null;
|
|
@@ -76,44 +76,17 @@ function getVapidPublicKey() {
|
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
78
|
* Sends a push notification to a single subscription.
|
|
79
|
-
* @param subscription - The push subscription to send to
|
|
80
|
-
* @param payload - The notification payload (title, body, etc.)
|
|
81
|
-
* @returns Result with ok=true on success, or error message and statusCode on failure
|
|
82
|
-
* @example
|
|
83
|
-
* const result = await sendPushNotification(subscription, {
|
|
84
|
-
* title: 'New Message',
|
|
85
|
-
* body: 'You have a new message'
|
|
86
|
-
* });
|
|
87
|
-
*/
|
|
88
|
-
async function sendPushNotification(subscription, payload) {
|
|
89
|
-
return sendPushWithDetails(subscription, payload);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Sends a push notification to multiple subscriptions in parallel.
|
|
93
|
-
* @param subscriptions - Array of push subscriptions
|
|
94
|
-
* @param payload - The notification payload (title, body, etc.)
|
|
95
|
-
* @returns Array of Results, one per subscription (in same order)
|
|
96
|
-
* @example
|
|
97
|
-
* const results = await sendPushToAll(subscriptions, { title: 'Alert', body: 'System update' });
|
|
98
|
-
* const failed = results.filter(r => !r.ok);
|
|
99
|
-
*/
|
|
100
|
-
async function sendPushToAll(subscriptions, payload) {
|
|
101
|
-
const results = await Promise.all(subscriptions.map(sub => sendPushWithDetails(sub, payload)));
|
|
102
|
-
return results;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Sends a push notification with detailed error information.
|
|
106
79
|
* Automatically injects basePath from subscription if not set in payload.
|
|
107
80
|
* @param subscription - The push subscription to send to
|
|
108
81
|
* @param payload - The notification payload
|
|
109
|
-
* @returns Result with ok=true on success, or error with
|
|
82
|
+
* @returns Result with ok=true on success, or error with status on failure
|
|
110
83
|
* @example
|
|
111
|
-
* const result = await
|
|
112
|
-
* if (!result.ok && isSubscriptionExpired(result.
|
|
84
|
+
* const result = await sendPush(subscription, { title: 'Alert', body: 'Update' });
|
|
85
|
+
* if (!result.ok && isSubscriptionExpired(result.status)) {
|
|
113
86
|
* await deleteSubscription(subscription);
|
|
114
87
|
* }
|
|
115
88
|
*/
|
|
116
|
-
async function
|
|
89
|
+
async function sendPush(subscription, payload) {
|
|
117
90
|
if (!vapidConfig) {
|
|
118
91
|
return (0, shared_1.err)('Push not initialized');
|
|
119
92
|
}
|
|
@@ -155,7 +128,7 @@ async function broadcastPush(subscriptions, payload) {
|
|
|
155
128
|
await Promise.all(subscriptions.map(async (subscription) => {
|
|
156
129
|
const actualPayload = typeof payload === 'function'
|
|
157
130
|
? payload(subscription) : payload;
|
|
158
|
-
const result = await
|
|
131
|
+
const result = await sendPush(subscription, actualPayload);
|
|
159
132
|
if (result.ok) {
|
|
160
133
|
sent++;
|
|
161
134
|
}
|
|
@@ -181,3 +154,15 @@ async function broadcastPush(subscriptions, payload) {
|
|
|
181
154
|
function isSubscriptionExpired(status) {
|
|
182
155
|
return status === 410;
|
|
183
156
|
}
|
|
157
|
+
// =============================================================================
|
|
158
|
+
// Deprecated aliases (remove in next major)
|
|
159
|
+
// =============================================================================
|
|
160
|
+
/** @deprecated Use `sendPush` instead */
|
|
161
|
+
exports.sendPushNotification = sendPush;
|
|
162
|
+
/** @deprecated Use `sendPush` instead */
|
|
163
|
+
exports.sendPushWithDetails = sendPush;
|
|
164
|
+
/** @deprecated Use `broadcastPush` instead */
|
|
165
|
+
async function sendPushToAll(subscriptions, payload) {
|
|
166
|
+
const results = await Promise.all(subscriptions.map(sub => sendPush(sub, payload)));
|
|
167
|
+
return results;
|
|
168
|
+
}
|