import {z} from 'zod' export type OAuthDeviceVerificationRequest = {user_code: string, trusted_device?: boolean | undefined}; export type UpdateApiApiV1DevicesDeviceIdVerifyArgs = {device_id: string, body: OAuthDeviceVerificationRequest}; export type UpdateApiApiV1DevicesDeviceIdVerifyOptions = {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 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 }; /** * Verify Authorized Device * * PUT /api/v1/devices/{device_id}/verify */ export const updateApiApiV1DevicesDeviceIdVerify = async (args: UpdateApiApiV1DevicesDeviceIdVerifyArgs, options: UpdateApiApiV1DevicesDeviceIdVerifyOptions) => { const {baseUrl, fetch: fetchFn = fetch, CookieOAuth2TokenBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/devices/${encodeURIComponent(String(args.device_id))}/verify${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(oAuthDeviceResponse, await res.json()) };