import {z} from 'zod' export type CreateApiApiV1ConversionsArgs = {workspace_id?: string | undefined, body: {amount_cents?: number | undefined, country?: string | undefined, currency?: string | undefined, event_id?: string | undefined, event_name: string, event_type?: ('lead' | 'sale' | 'custom') | undefined, external_id?: string | undefined, linkly_cid?: string | undefined, metadata?: Record | undefined, occurred_at?: string | undefined, visitor_ip?: string | undefined}}; export type CreateApiApiV1ConversionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1ConversionsResponse = z.object({amount_cents: z.number().int().nullable().optional(), click_id: z.string(), country: z.string().nullable().optional(), currency: z.string().nullable().optional(), event_id: z.string().nullable().optional(), event_name: z.string(), event_type: z.string(), external_id: z.string().nullable().optional(), id: z.string(), inserted_at: z.string(), ip_source: z.enum(["visitor", "server", "unknown"]).optional(), link_id: z.number().int(), metadata: z.object({}).nullable().optional(), occurred_at: z.string(), source: z.string()}); 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 }; /** * Report a conversion * * POST /api/v1/conversions */ export const createApiApiV1Conversions = async (args: CreateApiApiV1ConversionsArgs, options?: CreateApiApiV1ConversionsOptions) => { const {baseUrl = "https://api.linklyhq.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/conversions${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(createApiApiV1ConversionsResponse, await res.json()) };