import {z} from 'zod' export type GetApiV1MarketplaceEstablishmentsExtractArgs = {'integration-key': string, 'x-token': string, establishment_id: string, filters?: string | undefined, search?: string | undefined, perPage?: number | undefined, page?: number | undefined, sorters?: string | undefined}; export type GetApiV1MarketplaceEstablishmentsExtractOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const additionalInformationResponse = z.object({old_balance: z.number(), current_balance: z.number()}); export const establishmentExtractResponse = z.object({type: z.enum(["FEES", "PIX", "TED", "BILLET", "P2P"]), modality: z.enum(["OUT", "IN"]), created_at: z.string(), description: z.enum(["PAGAMENTO DE CONTAS", "RECEBIMENTO DE PAGAMENTO", "TRANSFERÊNCIA PIX", "RECEBIMENTO PIX", "DEPOSITO", "TRANSFERÊNCIA ENVIADA", "TRANSFERÊNCIA RECEBIDA", "TRANSFERÊNCIA ENTRE CONTAS (P2P)"]), gateway_key: z.string(), amount: z.number(), status: z.string(), additionalInformation: additionalInformationResponse}); export const establishmentExtractPaginatedResponse = z.object({total: z.number(), perPage: z.number(), page: z.number(), lastPage: z.number(), data: z.array(establishmentExtractResponse)}); 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 }; /** * Lista extrato do estabelecimento * * GET /v1/marketplace/establishments/extract */ export const getApiV1MarketplaceEstablishmentsExtract = async (args: GetApiV1MarketplaceEstablishmentsExtractArgs, options?: GetApiV1MarketplaceEstablishmentsExtractOptions) => { 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 (args.establishment_id != null) headers.set("establishment_id", String(args.establishment_id)) if (args.filters != null) query.append("filters", String(args.filters)) if (args.search != null) query.append("search", String(args.search)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.page != null) query.append("page", String(args.page)) if (args.sorters != null) query.append("sorters", String(args.sorters)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/establishments/extract${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(establishmentExtractPaginatedResponse, await res.json()) };