import {z} from 'zod' export type CreateApiApiV1TestCreateArgs = {body: {title: string, description?: (string | null) | undefined, sectionId?: (number | null) | undefined, new_section?: (string | null) | undefined, projectId: number, squadId?: (number | null) | undefined, new_squad?: (string | null) | undefined, preConditions?: (string | null) | undefined, steps?: (string | null) | undefined, expectedResult?: (string | null) | undefined, priorityId: number, typeId?: (number | null) | undefined, automationStatusId: number, testCoveredById?: (number | null) | undefined, platformId?: (number | null) | undefined, labelIds: Array, jiraTicket?: (string | null) | undefined, defects?: (string | null) | undefined, automationId?: (string | null) | undefined, additionalGroups?: (string | null) | undefined}}; export type CreateApiApiV1TestCreateOptions = {baseUrl?: string | undefined, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1TestCreateResponse = z.object({data: z.object({testId: z.number().int().optional(), testTitle: z.string().optional(), message: z.string().optional()}).optional(), status: z.number().int().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 }; /** * Create a test case * * POST /api/v1/test/create * * Also accepts auth: bearerAuth */ export const createApiApiV1TestCreate = async (args: CreateApiApiV1TestCreateArgs, options?: CreateApiApiV1TestCreateOptions) => { const {baseUrl = "http://localhost:3000", fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (cookieAuth !== undefined) cookies.push("user_session=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/test/create${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV1TestCreateResponse, await res.json()) };