import {z} from 'zod' export type HealthCheckCreate = {data: {type: 'healthCheck', attributes: {checkType: 0 | 3200, hashtypeId: number, crackerBinaryId: number}}}; export type CreateApiApiV2UiHealthchecksArgs = {body: HealthCheckCreate}; export type CreateApiApiV2UiHealthchecksOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const healthCheckPostPatchResponse = 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("healthCheck"), attributes: z.object({time: z.number().int(), status: z.union([z.literal(-1), z.literal(0), z.literal(1)]), checkType: z.union([z.literal(0), z.literal(3200)]), hashtypeId: z.number().int(), crackerBinaryId: z.number().int(), expectedCracks: z.number().int(), attackCmd: z.string()}), links: z.object({self: z.string()}), relationships: z.object({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()}), hashType: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("hashType"), id: z.number().int()}).nullable().optional()}), healthCheckAgents: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("healthCheckAgent"), 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("hashType"), attributes: z.object({description: z.string(), isSalted: z.boolean(), isSlowHash: z.boolean()})}), z.object({id: z.number().int(), type: z.literal("healthCheckAgent"), attributes: z.object({healthCheckId: z.number().int(), agentId: z.number().int(), status: z.union([z.literal(-1), z.literal(0), z.literal(1)]), cracked: z.number().int(), numGpus: z.number().int(), start: z.number().int(), end: z.number().int(), errors: 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 }; /** * Create HealthChecks * * POST /api/v2/ui/healthchecks */ export const createApiApiV2UiHealthchecks = async (args: CreateApiApiV2UiHealthchecksArgs, options: CreateApiApiV2UiHealthchecksOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/healthchecks${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(healthCheckPostPatchResponse, await res.json()) };