import {z} from 'zod' export type PatchApiV2UserEndUserIdDeviceAccessArgs = {id: number, body: {addDeviceIds?: Array | undefined, removeDeviceIds?: Array | undefined}}; export type PatchApiV2UserEndUserIdDeviceAccessOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiV2UserEndUserIdDeviceAccessResponse = z.object({id: z.number().int().optional(), uid: z.string().optional(), enabled: z.boolean().optional(), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), organizationId: z.number().int().optional(), fullPortalAccess: z.boolean().optional(), mustChangePw: z.boolean().optional(), mfaConfigured: z.boolean().optional(), scimUser: z.boolean().optional(), authType: z.string().optional(), invitationStatus: z.enum(["REGISTERED", "PENDING", "EXPIRED"]).optional(), assignedDeviceIds: z.array(z.number().int()).optional(), accessibleDeviceIds: z.array(z.number().int()).optional(), roles: z.array(z.string()).optional(), managerUid: 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 }; /** * Update device access for end user * * PATCH /v2/user/end-user/{id}/device-access * * Also accepts auth: sessionKey */ export const patchApiV2UserEndUserIdDeviceAccess = async (args: PatchApiV2UserEndUserIdDeviceAccessArgs, options: PatchApiV2UserEndUserIdDeviceAccessOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/user/end-user/${encodeURIComponent(String(args.id))}/device-access${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV2UserEndUserIdDeviceAccessResponse, await res.json()) };