import {z} from 'zod' export type GetApiSpreadsheetsArgs = {name?: string | undefined, card_network?: string | undefined, acquirer_company_id?: Array | undefined, created_greater_or_equal_than?: string | undefined, created_less_or_equal_than?: string | undefined}; export type GetApiSpreadsheetsOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const domainReceivable = z.object({card_network: z.string().optional(), cnab_id: z.number().int().optional(), company_id: z.number().int().optional(), compound_key: z.string().optional(), created_at: z.string().optional(), due_date: z.string().optional(), id: z.number().int().optional(), issuer: z.string().optional(), previous_id: z.number().int().optional(), spreadsheet_id: z.number().int().optional(), status: z.string().optional(), transactions_number: z.number().int().optional(), transactions_number_available: z.number().int().optional(), updated_at: z.string().optional(), value: z.number().optional(), value_available: z.number().optional()}); export const domainSpreadsheets = z.object({card_network: z.string().optional(), company_id: z.number().int().optional(), company_name: z.string().optional(), content_hash: z.string().optional(), created_at: z.string().optional(), currency: z.string().optional(), due_date: z.string().optional(), extension_type: z.string().optional(), id: z.number().int().optional(), link: z.string().optional(), name: z.string().optional(), processed_at: z.string().optional(), receivables: z.array(domainReceivable).optional(), status: z.string().optional(), storage_file_name: z.string().optional(), total_issuer: z.number().int().optional(), total_rows: z.number().int().optional(), total_values: z.number().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional(), user_name: z.string().optional()}); export const domainSpreadsheetFindResponse = z.object({limit: z.number().int().optional(), page: z.number().int().optional(), rows: z.array(domainSpreadsheets).optional(), sort: z.string().optional(), total_pages: z.number().int().optional(), total_rows: z.number().int().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 }; /** * Find spreadsheets by query filters. * * GET /spreadsheets */ export const getApiSpreadsheets = async (args: GetApiSpreadsheetsArgs, options?: GetApiSpreadsheetsOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.card_network != null) query.append("card_network", String(args.card_network)) for (const item of args.acquirer_company_id ?? []) query.append("acquirer_company_id", String(item)) if (args.created_greater_or_equal_than != null) query.append("created_greater_or_equal_than", String(args.created_greater_or_equal_than)) if (args.created_less_or_equal_than != null) query.append("created_less_or_equal_than", String(args.created_less_or_equal_than)) if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/spreadsheets${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(domainSpreadsheetFindResponse, await res.json()) };