import {z} from 'zod' export type Expense = {id?: string | undefined, integration?: string | undefined, srcId?: string | undefined, txnId?: string | undefined, type?: string | undefined, accountId?: string | undefined, currency?: (string | null) | undefined, subtotal?: (number | null) | undefined, totalAmount?: (number | null) | undefined, referenceNumber?: (string | null) | undefined, status?: (string | null) | undefined, invoiceId?: (string | null) | undefined, invoiceNumber?: (string | null) | undefined, modified?: (string | null) | undefined, accountName?: (string | null) | undefined, created?: (string | null) | undefined, accountCode?: (string | null) | undefined, contactId?: (string | null) | undefined, contactName?: (string | null) | undefined, contactType?: (string | null) | undefined, paymentMethodId?: (string | null) | undefined, date?: (string | null) | undefined, taxAmount?: (number | null) | undefined, description?: (string | null) | undefined, deleted?: boolean | undefined}; export type CreateApiExpensesArgs = {body: Expense}; export type CreateApiExpensesOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const expense = z.object({id: z.string().optional(), integration: z.string().optional(), srcId: z.string().optional(), txnId: z.string().optional(), type: z.string().optional(), accountId: z.string().optional(), currency: z.string().nullable().optional(), subtotal: z.number().int().nullable().optional(), totalAmount: z.number().int().nullable().optional(), referenceNumber: z.string().nullable().optional(), status: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), invoiceNumber: z.string().nullable().optional(), modified: z.string().nullable().optional(), accountName: z.string().nullable().optional(), created: z.string().nullable().optional(), accountCode: z.string().nullable().optional(), contactId: z.string().nullable().optional(), contactName: z.string().nullable().optional(), contactType: z.string().nullable().optional(), paymentMethodId: z.string().nullable().optional(), date: z.string().nullable().optional(), taxAmount: z.number().int().nullable().optional(), description: z.string().nullable().optional(), deleted: z.boolean().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 }; /** * Creates a Expense resource. * * POST /expenses */ export const createApiExpenses = async (args: CreateApiExpensesArgs, options?: CreateApiExpensesOptions) => { const {baseUrl = "https://example.myintranetapps.com/api", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/expenses${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(expense, await res.json()) };