import {z} from 'zod' export type BannerInCreate = {title?: string | undefined, url?: string | undefined, text?: string | undefined, storeItemId?: number | undefined, backgroundColor?: string | undefined, live?: boolean | undefined, audience?: string | undefined, appName?: string | undefined, expiryTime?: string | undefined, isOnboarding?: boolean | undefined, relativeExpiry?: number | undefined, image?: string | undefined}; export type BodyBannersAdminCreateApiV5AdminBannersPost = {banner: BannerInCreate}; export type CreateApiApiV5AdminBannersArgs = {body: BodyBannersAdminCreateApiV5AdminBannersPost}; export type CreateApiApiV5AdminBannersOptions = {baseUrl?: string | undefined, RWAPIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const banner = z.object({id: z.number().int().optional(), title: z.string().optional(), url: z.string().optional(), text: z.string().optional(), storeItemId: z.number().int().optional(), backgroundColor: z.string().optional(), backgroundImageFileName: z.string().optional(), backgroundImageContentType: z.string().optional(), backgroundImageFileSize: z.number().int().optional(), backgroundImageUpdatedAt: z.string().optional(), live: z.boolean().optional(), audience: z.string().optional(), appName: z.string().optional(), expiryTime: z.string().optional(), isOnboarding: z.boolean().optional(), relativeExpiry: z.number().int().optional(), imageId: z.number().int().optional(), backgroundImageUrl: z.string().optional()}); export const bannerInResponse = z.object({banner: banner}); 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 banner * * POST /api/v5/admin/banners/ */ export const createApiApiV5AdminBanners = async (args: CreateApiApiV5AdminBannersArgs, options?: CreateApiApiV5AdminBannersOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, RWAPIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (RWAPIKeyHeader !== undefined) headers.set("Authorization", RWAPIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/admin/banners/${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(bannerInResponse, await res.json()) };