import {z} from 'zod' export type GetApiV2CampaignsCampaignIdClicksWithGroupsArgs = {campaignId: number, select?: number | undefined, skip?: number | undefined}; export type GetApiV2CampaignsCampaignIdClicksWithGroupsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiCampaignLinkGroup = z.object({name: z.string().nullable().optional(), dateCreated: z.string().optional()}); export const apiCampaignContactClickWithGroups = z.object({contactId: z.number().int().optional(), email: z.string().nullable().optional(), url: z.string().nullable().optional(), ipAddress: z.string().nullable().optional(), userAgent: z.string().nullable().optional(), dateClicked: z.string().optional(), keyword: z.string().nullable().optional(), groups: z.array(apiCampaignLinkGroup).nullable().optional()}); export const getApiV2CampaignsCampaignIdClicksWithGroupsResponse = z.array(apiCampaignContactClickWithGroups); 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 }; /** * Gets a list of campaign clicks with link groups * * GET /v2/campaigns/{campaignId}/clicks-with-groups */ export const getApiV2CampaignsCampaignIdClicksWithGroups = async (args: GetApiV2CampaignsCampaignIdClicksWithGroupsArgs, options?: GetApiV2CampaignsCampaignIdClicksWithGroupsOptions) => { const {baseUrl = "https://api.dotdigital.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.select != null) query.append("select", String(args.select)) if (args.skip != null) query.append("skip", String(args.skip)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/campaigns/${encodeURIComponent(String(args.campaignId))}/clicks-with-groups${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(getApiV2CampaignsCampaignIdClicksWithGroupsResponse, await res.json()) };