import {z} from 'zod' export type CreateApiApiV2MeCartReverseArgs = {Accept: string, 'Content-Type': string, Authorization: string, 'User-Agent': string, body: {service: number, new_sender_mail: string, new_sender_phone: string, insurance_value: number, agency?: number | undefined, from: {name: string, phone?: string | undefined, email: string, document: string, company_document: string, state_register?: string | undefined, address: string, complement?: string | undefined, number: string, district: string, city: string, country_id: string, postal_code: string, state_abbr: string, note?: string | undefined}, to: {name: string, phone?: string | undefined, email?: string | undefined, document: string, company_document: string, state_register?: string | undefined, address: string, complement?: string | undefined, number: string, district: string, city: string, country_id: string, postal_code: string, state_abbr: string, note?: string | undefined}, products?: Array<{name: string, quantity: string, unitary_value: string, weight: number}> | undefined, package: {height: number, width: number, length: number, weight: number}, options?: (Array<{own_hand?: boolean | undefined, receipt?: boolean | undefined, platform?: string | undefined, tags?: (Array<{tag?: string | undefined, Url?: string | undefined}>) | undefined, dce?: ({key?: string | undefined}) | undefined}>) | undefined}}; export type CreateApiApiV2MeCartReverseOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV2MeCartReverseResponse = z.object({id: z.string().optional(), protocol: z.string().optional(), service_id: z.number().int().optional(), agency_id: z.number().int().optional(), contract: z.unknown(), service_code: z.unknown(), quote: z.number().optional(), price: z.number().optional(), coupon: z.unknown(), discount: z.number().optional(), delivery_min: z.number().int().optional(), delivery_max: z.number().int().optional(), status: z.string().optional(), reminder: z.unknown(), insurance_value: z.number().int().optional(), weight: z.unknown(), width: z.unknown(), height: z.unknown(), length: z.unknown(), diameter: z.unknown(), format: z.string().optional(), billed_weight: z.number().optional(), receipt: z.boolean().optional(), own_hand: z.boolean().optional(), collect: z.boolean().optional(), collect_scheduled_at: z.unknown(), reverse: z.boolean().optional(), non_commercial: z.boolean().optional(), authorization_code: z.unknown(), tracking: z.unknown(), self_tracking: z.unknown(), delivery_receipt: z.unknown(), additional_info: z.unknown(), cte_key: z.unknown(), paid_at: z.unknown(), generated_at: z.unknown(), posted_at: z.unknown(), delivered_at: z.unknown(), canceled_at: z.unknown(), suspended_at: z.unknown(), expired_at: z.unknown(), created_at: z.string().optional(), updated_at: z.string().optional(), parse_pi_at: z.unknown(), products: z.array(z.object({name: z.string().optional(), quantity: z.number().int().optional(), unitary_value: z.number().int().optional(), weight: z.unknown()})).optional(), volumes: z.array(z.object({id: z.number().int().optional(), height: z.string().optional(), width: z.string().optional(), length: z.string().optional(), diameter: z.string().optional(), weight: z.string().optional(), format: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).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 }; /** * Inserir LogĂ­stica Reversa no carrinho * * POST /api/v2/me/cart/reverse */ export const createApiApiV2MeCartReverse = async (args: CreateApiApiV2MeCartReverseArgs, options?: CreateApiApiV2MeCartReverseOptions) => { const {baseUrl = "https://sandbox.melhorenvio.com.br", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/me/cart/reverse${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(createApiApiV2MeCartReverseResponse, await res.json()) };