import {z} from 'zod' export type GetApi10InvoicesInvoiceIdArgs = {invoiceId: number, includeFields?: (Array<'notes' | 'attachments'>) | undefined, includeAllFields?: boolean | undefined}; export type GetApi10InvoicesInvoiceIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const projectVendorUserPublicAPI = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const customerCompanyProjectPublicResponse = z.object({companyId: z.number().int().gte(1).optional(), companyName: z.string().optional(), companyUrl: z.string().optional()}); export const invoiceProjectPublicAPIResponseEntity = z.object({projectId: z.number().int().gte(1).optional(), projectName: z.string().optional()}); export const invoicePublicProjectFinancialsBudgetResponse = z.object({budgetId: z.number().int().gte(1).optional(), budgetName: z.string().optional()}); export const invoiceFieldPublicResponseEntity = z.object({fieldId: z.number().int().gte(1).optional(), fieldLabel: z.string().optional(), fieldValue: z.object({}).optional(), fieldValueLabel: z.string().optional()}); export const invoiceAttachmentPublicAPIResponseEntity = z.object({attachmentId: z.number().int().optional(), attachmentName: z.string().optional(), createdAt: z.number().int().optional(), location: z.string().optional(), thumbLocation: z.string().optional(), visibility: z.boolean().optional()}); export const invoicePublicAPIResponseEntity = z.object({invoiceId: z.number().int().gte(1).optional(), invoiceNumber: z.string().optional(), dateOfIssue: z.string().optional(), dueDate: z.string().optional(), currency: z.enum(["AFN", "ALL", "DZD", "ARS", "AMD", "AUD", "AZN", "BHD", "BDT", "BYN", "BZD", "BOB", "BAM", "BWP", "BRL", "GBP", "BND", "BGN", "BIF", "KHR", "CAD", "CVE", "XAF", "CLP", "CNY", "COP", "KMF", "CDF", "CRC", "HRK", "CZK", "DKK", "DJF", "DOP", "EGP", "ERN", "EEK", "ETB", "EUR", "GEL", "GHS", "GTQ", "GNF", "HNL", "HKD", "HUF", "ISK", "INR", "IDR", "IRR", "IQD", "ILS", "JMD", "JPY", "JOD", "KZT", "KES", "KWD", "LVL", "LBP", "LYD", "LTL", "MOP", "MKD", "MGA", "MYR", "MUR", "MXN", "MDL", "MAD", "MZN", "MMK", "NAD", "NPR", "TWD", "NZD", "NIO", "NGN", "NOK", "OMR", "PKR", "PAB", "PYG", "PEN", "PHP", "PLN", "QAR", "RON", "RUB", "RWF", "SAR", "RSD", "SGD", "SOS", "ZAR", "KRW", "LKR", "SDG", "SEK", "CHF", "SYP", "TZS", "THB", "TOP", "TTD", "TND", "TRY", "USD", "UGX", "UAH", "AED", "UYU", "UZS", "VEF", "VND", "XOF", "YER", "ZMK", "ZWL"]).optional(), status: z.string().optional(), amount: z.number().optional(), tax: z.number().optional(), subTotal: z.number().optional(), amountOutstanding: z.number().optional(), amountPaid: z.number().optional(), amountWrittenOff: z.number().optional(), notes: z.string().optional(), createdAt: z.number().int().optional(), updatedAt: z.number().int().optional(), createdBy: projectVendorUserPublicAPI.optional(), updatedBy: projectVendorUserPublicAPI.optional(), company: customerCompanyProjectPublicResponse.optional(), projects: z.array(invoiceProjectPublicAPIResponseEntity).optional(), budgets: z.array(invoicePublicProjectFinancialsBudgetResponse).optional(), fields: z.array(invoiceFieldPublicResponseEntity).optional(), attachments: z.array(invoiceAttachmentPublicAPIResponseEntity).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 }; /** * Get invoice by Id * * GET /1.0/invoices/{invoiceId} */ export const getApi10InvoicesInvoiceId = async (args: GetApi10InvoicesInvoiceIdArgs, options?: GetApi10InvoicesInvoiceIdOptions) => { const {baseUrl = "https://api.rocketlane.com/api", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeFields != null) query.append("includeFields", args.includeFields.join(',')) if (args.includeAllFields != null) query.append("includeAllFields", String(args.includeAllFields)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/invoices/${encodeURIComponent(String(args.invoiceId))}${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(invoicePublicAPIResponseEntity, await res.json()) };