import {z} from 'zod' export type LocationMediaType = 'LOGO' | 'COVER' | 'ADDITIONAL'; export type LocationMediaInputType = {photo: string, type?: LocationMediaType | undefined, starred?: boolean | undefined, source?: string | undefined, publisherOverride?: string | undefined, partnerMediaId?: string | undefined}; export type AddLocationMediaItemsInputType = {locationId: string, photos: Array}; export type CreateApiApiV4LocationsPhotosArgs = {body: {input: AddLocationMediaItemsInputType}}; export type CreateApiApiV4LocationsPhotosOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const contextInfoType = z.object({key: z.string().optional(), value: z.string().optional()}); export const errorType = z.object({message: z.string().optional(), contextInfo: z.array(contextInfoType).optional(), code: z.string().optional()}); export const locationMediaType = z.enum(["LOGO", "COVER", "ADDITIONAL"]); export const locationMedia = z.object({databaseId: z.number().int().optional(), fileSize: z.number().int().optional(), id: z.string().optional(), name: z.string().optional(), starred: z.boolean().optional(), thumbnailUrl: z.string().optional(), type: locationMediaType.optional(), url: z.string().optional(), publisherOverride: z.string().optional(), partnerMediaId: z.string().optional(), originalUrl: z.string().optional()}); export const requestType = z.object({requestId: z.string().optional(), status: z.string().optional()}); export const addLocationPhotosMutation = z.object({success: z.boolean().optional(), errors: z.array(errorType).optional(), photos: z.array(locationMedia).optional(), request: requestType.optional(), clientMutationId: z.string().optional()}); export const apiError = z.object({message: z.string(), code: z.string().optional(), contextInfo: z.array(z.object({key: z.string().optional(), value: z.string().optional()})).optional()}); export const createApiApiV4LocationsPhotosResponse = z.object({data: z.object({addLocationPhotos: addLocationPhotosMutation.optional()}), errors: z.array(apiError).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 }; /** * Attach photos to a location by public image URL. * * POST /api/v4/locations/photos * * Also accepts auth: rapidApiKey + rapidApiHost */ export const createApiApiV4LocationsPhotos = async (args: CreateApiApiV4LocationsPhotosArgs, options?: CreateApiApiV4LocationsPhotosOptions) => { const {baseUrl = "https://listingsapi.com", 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("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/locations/photos${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(createApiApiV4LocationsPhotosResponse, await res.json()) };