import {z} from 'zod' export type EstablishmentsDto = {id: number, active: boolean, value: number}; export type CreateSplitRuleDto = {title: string, modality: 'ALL' | 'CREDIT' | 'DEBIT' | 'PIX', channel: 'ALL' | 'CHIP' | 'TAP' | 'SMART' | 'ONLINE', division: 'PERCENTAGE' | 'CURRENCY', active: boolean, installment?: number | undefined, establishments: Array}; export type CreateApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreArgs = {'integration-key': string, 'x-token': string, establishment_id: string, body: CreateSplitRuleDto}; export type CreateApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const establishmentType = z.object({id: z.number(), first_name: z.string(), last_name: z.string(), document: z.string()}); export const establishmentsType = z.object({value: z.number(), priority: z.number(), active: z.boolean(), establishment: establishmentType}); export const metaResponse = z.object({ecs_count: z.number()}); export const splitRuleResponse = z.object({id: z.number(), title: z.string(), modality: z.enum(["ALL", "CREDIT", "DEBIT", "PIX"]), channel: z.enum(["ALL", "CHIP", "TAP", "SMART", "ONLINE"]), division: z.enum(["PERCENTAGE", "CURRENCY"]), installment: z.number().nullable(), active: z.boolean(), establishment_percentage: z.number().optional(), establishment: establishmentType, establishments: z.array(establishmentsType), __meta__: metaResponse}); 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 regra de pré-split * * POST /v1/marketplace/establishments/{establishment_id}/split-pre */ export const createApiV1MarketplaceEstablishmentsEstablishmentIdSplitPre = async (args: CreateApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreArgs, options?: CreateApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['integration-key'] != null) headers.set("integration-key", String(args['integration-key'])) if (args['x-token'] != null) headers.set("x-token", String(args['x-token'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/establishments/${encodeURIComponent(String(args.establishment_id))}/split-pre${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(splitRuleResponse, await res.json()) };