import {z} from 'zod' export type CreateApiV1ConnectGooglebusinessSelectLocationArgs = {body: {profileId: string, locationId?: string | undefined, locations?: (Array<{locationId: string, accountId?: string | undefined}>) | undefined, accountId?: string | undefined, pendingDataToken: string, redirect_url?: string | undefined}}; export type CreateApiV1ConnectGooglebusinessSelectLocationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ConnectGooglebusinessSelectLocationResponse = z.object({message: z.string().optional(), redirect_url: z.string().optional(), account: z.object({accountId: z.string().optional(), platform: z.literal("googlebusiness").optional(), username: z.string().optional(), displayName: z.string().optional(), isActive: z.boolean().optional(), selectedLocationName: z.string().optional(), selectedLocationId: z.string().optional()}).optional(), accounts: z.array(z.object({})).optional(), failed: z.array(z.object({id: z.string().optional(), code: z.string().optional(), message: z.string().optional()})).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 }; /** * Select Google Business Profile location * * POST /v1/connect/googlebusiness/select-location * * Also accepts auth: connectToken */ export const createApiV1ConnectGooglebusinessSelectLocation = async (args: CreateApiV1ConnectGooglebusinessSelectLocationArgs, options?: CreateApiV1ConnectGooglebusinessSelectLocationOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/connect/googlebusiness/select-location${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(createApiV1ConnectGooglebusinessSelectLocationResponse, await res.json()) };