import {z} from 'zod' export type GetApiApiV2UiSpeedsIdTaskArgs = {id: number}; export type GetApiApiV2UiSpeedsIdTaskOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const taskResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string()}), data: z.object({id: z.number().int(), type: z.literal("task"), attributes: z.object({taskName: z.string(), attackCmd: z.string(), chunkTime: z.number().int(), statusTimer: z.number().int(), keyspace: z.number().int(), keyspaceProgress: z.number().int(), priority: z.number().int(), maxAgents: z.number().int(), color: z.string().nullable(), isSmall: z.boolean(), isCpuTask: z.boolean(), useNewBench: z.boolean(), skipKeyspace: z.number().int(), crackerBinaryId: z.number().int(), crackerBinaryTypeId: z.number().int().nullable(), taskWrapperId: z.number().int(), isArchived: z.boolean(), notes: z.string(), staticChunks: z.number().int(), chunkSize: z.number().int(), forcePipe: z.boolean(), preprocessorId: z.number().int(), preprocessorCommand: z.string(), totalAssignedAgents: z.number().int().optional(), dispatched: z.string().optional(), searched: z.string().optional(), status: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]).optional(), totalNumberOfChunks: z.number().int().optional(), currentSpeed: z.number().int().optional(), estimatedTime: z.number().int().optional(), cprogress: z.number().int().optional(), timeSpent: z.number().int().optional(), cracked: z.number().int().optional()}), links: z.object({self: z.string()}), relationships: z.object({assignedAgents: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("agent"), id: z.number().int()})).optional()}), crackerBinary: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("crackerBinary"), id: z.number().int()}).nullable().optional()}), crackerBinaryType: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("crackerBinaryType"), id: z.number().int()}).nullable().optional()}), files: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("file"), id: z.number().int()})).optional()}), hashlist: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("hashlist"), id: z.number().int()}).nullable().optional()}), speeds: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("speed"), id: z.number().int()})).optional()})})}), included: z.array(z.discriminatedUnion("type", [z.object({id: z.number().int(), type: z.literal("crackerBinary"), attributes: z.object({crackerBinaryTypeId: z.number().int(), version: z.string(), downloadUrl: z.string().nullable(), binaryName: z.string(), filename: z.string().nullable(), accessGroupId: z.number().int()})}), z.object({id: z.number().int(), type: z.literal("crackerBinaryType"), attributes: z.object({typeName: z.string(), isChunkingAvailable: z.boolean().nullable()})}), z.object({id: z.number().int(), type: z.literal("hashlist"), attributes: z.object({name: z.string(), format: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]), hashTypeId: z.number().int(), hashCount: z.number().int(), separator: z.string().nullable(), cracked: z.number().int(), isSecret: z.boolean(), isHexSalt: z.boolean(), isSalted: z.boolean(), accessGroupId: z.number().int(), notes: z.string(), useBrain: z.boolean(), brainFeatures: z.number().int(), isArchived: z.boolean()})}), z.object({id: z.number().int(), type: z.literal("agent"), attributes: z.object({agentName: z.string(), uid: z.string(), os: z.union([z.literal(0), z.literal(1), z.literal(2)]), devices: z.string(), cmdPars: z.string(), ignoreErrors: z.union([z.literal(0), z.literal(1), z.literal(2)]), isActive: z.boolean(), isTrusted: z.boolean(), token: z.string(), lastAct: z.string(), lastTime: z.number().int(), lastIp: z.string(), userId: z.number().int().nullable(), cpuOnly: z.boolean(), clientSignature: z.string()})}), z.object({id: z.number().int(), type: z.literal("file"), attributes: z.object({filename: z.string(), size: z.number().int(), isSecret: z.boolean(), fileType: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(100)]), accessGroupId: z.number().int(), lineCount: z.number().int()})}), z.object({id: z.number().int(), type: z.literal("speed"), attributes: z.object({agentId: z.number().int(), taskId: z.number().int(), speed: z.number().int(), time: 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 }; /** * Get Speeds * * GET /api/v2/ui/speeds/{id}/task */ export const getApiApiV2UiSpeedsIdTask = async (args: GetApiApiV2UiSpeedsIdTaskArgs, options: GetApiApiV2UiSpeedsIdTaskOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/speeds/${encodeURIComponent(String(args.id))}/task${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(taskResponse, await res.json()) };