import {z} from 'zod' export type BookingV2ApiV1LimitedInventoryProductType = 'TopSpot' | 'TopSpotRural' | 'ListingShowcase' | 'DreamHomes'; export type BookingV2ApiV1LimitedInventoryListingCategory = 'Sale' | 'Lease'; export type BookingV2ApiV1ListingFilterType = 'Residential' | 'NewDevelopment' | 'Project' | 'Rural'; export type BookingV2ApiV1Channel = 'Residential' | 'Commercial' | 'Business'; export type BookingV2ApiV1DurationDto = {durationNo?: number | undefined, dateStart?: string | undefined, dateEnd?: string | undefined}; export type BookingV2ApiV1ReserveInventoryRequest = {productType?: BookingV2ApiV1LimitedInventoryProductType | undefined, listingCategory?: BookingV2ApiV1LimitedInventoryListingCategory | undefined, listingFilterType?: BookingV2ApiV1ListingFilterType | undefined, channel?: BookingV2ApiV1Channel | undefined, suburbSlug?: (string | null) | undefined, requestedBy?: (string | null) | undefined, durations?: (Array | null) | undefined, ipAddress?: (string | null) | undefined, legacyContractId?: (number | null) | undefined, assetReferenceId: string, isExclusive?: boolean | undefined}; export type BookingV2ApiV1ReserveInventoryByBatchRequest = {agencyId?: number | undefined, requestedBy: string, reservations?: (Array | null) | undefined}; export type CreateApiV1ProductsLimitedInventoryReserveByBatchArgs = {body: BookingV2ApiV1ReserveInventoryByBatchRequest}; export type CreateApiV1ProductsLimitedInventoryReserveByBatchOptions = {baseUrl?: string | undefined, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const bookingV2ApiV1LimitedInventoryProductType = z.enum(["TopSpot", "TopSpotRural", "ListingShowcase", "DreamHomes"]); export const bookingV2ApiV1LimitedInventoryListingCategory = z.enum(["Sale", "Lease"]); export const bookingV2ApiV1ReservationResponse = z.object({error: z.string().nullable().optional(), reservationId: z.number().int().optional(), agencyId: z.number().int().optional(), suburbSlug: z.string().nullable().optional(), productType: bookingV2ApiV1LimitedInventoryProductType.optional(), listingCategory: bookingV2ApiV1LimitedInventoryListingCategory.optional(), dateStart: z.string().optional(), dateEnd: z.string().optional(), noOfDurations: z.number().int().optional(), assetReferenceId: z.string().nullable().optional()}); export const bookingV2ApiV1BatchSummaryResponse = z.object({requested: z.number().int().optional(), succeeded: z.number().int().optional(), failed: z.number().int().optional()}); export const bookingV2ApiV1ReserveInventoryByBatchResponse = z.object({results: z.array(bookingV2ApiV1ReservationResponse).nullable().optional(), summary: bookingV2ApiV1BatchSummaryResponse.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 }; /** * Reserves inventory for a batch of products based on the provided request. * * POST /v1/Products/LimitedInventory/ReserveByBatch * * Also accepts auth: oauth2 */ export const createApiV1ProductsLimitedInventoryReserveByBatch = async (args: CreateApiV1ProductsLimitedInventoryReserveByBatchArgs, options?: CreateApiV1ProductsLimitedInventoryReserveByBatchOptions) => { const {baseUrl = "https://api.domain.com.au", fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apikey !== undefined) headers.set("x-api-key", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Products/LimitedInventory/ReserveByBatch${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(bookingV2ApiV1ReserveInventoryByBatchResponse, await res.json()) };