import {z} from 'zod' export type UpdateScimUserRequest = {id?: string | undefined, schemas?: Array | undefined, userName?: string | undefined, emails?: (Array<{primary?: boolean | undefined, value?: string | undefined, type?: string | undefined}>) | undefined, phoneNumbers?: (Array<{value?: string | undefined, type?: string | undefined}>) | undefined, name?: ({familyName?: string | undefined, givenName?: string | undefined}) | undefined, active?: boolean | undefined, password?: string | undefined, groups?: (Array<{display?: string | undefined, value?: string | undefined}>) | undefined, 'urn:okta:onprem_app:1.0:user:custom'?: ({roleUUID?: string | undefined}) | undefined}; export type UpdateApiScimUsersIdArgs = {id: string, body: UpdateScimUserRequest}; export type UpdateApiScimUsersIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const scimUser = z.object({id: z.string(), externalId: z.string(), meta: z.object({created: z.string().optional(), lastModified: z.string().optional(), version: z.string().optional()}), schemas: z.array(z.string()), userName: z.string(), displayName: z.string().optional(), preferredLanguage: z.string().optional(), locale: z.string().optional(), timezone: z.string().optional(), active: z.boolean(), emails: z.array(z.object({value: z.string().optional(), type: z.string().optional(), primary: z.boolean().optional()})), phoneNumbers: z.array(z.object({value: z.string().optional(), type: z.string().optional()})), groups: z.array(z.object({display: z.string().optional(), value: z.string().optional()})).optional(), name: z.object({familyName: z.string().optional(), givenName: z.string().optional()}), "urn:okta:onprem_app:1.0:user:custom": z.object({roleUUID: 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 SCIM User * * PUT /scim/Users/{id} */ export const updateApiScimUsersId = async (args: UpdateApiScimUsersIdArgs, options?: UpdateApiScimUsersIdOptions) => { const {baseUrl = "https://api.attention.tech/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/scim/Users/${encodeURIComponent(String(args.id))}${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(scimUser, await res.json()) };