import {z} from 'zod' export type ZonedDateTime = string; export type SnoozeRequest = {comment: string, expires_at: ZonedDateTime, having_exactly_tags: Array}; export type CreateApiV6NotificationsSnoozesArgs = {body: SnoozeRequest}; export type CreateApiV6NotificationsSnoozesOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const snoozeResponse = z.object({comment: z.string(), expires_at: zonedDateTime, having_exactly_tags: z.array(z.string()), snooze_id: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a notification snooze for the caller * * POST /v6/notifications/snoozes * * Also accepts auth: QUERY, COOKIE */ export const createApiV6NotificationsSnoozes = async (args: CreateApiV6NotificationsSnoozesArgs, options?: CreateApiV6NotificationsSnoozesOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/notifications/snoozes${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 validateResponse(snoozeResponse, await res.json()) };