import {z} from 'zod' export type FeedbackEmailCampaignRecipient = {email_address: string, unsubscribe_url?: (string | null) | undefined, variables: {[key: string]: string}}; export type SendCampaignRequest = {feedback_form_id: string, feedback_email_template_id: number, sender_email: string, sender_name: string, recipients: Array, is_dry_run?: boolean | undefined}; export type CreateApiFeedbackCampaignSendArgs = {body: SendCampaignRequest}; export type CreateApiFeedbackCampaignSendOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const senderValidation = z.object({sender_email: z.string(), domain: z.string(), status: z.enum(["validated", "not_validated"])}); export const sendCampaignResponse = z.object({status: z.enum(["accepted", "failed"]), feedback_email_campaign_id: z.number().int().nullable().optional(), feedback_form_id: z.string(), feedback_email_template_id: z.number().int(), recipient_count: z.number().int(), recipient_count_exceeded: z.boolean().optional(), duplicate_emails: z.array(z.string()).nullable().optional(), invalid_emails: z.array(z.string()).nullable().optional(), missing_variables: z.array(z.array(z.unknown())).nullable().optional(), empty_variables: z.array(z.array(z.unknown())).nullable().optional(), invalid_store_codes: z.array(z.string()).nullable().optional(), sender_validation: senderValidation.nullable().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 }; /** * Send a feedback email campaign * * POST /feedback/campaign/send */ export const createApiFeedbackCampaignSend = async (args: CreateApiFeedbackCampaignSendArgs, options?: CreateApiFeedbackCampaignSendOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/feedback/campaign/send${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(sendCampaignResponse, await res.json()) };