import {z} from 'zod' export type PageOrder = 'ASC' | 'ASC_NULLS_FIRST' | 'ASC_NULLS_LAST' | 'DESC' | 'DESC_NULLS_FIRST' | 'DESC_NULLS_LAST'; export type BookkeepingAccountSearchRequestV4 = {archived?: (boolean | null) | undefined, code?: (string | null) | undefined, companyId: string, externalId?: (string | null) | undefined, ids?: (Array | null) | undefined, name?: (string | null) | undefined}; export type CreateApiV1BetaAccountsSearchArgs = {before?: string | undefined, after?: string | undefined, offset?: number | undefined, limit?: number | undefined, sorting_keys?: Array | undefined, sorting_order?: Array | undefined, body: BookkeepingAccountSearchRequestV4}; export type CreateApiV1BetaAccountsSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bookkeepingAccountRestModelV4 = z.object({archived: z.boolean(), code: z.string().nullable().optional(), companyId: z.string(), externalId: z.string(), id: z.string(), metadata: z.record(z.string(), z.object({}).nullable()).nullable().optional(), name: z.string(), taxCodeExternalId: z.string().nullable().optional()}); export const pageOrder = z.enum(["ASC", "ASC_NULLS_FIRST", "ASC_NULLS_LAST", "DESC", "DESC_NULLS_FIRST", "DESC_NULLS_LAST"]); export const cursorPageCurrentRequestInfo = z.object({after: z.string().optional(), before: z.string().optional(), limit: z.number().int().optional(), offset: z.number().int().optional(), parameters: z.record(z.string(), z.array(z.string())), sortingKeys: z.array(z.string()).optional(), sortingOrder: z.array(pageOrder).optional()}); export const cursorPageInfo = z.object({currentRequestPagination: cursorPageCurrentRequestInfo, endCursor: z.string().optional(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().optional(), total: z.number().int().optional()}); export const cursorPaginatedResponseBookkeepingAccountRestModelV4 = z.object({data: z.array(bookkeepingAccountRestModelV4), pagination: cursorPageInfo}); 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 }; /** * Fetch a list of accounts * * POST /v1-beta/accounts:search */ export const createApiV1BetaAccountsSearch = async (args: CreateApiV1BetaAccountsSearchArgs, options?: CreateApiV1BetaAccountsSearchOptions) => { const {baseUrl = "https://external.pleo.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json;charset=UTF-8") headers.set('accept', "application/json") if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.sorting_keys ?? []) query.append("sorting_keys", String(item)) for (const item of args.sorting_order ?? []) query.append("sorting_order", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1-beta/accounts:search${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(cursorPaginatedResponseBookkeepingAccountRestModelV4, await res.json()) };