import {z} from 'zod' export type GetApiBifrostOrdersArgs = {year?: (number | null) | undefined, months?: (number | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, start_value?: (number | null) | undefined, end_value?: (number | null) | undefined, state?: (string | null) | undefined, protocol?: (string | null) | undefined, license_plate?: (string | null) | undefined, renavam?: (string | null) | undefined, status?: (string | null) | undefined, status_in?: (string | null) | undefined, payment_type_in?: (string | null) | undefined, page?: (number | null) | undefined, size?: (number | null) | undefined, order_by?: (string | null) | undefined}; export type GetApiBifrostOrdersOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({count: z.number().int().nullable().optional(), total_pages: z.number().int().nullable().optional(), offset_page: z.number().int().nullable().optional()}); export const order = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), state: z.string().nullable().optional(), protocol: z.string().nullable().optional(), status: z.string().nullable().optional(), base_amount: z.number().nullable().optional(), total_amount: z.number().nullable().optional(), initial_amount: z.number().nullable().optional(), external_order_number: z.string().nullable().optional()}); export const vehicle = z.object({year: z.string().nullable().optional(), color: z.string().nullable().optional(), model: z.string().nullable().optional(), owner: z.string().nullable().optional()}); export const v2PackagesEnterpriseSchemasBifrostBifrostResponseSchemasCustomer = z.object({id: z.number().int().nullable().optional(), renavam: z.string().nullable().optional(), license_plate: z.string().nullable().optional()}); export const item = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), unique_key: z.string().nullable().optional(), category: z.string().nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), amount: z.number().nullable().optional(), type: z.string().nullable().optional(), settled: z.boolean().nullable().optional(), status: z.string().nullable().optional(), canceled: z.boolean().nullable().optional()}); export const v2PackagesEnterpriseSchemasBifrostBifrostResponseSchemasCard = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), last_four: z.string().nullable().optional(), brand: z.string().nullable().optional(), bin: z.string().nullable().optional(), trunc: z.string().nullable().optional()}); export const paymentInput = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), capture_date: z.string().nullable().optional(), installments: z.number().int().nullable().optional(), installments_amount: z.number().nullable().optional(), amount: z.number().nullable().optional(), status: z.string().nullable().optional(), type: z.string().nullable().optional(), card: v2PackagesEnterpriseSchemasBifrostBifrostResponseSchemasCard.nullable().optional()}); export const establishment = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), title: z.string().nullable().optional()}); export const portalOrderInput = z.object({id: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), order: order.nullable().optional(), vehicle: vehicle.nullable().optional(), customer: v2PackagesEnterpriseSchemasBifrostBifrostResponseSchemasCustomer.nullable().optional(), items: z.array(item).nullable().optional(), payments: z.array(paymentInput).nullable().optional(), establishment: establishment.nullable().optional()}); export const bifrostResponseDTOInput = z.object({pagination: pagination.nullable().optional(), portal_orders: z.array(portalOrderInput).nullable().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 }; /** * Buscar pedidos realizados * * GET /bifrost/orders */ export const getApiBifrostOrders = async (args: GetApiBifrostOrdersArgs, options?: GetApiBifrostOrdersOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.year != null) query.append("year", String(args.year)) if (args.months != null) query.append("months", String(args.months)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.start_value != null) query.append("start_value", String(args.start_value)) if (args.end_value != null) query.append("end_value", String(args.end_value)) if (args.state != null) query.append("state", String(args.state)) if (args.protocol != null) query.append("protocol", String(args.protocol)) if (args.license_plate != null) query.append("license_plate", String(args.license_plate)) if (args.renavam != null) query.append("renavam", String(args.renavam)) if (args.status != null) query.append("status", String(args.status)) if (args.status_in != null) query.append("status_in", String(args.status_in)) if (args.payment_type_in != null) query.append("payment_type_in", String(args.payment_type_in)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/bifrost/orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(bifrostResponseDTOInput, await res.json()) };