import {z} from 'zod' export type CreateApiV2GbpMediaArgs = {body: {api_key: string, place_id: string, media_id?: string | undefined, category?: string | undefined, limit?: number | undefined, next_token?: string | undefined}}; export type CreateApiV2GbpMediaOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2GbpMediaResponse = 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({}).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 }; /** * List Business Profile Media * * POST /v2/gbp/media/ */ export const createApiV2GbpMedia = async (args: CreateApiV2GbpMediaArgs, options?: CreateApiV2GbpMediaOptions) => { 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/media/${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(createApiV2GbpMediaResponse, await res.json()) };