import {z} from 'zod' export type CreateApiV2GbpReplyReviewArgs = {body: {api_key: string, place_id?: string | undefined, review_id?: string | undefined, reply?: string | undefined, replies?: Array<{place_id: string, review_id: string, reply: string}> | undefined}}; export type CreateApiV2GbpReplyReviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2GbpReplyReviewResponse = z.object({code: z.number().int().optional(), code_desc: z.union([z.string(), z.boolean()]).optional(), success: z.boolean().optional(), message: z.union([z.string(), z.boolean()]).optional(), parameters: z.object({}).optional(), data: z.object({count: z.number().int().optional(), succeeded: z.number().int().optional(), failed: z.number().int().optional(), results: z.array(z.object({place_id: z.string().optional(), review_id: z.string().optional(), success: z.boolean().optional(), error: z.union([z.string(), z.boolean()]).optional(), message: z.union([z.string(), z.boolean()]).optional(), reply: z.string().optional()})).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 }; /** * Reply to Business Profile Reviews * * POST /v2/gbp/reply-review/ */ export const createApiV2GbpReplyReview = async (args: CreateApiV2GbpReplyReviewArgs, options?: CreateApiV2GbpReplyReviewOptions) => { const {baseUrl = "https://api.localfalcon.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/gbp/reply-review/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2GbpReplyReviewResponse, await res.json()) };