import {z} from 'zod' export type PageLimit = number; export type PageCursor = string; export type GetApiVendorCreditsArgs = {vendor_id?: string | undefined, limit?: PageLimit | undefined, cursor?: PageCursor | undefined, subsidiary_id?: string | undefined, 'updated.gt'?: string | undefined}; export type GetApiVendorCreditsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountCode = z.string(); export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const countryCode = z.string(); export const expenseTaxRate = z.object({percentage: z.string(), tax_amount: roundedMonetaryAmount, country: countryCode, type: z.enum(["VAT", "SALES_TAX"]), description: z.string(), tax_code: z.string().optional(), coverage: z.enum(["INCLUSIVE", "EXCLUSIVE"])}); export const fieldAssignments = z.array(z.object({field_id: z.string(), field_value_id: z.string()})); export const vendorCreditStatus = z.enum(["UNAPPLIED", "PARTIALLY_APPLIED", "REFUNDED", "APPLIED"]); export const vendorCreditApplication = z.object({id: z.string(), vendor_credit_id: z.string(), bill_id: z.string(), bill_number: z.string().optional(), amount: roundedMonetaryAmount, application_date: z.string()}); export const vendorCredit = z.object({credit_number: z.string(), date: z.string(), gl_impact_date: z.string(), memo: z.string().optional(), line_items: z.array(z.object({account_code: accountCode, amount: roundedMonetaryAmount, description: z.string().optional(), tax_rate: expenseTaxRate.optional(), service_period: z.object({start: z.string(), end: z.string()}).nullable().optional(), fields: fieldAssignments.optional(), id: z.string(), net_amount: roundedMonetaryAmount.optional(), tax_amount: roundedMonetaryAmount.optional()})), vendor_id: z.string(), subsidiary_id: z.string(), id: z.string(), organization_id: z.string(), status: vendorCreditStatus, amount: roundedMonetaryAmount.optional(), applied_amount: roundedMonetaryAmount, remaining_amount: roundedMonetaryAmount, applications: z.array(vendorCreditApplication), updated_at: z.string()}); export const pageCursor = z.string(); export const pagination = z.object({next_cursor: pageCursor.optional()}); export const getApiVendorCreditsResponse = z.object({vendor_credits: z.array(vendorCredit), pagination: pagination}); 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 }; /** * Lists all vendor credits * * GET /vendor-credits */ export const getApiVendorCredits = async (args: GetApiVendorCreditsArgs, options?: GetApiVendorCreditsOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vendor_id != null) query.append("vendor_id", String(args.vendor_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.subsidiary_id != null) query.append("subsidiary_id", String(args.subsidiary_id)) if (args['updated.gt'] != null) query.append("updated.gt", String(args['updated.gt'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/vendor-credits${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(getApiVendorCreditsResponse, await res.json()) };