import {z} from 'zod' export type ListDropoffLocationsRequest = {categories: Array, limit?: (number | null) | undefined, offset?: (number | null) | undefined, location_id?: (string | null) | undefined, postal_code?: (string | null) | undefined, country?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, longitude?: (number | null) | undefined, latitude?: (number | null) | undefined, distance?: (number | null) | undefined}; export type CreateApiDropoffLocationsArgs = {body: ListDropoffLocationsRequest}; export type CreateApiDropoffLocationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const openingHoursDay = z.object({day: z.string().optional(), open: z.string().optional(), close: z.string().optional()}); export const dropoffLocation = z.object({location_id: z.string().optional(), location_name: z.string().optional(), location_type: z.string().optional(), categories: z.array(z.string()).optional(), available_compartments: z.unknown(), longitude: z.number().optional(), latitude: z.number().optional(), country: z.string().optional(), city: z.string().optional(), district: z.string().optional(), postal_code: z.string().optional(), address: z.string().optional(), neighborhood: z.string().optional(), state: z.string().optional(), complement: z.string().optional(), opening_hours: z.string().optional(), opening_hours_detail: z.string().optional(), navigation_link: z.unknown(), special_holidays: z.unknown(), site_condition: z.unknown(), opening_hours_details: z.array(openingHoursDay).optional()}); export const listDropoffLocationsPagination = z.array(dropoffLocation); export const listDropoffLocationsMessage = z.object({limit: z.number().int(), offset: z.number().int(), current_page: z.number().int(), total_pages: z.number().int(), total_locations: z.number().int(), pagination: listDropoffLocationsPagination}); export const listDropoffLocationsResponse = z.object({status: z.literal("success"), message: listDropoffLocationsMessage.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 Loggi Pontos. * * POST /dropoff/locations */ export const createApiDropoffLocations = async (args: CreateApiDropoffLocationsArgs, options?: CreateApiDropoffLocationsOptions) => { 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(/\/+$/, '')}/dropoff/locations${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(listDropoffLocationsResponse, await res.json()) };