import {z} from 'zod' export type SplitSellerRequest = {idSeller: number, value: number, type: 'fixed' | 'percent'}; export type SplitRequest = {sellers: Array}; export type CreateApiV2SellerIdSellerChargesChargeUuidSplitArgs = {idSeller: number, chargeUuid: string, 'x-token-api': string, 'Content-Type'?: 'application/json' | undefined, body: SplitRequest}; export type CreateApiV2SellerIdSellerChargesChargeUuidSplitOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const splitSellerResponse = z.object({idSeller: z.number().int().optional(), splitValue: z.number().optional()}); export const splitResponse = z.object({uuid: z.string().optional(), status: z.enum(["SUCCESS", "PROCESSING", "ERROR"]).optional(), chargeValue: z.number().optional(), split: z.array(splitSellerResponse).optional(), timestamp: z.string().optional()}); export const createApiV2SellerIdSellerChargesChargeUuidSplitResponse = z.array(splitResponse); 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 Split de Pagamento * * POST /v2/seller/{idSeller}/charges/{chargeUuid}/split */ export const createApiV2SellerIdSellerChargesChargeUuidSplit = async (args: CreateApiV2SellerIdSellerChargesChargeUuidSplitArgs, options?: CreateApiV2SellerIdSellerChargesChargeUuidSplitOptions) => { const {baseUrl = "https://api.barte.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-token-api'] != null) headers.set("x-token-api", String(args['x-token-api'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (ApiKeyAuth !== undefined) headers.set("X-Token-Api", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/seller/${encodeURIComponent(String(args.idSeller))}/charges/${encodeURIComponent(String(args.chargeUuid))}/split${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(createApiV2SellerIdSellerChargesChargeUuidSplitResponse, await res.json()) };