import {z} from 'zod' export type ResourceId = string; export type TransactionResource = {name: string, mimeType: 'text/plain' | 'application/xml' | 'application/json' | 'image/jpeg' | 'image/png' | 'image/bmp' | 'image/gif' | 'application/msword' | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | 'text/html' | 'application/vnd.ms-xpsdocument' | 'application/emf' | 'application/zip' | 'application/pdf' | 'text/csv', type?: string | undefined, id: string}; export type TransactionEventRequest = {text: string, type: string, from: string, comments?: string | undefined, resources?: Array | undefined}; export type CreateApiTransactionsTransactionIdEventsArgs = {Authorization: string, transaction_id: ResourceId, body: TransactionEventRequest}; export type CreateApiTransactionsTransactionIdEventsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionResource = z.object({name: z.string(), mimeType: z.enum(["text/plain", "application/xml", "application/json", "image/jpeg", "image/png", "image/bmp", "image/gif", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "text/html", "application/vnd.ms-xpsdocument", "application/emf", "application/zip", "application/pdf", "text/csv"]), type: z.string().optional(), id: z.string()}); export const transactionEvent = z.object({id: z.string().optional(), text: z.string().optional(), type: z.string().optional(), from: z.string().optional(), sent: z.string().optional(), comments: z.string().optional(), resources: z.array(transactionResource).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 }; /** * Create a new transaction event * * POST /transactions/{transaction_id}/events */ export const createApiTransactionsTransactionIdEvents = async (args: CreateApiTransactionsTransactionIdEventsArgs, options?: CreateApiTransactionsTransactionIdEventsOptions) => { const {baseUrl = "https://api.elliemae.com/oauth2/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/${encodeURIComponent(String(args.transaction_id))}/events${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(transactionEvent, await res.json()) };