import {z} from 'zod' export type AppName = 'core' | 'gratitude' | 'pavlok3'; export type PlatformName = 'android' | 'ios'; export type DeviceName = 'PV2' | 'SC2' | 'PV3' | 'SC3'; export type GetApiApiV5BannersArgs = {app_name: AppName, platform_name: PlatformName, device_name?: DeviceName | undefined}; export type GetApiApiV5BannersOptions = {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 listOfBannersInResponse = z.object({banners: z.array(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 }; /** * Get banners * * GET /api/v5/banners/ */ export const getApiApiV5Banners = async (args: GetApiApiV5BannersArgs, options?: GetApiApiV5BannersOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, RWAPIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.app_name != null) query.append("app_name", String(args.app_name)) if (args.platform_name != null) query.append("platform_name", String(args.platform_name)) if (args.device_name != null) query.append("device_name", String(args.device_name)) if (RWAPIKeyHeader !== undefined) headers.set("Authorization", RWAPIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/banners/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listOfBannersInResponse, await res.json()) };