import {z} from 'zod' export type LogicalOperators = 'or' | 'and'; export type OAuthDeviceStatus = 'pending' | 'verified' | 'active' | 'locked'; export type GetApiApiV1DevicesArgs = {hydrate?: boolean | undefined, sort_by?: string | undefined, logical_operator?: LogicalOperators | undefined, page?: number | undefined, size?: number | undefined, id?: (string | string | null) | undefined, created?: (string | string | null) | undefined, updated?: (string | string | null) | undefined, scope_user?: (string | null) | undefined, expires?: (string | string | null) | undefined, client_id?: (string | string | null) | undefined, status?: (OAuthDeviceStatus | string | null) | undefined, trusted_device?: (boolean | string | null) | undefined, failed_auth_attempts?: (number | string | null) | undefined, last_login?: (string | string | null) | undefined}; export type GetApiApiV1DevicesOptions = {baseUrl: string, CookieOAuth2TokenBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userResponseBody = z.object({created: z.string(), updated: z.string(), active: z.boolean().optional(), activation_token: z.string().max(255).nullable().optional(), full_name: z.string().max(255).optional(), email_opted_in: z.boolean().nullable().optional(), is_service_account: z.boolean(), is_admin: z.boolean()}); export const userResponseMetadata = z.object({email: z.string().max(255).nullable().optional(), external_user_id: z.string().nullable().optional(), user_metadata: z.object({}).optional()}); export const userResponseResources = z.record(z.string(), z.unknown()); export const userResponse = z.object({body: userResponseBody.nullable().optional(), metadata: userResponseMetadata.nullable().optional(), resources: userResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional(), name: z.string().max(255)}); export const oAuthDeviceStatus = z.enum(["pending", "verified", "active", "locked"]); export const oAuthDeviceResponseBody = z.object({created: z.string(), updated: z.string(), user: userResponse.nullable().optional(), client_id: z.string(), expires: z.string().nullable().optional(), trusted_device: z.boolean(), status: oAuthDeviceStatus, os: z.string().nullable().optional(), ip_address: z.string().nullable().optional(), hostname: z.string().nullable().optional()}); export const oAuthDeviceResponseMetadata = z.object({python_version: z.string().nullable().optional(), zenml_version: z.string().nullable().optional(), city: z.string().nullable().optional(), region: z.string().nullable().optional(), country: z.string().nullable().optional(), failed_auth_attempts: z.number().int(), last_login: z.string().nullable()}); export const oAuthDeviceResponseResources = z.record(z.string(), z.unknown()); export const oAuthDeviceResponse = z.object({body: oAuthDeviceResponseBody.nullable().optional(), metadata: oAuthDeviceResponseMetadata.nullable().optional(), resources: oAuthDeviceResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional()}); export const pageOAuthDeviceResponse = z.object({index: z.number().int().gt(0), max_size: z.number().int().gt(0), total_pages: z.number().int().gte(0), total: z.number().int().gte(0), items: z.array(oAuthDeviceResponse)}); 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 Authorized Devices * * GET /api/v1/devices */ export const getApiApiV1Devices = async (args: GetApiApiV1DevicesArgs, options: GetApiApiV1DevicesOptions) => { const {baseUrl, fetch: fetchFn = fetch, CookieOAuth2TokenBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hydrate != null) query.append("hydrate", String(args.hydrate)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.logical_operator != null) query.append("logical_operator", String(args.logical_operator)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.id != null) query.append("id", String(args.id)) if (args.created != null) query.append("created", String(args.created)) if (args.updated != null) query.append("updated", String(args.updated)) if (args.scope_user != null) query.append("scope_user", String(args.scope_user)) if (args.expires != null) query.append("expires", String(args.expires)) if (args.client_id != null) query.append("client_id", String(args.client_id)) if (args.status != null) query.append("status", String(args.status)) if (args.trusted_device != null) query.append("trusted_device", String(args.trusted_device)) if (args.failed_auth_attempts != null) query.append("failed_auth_attempts", String(args.failed_auth_attempts)) if (args.last_login != null) query.append("last_login", String(args.last_login)) if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/devices${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(pageOAuthDeviceResponse, await res.json()) };