import {z} from 'zod' export type GetApiApiPlanRunsArgs = {projectId: string, planId?: string | undefined, status?: ('created' | 'running' | 'completed' | 'failed' | 'cancelled') | undefined, tag?: Array | undefined, limit?: number | undefined, cursor?: string | undefined, sort?: ('createdAt' | 'startedAt' | 'status') | undefined, order?: ('asc' | 'desc') | undefined, locale?: ('en' | 'ja' | 'ko' | 'und') | undefined}; export type GetApiApiPlanRunsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const tags = z.array(z.string().min(1).max(64)); export const projectPlanRunItem = z.object({id: z.string(), status: z.enum(["created", "running", "completed", "failed", "cancelled"]), triggerSource: z.enum(["manual", "scheduled", "api"]), executionMode: z.enum(["parallel", "sequential"]), deviceMode: z.enum(["pool", "matrix"]), totalCases: z.number().int(), completedCases: z.number().int(), passedCases: z.number().int(), failedCases: z.number().int(), startedAt: z.string().nullable().optional(), completedAt: z.string().nullable().optional(), createdAt: z.string(), tags: tags, planId: z.string(), plan: z.object({id: z.string(), title: z.string()})}); export const getApiApiPlanRunsResponse = z.object({items: z.array(projectPlanRunItem), hasMore: z.boolean().optional(), nextCursor: z.string().optional()}); 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 plan runs across a project * * GET /api/plan-runs */ export const getApiApiPlanRuns = async (args: GetApiApiPlanRunsArgs, options: GetApiApiPlanRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.planId != null) query.append("planId", String(args.planId)) if (args.status != null) query.append("status", String(args.status)) for (const item of args.tag ?? []) query.append("tag", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.locale != null) query.append("locale", String(args.locale)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/plan-runs${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(getApiApiPlanRunsResponse, await res.json()) };