import {z} from 'zod' export type GetApiAutogameGoogleSearchArgs = {c2coff?: string | undefined, cr?: string | undefined, cx?: string | undefined, dateRestrict?: string | undefined, exactTerms?: string | undefined, excludeTerms?: string | undefined, fileType?: string | undefined, filter?: string | undefined, gl?: string | undefined, googlehost?: string | undefined, highRange?: string | undefined, hl?: string | undefined, hq?: string | undefined, imgColorType?: string | undefined, imgDominantColor?: string | undefined, imgSize?: string | undefined, imgType?: string | undefined, linkSite?: string | undefined, lowRange?: string | undefined, lr?: string | undefined, num?: number | undefined, orTerms?: string | undefined, q: string, relatedSite?: string | undefined, rights?: string | undefined, safe?: string | undefined, searchType?: string | undefined, siteSearch?: string | undefined, siteSearchFilter?: string | undefined, sort?: string | undefined, start?: number | undefined}; export type GetApiAutogameGoogleSearchOptions = {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 }; /** * GET /autogame/google_search */ export const getApiAutogameGoogleSearch = async (args: GetApiAutogameGoogleSearchArgs, options: GetApiAutogameGoogleSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.c2coff != null) query.append("c2coff", String(args.c2coff)) if (args.cr != null) query.append("cr", String(args.cr)) if (args.cx != null) query.append("cx", String(args.cx)) if (args.dateRestrict != null) query.append("dateRestrict", String(args.dateRestrict)) if (args.exactTerms != null) query.append("exactTerms", String(args.exactTerms)) if (args.excludeTerms != null) query.append("excludeTerms", String(args.excludeTerms)) if (args.fileType != null) query.append("fileType", String(args.fileType)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.gl != null) query.append("gl", String(args.gl)) if (args.googlehost != null) query.append("googlehost", String(args.googlehost)) if (args.highRange != null) query.append("highRange", String(args.highRange)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.hq != null) query.append("hq", String(args.hq)) if (args.imgColorType != null) query.append("imgColorType", String(args.imgColorType)) if (args.imgDominantColor != null) query.append("imgDominantColor", String(args.imgDominantColor)) if (args.imgSize != null) query.append("imgSize", String(args.imgSize)) if (args.imgType != null) query.append("imgType", String(args.imgType)) if (args.linkSite != null) query.append("linkSite", String(args.linkSite)) if (args.lowRange != null) query.append("lowRange", String(args.lowRange)) if (args.lr != null) query.append("lr", String(args.lr)) if (args.num != null) query.append("num", String(args.num)) if (args.orTerms != null) query.append("orTerms", String(args.orTerms)) if (args.q != null) query.append("q", String(args.q)) if (args.relatedSite != null) query.append("relatedSite", String(args.relatedSite)) if (args.rights != null) query.append("rights", String(args.rights)) if (args.safe != null) query.append("safe", String(args.safe)) if (args.searchType != null) query.append("searchType", String(args.searchType)) if (args.siteSearch != null) query.append("siteSearch", String(args.siteSearch)) if (args.siteSearchFilter != null) query.append("siteSearchFilter", String(args.siteSearchFilter)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.start != null) query.append("start", String(args.start)) 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: "GET", headers}) 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()) };