import {z} from 'zod' export type CurrencyCode = string; export type RoundedMonetaryAmount = {amount: string, currency: CurrencyCode}; export type VendorCreditApplicationRequest = {bill_id: string, amount: RoundedMonetaryAmount, application_date?: string | undefined}; export type ApplyVendorCreditRequest = {applications: Array}; export type CreateApiVendorCreditsVendorCreditIdApplicationsArgs = {vendor_credit_id: string, body: ApplyVendorCreditRequest}; export type CreateApiVendorCreditsVendorCreditIdApplicationsOptions = {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 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 }; /** * Applies a vendor credit to bills * * POST /vendor-credits/{vendor_credit_id}/applications */ export const createApiVendorCreditsVendorCreditIdApplications = async (args: CreateApiVendorCreditsVendorCreditIdApplicationsArgs, options?: CreateApiVendorCreditsVendorCreditIdApplicationsOptions) => { const {baseUrl = "https://api.rillet.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/vendor-credits/${encodeURIComponent(String(args.vendor_credit_id))}/applications${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(vendorCredit, await res.json()) };