import {z} from 'zod' export type RemittanceCreateCnabObject = {bank_billet_account_id?: (number | null) | undefined, bank_billet_remittance_ids?: (Array | null) | undefined, external_id?: (string | null) | undefined, custom_data?: (Record | null) | undefined, tags?: (Array | null) | undefined}; export type CreateApiV1RemittancesArgs = {'User-Agent'?: string | undefined, 'X-Idempotency-Key'?: string | undefined, body: RemittanceCreateCnabObject}; export type CreateApiV1RemittancesOptions = {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 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 }; /** * Criar CNAB * * POST /v1/remittances */ export const createApiV1Remittances = async (args: CreateApiV1RemittancesArgs, options?: CreateApiV1RemittancesOptions) => { const {baseUrl = "https://api-sandbox.kobana.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (args['X-Idempotency-Key'] != null) headers.set("X-Idempotency-Key", String(args['X-Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/remittances${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(remittanceObject, await res.json()) };