import {z} from 'zod' export type GetApiApiPlansArgs = {projectId: string, search?: string | undefined, page?: number | undefined, pageSize?: number | undefined, sort?: ('title' | 'createdAt' | 'updatedAt') | undefined, order?: ('asc' | 'desc') | undefined, locale?: ('en' | 'ja' | 'ko' | 'und') | undefined}; export type GetApiApiPlansOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const planListItem = z.object({id: z.string(), projectId: z.string(), title: z.string(), description: z.string().nullable().optional(), apiEnabled: z.boolean(), caseCount: z.number().int(), hasMobileCases: z.boolean(), createdAt: z.string(), updatedAt: z.string(), createdBy: z.object({id: z.string().optional(), name: z.string().nullable().optional(), email: z.string().optional()}).nullable().optional()}); export const getApiApiPlansResponse = z.object({items: z.array(planListItem), 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 plans * * GET /api/plans */ export const getApiApiPlans = async (args: GetApiApiPlansArgs, options: GetApiApiPlansOptions) => { 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.search != null) query.append("search", String(args.search)) 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/plans${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(getApiApiPlansResponse, await res.json()) };