import {z} from 'zod' export type GetApiV10ListingArgs = {userId?: number | undefined, listingId?: string | undefined, nextPageKey?: string | undefined, soldNextPageKey?: string | undefined, mlsListingIds?: string | undefined, street?: string | undefined, limit?: number | undefined, soldListingId?: string | undefined, officeListings?: boolean | undefined, agentListings?: boolean | undefined, buildingName?: string | undefined, buildingAddress?: string | undefined, showCondition?: boolean | undefined, Authorization: string}; export type GetApiV10ListingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const listingMetaData = z.object({collection: z.string().optional(), limit: z.number().int().optional(), total: z.number().int().optional(), activeTotal: z.number().int().optional(), soldTotal: z.number().int().optional(), nextPageKey: z.string().optional(), soldNextPageKey: z.string().optional()}); export const listingSearchResponse = z.object({listIng: z.object({}).optional(), soldListing: z.object({}).optional(), get_listingMetadata: listingMetaData.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 }; /** * Search by Agent / Office * * GET /v1.0/listing */ export const getApiV10Listing = async (args: GetApiV10ListingArgs, options?: GetApiV10ListingOptions) => { const {baseUrl = "https://api.lofty.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.userId != null) query.append("userId", String(args.userId)) if (args.listingId != null) query.append("listingId", String(args.listingId)) if (args.nextPageKey != null) query.append("nextPageKey", String(args.nextPageKey)) if (args.soldNextPageKey != null) query.append("soldNextPageKey", String(args.soldNextPageKey)) if (args.mlsListingIds != null) query.append("mlsListingIds", String(args.mlsListingIds)) if (args.street != null) query.append("street", String(args.street)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.soldListingId != null) query.append("soldListingId", String(args.soldListingId)) if (args.officeListings != null) query.append("officeListings", String(args.officeListings)) if (args.agentListings != null) query.append("agentListings", String(args.agentListings)) if (args.buildingName != null) query.append("buildingName", String(args.buildingName)) if (args.buildingAddress != null) query.append("buildingAddress", String(args.buildingAddress)) if (args.showCondition != null) query.append("showCondition", String(args.showCondition)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/listing${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listingSearchResponse, await res.json()) };