import {z} from 'zod' export type CompaniesStatus = 'accepted' | 'added' | 'invited'; export type SharedTaxFormStatus = 'archived' | 'completed' | 'expired' | 'none'; export type GetApiV1CompaniesArgs = {page?: number | undefined, page_size?: number | undefined, customer_id?: string | undefined, external_id?: string | undefined, search?: string | undefined, status?: Array | undefined, vendor_id?: string | undefined, tax_form_status?: Array | undefined}; export type GetApiV1CompaniesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sharedSelfLink = z.string(); export const sharedListLinks = z.object({self: sharedSelfLink, next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const sharedPhoneNumber = z.string().min(6).max(16); export const isoCountryCodeAll = z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "XK", "XX", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW"]).min(2).max(2); export const contactsContactLinks = z.object({company: z.string(), invitation_url: z.string().optional(), self: sharedSelfLink}); export const contactsContact = z.object({object: z.literal("Contact"), id: z.string(), default_contact_for_company_management: z.enum(["actionable", "read_only", "none", "", "self_managed"]), default_contact_for_payable_and_receivable: z.enum(["actionable", "read_only", "none", "", "self_managed"]), email: z.string().nullable().optional(), first_name: z.string().optional(), is_archived: z.boolean(), last_email_delivery_problem: z.enum(["auto_responder", "hard_bounce", "refused", "soft_bounce", "spam_complaint", "spam_notification", "transient", "unknown"]).nullable().optional(), last_name: z.string().optional(), phone_number: sharedPhoneNumber.nullable(), phone_number_country: isoCountryCodeAll.nullable(), links: contactsContactLinks}); export const contactsContactList = z.object({object: z.literal("List"), links: sharedListLinks, results: z.array(contactsContact)}); export const isoCountryCode = z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BW", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CL", "CM", "CN", "CO", "CR", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IQ", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MX", "MW", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SX", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VI", "VN", "VU", "XK", "WF", "WS", "XX", "YE", "YT", "ZA", "ZM", "ZW"]).min(2).max(2); export const companiesCompanyLedgerMeta = z.object({customer_id: z.string().nullable().optional(), vendor_id: z.string().nullable().optional()}); export const companiesRegisteredAddress = z.object({address_line_1: z.string(), address_line_2: z.string().nullable().optional(), city: z.string(), country: isoCountryCode, postal_code: z.string(), state: z.string().optional(), object: z.literal("Address"), id: z.string()}).nullable(); export const complianceChecksComplianceStatus = z.enum(["cant_validate", "dismissed", "not_evaluated", "outdated", "passed", "queued", "review_required"]); export const companiesStatus = z.enum(["accepted", "added", "invited"]); export const sharedTaxFormStatus = z.enum(["archived", "completed", "expired", "none"]); export const sharedCompanyType = z.enum(["business", "personal"]); export const companiesCompanyLinks = z.object({self: sharedSelfLink, compliance_checks: z.string(), contacts: z.string(), payables: z.string(), payment_methods: z.string(), receivables: z.string(), tax_forms: z.string()}); export const companiesCompany = z.object({object: z.literal("Company"), id: z.string(), business_name: z.string(), collect_tax_form: z.boolean().optional(), contacts: contactsContactList.optional(), country_code: isoCountryCode.optional(), created_at: z.string(), display_name: z.string(), external_id: z.string().nullable(), is_archived: z.boolean().optional(), is_customer: z.boolean(), is_vendor: z.boolean(), ledger: companiesCompanyLedgerMeta, registered_address: z.union([companiesRegisteredAddress, z.object({}).nullable()]).optional(), risk_summary: complianceChecksComplianceStatus.optional(), status: companiesStatus, tax_form_status: sharedTaxFormStatus, tax_form_type: z.enum(["w9", "w8ben", "w8ben_e"]).nullable().optional(), type: sharedCompanyType, links: companiesCompanyLinks}); export const companiesCompanyList = z.object({object: z.literal("List"), links: sharedListLinks, results: z.array(companiesCompany)}); 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 Companies * * GET /v1/companies */ export const getApiV1Companies = async (args: GetApiV1CompaniesArgs, options?: GetApiV1CompaniesOptions) => { const {baseUrl = "https://api.routable.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.vendor_id != null) query.append("vendor_id", String(args.vendor_id)) for (const item of args.tax_form_status ?? []) query.append("tax_form_status", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies${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(companiesCompanyList, await res.json()) };