import {z} from 'zod' export type DayOfTheWeekType = 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY' | 'SUNDAY'; export type BusinessHoursSlotInput = {start: string, end: string}; export type BusinessHours = 'OPEN' | 'CLOSED'; export type BusinessHoursDayInput = {day: DayOfTheWeekType, slots?: Array | undefined, type: BusinessHours}; export type PlaceInfoInput = {placeId: string, placeName?: string | undefined}; export type ServiceAreaInput = {businessType?: string | undefined, regionCode?: string | undefined, placeInfos?: Array | undefined}; export type PlaceActionTypeEnum = 'APPOINTMENT' | 'ONLINE_APPOINTMENT' | 'DINING_RESERVATION' | 'FOOD_ORDERING' | 'FOOD_DELIVERY' | 'FOOD_TAKEOUT' | 'SHOP_ONLINE'; export type PlaceActionLinkInputType = {placeActionType: PlaceActionTypeEnum, uri: string, isPreferred?: boolean | undefined, name?: string | undefined, providerType?: string | undefined, isEditable?: boolean | undefined, action?: string | undefined}; export type ServiceItemInputType = {name: string, categoryId?: string | undefined, price?: number | undefined, description?: string | undefined, currencyCode?: string | undefined}; export type CustomAttributeInputTypeEnum = 'STRING' | 'NUMBER' | 'BOOLEAN' | 'DATE' | 'ENUM' | 'URL'; export type CustomAttributeLevelTypeEnum = 'ACCOUNT' | 'LOCATION'; export type CustomAttributeInputType = {name: string, value?: string | undefined, type?: CustomAttributeInputTypeEnum | undefined, levelType?: CustomAttributeLevelTypeEnum | undefined}; export type PaymentMethodType = 'VISA' | 'MASTERCARD' | 'AMEX' | 'CASH' | 'CHEQUE' | 'CRYPTO' | 'DINERS_CLUB' | 'DISCOVER' | 'FINANCING' | 'INVOICE' | 'TRAVELERS_CHECK' | 'APPLE_PAY' | 'SAMSUNG_PAY' | 'PAYPAL' | 'ANDROID_PAY'; export type CreateLocationInput = {clientMutationId?: string | undefined, name: string, description: string, storeId?: string | undefined, street?: string | undefined, city?: string | undefined, stateIso: string, postalCode?: string | undefined, countryIso: string, subCategoryId: number, phone: string, businessHours?: Array | undefined, serviceArea?: ServiceAreaInput | undefined, enabledSiteIds?: Array | undefined, placeActionLinks?: Array | undefined, bizUrl: string, primaryGbpSiteCategoryId?: string | undefined, additionalGbpSiteCategoryIds?: Array | undefined, offeringsUrl?: string | undefined, bookingUrl?: string | undefined, reservationUrl?: string | undefined, street1?: string | undefined, latitude?: string | undefined, longitude?: string | undefined, ownerEmail?: string | undefined, ownerName?: string | undefined, facebookUrl?: string | undefined, twitterUrl?: string | undefined, linkedinUrl?: string | undefined, instagramUrl?: string | undefined, youtubeUrl?: string | undefined, tiktokUrl?: string | undefined, pinterestUrl?: string | undefined, tagline?: string | undefined, yearOfIncorporation?: string | undefined, googleAdsPhone?: string | undefined, additionalPhones?: Array | undefined, videos?: Array | undefined, additionalCategoryIds?: Array | undefined, submissionDisabledSiteIds?: Array | undefined, hideAddress?: boolean | undefined, temporarilyClosed?: boolean | undefined, serviceItems?: Array | undefined, customAttributes?: Array | undefined, paymentMethods?: Array | undefined}; export type CreateApiApiV4LocationsArgs = {body: {input: CreateLocationInput}}; export type CreateApiApiV4LocationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const locationStatusType = z.enum(["ACTIVE", "ARCHIVED", "PENDING", "TEMPORARILY_CLOSED", "SCHEDULED_FOR_ARCHIVE"]); export const placeInfoType = z.object({placeId: z.string(), placeName: z.string().optional()}); export const serviceAreaType = z.object({businessType: z.string().optional(), regionCode: z.string().optional(), placeInfos: z.array(placeInfoType).optional()}); export const placeActionLinkType = z.object({placeActionType: z.string(), uri: z.string(), isPreferred: z.boolean().optional(), name: z.string().optional(), providerType: z.string().optional(), isEditable: z.boolean().optional(), submissionStatus: z.string().optional(), submissionMessage: z.string().optional(), submissionCode: z.string().optional(), lastSubmittedAt: z.string().optional()}); export const locationSummaryType = z.object({id: z.string(), databaseId: z.number().int().optional(), storeId: z.string().optional(), name: z.string(), street: z.string().optional(), city: z.string().optional(), stateIso: z.string().optional(), countryIso: z.string().optional(), postalCode: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), phone: z.string().optional(), website: z.string().optional(), status: locationStatusType.optional(), primaryGbpSiteCategoryId: z.string().optional(), additionalGbpSiteCategoryIds: z.array(z.string()).optional(), serviceArea: z.array(serviceAreaType).optional(), placeActionLinks: z.array(placeActionLinkType).optional(), discoveredPlaceActionLinks: z.array(placeActionLinkType).optional()}); export const mutationErrorType = z.object({code: z.string().optional(), message: z.string(), path: z.array(z.string()).optional()}); export const createLocationPayload = z.object({clientMutationId: z.string().optional(), location: locationSummaryType.optional(), errors: z.array(mutationErrorType).optional(), success: z.boolean().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 createApiApiV4LocationsResponse = z.object({data: z.object({createLocation: createLocationPayload.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 }; /** * Create a new location. * * POST /api/v4/locations * * Also accepts auth: rapidApiKey + rapidApiHost */ export const createApiApiV4Locations = async (args: CreateApiApiV4LocationsArgs, options?: CreateApiApiV4LocationsOptions) => { 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${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(createApiApiV4LocationsResponse, await res.json()) };