import {z} from 'zod' export type GetApiV1RemittancesArgs = {'User-Agent'?: string | undefined, page?: number | undefined, per_page?: number | undefined, expire_from?: string | undefined, expire_to?: string | undefined, created_from?: string | undefined, created_to?: string | undefined, bank_billet_account_id?: string | undefined, our_code?: string | undefined, tags?: string | undefined}; export type GetApiV1RemittancesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankBilletRemittanceObject = z.object({id: z.number().int().optional(), our_code: z.string().max(6).nullable(), occurrence: z.string().max(3).nullable().optional(), remittance_id: z.number().int().nullable(), bank_billet_id: z.number().int().nullable(), bank_billet_account_id: z.number().int().nullable(), processed_at: z.string().nullable().optional(), occurrence_detail: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const remittanceObject = z.object({id: z.number().int().optional(), content: z.string().nullable().optional(), filename: z.string().max(255).nullable().optional(), status: z.enum(["unprocessed", "processed", "send"]).optional(), processed_at: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), url: z.string().nullable().optional(), created_via_api: z.boolean().nullable().optional(), bank_billet_account_id: z.number().int().nullable().optional(), remittance_number: z.number().int().nullable().optional(), sent_via_integration: z.string().nullable().optional(), bank_billet_ids: z.array(z.number().int()).nullable().optional(), bank_billet_remittances: z.array(bankBilletRemittanceObject).nullable().optional(), external_id: z.string().nullable().optional(), custom_data: z.object({}).nullable().optional(), tags: z.array(z.string()).nullable().optional(), tag_list: z.string().nullable().optional()}); export const getApiV1RemittancesResponse = z.array(remittanceObject); 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 }; /** * Listar CNABs * * GET /v1/remittances */ export const getApiV1Remittances = async (args: GetApiV1RemittancesArgs, options?: GetApiV1RemittancesOptions) => { const {baseUrl = "https://api-sandbox.kobana.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.expire_from != null) query.append("expire_from", String(args.expire_from)) if (args.expire_to != null) query.append("expire_to", String(args.expire_to)) if (args.created_from != null) query.append("created_from", String(args.created_from)) if (args.created_to != null) query.append("created_to", String(args.created_to)) if (args.bank_billet_account_id != null) query.append("bank_billet_account_id", String(args.bank_billet_account_id)) if (args.our_code != null) query.append("our_code", String(args.our_code)) if (args.tags != null) query.append("tags", String(args.tags)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/remittances${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(getApiV1RemittancesResponse, await res.json()) };