import {z} from 'zod' export type GetApiApiCasesArgs = {projectId: string, type?: ('web' | 'mobile' | 'desktop' | 'multi_device') | undefined, search?: string | undefined, directoryId?: string | undefined, tag?: Array | undefined, temporary?: ('true' | 'false') | undefined, page?: number | undefined, pageSize?: number | undefined, sort?: ('title' | 'createdAt' | 'updatedAt' | 'lastRunAt' | 'lastStatus') | undefined, order?: ('asc' | 'desc') | undefined, locale?: ('en' | 'ja' | 'ko' | 'und') | undefined}; export type GetApiApiCasesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const caseTags = z.array(z.string().min(1).max(64)); export const caseListItem = z.object({id: z.string(), projectId: z.string(), type: z.enum(["web", "mobile", "desktop", "multi_device"]), title: z.string().nullable().optional(), description: z.string().nullable().optional(), startUrl: z.string().nullable().optional(), lastRunAt: z.string().nullable().optional(), lastStatus: z.string().nullable().optional(), createdAt: z.string(), updatedAt: z.string(), tags: caseTags}); export const caseList = z.object({items: z.array(caseListItem), totalCount: z.number().int(), page: z.number().int(), pageSize: z.number().int(), totalPages: z.number().int()}); 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 test cases * * GET /api/cases */ export const getApiApiCases = async (args: GetApiApiCasesArgs, options: GetApiApiCasesOptions) => { 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.type != null) query.append("type", String(args.type)) if (args.search != null) query.append("search", String(args.search)) if (args.directoryId != null) query.append("directoryId", String(args.directoryId)) for (const item of args.tag ?? []) query.append("tag", String(item)) if (args.temporary != null) query.append("temporary", String(args.temporary)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) 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/cases${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(caseList, await res.json()) };