@pushforge/builder
Version:
A robust, cross-platform Web Push notification library that handles VAPID authentication and payload encryption following the Web Push Protocol standard. Works in Node.js 16+, Browsers, Deno, Bun and Cloudflare Workers.
61 lines (60 loc) • 2.19 kB
TypeScript
import type { BuilderOptions } from './types.js';
/**
* Builds an HTTP request body and headers for sending a push notification.
*
* This function constructs the necessary components for a push notification request,
* including the payload, headers, and any required cryptographic operations.
*
* @param {BuilderOptions} options - The options for building the push notification request.
* @param {JsonWebKey | string} options.privateJWK - The private JSON Web Key (JWK) used for signing.
* @param {PushMessage} options.message - The message to be sent in the push notification, including user-defined options.
* @param {PushSubscription} options.subscription - The subscription details for the push notification.
* @returns {Promise<{ endpoint: string, body: ArrayBuffer, headers: Record<string, string> | Headers }>} A promise that resolves to an object containing the endpoint, encrypted body, and headers for the push notification.
*
* @throws {Error} Throws an error if the privateJWK is invalid, if the request fails, or if the payload encryption fails.
*
* @example
* // Example usage
* const privateJWK = '{"kty":"EC","crv":"P-256","d":"_eQ..."}'; // Your private VAPID key
*
* const message = {
* payload: {
* title: "New Message",
* body: "You have a new message!",
* icon: "/images/icon.png"
* },
* options: {
* ttl: 3600, // 1 hour in seconds
* urgency: "high",
* topic: "new-messages"
* },
* adminContact: "mailto:admin@example.com"
* };
*
* const subscription = {
* endpoint: "https://fcm.googleapis.com/fcm/send/...",
* keys: {
* p256dh: "BNn5....",
* auth: "tBHI...."
* }
* };
*
* // Build the request
* const request = await buildPushHTTPRequest({
* privateJWK,
* message,
* subscription
* });
*
* // Send the push notification
* const response = await fetch(request.endpoint, {
* method: 'POST',
* headers: request.headers,
* body: request.body
* });
*/
export declare function buildPushHTTPRequest({ privateJWK, message, subscription, }: BuilderOptions): Promise<{
endpoint: string;
body: ArrayBuffer;
headers: Record<string, string> | Headers;
}>;