import {z} from 'zod' export type GetApiUsersIdArgs = {id: string, attributes?: string | undefined, excludedAttributes?: string | undefined}; export type GetApiUsersIdOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const userResponseObject = z.object({meta: z.object({created: z.string().optional(), location: z.string().optional(), lastModified: z.string().optional(), resourceType: z.string().optional()}), schemas: z.array(z.string()).optional(), username: z.string().optional(), id: z.string().optional(), name: z.object({givenName: z.string().optional(), familyName: z.string().optional()}).optional(), emails: z.array(z.union([z.string(), z.object({})])).optional(), "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": z.object({manager: z.object({value: z.string().optional()}).optional()}).optional(), roles: z.array(z.object({type: z.string().optional(), value: z.string().optional()})).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 }; /** * Get User by ID * * GET /Users/{id} */ export const getApiUsersId = async (args: GetApiUsersIdArgs, options?: GetApiUsersIdOptions) => { const {baseUrl = "https://api.asgardeo.io/t//scim2", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/scim+json") if (args.attributes != null) query.append("attributes", String(args.attributes)) if (args.excludedAttributes != null) query.append("excludedAttributes", String(args.excludedAttributes)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/Users/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(userResponseObject, await res.json()) };