import {z} from 'zod' export type GetApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreArgs = {'integration-key': string, 'x-token': string, establishment_id: string}; export type GetApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const metaResponse = z.object({ecs_count: z.number()}); export const splitRuleIndexResponse = 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(), establishment_id: z.number(), active: z.boolean(), __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 }; /** * Listar todas as regras de pré-split da loja * * GET /v1/marketplace/establishments/{establishment_id}/split-pre */ export const getApiV1MarketplaceEstablishmentsEstablishmentIdSplitPre = async (args: GetApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreArgs, options?: GetApiV1MarketplaceEstablishmentsEstablishmentIdSplitPreOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(splitRuleIndexResponse, await res.json()) };