import {z} from 'zod' export type CreateApiP2pDeliveryOccupancyCandidateShippingWindowsArgs = {body: {origin_station?: string | undefined, origin_address?: ({city_id: number, full_address: string, latitude?: number | undefined, longitude?: number | undefined}) | undefined, sender_person?: {firstname: string, lastname: string, phone_number: string} | undefined, recipient_person: {firstname: string, lastname: string, phone_number: string}, destination_address: {city_id: number, full_address: string, latitude?: number | undefined, longitude?: number | undefined}, logistic_company: string, parcels: Array<{dimension: {width: number, height: number, length: number}, weight: number | null, value: number | null, submitter_id?: string | undefined, packaging_type?: ('box' | 'packet') | undefined, characteristics?: ('gold' | 'normal') | undefined}>, pod?: string | undefined, ready_time?: string | undefined, off_day_dates?: Array | undefined}}; export type CreateApiP2pDeliveryOccupancyCandidateShippingWindowsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiP2pDeliveryOccupancyCandidateShippingWindowsResponse = z.object({origin_station: z.string().optional(), origin_address: z.object({city_id: z.number().int(), full_address: z.string().min(1), latitude: z.number().optional(), longitude: z.number().optional()}).optional(), sender_person: z.object({firstname: z.string().min(1), lastname: z.string().min(1), phone_number: z.string().min(1)}).optional(), recipient_person: z.object({firstname: z.string().min(1), lastname: z.string().min(1), phone_number: z.string().min(1)}), destination_address: z.object({city_id: z.number().int(), full_address: z.string().min(1), latitude: z.number().optional(), longitude: z.number().optional()}), logistic_company: z.string(), parcels: z.array(z.object({dimension: z.object({width: z.number(), height: z.number(), length: z.number()}), weight: z.number().int().gte(-2147483648).lte(2147483647).nullable(), value: z.number().nullable(), submitter_id: z.string().min(1).optional(), packaging_type: z.enum(["box", "packet"]).optional(), characteristics: z.enum(["gold", "normal"]).optional()})), pod: z.string().min(5).max(5).optional(), ready_time: z.string().optional(), off_day_dates: z.array(z.string()).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 }; /** * POST /p2p-delivery/occupancy/candidate-shipping-windows */ export const createApiP2pDeliveryOccupancyCandidateShippingWindows = async (args: CreateApiP2pDeliveryOccupancyCandidateShippingWindowsArgs, options?: CreateApiP2pDeliveryOccupancyCandidateShippingWindowsOptions) => { const {baseUrl = "https://api.ganje.net/v1.1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/p2p-delivery/occupancy/candidate-shipping-windows${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(createApiP2pDeliveryOccupancyCandidateShippingWindowsResponse, await res.json()) };