import {z} from 'zod' export type GetApiV2PaymentLinksArgs = {'X-Token-Api': string, 'Content-Type'?: 'application/json' | undefined, 'x-ip-origin-request'?: string | undefined, 'x-idempotency-key'?: string | undefined, type: 'ORDER' | 'SUBSCRIPTION' | 'FAST_ORDER', customerDocument?: string | undefined, name?: string | undefined, status?: string | undefined, email?: string | undefined, document?: string | undefined, notificationEmail?: string | undefined, paymentMethod?: string | undefined, expirationDateInitial?: string | undefined, expirationDateFinal?: string | undefined, page?: number | undefined, size?: number | undefined, sort?: Array | undefined}; export type GetApiV2PaymentLinksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentLinkBasicResponse = z.object({uuid: z.string(), createdAt: z.string(), title: z.string(), value: z.number(), url: z.string(), processed: z.number().int().optional()}); export const getApiV2PaymentLinksResponse = z.object({content: z.array(paymentLinkBasicResponse).optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), size: z.number().int().optional(), number: z.number().int().optional(), numberOfElements: z.number().int().optional(), first: z.boolean().optional(), last: z.boolean().optional(), empty: z.boolean().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 }; /** * Listar links de pagamento * * GET /v2/payment-links */ export const getApiV2PaymentLinks = async (args: GetApiV2PaymentLinksArgs, options?: GetApiV2PaymentLinksOptions) => { const {baseUrl = "https://api.barte.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (args['x-ip-origin-request'] != null) headers.set("x-ip-origin-request", String(args['x-ip-origin-request'])) if (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (args.type != null) query.append("type", String(args.type)) if (args.customerDocument != null) query.append("customerDocument", String(args.customerDocument)) if (args.name != null) query.append("name", String(args.name)) if (args.status != null) query.append("status", String(args.status)) if (args.email != null) query.append("email", String(args.email)) if (args.document != null) query.append("document", String(args.document)) if (args.notificationEmail != null) query.append("notificationEmail", String(args.notificationEmail)) if (args.paymentMethod != null) query.append("paymentMethod", String(args.paymentMethod)) if (args.expirationDateInitial != null) query.append("expirationDateInitial", String(args.expirationDateInitial)) if (args.expirationDateFinal != null) query.append("expirationDateFinal", String(args.expirationDateFinal)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", args.sort.join(',')) if (ApiKeyAuth !== undefined) headers.set("X-Token-Api", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payment-links${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(getApiV2PaymentLinksResponse, await res.json()) };