import {z} from 'zod' export type GetApiV1ProjectsProjectIdGeoScansArgs = {projectId: string, limit?: number | undefined, page?: number | undefined}; export type GetApiV1ProjectsProjectIdGeoScansOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geoScan = z.object({id: z.string(), projectId: z.string(), status: z.enum(["running", "completed", "failed"]), startedAt: z.string(), finishedAt: z.string().nullable(), createdAt: z.string(), summary: z.object({plannedChecks: z.number().int().gte(0).nullable(), completedChecks: z.number().int().gte(0), mentionCount: z.number().int().gte(0), failedChecks: z.number().int().gte(0), engines: z.array(z.object({engine: z.string(), plannedChecks: z.number().int().gte(0).nullable(), completedChecks: z.number().int().gte(0), mentionCount: z.number().int().gte(0), failedChecks: z.number().int().gte(0)}))}), errorCode: z.string().nullable(), errorMessage: z.string().nullable(), failedStage: z.enum(["handoff", "execution", "stale", "null"]).nullable(), retryable: z.boolean().nullable()}); export const listGeoScansResponse = z.object({scans: z.array(geoScan), pagination: z.object({limit: z.number().int().gte(1), currentPage: z.number().int().gte(1), nextPage: z.number().int().gte(1).nullable(), previousPage: z.number().int().gte(1).nullable(), totalPages: z.number().int().gte(1), totalItems: z.number().int().gte(0)}), organization: z.object({id: z.string(), slug: z.string(), name: z.string(), logo: z.string().nullable()})}); 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 GEO scans * * GET /v1/projects/{projectId}/geo/scans */ export const getApiV1ProjectsProjectIdGeoScans = async (args: GetApiV1ProjectsProjectIdGeoScansArgs, options?: GetApiV1ProjectsProjectIdGeoScansOptions) => { const {baseUrl = "https://api.usenotra.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/geo/scans${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(listGeoScansResponse, await res.json()) };