import {z} from 'zod' export type GetApiV1CampaignProjectPerformanceArgs = {projectId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiV1CampaignProjectPerformanceOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignAPIV1ProjectPerformanceResponse = z.object({date: z.string().optional(), domainAgencyId: z.string().nullable(), ioNumber: z.string().nullable(), projectId: z.string().nullable(), projectName: z.string().nullable().optional(), pageViews: z.number().int().optional(), saveAndShares: z.number().int().optional(), totalLeads: z.number().int().optional(), emailEnquiries: z.number().int().optional(), phoneLeads: z.number().int().optional(), phoneReveals: z.number().int().optional(), agentDetailsViews: z.number().int().optional(), photoViews: z.number().int().optional(), videoViews: z.number().int().optional(), mapViews: z.number().int().optional(), spend: z.number().nullable().optional(), cpe: z.number().nullable().optional()}); export const campaignAPIV1ProjectPerformanceDataResponse = z.object({data: z.array(campaignAPIV1ProjectPerformanceResponse).nullable()}); 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 }; /** * Retrieves project performance metrics for the New Developer Projects managed by advertisers. * * GET /v1/campaign/project-performance * * Also accepts auth: oauth2 */ export const getApiV1CampaignProjectPerformance = async (args: GetApiV1CampaignProjectPerformanceArgs, options?: GetApiV1CampaignProjectPerformanceOptions) => { const {baseUrl = "https://api.domain.com.au", fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/campaign/project-performance${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(campaignAPIV1ProjectPerformanceDataResponse, await res.json()) };