import {z} from 'zod' export type CreateApiV1GuardPlaceIdArgs = {place_id: string, body: {api_key: string, place_id?: string | undefined, start_date?: string | undefined, end_date?: string | undefined}}; export type CreateApiV1GuardPlaceIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1GuardPlaceIdResponse = z.object({code: z.number().int().optional(), code_desc: z.boolean().optional(), success: z.boolean().optional(), message: z.boolean().optional(), parameters: z.object({}).optional(), data: z.object({id: z.string().optional(), place_id: z.string().optional(), location: z.object({place_id: z.string().optional(), fid: z.string().optional(), cid: z.string().optional(), name: z.string().optional(), address: z.union([z.string(), z.boolean()]).optional(), full_address: z.union([z.string(), z.boolean()]).optional(), is_sab: z.boolean().optional(), lat: z.string().optional(), lng: z.string().optional(), plus_code: z.union([z.string(), z.boolean()]).optional(), store_code: z.string().optional(), description: z.string().optional(), phone: z.string().optional(), url: z.string().optional(), display_url: z.string().optional(), hours: z.object({}).optional(), rating: z.string().optional(), reviews: z.string().optional(), primary_category: z.string().optional(), secondary_categories: z.array(z.string()).optional()}).optional(), changes: z.array(z.object({})).optional(), metrics: z.object({}).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 }; /** * Get Specific Falcon Guard Report * * POST /v1/guard/{place_id} */ export const createApiV1GuardPlaceId = async (args: CreateApiV1GuardPlaceIdArgs, options?: CreateApiV1GuardPlaceIdOptions) => { 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(/\/+$/, '')}/v1/guard/${encodeURIComponent(String(args.place_id))}${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(createApiV1GuardPlaceIdResponse, await res.json()) };