import {z} from 'zod' export type Start = number; export type End = number; export type Metric = 'created' | 'attempted' | 'sent' | 'delivered' | 'opened' | 'clicked' | 'converted' | 'bounced' | 'spammed' | 'unsubscribed' | 'dropped' | 'failed' | 'undeliverable'; export type Drafts = boolean; export type CreateApiV1ExportsDeliveriesArgs = {body: {newsletter_id: number, start?: Start | undefined, end?: End | undefined, attributes?: Array | undefined, metric?: Metric | undefined, drafts?: Drafts | undefined, template_id?: number | undefined} | {campaign_id: number, start?: Start | undefined, end?: End | undefined, attributes?: Array | undefined, metric?: Metric | undefined, drafts?: Drafts | undefined, template_id?: number | undefined} | {transactional_message_id: number, start?: Start | undefined, end?: End | undefined, attributes?: Array | undefined, metric?: Metric | undefined, drafts?: Drafts | undefined, template_id?: number | undefined} | {action_id: number, start?: Start | undefined, end?: End | undefined, attributes?: Array | undefined, metric?: Metric | undefined, drafts?: Drafts | undefined, template_id?: number | undefined}}; export type CreateApiV1ExportsDeliveriesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const exportId = z.number().int(); export const deduplicateId = z.string(); export const exportType = z.enum(["customers", "deliveries"]); export const exportFailed = z.boolean(); export const exportDescription = z.string(); export const exportDownloads = z.number().int(); export const created = z.number().int(); export const updated = z.number().int(); export const exportObject = z.object({id: exportId.optional(), user_id: z.number().int().optional(), user_email: z.string().optional(), total: z.number().int().optional(), deduplicate_id: deduplicateId.optional(), type: exportType.optional(), failed: exportFailed.optional(), description: exportDescription.optional(), downloads: exportDownloads.optional(), created_at: created.optional(), updated_at: updated.optional(), status: z.enum(["done", "pending", "failed"]).optional()}); export const createApiV1ExportsDeliveriesResponse = z.object({export: exportObject.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 }; /** * Export information about deliveries * * POST /v1/exports/deliveries */ export const createApiV1ExportsDeliveries = async (args: CreateApiV1ExportsDeliveriesArgs, options?: CreateApiV1ExportsDeliveriesOptions) => { const {baseUrl = "https://track.customer.io", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/exports/deliveries${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(createApiV1ExportsDeliveriesResponse, await res.json()) };