import {z} from 'zod' export type BannerCampaign = {BannerId?: number | undefined, DateFrom?: string | undefined, DateTo?: string | undefined, Weight?: number | undefined}; export type Campaign = {Id?: number | undefined, SyncGuid?: string | undefined, Name: string, ZoneId?: number | undefined, MinWidth?: number | undefined, MaxWidth?: number | undefined, MaxHeight?: number | undefined, SortOrder?: number | undefined, RuleXml?: string | undefined, IsTrigger?: boolean | undefined, Banners?: Array | undefined, Tags?: string | undefined, CreateDate?: string | undefined, ModifyDate?: string | undefined}; export type CreateApiApiV11CampaignArgs = {'X-HawkSearch-ApiKey': string, body: Campaign}; export type CreateApiApiV11CampaignOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bannerCampaign = z.object({BannerId: z.number().int().optional(), DateFrom: z.string().optional(), DateTo: z.string().optional(), Weight: z.number().int().gte(0).lte(100).optional()}); export const campaign = z.object({Id: z.number().int().optional(), SyncGuid: z.string().optional(), Name: z.string(), ZoneId: z.number().int().optional(), MinWidth: z.number().int().gte(0).lte(2147483647).optional(), MaxWidth: z.number().int().gte(0).lte(2147483647).optional(), MaxHeight: z.number().int().gte(0).lte(2147483647).optional(), SortOrder: z.number().int().gte(0).lte(2147483647).optional(), RuleXml: z.string().optional(), IsTrigger: z.boolean().optional(), Banners: z.array(bannerCampaign).optional(), Tags: z.string().optional(), CreateDate: z.string().optional(), ModifyDate: z.string().optional()}); 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 }; /** * Add campaign * * POST /api/v11/campaign */ export const createApiApiV11Campaign = async (args: CreateApiApiV11CampaignArgs, options?: CreateApiApiV11CampaignOptions) => { const {baseUrl = "https://indexing-dev.hawksearch.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-HawkSearch-ApiKey'] != null) headers.set("X-HawkSearch-ApiKey", String(args['X-HawkSearch-ApiKey'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v11/campaign${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(campaign, await res.json()) };