import {z} from 'zod' export type AccountCode = string; export type CurrencyCode = string; export type RoundedMonetaryAmount = {amount: string, currency: CurrencyCode}; export type CountryCode = string; export type ExpenseTaxRate = {percentage: unknown, tax_amount: RoundedMonetaryAmount, country: CountryCode, type: 'VAT' | 'SALES_TAX', description: string, tax_code?: string | undefined, coverage: 'INCLUSIVE' | 'EXCLUSIVE'}; export type CalendarPeriod = {start: string, end: string}; export type FieldAssignments = Array<{field_id: string, field_value_id: string}>; export type VendorCreditLineItemRequest = {account_code: AccountCode, amount: RoundedMonetaryAmount, description?: string | undefined, tax_rate?: ExpenseTaxRate | undefined, service_period?: (CalendarPeriod | null) | undefined, fields?: FieldAssignments | undefined}; export type CreateVendorCreditRequest = {credit_number: string, date: string, gl_impact_date: string, memo?: string | undefined, line_items: Array, vendor_id: string, subsidiary_id: string}; export type CreateApiVendorCreditsArgs = {body: CreateVendorCreditRequest}; export type CreateApiVendorCreditsOptions = {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 }; /** * Creates a vendor credit * * POST /vendor-credits */ export const createApiVendorCredits = async (args: CreateApiVendorCreditsArgs, options?: CreateApiVendorCreditsOptions) => { 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${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()) };