import {z} from 'zod' export type GetApiApiMarketingBannersApplicableArgs = {audience?: string | undefined, country?: string | undefined, surface?: ('bar' | 'hero_pill' | 'app_bar') | undefined}; export type GetApiApiMarketingBannersApplicableOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiMarketingBannersApplicableResponse = z.object({data: z.object({banners: z.array(z.object({content: z.string(), linkUrl: z.string().nullable(), colorScheme: z.string().nullable(), beginDate: z.string().nullable(), endDate: z.string().nullable(), audiences: z.array(z.string()), isPublic: z.boolean(), countries: z.array(z.string()), surface: z.enum(["bar", "hero_pill", "app_bar"]), targetPathnames: z.array(z.string()), id: z.string(), saleId: z.string().nullable(), createdAt: z.string(), updatedAt: z.string()}))}), errors: z.unknown()}); 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 }; /** * Public: get the currently applicable banners for an audience * * GET /api/marketing/banners/applicable */ export const getApiApiMarketingBannersApplicable = async (args: GetApiApiMarketingBannersApplicableArgs, options?: GetApiApiMarketingBannersApplicableOptions) => { const {baseUrl = "https://app.luxalgo.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.audience != null) query.append("audience", String(args.audience)) if (args.country != null) query.append("country", String(args.country)) if (args.surface != null) query.append("surface", String(args.surface)) const url = `${baseUrl.replace(/\/+$/, '')}/api/marketing/banners/applicable${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(getApiApiMarketingBannersApplicableResponse, await res.json()) };