import {z} from 'zod' export type QuoteRequest = {shipFrom: {correios?: ({logradouro: string, numero?: string | undefined, complemento?: string | undefined, bairro?: string | undefined, cep: string, cidade: string, uf: string}) | undefined} | {lines?: ({addressLine1: string, addressLine2?: string | undefined, postalCode: string, city: string, state: string, country?: string | undefined}) | undefined} | {widget?: ({address: string, complement?: string | undefined, placesApiInfo?: ({placeId?: string | undefined, sessionId?: string | undefined}) | undefined, userContext?: ({userIp?: string | undefined, position?: ({latitude?: number | undefined, longitude?: number | undefined}) | undefined}) | undefined}) | undefined}, shipTo: {correios?: ({logradouro: string, numero?: string | undefined, complemento?: string | undefined, bairro?: string | undefined, cep: string, cidade: string, uf: string}) | undefined} | {lines?: ({addressLine1: string, addressLine2?: string | undefined, postalCode: string, city: string, state: string, country?: string | undefined}) | undefined} | {widget?: ({address: string, complement?: string | undefined, placesApiInfo?: ({placeId?: string | undefined, sessionId?: string | undefined}) | undefined, userContext?: ({userIp?: string | undefined, position?: ({latitude?: number | undefined, longitude?: number | undefined}) | undefined}) | undefined}) | undefined}, packages?: Array<{weightG: number, lengthCm: number, widthCm: number, heightCm: number, goodsValue: {currencyCode: string, units: unknown, nanos: number}}> | undefined, externalServiceIds: Array} | {shipFrom: {correios?: ({logradouro: string, numero?: string | undefined, complemento?: string | undefined, bairro?: string | undefined, cep: string, cidade: string, uf: string}) | undefined} | {lines?: ({addressLine1: string, addressLine2?: string | undefined, postalCode: string, city: string, state: string, country?: string | undefined}) | undefined} | {widget?: ({address: string, complement?: string | undefined, placesApiInfo?: ({placeId?: string | undefined, sessionId?: string | undefined}) | undefined, userContext?: ({userIp?: string | undefined, position?: ({latitude?: number | undefined, longitude?: number | undefined}) | undefined}) | undefined}) | undefined}, shipTo: {correios?: ({logradouro: string, numero?: string | undefined, complemento?: string | undefined, bairro?: string | undefined, cep: string, cidade: string, uf: string}) | undefined} | {lines?: ({addressLine1: string, addressLine2?: string | undefined, postalCode: string, city: string, state: string, country?: string | undefined}) | undefined} | {widget?: ({address: string, complement?: string | undefined, placesApiInfo?: ({placeId?: string | undefined, sessionId?: string | undefined}) | undefined, userContext?: ({userIp?: string | undefined, position?: ({latitude?: number | undefined, longitude?: number | undefined}) | undefined}) | undefined}) | undefined}, packages?: Array<{weightG: number, lengthCm: number, widthCm: number, heightCm: number, goodsValue: {currencyCode: string, units: unknown, nanos: number}}> | undefined, pickupTypes: Array<'PICKUP_TYPE_SPOT' | 'PICKUP_TYPE_DEDICATED' | 'PICKUP_TYPE_MILK_RUN' | 'PICKUP_TYPE_DROP_OFF'>}; export type CreateApiV1CompaniesCompanyIdQuotationsArgs = {company_id: string, body: QuoteRequest}; export type CreateApiV1CompaniesCompanyIdQuotationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const quoteResponse = z.object({packagesQuotations: z.object({quotations: z.array(z.object({price: z.object({totalAmount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional(), baseAmount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional(), taxesAndFees: z.object({pis: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional(), cofins: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional(), gris: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional(), advalorem: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional(), icms: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional(), iss: z.object({rate_tax: z.string().optional(), amount: z.object({currencyCode: z.string(), units: z.string(), nanos: z.number().int()}).optional()}).optional()}).optional()}).optional(), sloInDays: z.number().int().optional(), freightType: z.enum(["FREIGHT_TYPE_EXPRESS", "FREIGHT_TYPE_ECONOMIC"]).optional(), freightTypeLabel: z.string().optional(), pickup_type: z.enum(["PICKUP_TYPE_SPOT", "PICKUP_TYPE_DEDICATED", "PICKUP_TYPE_MILK_RUN", "PICKUP_TYPE_DROP_OFF"]).optional(), externalServiceIds: z.string().optional()})).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 }; /** * Criar Cotação * * POST /v1/companies/{company_id}/quotations */ export const createApiV1CompaniesCompanyIdQuotations = async (args: CreateApiV1CompaniesCompanyIdQuotationsArgs, options?: CreateApiV1CompaniesCompanyIdQuotationsOptions) => { const {baseUrl = "https://stg.api.loggi.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies/${encodeURIComponent(String(args.company_id))}/quotations${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(quoteResponse, await res.json()) };