@molecule/api-push-notifications-molecule
Provider bond · push-notifications · API (Node) · v1.2.1 · Apache-2.0
molecule.dev hosted push notifications provider — web push delivered through your molecule project, VAPID handled for you
npm install @molecule/api-push-notifications-moleculenpm · Source on GitHub · Implements @molecule/api-push-notifications
How it works
@molecule/api-push-notifications-molecule is a provider bond on the API (Node) side: it implements the push-notifications core interface (@molecule/api-push-notifications) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
Works with: @molecule/api-push-notifications, @molecule/api-secrets
Secrets: MOLECULE_API_KEY
Reference
molecule.dev hosted push notifications provider — web push delivered through your molecule project, VAPID handled for you
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Type
provider
Installation
npm install @molecule/api-push-notifications-molecule @molecule/api-push-notifications @molecule/api-secrets
API
Interfaces
MoleculePushConfig
Options for createProvider. Every field falls back to an env var,
so the zero-argument provider export works from .env alone.
interface MoleculePushConfig {
/**
* The molecule project API key (`mk_…`), or the sandbox token molecule.dev
* writes for in-IDE apps (`mbk_…`). Defaults to `MOLECULE_API_KEY`.
*/
apiKey?: string
/**
* The hosted services base URL, without a trailing slash. Defaults to
* `MOLECULE_SERVICES_URL`, then `https://api.molecule.dev/api/v1/services`.
*/
servicesUrl?: string
/** Per-request timeout in milliseconds. Default 15000. */
timeoutMs?: number
}
ProcessEnv
Environment variables this provider reads.
interface ProcessEnv {
MOLECULE_API_KEY?: string
MOLECULE_SERVICES_URL?: string
}
Classes
MoleculeServiceError
Error thrown for a refused or failed hosted-service call.
class MoleculeServiceError extends Error {
/** HTTP status from molecule.dev. */
readonly status: number
/** Stable error key from molecule.dev, e.g. `broker.error.budgetExceeded`. */
readonly errorKey: string | undefined
/**
* Create the error.
*
* @param message - Human-readable message.
* @param status - HTTP status.
* @param errorKey - Stable error key, when the service sent one.
* @param cause - The underlying error, if any.
*/
constructor(message: string, status: number, errorKey?: string, cause?: unknown) {
super(message, cause === undefined ? undefined : { cause })
this.name = 'MoleculeServiceError'
this.status = status
this.errorKey = errorKey
}
}
PushServiceError
The push service (FCM, Mozilla autopush, Apple) answered a send with a status outside 2xx.
Thrown by send and placed in SendManyResult.error, with the push service's status on
statusCode — the shape web-push's WebPushError has — so the core's documented pruning
(error.statusCode 404 or 410 means the subscription is gone) works whichever push bond is
wired. A resolved SendResult therefore always means the push service accepted the message.
class PushServiceError extends Error {
/** The push service's HTTP status: 404/410 mean the subscription is gone; 429 means slow down. */
readonly statusCode: number
/** The push service's response headers. */
readonly headers: Record<string, string>
/** The push service's response body. */
readonly body: string
/**
* Create the error.
*
* @param result - The push service's answer, as molecule.dev relayed it.
*/
constructor(result: SendResult) {
const gone = result.statusCode === 404 || result.statusCode === 410
super(
`The push service answered ${result.statusCode}` +
(gone ? ': the subscription is gone; delete it.' : '.'),
)
this.name = 'PushServiceError'
this.statusCode = result.statusCode
this.headers = result.headers ?? {}
this.body = result.body ?? ''
}
}
MoleculePushProvider
Push provider backed by molecule.dev's hosted service.
class MoleculePushProvider implements PushNotificationProvider {
readonly name = 'molecule'
private readonly apiKey: string
private readonly servicesUrl: string
private readonly timeoutMs: number
private cachedPublicKey: string | undefined
private publicKeyRequest: Promise<string> | undefined
/**
* Create the provider.
*
* @param config - Options; each falls back to its env var.
*/
constructor(config: MoleculePushConfig = {}) {
this.apiKey = config.apiKey ?? process.env.MOLECULE_API_KEY ?? ''
this.servicesUrl = resolveServicesUrl(
config.servicesUrl ?? process.env.MOLECULE_SERVICES_URL ?? DEFAULT_SERVICES_URL,
)
this.timeoutMs = config.timeoutMs ?? 15_000
}
/**
* Nothing to configure locally — molecule.dev holds the VAPID keys and
* signs every send. The parameter is accepted so the provider satisfies
* the core interface; a self-hosted setup should use
* `@molecule/api-push-notifications-web-push` instead.
*
* @param _config - Ignored.
*/
configure(_config?: VapidConfig): void {}
/**
* Sends a push notification to a subscription endpoint.
*
* @param subscription - The push subscription (endpoint + keys).
* @param payload - The notification payload (title + display options).
* @param options - Optional delivery settings (TTL, urgency, topic); molecule.dev sends them
* to the push service as its TTL, Urgency and Topic headers.
* @returns The push service's acceptance (`statusCode` 2xx).
* @throws {PushServiceError} When the push service answered outside 2xx; `statusCode`
* 404/410 means the subscription is dead and should be pruned.
*/
async send(
subscription: PushSubscription,
payload: NotificationPayload,
options?: SendOptions,
): Promise<SendResult> {
assertValidSubscription(subscription)
assertValidPayload(payload)
const result = await this.request<SendResult>('send', {
subscription,
payload,
...(options ? { options } : {}),
})
if (result.statusCode < 200 || result.statusCode > 299) throw new PushServiceError(result)
return result
}
/**
* Sends a push notification to multiple subscriptions. One refused or
* failed subscription never aborts the batch — each entry carries either
* `result` or `error` (a dead subscription arrives as a `PushServiceError`
* in `error` whose `statusCode` is 404/410; prune on it).
*
* @param subscriptions - The push subscriptions to send to.
* @param payload - The notification payload with title and optional display options.
* @param options - Optional delivery settings applied to every message.
* @returns One result entry per subscription.
*/
async sendMany(
subscriptions: PushSubscription[],
payload: NotificationPayload,
options?: SendOptions,
): Promise<SendManyResult[]> {
const settled = await Promise.allSettled(
(subscriptions ?? []).map((subscription) => this.send(subscription, payload, options)),
)
return settled.map((outcome, i) =>
outcome.status === 'fulfilled'
? { subscription: subscriptions[i], result: outcome.value }
: { subscription: subscriptions[i], error: outcome.reason as Error },
)
}
/**
* Returns molecule.dev's public VAPID key — the `applicationServerKey` a
* browser's `pushManager.subscribe()` must be given for the resulting
* subscriptions to be sendable through molecule.dev.
*
* The core's method is SYNCHRONOUS while the key lives behind a service
* call, so this returns the cache: `undefined` until `fetchPublicKey`
* has run. Serve the key with the core's `fetchPublicKey()`,
* which calls `fetchPublicKey` on this provider:
*
* ```typescript
* import { fetchPublicKey } from '@molecule/api-push-notifications'
* const applicationServerKey = await fetchPublicKey()
* ```
*
* @returns The cached public key, or `undefined` before the first fetch.
*/
getPublicKey(): string | undefined {
return this.cachedPublicKey
}
/**
* Fetches molecule.dev's public VAPID key from the hosted service once and caches it
* for `getPublicKey`. Later calls answer from the cache, and calls made while
* the request is in flight share it, so a public-key route that calls this per request
* asks the service once per process. A failed request is not cached: the next call
* asks again.
*
* @returns The public key string.
*/
fetchPublicKey(): Promise<string> {
if (this.cachedPublicKey) return Promise.resolve(this.cachedPublicKey)
this.publicKeyRequest ??= this.request<{ publicKey: string }>('vapid-key', {}).then(
({ publicKey }) => {
this.cachedPublicKey = publicKey
return publicKey
},
(error: unknown) => {
this.publicKeyRequest = undefined
throw error
},
)
return this.publicKeyRequest
}
/**
* Not supported on the hosted path: sends are signed with molecule.dev's
* VAPID identity, and subscriptions are bound to the key they were created
* with — a locally generated pair could never receive a hosted send. Use
* `@molecule/api-push-notifications-web-push` (which serves your own keys)
* when you need your own identity.
*
* @throws {MoleculeServiceError} Always, with the self-hosted pointer.
*/
generateVapidKeys(): never {
throw new MoleculeServiceError(
"generateVapidKeys() is a self-hosted concern: the hosted service signs sends with molecule.dev's VAPID identity. Use @molecule/api-push-notifications-web-push to serve your own keys.",
400,
'hostedServices.error.invalidInput',
)
}
/** One POST to the hosted service. */
private async request<T>(
operation: 'send' | 'vapid-key',
body: Record<string, unknown>,
): Promise<T> {
if (!this.apiKey) {
throw new MoleculeServiceError(
'MOLECULE_API_KEY is not set. Create a project API key on molecule.dev (or with `mlcl apikey create`) and put it in the API .env.',
401,
'hostedServices.error.tokenInvalid',
)
}
const response = await fetch(`${this.servicesUrl}/push/${operation}`, {
method: 'POST',
headers: { authorization: `Bearer ${this.apiKey}`, 'content-type': 'application/json' },
body: JSON.stringify(body),
signal: AbortSignal.timeout(this.timeoutMs),
})
const text = await response.text()
let data: unknown
try {
data = text ? JSON.parse(text) : {}
} catch (error) {
// A proxy/gateway error page, not the service: report the status only.
throw new MoleculeServiceError(
`molecule.dev returned ${response.status} with a non-JSON body.`,
response.status,
undefined,
error,
)
}
if (!response.ok) {
const err = data as { error?: string; errorKey?: string }
const hint = HINTS[response.status]
throw new MoleculeServiceError(
`molecule.dev push/${operation} failed (${response.status}): ${err.error ?? 'error'}${hint ? ` ${hint}` : ''}`,
response.status,
err.errorKey,
)
}
return data as T
}
}
Functions
createProvider(config)
Create a hosted push notifications provider.
function createProvider(config?: MoleculePushConfig): PushNotificationProvider
config— Options; each falls back to its env var.
Returns: A PushNotificationProvider backed by molecule.dev.
Constants
provider
The provider implementation (wire with setProvider).
const provider: PushNotificationProvider
DEFAULT_SERVICES_URL
Default hosted services base URL.
const DEFAULT_SERVICES_URL: 'https://api.molecule.dev/api/v1/services'
PUSH_SERVICE_LIMITS
Per-request limits of the hosted service.
const PUSH_SERVICE_LIMITS: {
readonly maxPayloadChars: 3072
readonly maxKeyChars: 512
readonly maxEndpointChars: 2048
}
pushNotificationsMoleculeSecretDefinitions
Secret definitions required by the molecule.dev hosted push bond.
const pushNotificationsMoleculeSecretDefinitions: SecretDefinition[]
Core Interface
Implements @molecule/api-push-notifications interface.
Bond Wiring
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/api-push-notifications'
import { provider } from '@molecule/api-push-notifications-molecule'
export function setupPushNotificationsMolecule(): void {
setProvider(provider)
}
Injection Notes
Requirements
Peer dependencies:
@molecule/api-push-notifications^1.1.0@molecule/api-secrets^1.0.1
Environment Variables
MOLECULE_API_KEY(required) — molecule.dev project API key- Setup: Create one for your project with
mlcl apikey create --project <id> --name <name>(keys start with mk_). - Get it here: https://www.molecule.dev
- Example:
mk_...
- Setup: Create one for your project with
Runtime Dependencies
@molecule/api-push-notifications@molecule/api-secrets
E2E Tests
Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip:
- The UI offers an enable-notifications control; activating it triggers the browser permission prompt and, once granted, the subscription is stored (the UI still shows "enabled" after a full reload).
- An event this app notifies about actually delivers a push to the
subscribed session, with a readable title/body (not raw JSON). The sandbox
CAPTURES outbound pushes instead of delivering — read the captured message
with the
read_activitytool (filter type 'push'); never mock the flow or modify production code to expose it. - Clicking the delivered notification opens/focuses the relevant screen (when the app claims deep-linking).
- Denying the permission leaves the app fully usable and truthful about the state (no crash, no false "enabled").
- Disabling/unsubscribing stops deliveries, and the disabled state persists across a reload.