import {z} from 'zod' export type SearchInformation = {totalResults?: string | undefined}; export type Image = {byteSize?: number | undefined, contextLink?: string | undefined, height?: number | undefined, thumbnailHeight?: number | undefined, thumbnailLink?: string | undefined, thumbnailWidth?: number | undefined, width?: number | undefined}; export type Items = {link?: string | undefined, fileFormat?: string | undefined, title?: string | undefined, htmlTitle?: string | undefined, displayLink?: string | undefined, mime?: string | undefined, image?: Image | undefined}; export type InsertRawSearchDto = {searchInformation?: SearchInformation | undefined, items: Array}; export type CreateApiAutogameGoogleSearchArgs = {body: InsertRawSearchDto}; export type CreateApiAutogameGoogleSearchOptions = {baseUrl: string, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const searchInformation = z.object({totalResults: z.string().optional()}); export const image = z.object({byteSize: z.number().optional(), contextLink: z.string().optional(), height: z.number().optional(), thumbnailHeight: z.number().optional(), thumbnailLink: z.string().optional(), thumbnailWidth: z.number().optional(), width: z.number().optional()}); export const items = z.object({link: z.string().optional(), fileFormat: z.string().optional(), title: z.string().optional(), htmlTitle: z.string().optional(), displayLink: z.string().optional(), mime: z.string().optional(), image: image.optional()}); export const googleSearchResult = z.object({searchInformation: searchInformation.optional(), items: z.array(items)}); export const googleSearch = z.object({id: z.string(), result: googleSearchResult}); 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 }; /** * POST /autogame/google_search */ export const createApiAutogameGoogleSearch = async (args: CreateApiAutogameGoogleSearchArgs, options: CreateApiAutogameGoogleSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/autogame/google_search${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(googleSearch, await res.json()) };