import {z} from 'zod' export type AccountingSoftwareAccountingSoftwareObjectType = 'accounting-periods' | 'accounts' | 'amortization_templates' | 'classes' | 'contracts' | 'departments' | 'employees' | 'items' | 'locations' | 'projects' | 'tasks' | 'tax-codes' | 'warehouses'; export type GetApiV1AccountingSoftwareObjectsLedgerObjectArgs = {ledger_object: AccountingSoftwareAccountingSoftwareObjectType, search?: string | undefined}; export type GetApiV1AccountingSoftwareObjectsLedgerObjectOptions = {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 accountingSoftwareAccountingSoftwareObject = z.object({id: z.string().optional(), name: z.string().optional(), code: z.string().optional(), object: z.literal("AccountingSoftwareObject").optional()}); export const accountingSoftwareAccountingSoftwareObjectList = z.object({object: z.literal("List"), links: sharedListLinks, results: z.array(accountingSoftwareAccountingSoftwareObject)}); 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 Accounting Software Objects * * GET /v1/accounting-software/objects/{ledger_object} */ export const getApiV1AccountingSoftwareObjectsLedgerObject = async (args: GetApiV1AccountingSoftwareObjectsLedgerObjectArgs, options?: GetApiV1AccountingSoftwareObjectsLedgerObjectOptions) => { 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.search != null) query.append("search", String(args.search)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounting-software/objects/${encodeURIComponent(String(args.ledger_object))}${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(accountingSoftwareAccountingSoftwareObjectList, await res.json()) };