import {z} from 'zod' export type PublicInvoiceEmailPdfAttachment = {object_type?: (string | null) | undefined, record_id?: (string | null) | undefined, template_select?: (string | null) | undefined, filename?: (string | null) | undefined}; export type PublicInvoiceEmailRequest = {action?: ('draft' | 'send' | 'schedule') | undefined, to?: Array | undefined, cc?: Array | undefined, subject?: (string | null) | undefined, body?: (string | null) | undefined, scheduled_at?: (string | null) | undefined, template_select?: (string | null) | undefined, additional_pdf_attachments?: Array | undefined, channel_id?: (string | null) | undefined, external_id?: (string | null) | undefined}; export type CreateApiV2PublicInvoicesInvoiceIdEmailArgs = {invoice_id: string, lang?: (string | null) | undefined, language?: (string | null) | undefined, workspace_id?: (string | null) | undefined, 'Accept-Language'?: (string | null) | undefined, 'X-Workspace-Code'?: string | undefined, body: PublicInvoiceEmailRequest}; export type CreateApiV2PublicInvoicesInvoiceIdEmailOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const publicInvoiceEmailAttachmentData = z.object({object_type: z.string(), record_id: z.string(), filename: z.string(), template_id: z.string().nullable().optional()}); export const publicInvoiceEmailData = z.object({workspace_id: z.string().nullable().optional(), workspace_code: z.string().nullable().optional(), app_url: z.string().nullable().optional(), invoice_id: z.string(), formatted_invoice_id: z.number().int().nullable().optional(), action: z.enum(["draft", "send", "schedule"]), status: z.string(), thread_id: z.string().nullable().optional(), message_id: z.string().nullable().optional(), to: z.array(z.string()).optional(), cc: z.array(z.string()).optional(), scheduled_at: z.string().nullable().optional(), provider: z.string().nullable().optional(), provider_message_id: z.string().nullable().optional(), attachment_count: z.number().int().optional(), attachments: z.array(publicInvoiceEmailAttachmentData).optional(), message: z.string().optional()}); export const paginationMeta = z.object({page: z.number().int().gte(1), page_size: z.number().int().gte(1).lte(500), total: z.number().int().gte(0), next_cursor: z.string().nullable().optional()}); export const toastMessageI18n = z.object({en: z.string(), ja: z.string()}); export const toastMessage = z.object({variant: z.enum(["success", "error", "info", "warning"]), code: z.string(), message: z.string(), i18n: toastMessageI18n}); export const envelopeMeta = z.object({ctx_id: z.string(), pagination: paginationMeta.optional(), toast: toastMessage.optional()}); export const sendPublicInvoiceEmailApiV2PublicInvoicesInvoiceIdEmailPost200Envelope = z.object({success: z.literal(true), data: publicInvoiceEmailData, meta: envelopeMeta}); 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 Public Invoice Email * * POST /v2/public/invoices/{invoice_id}/email */ export const createApiV2PublicInvoicesInvoiceIdEmail = async (args: CreateApiV2PublicInvoicesInvoiceIdEmailArgs, options?: CreateApiV2PublicInvoicesInvoiceIdEmailOptions) => { const {baseUrl = "https://api.sanka.com", 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 (args.lang != null) query.append("lang", String(args.lang)) if (args.language != null) query.append("language", String(args.language)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Workspace-Code'] != null) headers.set("X-Workspace-Code", String(args['X-Workspace-Code'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/public/invoices/${encodeURIComponent(String(args.invoice_id))}/email${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(sendPublicInvoiceEmailApiV2PublicInvoicesInvoiceIdEmailPost200Envelope, await res.json()) };