import {z} from 'zod' export type PatchScimUserRequest = {schemas: Array, Operations: Array<{op: 'add' | 'remove' | 'replace'}>}; export type PatchApiScimUsersIdArgs = {id: string, body: PatchScimUserRequest}; export type PatchApiScimUsersIdOptions = {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 }; /** * Patch SCIM User * * PATCH /scim/Users/{id} */ export const patchApiScimUsersId = async (args: PatchApiScimUsersIdArgs, options?: PatchApiScimUsersIdOptions) => { 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: "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(scimUser, await res.json()) };