import {z} from 'zod' export type GiftCardMerchantThemeBorderRadius = 'none' | 'sm' | 'md' | 'lg' | 'full'; export type GiftCardMerchantTheme = {presetId?: string | undefined, primary?: string | undefined, primaryFg?: string | undefined, sidebarBg?: string | undefined, sidebarText?: string | undefined, sidebarActiveBg?: string | undefined, sidebarActiveText?: string | undefined, sidebarBadge?: string | undefined, background?: string | undefined, foreground?: string | undefined, surface?: string | undefined, border?: string | undefined, muted?: string | undefined, logoUrl?: (string | null) | undefined, logoAlt?: (string | null) | undefined, fontFamily?: (string | null) | undefined, borderRadius?: GiftCardMerchantThemeBorderRadius | undefined}; export type UpdateApiV2GiftcardsMerchantsThemeArgs = {'X-Eposn-Merchant-Token'?: (string | null) | undefined, body: GiftCardMerchantTheme}; export type UpdateApiV2GiftcardsMerchantsThemeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const giftCardMerchantThemeBorderRadius = z.enum(["none", "sm", "md", "lg", "full"]); export const giftCardMerchantTheme = z.object({presetId: z.string().min(1).max(100).optional(), primary: z.string().max(7).optional(), primaryFg: z.string().max(7).optional(), sidebarBg: z.string().max(7).optional(), sidebarText: z.string().max(7).optional(), sidebarActiveBg: z.string().max(7).optional(), sidebarActiveText: z.string().max(7).optional(), sidebarBadge: z.string().max(7).optional(), background: z.string().max(7).optional(), foreground: z.string().max(7).optional(), surface: z.string().max(7).optional(), border: z.string().max(7).optional(), muted: z.string().max(7).optional(), logoUrl: z.string().max(2048).nullable().optional(), logoAlt: z.string().max(255).nullable().optional(), fontFamily: z.string().max(255).nullable().optional(), borderRadius: giftCardMerchantThemeBorderRadius.optional()}); export const themeValidationIssue = z.object({field: z.string(), severity: z.string(), code: z.string(), message: z.string()}); export const themePerformanceIssue = z.object({severity: z.string(), code: z.string(), message: z.string(), estimatedMs: z.number().int().nullable().optional()}); export const themePerformanceReport = z.object({cssVarChanges: z.number().int(), hasCustomFont: z.boolean(), hasLargeLogo: z.boolean(), issues: z.array(themePerformanceIssue), score: z.number().int()}); export const saveMerchantThemeResponse = z.object({theme: giftCardMerchantTheme, validation: z.record(z.string(), z.array(themeValidationIssue)), performance: themePerformanceReport}); 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 }; /** * Savemerchanttheme * * PUT /v2/giftcards/merchants/theme */ export const updateApiV2GiftcardsMerchantsTheme = async (args: UpdateApiV2GiftcardsMerchantsThemeArgs, options?: UpdateApiV2GiftcardsMerchantsThemeOptions) => { const {baseUrl = "https://api.loyalty.dog", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Eposn-Merchant-Token'] != null) headers.set("X-Eposn-Merchant-Token", String(args['X-Eposn-Merchant-Token'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/giftcards/merchants/theme${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(saveMerchantThemeResponse, await res.json()) };