import {z} from 'zod' export type GetApiLocationsSearchArgs = {query: string, fields?: Array | undefined, first?: number | undefined, last?: number | undefined, after?: string | undefined, before?: string | undefined, archived?: string | undefined, Authorization: string}; export type GetApiLocationsSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLocationsSearchResponse = z.object({data: z.object({searchLocations: z.object({edges: z.array(z.object({cursor: z.string(), node: z.object({accountId: z.number().int(), addOns: z.array(z.string()), additionalCategoryIds: z.array(z.number().int()), additionalCategoryNames: z.array(z.string()), additionalInfo: z.unknown(), additionalPhones: z.array(z.string()), approved: z.string(), archivalScheduledAt: z.unknown(), archived: z.boolean(), archivedAt: z.unknown(), bizUrl: z.unknown(), bizUrlUtm: z.boolean(), bookingUrl: z.unknown(), businessHours: z.array(z.object({day: z.string(), slots: z.array(z.object({end: z.string(), start: z.string()})), specialDate: z.unknown(), type: z.string()})), cancellationScheduledAt: z.unknown(), categoryId: z.number().int(), city: z.string(), contentPreferredLanguage: z.string(), countryCode: z.string(), countryIso: z.string(), createErrors: z.array(z.string()), createdDate: z.string(), customAttributes: z.array(z.object({archived: z.boolean(), enumValues: z.array(z.string()), name: z.string(), slug: z.string(), type: z.string(), value: z.array(z.string())})), databaseId: z.number().int(), description: z.string(), enabledSiteIds: z.array(z.string()), facebookUrl: z.string().nullable(), folderId: z.string(), folderName: z.string(), franchise: z.boolean(), googleAdsPhone: z.string().nullable(), googleVerificationStatus: z.object({message: z.unknown(), status: z.string()}), googleplusUrl: z.unknown(), hideAddress: z.boolean(), id: z.string(), instagramUrl: z.unknown(), isRestaurant: z.boolean(), lastUpdatedDate: z.string(), latitude: z.string(), linkedinUrl: z.string().nullable(), locationPhotos: z.array(z.object({databaseId: z.number().int(), fileSize: z.number().int(), id: z.string(), name: z.string(), starred: z.boolean(), thumbnailUrl: z.string(), type: z.string(), url: z.string()})), longitude: z.string(), managedServiceSiteBuckets: z.array(z.string()), managedServiceSites: z.array(z.string()), moreHours: z.array(z.string()), name: z.string(), offeringsUrl: z.unknown(), ownerEmail: z.string().nullable(), ownerName: z.string().nullable(), package: z.string(), paymentMethods: z.array(z.string()), phone: z.string(), pinterestUrl: z.unknown(), planId: z.number().int(), planName: z.string(), postalCode: z.string(), publisherAttributes: z.array(z.object({attributeName: z.string().optional(), publisherName: z.string().optional(), value: z.array(z.string()).optional()})), reservationUrl: z.unknown(), serviceItems: z.array(z.string()), stateIso: z.string(), stateName: z.string(), storeId: z.string(), street: z.string(), street1: z.string(), subCategoryId: z.number().int(), subCategoryName: z.string(), tagline: z.string().nullable(), tags: z.array(z.string()), temporarilyClosed: z.boolean(), tiktokUrl: z.unknown(), toplineMessage: z.unknown(), toplineMessageText: z.unknown(), twitterUrl: z.string().nullable(), uid: z.string(), updatedAt: z.string(), videos: z.array(z.string()), voiceStatus: z.number().int(), yearOfIncorporation: z.number().int().nullable(), youtubeUrl: z.unknown()})})), pageInfo: z.object({endCursor: z.string(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), total: z.number().int()})})})}); 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 locations * * GET /locations/search */ export const getApiLocationsSearch = async (args: GetApiLocationsSearchArgs, options?: GetApiLocationsSearchOptions) => { const {baseUrl = "https://api.synup.com/api/v4", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.first != null) query.append("first", String(args.first)) if (args.last != null) query.append("last", String(args.last)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.archived != null) query.append("archived", String(args.archived)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/locations/search${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(getApiLocationsSearchResponse, await res.json()) };