import {z} from 'zod' export type PatchApiChargesChargeUuidArgs = {charge_uuid: string, body: {dueDate?: string | undefined, abatementValue?: number | undefined, interestType?: ('fixed_value' | 'percentage') | undefined, interestValue?: number | undefined, applicableFine?: number | undefined}}; export type PatchApiChargesChargeUuidOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiChargesChargeUuidResponse = z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), status: z.string().optional(), paymentMethod: z.string().optional(), dueDate: z.string().optional(), value: z.number().int().optional(), discount: z.object({range: z.string().optional(), discountType: z.string().optional(), discountValue: z.number().int().optional()}).optional(), abatementValue: z.number().optional(), interestType: z.string().optional(), interestValue: z.number().int().optional(), applicableFine: z.number().int().optional(), updateStatus: z.string().optional(), updateDetails: z.array(z.object({field: z.string().optional(), status: z.string().optional()})).optional(), payer: z.object({data: z.object({type: z.string().optional(), attributes: z.object({name: z.string().optional(), documentNumber: z.string().optional(), email: z.string().optional(), address: z.object({street: z.string().optional(), number: z.string().optional(), complement: z.string().optional(), zipCode: z.string().optional(), neighborhood: z.string().optional(), city: z.string().optional(), state: z.string().optional()}).optional(), phone: z.object({countryCode: z.string().optional(), areaCode: z.string().optional(), number: z.string().optional()}).optional()}).optional()}).optional()}).optional(), grantor: z.object({data: z.object({type: z.string().optional(), attributes: z.object({name: z.string().optional(), documentNumber: z.string().optional(), address: z.object({street: z.string().optional(), number: z.string().optional(), complement: z.string().optional(), zipCode: z.string().optional(), neighborhood: z.string().optional(), city: z.string().optional(), state: z.string().optional()}).optional()}).optional()}).optional()}).optional(), fees: z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({kind: z.string().optional(), value: z.number().optional()}).optional()})).optional()}).optional(), boleto: z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({ourNumber: z.number().int().optional(), ourNumberDv: z.string().optional(), barCode: z.string().optional(), emv: z.string().optional(), image: z.string().optional(), quickPdfUrl: z.string().optional(), lineCode: z.string().optional(), registrationStatus: z.string().optional(), pdf: z.string().optional(), yourNumber: z.string().optional(), registrationMethod: z.string().optional(), messagingRuleId: z.string().optional(), observation: z.string().optional()}).optional()}).optional()}).optional(), payments: z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({value: z.number().int().optional(), receivedAt: z.string().optional(), processedAt: z.string().optional(), paymentBankCode: z.unknown(), paymentAgency: z.unknown()}).optional()})).optional()}).optional(), clientControlNumber: z.string().optional(), origin: z.string().optional(), receiptNumber: z.string().optional(), expiresAfter: z.number().int().optional(), titleType: z.string().optional()}).optional()}).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 }; /** * Atualizar cobrança * * PATCH /charges/{charge_uuid} */ export const patchApiChargesChargeUuid = async (args: PatchApiChargesChargeUuidArgs, options?: PatchApiChargesChargeUuidOptions) => { const {baseUrl = "https://pagamentos.grafeno.be/api/v2", fetch: fetchFn = fetch, sec0, sec1} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/charges/${encodeURIComponent(String(args.charge_uuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiChargesChargeUuidResponse, await res.json()) };