import {z} from 'zod' export type SCIMUser = {schemas: Array, id?: string | undefined, externalId?: string | undefined, userName?: string | undefined, displayName?: string | undefined, active?: boolean | undefined, emails?: (Array<{primary?: boolean | undefined, value?: string | undefined, type?: ('work' | 'home' | 'other') | undefined}>) | undefined, name?: ({givenName?: string | undefined, familyName?: string | undefined}) | undefined, 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'?: ({employeeNumber?: string | undefined}) | undefined, groups?: (Array<{value?: string | undefined, $ref?: string | undefined, type?: string | undefined}>) | undefined, meta?: {resourceType: string, created: string, lastModified: string, location: string, version: string} | undefined}; export type UpdateApiScimV2UsersUserIdArgs = {user_id: string, body: {user: SCIMUser}}; export type UpdateApiScimV2UsersUserIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sCIMUser = z.object({schemas: z.array(z.string()), id: z.string().min(1).optional(), externalId: z.string().optional(), userName: z.string().min(1).optional(), displayName: z.string().min(1).optional(), active: z.boolean().optional(), emails: z.array(z.object({primary: z.boolean().optional(), value: z.string().optional(), type: z.enum(["work", "home", "other"]).optional()})).optional(), name: z.object({givenName: z.string().optional(), familyName: z.string().optional()}).optional(), "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": z.object({employeeNumber: z.string().optional()}).optional(), groups: z.array(z.object({value: z.string().optional(), $ref: z.string().optional(), type: z.string().optional()})).optional(), meta: z.object({resourceType: z.string(), created: z.string(), lastModified: z.string(), location: z.string(), version: 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 }; /** * Replace a User * * PUT /scim/v2/Users/{user_id} */ export const updateApiScimV2UsersUserId = async (args: UpdateApiScimV2UsersUserIdArgs, options?: UpdateApiScimV2UsersUserIdOptions) => { const {baseUrl = "https://api.byndid.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/scim/v2/Users/${encodeURIComponent(String(args.user_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()) };