import {z} from 'zod' export type GetApiApiAppsAppIdGoogleAdsGeoTargetsSearchArgs = {app_id: string, query?: string | undefined, country_code?: string | undefined, limit?: number | undefined}; export type GetApiApiAppsAppIdGoogleAdsGeoTargetsSearchOptions = {baseUrl?: string | undefined, PersonalAccessTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleAdsGeoTarget = z.object({resource_name: z.string(), name: z.string(), canonical_name: z.string(), country_code: z.string(), target_type: z.string(), reach: z.number().int(), search_term: z.string()}); export const getApiApiAppsAppIdGoogleAdsGeoTargetsSearchResponse = z.array(googleAdsGeoTarget); 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 }; /** * Search Google Ads locations * * GET /api/apps/{app_id}/google-ads/geo-targets/search * * Also accepts auth: WorkspaceApiKeyAuth, WorkspaceApiKeyBearerAuth */ export const getApiApiAppsAppIdGoogleAdsGeoTargetsSearch = async (args: GetApiApiAppsAppIdGoogleAdsGeoTargetsSearchArgs, options?: GetApiApiAppsAppIdGoogleAdsGeoTargetsSearchOptions) => { const {baseUrl = "https://app.base44.com", fetch: fetchFn = fetch, PersonalAccessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (args.limit != null) query.append("limit", String(args.limit)) if (PersonalAccessTokenAuth !== undefined) headers.set('authorization', `Bearer ${PersonalAccessTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/apps/${encodeURIComponent(String(args.app_id))}/google-ads/geo-targets/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(getApiApiAppsAppIdGoogleAdsGeoTargetsSearchResponse, await res.json()) };