import {z} from 'zod' export type MailingListJobsRequest = {addContacts?: Array | undefined, removeContacts?: Array | undefined, addMailingListImports?: Array | undefined, removeMailingListImports?: Array | undefined}; export type CreateApiPrintMailV1MailingListsIdJobsArgs = {id: string, body: MailingListJobsRequest}; export type CreateApiPrintMailV1MailingListsIdJobsOptions = {baseUrl?: string | undefined, PrintMailApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const processStatus = z.enum(["creating_contacts", "removing_contacts", "counting_recipient_country_codes", "completed"]); export const processErrorType = z.enum(["mailing_list_imports_not_found_error", "download_file_error", "operational_error", "internal_service_error"]); export const processError = z.object({type: processErrorType, message: z.string()}); export const mailingList = z.object({id: z.string(), description: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional(), live: z.boolean(), createdAt: z.string(), updatedAt: z.string(), status: processStatus, errors: z.array(processError).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 }; /** * Submit a Mailing List Job * * POST /print-mail/v1/mailing_lists/{id}/jobs */ export const createApiPrintMailV1MailingListsIdJobs = async (args: CreateApiPrintMailV1MailingListsIdJobsArgs, options?: CreateApiPrintMailV1MailingListsIdJobsOptions) => { const {baseUrl = "https://api.postgrid.com", fetch: fetchFn = fetch, PrintMailApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (PrintMailApiKeyAuth !== undefined) headers.set("X-API-Key", PrintMailApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/print-mail/v1/mailing_lists/${encodeURIComponent(String(args.id))}/jobs${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(mailingList, await res.json()) };