import {z} from 'zod' export type GetApiReportsUrlsArgs = {'Account-ID': string, columns: Array<'clicks_count' | 'revenue_amount' | 'promoter_earnings_amount' | 'referrals_count' | 'customers_count' | 'sales_count' | 'refunds_count' | 'cancelled_customers_count' | 'referrals_to_customers_cr' | 'clicks_to_customers_cr' | 'clicks_to_referrals_cr' | 'url'>, q?: string | undefined, group_by: 'day' | 'week' | 'month' | 'year', start_date: string, end_date: string, sorting?: ({[key: string]: 'asc' | 'desc'}) | undefined}; export type GetApiReportsUrlsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uRLsResponse = z.array(z.object({url: z.string().optional(), id: z.string().optional(), data: z.object({clicks_count: z.number().int().optional(), revenue_amount: z.number().optional(), promoter_earnings_amount: z.number().optional(), referrals_count: z.number().int().optional(), customers_count: z.number().int().optional(), sales_count: z.number().int().optional(), refunds_count: z.number().int().optional(), cancelled_customers_count: z.number().int().optional(), referrals_to_customers_cr: z.number().optional(), clicks_to_customers_cr: z.number().optional(), clicks_to_referrals_cr: z.number().optional()}).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 }; /** * Get reports for URLs * * GET /reports/urls */ export const getApiReportsUrls = async (args: GetApiReportsUrlsArgs, options?: GetApiReportsUrlsOptions) => { const {baseUrl = "https://api.firstpromoter.com/api/v2/company", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Account-ID'] != null) headers.set("Account-ID", String(args['Account-ID'])) for (const item of args.columns ?? []) query.append("columns", String(item)) if (args.q != null) query.append("q", String(args.q)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.sorting != null) query.append("sorting", JSON.stringify(args.sorting)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/urls${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(uRLsResponse, await res.json()) };