import {z} from 'zod' export type ScimUserAddress = {country?: string | undefined, locality?: string | undefined, postalCode?: string | undefined, region?: string | undefined, streetAddress?: string | undefined, type: 'work'}; export type ScimEmail = {type: 'work', value: string}; export type ScimName = {familyName: string, givenName: string}; export type ScimPhoneNumber = {type: 'work' | 'mobile' | 'other', value: string}; export type ScimPhoto = {type: 'photo', value: string}; export type ScimEnterpriseUser = {department?: string | undefined}; export type ScimUser = {active?: boolean | undefined, addresses?: Array | undefined, emails: Array, externalId?: string | undefined, id?: string | undefined, name: ScimName, phoneNumbers?: Array | undefined, photos?: Array | undefined, schemas: Array<'urn:ietf:params:scim:schemas:core:2.0:User'>, title?: string | undefined, 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'?: ScimEnterpriseUser | undefined, userName: string}; export type UpdateApiScimV2UsersScimUserIdArgs = {scimUserId: string, body: ScimUser}; export type UpdateApiScimV2UsersScimUserIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const scimUserAddress = z.object({country: z.string().optional(), locality: z.string().optional(), postalCode: z.string().optional(), region: z.string().optional(), streetAddress: z.string().optional(), type: z.literal("work")}); export const scimEmail = z.object({type: z.literal("work"), value: z.string()}); export const scimName = z.object({familyName: z.string(), givenName: z.string()}); export const scimPhoneNumber = z.object({type: z.enum(["work", "mobile", "other"]), value: z.string()}); export const scimPhoto = z.object({type: z.literal("photo"), value: z.string()}); export const scimEnterpriseUser = z.object({department: z.string().optional()}); export const scimMeta = z.object({created: z.string().optional(), lastModified: z.string().optional(), location: z.string().optional(), resourceType: z.enum(["User", "Group", "ResourceType", "Schema"]).optional()}); export const scimUserResponse = z.object({active: z.boolean().optional(), addresses: z.array(scimUserAddress).optional(), emails: z.array(scimEmail), externalId: z.string().optional(), id: z.string().optional(), name: scimName, phoneNumbers: z.array(scimPhoneNumber).optional(), photos: z.array(scimPhoto).optional(), schemas: z.array(z.literal("urn:ietf:params:scim:schemas:core:2.0:User")), title: z.string().optional(), "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": scimEnterpriseUser.optional(), userName: z.string(), meta: scimMeta.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/Replace User * * PUT /scim/v2/Users/{scimUserId} */ export const updateApiScimV2UsersScimUserId = async (args: UpdateApiScimV2UsersScimUserIdArgs, options?: UpdateApiScimV2UsersScimUserIdOptions) => { const {baseUrl = "https://platform.ringcentral.com", 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(/\/+$/, '')}/scim/v2/Users/${encodeURIComponent(String(args.scimUserId))}${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(scimUserResponse, await res.json()) };