import {z} from 'zod' export type GetApiApiV2UiSupertasksArgs = {'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined, filter?: {[key: string]: string} | undefined, include?: Array<'pretasks'> | undefined, aggregate?: {[key: string]: string} | undefined}; export type GetApiApiV2UiSupertasksOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const supertaskListResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string(), first: z.string(), last: z.string().nullable(), next: z.string().nullable(), prev: z.string().nullable()}), meta: z.object({page: z.object({total_elements: z.number().int()})}), data: z.array(z.object({id: z.number().int(), type: z.literal("supertask"), attributes: z.object({supertaskName: z.string(), crackerBinaryTypeId: z.number().int(), amountPretasks: z.number().int().optional()}), links: z.object({self: z.string()}), relationships: z.object({pretasks: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("preTask"), id: z.number().int()})).optional()})})})), included: z.array(z.object({id: z.number().int(), type: z.literal("preTask"), attributes: z.object({taskName: z.string(), attackCmd: z.string(), chunkTime: z.number().int(), statusTimer: z.number().int(), color: z.string(), isSmall: z.boolean(), isCpuTask: z.boolean(), useNewBench: z.boolean(), priority: z.number().int(), maxAgents: z.number().int(), isMaskImport: z.boolean(), crackerBinaryTypeId: 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 }; /** * List Supertasks * * GET /api/v2/ui/supertasks */ export const getApiApiV2UiSupertasks = async (args: GetApiApiV2UiSupertasksArgs, options: GetApiApiV2UiSupertasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.include != null) query.append("include", args.include.join(',')) if (args.aggregate != null) query.append("aggregate", JSON.stringify(args.aggregate)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/supertasks${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(supertaskListResponse, await res.json()) };