import {z} from 'zod' export type InboxConfigPayload = {images: {emptyInboxUrl: string} | null, locale: string | null, theme: {banner?: ({backgroundColor: string, backgroundOpacity?: number | undefined, fontSize: string, textColor: string}) | undefined, dialog?: {accentColor: string, backgroundColor: string, textColor: string} | undefined, footer?: {backgroundColor: string, borderRadius: string, fontSize: string, textColor: string} | undefined, header?: {backgroundColor: string, borderRadius: string, fontFamily: string, fontSize: string, textColor: string} | undefined, icon?: {borderColor: string, width: string} | undefined, notification?: ({default: {backgroundColor: string, borderRadius: string, fontFamily: string, fontSize: string, hover?: {backgroundColor: string} | undefined, margin: string, state?: {color: string} | undefined, textColor: string}, unread: {backgroundColor: string, hover?: {backgroundColor: string} | undefined, state?: {color: string} | undefined, textColor: string}, unseen: {backgroundColor: string, hover?: {backgroundColor: string} | undefined, state?: {color: string} | undefined, textColor: string}}) | undefined, unseenBadge?: {backgroundColor: string} | undefined} | null}; export type UpdateApiIntegrationsInboxInstallationsArgs = {body: InboxConfigPayload}; export type UpdateApiIntegrationsInboxInstallationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const inboxConfigPayload = z.object({images: z.object({emptyInboxUrl: z.string()}).nullable(), locale: z.string().min(2).nullable(), theme: z.object({banner: z.object({backgroundColor: z.string(), backgroundOpacity: z.number().optional(), fontSize: z.string(), textColor: z.string()}).optional(), dialog: z.object({accentColor: z.string(), backgroundColor: z.string(), textColor: z.string()}).optional(), footer: z.object({backgroundColor: z.string(), borderRadius: z.string(), fontSize: z.string(), textColor: z.string()}).optional(), header: z.object({backgroundColor: z.string(), borderRadius: z.string(), fontFamily: z.string(), fontSize: z.string(), textColor: z.string()}).optional(), icon: z.object({borderColor: z.string(), width: z.string()}).optional(), notification: z.object({default: z.object({backgroundColor: z.string(), borderRadius: z.string(), fontFamily: z.string(), fontSize: z.string(), hover: z.object({backgroundColor: z.string()}).optional(), margin: z.string(), state: z.object({color: z.string()}).optional(), textColor: z.string()}), unread: z.object({backgroundColor: z.string(), hover: z.object({backgroundColor: z.string()}).optional(), state: z.object({color: z.string()}).optional(), textColor: z.string()}), unseen: z.object({backgroundColor: z.string(), hover: z.object({backgroundColor: z.string()}).optional(), state: z.object({color: z.string()}).optional(), textColor: z.string()})}).optional(), unseenBadge: z.object({backgroundColor: z.string()}).optional()}).nullable()}); 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 }; /** * Save an Inbox installation * * PUT /integrations/inbox/installations */ export const updateApiIntegrationsInboxInstallations = async (args: UpdateApiIntegrationsInboxInstallationsArgs, options?: UpdateApiIntegrationsInboxInstallationsOptions) => { const {baseUrl = "https://api.magicbell.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/integrations/inbox/installations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(inboxConfigPayload, await res.json()) };