export type SerializedBadgeNotificationPost = {badgeCode?: string | undefined, subject?: string | undefined, message?: string | undefined, ctaUrl?: string | undefined, imageUrl?: string | undefined, sender?: ({domain?: string | undefined}) | undefined}; export type CreateApiPushNotificationBadgeArgs = {'x-auth-domain': string, 'x-auth-expires': string, 'x-auth-signature': string, body: SerializedBadgeNotificationPost}; export type CreateApiPushNotificationBadgeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Send a message to holders of a badge * * POST /push/notification/badge */ export const createApiPushNotificationBadge = async (args: CreateApiPushNotificationBadgeArgs, options?: CreateApiPushNotificationBadgeOptions) => { const {baseUrl = "https://api.unstoppabledomains.com/messaging", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args['x-auth-domain'] != null) headers.set("x-auth-domain", String(args['x-auth-domain'])) if (args['x-auth-expires'] != null) headers.set("x-auth-expires", String(args['x-auth-expires'])) if (args['x-auth-signature'] != null) headers.set("x-auth-signature", String(args['x-auth-signature'])) const url = `${baseUrl.replace(/\/+$/, '')}/push/notification/badge${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };