import {z} from 'zod' export type GetApiV1ProjectsProjectIdGeoTrafficImportsArgs = {projectId: string}; export type GetApiV1ProjectsProjectIdGeoTrafficImportsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geoAnalyticsImportsResponse = z.object({imports: z.array(z.object({id: z.string(), projectId: z.string(), provider: z.enum(["plausible", "plausible_csv", "vercel", "cloudflare", "cloudflare_rum", "rybbit", "databuddy"]), kind: z.enum(["web", "geo"]), sourceLabel: z.string(), host: z.string(), status: z.enum(["pending", "running", "waiting_rate_limit", "completed", "failed", "deleting", "deleted"]), startDay: z.string().nullable(), endDay: z.string().nullable(), cutoffDay: z.string().nullable(), chunksTotal: z.number().int(), chunksDone: z.number().int(), pageviewsImported: z.number().int(), rowsWritten: z.number().int(), capabilities: z.object({visitors: z.boolean(), visits: z.boolean(), bounces: z.boolean(), utm: z.boolean(), entryPages: z.boolean(), sampled: z.boolean()}), lastError: z.string().nullable(), retryAt: z.string().nullable(), createdAt: z.string(), completedAt: z.string().nullable()})), 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 history imports * * GET /v1/projects/{projectId}/geo/traffic/imports */ export const getApiV1ProjectsProjectIdGeoTrafficImports = async (args: GetApiV1ProjectsProjectIdGeoTrafficImportsArgs, options?: GetApiV1ProjectsProjectIdGeoTrafficImportsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/geo/traffic/imports${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(geoAnalyticsImportsResponse, await res.json()) };