import {z} from 'zod' export type Dictionary = Dictionary; export type CreateApiExecutionsArgs = {async?: boolean | undefined, resourceType?: string | undefined, workspace?: string | undefined, body: Dictionary}; export type CreateApiExecutionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const listPlanNodeSnapshot = z.array(z.lazy(() => planNodeSnapshot)); export const counters: z.ZodType = z.lazy(() => counters); export const listString = z.array(z.string()); export const nodeType = z.enum(["AGGREGATOR", "STEP", "STEP_AGGREGATOR", "TEST_CASE", "VIRTUAL_STEP"]); export const dictionary: z.ZodType = z.lazy(() => dictionary); export const result = z.enum(["ERROR", "FAILED", "PASSED", "SKIPPED", "UNDEFINED"]); export const planNodeSnapshot: z.ZodType = z.object({children: listPlanNodeSnapshot.optional(), childrenResults: counters.optional(), dataTable: z.array(z.array(z.string())).optional(), description: listString.optional(), displayName: z.string().optional(), document: z.string().optional(), documentType: z.string().optional(), duration: z.number().int().optional(), errorMessage: z.string().optional(), errorTrace: z.string().optional(), executionID: z.string().optional(), finishInstant: z.string().optional(), id: z.string().optional(), keyword: z.string().optional(), language: z.string().optional(), name: z.string().optional(), nodeType: nodeType.optional(), properties: dictionary.optional(), result: result.optional(), snapshotInstant: z.string().optional(), source: z.string().optional(), startInstant: z.string().optional(), tags: listString.optional(), testCaseResults: counters.optional()}); export const wakamitiExecution = z.object({data: planNodeSnapshot.optional(), executionID: z.string().optional(), executionInstant: z.string().optional(), owner: 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 }; /** * Send either a resource or a set of resources to be executed as a Wakamiti test plan * * POST /executions */ export const createApiExecutions = async (args: CreateApiExecutionsArgs, options: CreateApiExecutionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json;charset=UTF-8") headers.set('accept', "application/json;charset=UTF-8") if (args.async != null) query.append("async", String(args.async)) if (args.resourceType != null) query.append("resourceType", String(args.resourceType)) if (args.workspace != null) query.append("workspace", String(args.workspace)) const url = `${baseUrl.replace(/\/+$/, '')}/executions${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(wakamitiExecution, await res.json()) };