import {z} from 'zod' export type BillStatusEnum = 'CANCELED' | 'DRAFT_PROCESSING' | 'DRAFT' | 'PAYMENT_INFO_NEEDED' | 'PENDING' | 'APPROVED' | 'REJECTED' | 'SCHEDULED' | 'PAYMENT_INITIATED' | 'PAYMENT_DELIVERED' | 'PAYMENT_FAILED' | 'PAYMENT_CANCELED'; export type BillSortEnum = 'VENDOR_NAME' | 'STATUS' | 'AMOUNT' | 'PAYMENT_DATE' | 'BILL_DATE' | 'CREATED_TIME' | 'BILL_DUE_DATE' | 'INVOICE_NUMBER' | 'DESCRIPTION' | 'PAYMENT_TYPE' | 'CATEGORY'; export type GetApiBillsArgs = {statuses?: (Array | null) | undefined, contact_id?: (Array | null) | undefined, amount_min?: (number | string | null) | undefined, amount_max?: (number | string | null) | undefined, bill_date_from?: (string | null) | undefined, bill_date_to?: (string | null) | undefined, due_date_from?: (string | null) | undefined, due_date_to?: (string | null) | undefined, search?: (string | null) | undefined, sort_by?: (BillSortEnum | null) | undefined, sort_descending?: boolean | undefined, limit?: number | undefined, offset?: (string | null) | undefined, 'x-entity-id'?: string | undefined}; export type GetApiBillsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billStatusEnum = z.enum(["CANCELED", "DRAFT_PROCESSING", "DRAFT", "PAYMENT_INFO_NEEDED", "PENDING", "APPROVED", "REJECTED", "SCHEDULED", "PAYMENT_INITIATED", "PAYMENT_DELIVERED", "PAYMENT_FAILED", "PAYMENT_CANCELED"]); export const dateString = z.string(); export const iso4217Code = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BOV", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLF", "CLP", "CNY", "COP", "COU", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SLE", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USN", "UYI", "UYU", "UYW", "UZS", "VED", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "XSU", "XTS", "XUA", "XXX", "YER", "ZAR", "ZMW", "ZWG", "ZWL"]); export const billPaymentTypeEnum = z.enum(["ACH", "CRYPTO", "INTERNATIONAL_PAYMENT_FX", "WIRE", "CHECK"]); export const billCreationSourceEnum = z.enum(["USER", "EMAIL", "SCHEDULE"]); export const timestamp = z.string(); export const billSummaryResponse = z.object({id: z.string(), status: billStatusEnum, vendor_name: z.string(), invoice_number: z.string().nullable().optional(), description: z.string().nullable().optional(), memo: z.string().nullable().optional(), bill_date: dateString.nullable().optional(), bill_due_date: dateString.nullable().optional(), bill_total: z.string().nullable().optional(), currency: iso4217Code.nullable().optional(), payment_type: billPaymentTypeEnum.nullable().optional(), contact_id: z.string().nullable().optional(), category_id: z.string().nullable().optional(), category_name: z.string().nullable().optional(), creation_source: billCreationSourceEnum, created_by: z.string(), created_at: timestamp, updated_at: timestamp.nullable().optional(), submitted_at: timestamp.nullable().optional(), submitted_by: z.string().nullable().optional(), paid_at: timestamp.nullable().optional(), paid_by: z.string().nullable().optional(), scheduled_at: timestamp.nullable().optional(), resolved_at: timestamp.nullable().optional(), canceled_at: timestamp.nullable().optional(), canceled_by: z.string().nullable().optional(), bill_schedule_id: z.string().nullable().optional(), ordinal: z.number().int().nullable().optional()}); export const pageMetadata = z.object({nextOffset: z.string().nullable().optional()}); export const billsResponse = z.object({bills: z.array(billSummaryResponse), page: pageMetadata.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 }; /** * List Bills * * GET /bills */ export const getApiBills = async (args: GetApiBillsArgs, options?: GetApiBillsOptions) => { const {baseUrl = "https://api.meow.com/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.contact_id ?? []) query.append("contact_id", String(item)) if (args.amount_min != null) query.append("amount_min", String(args.amount_min)) if (args.amount_max != null) query.append("amount_max", String(args.amount_max)) if (args.bill_date_from != null) query.append("bill_date_from", String(args.bill_date_from)) if (args.bill_date_to != null) query.append("bill_date_to", String(args.bill_date_to)) if (args.due_date_from != null) query.append("due_date_from", String(args.due_date_from)) if (args.due_date_to != null) query.append("due_date_to", String(args.due_date_to)) if (args.search != null) query.append("search", String(args.search)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_descending != null) query.append("sort_descending", String(args.sort_descending)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args['x-entity-id'] != null) headers.set("x-entity-id", String(args['x-entity-id'])) if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/bills${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(billsResponse, await res.json()) };