import {z} from 'zod' export type GetApiUsersArgs = {offset?: number | undefined, limit?: number | undefined, countLimit?: number | undefined, userId?: Array | undefined, name?: string | undefined, title?: string | undefined, department?: string | undefined, userSource?: Array | undefined, nameSearchFields?: (Array<'USERNAME' | 'FIRSTNAME' | 'LASTNAME' | 'LASTNAME_FIRSTNAME' | 'FIRSTNAME_LASTNAME' | 'EMAIL'>) | undefined, groupId?: string | undefined, onlyLoggedIn?: boolean | undefined, includeDisabled?: boolean | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, sortField?: ('USERNAME' | 'FIRSTNAME' | 'LASTNAME' | 'LASTNAME_FIRSTNAME' | 'FIRSTNAME_LASTNAME' | 'EMAIL') | undefined}; export type GetApiUsersOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.string()}); export const userPagedResponse = z.object({total: z.number().int().optional(), offset: z.number().int().optional(), limit: z.number().int().optional(), results: z.array(user).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 }; /** * Returns users matching the given search criteria * * GET /users * * Also accepts auth: jwtAuth, oauth2, oauth2 */ export const getApiUsers = async (args: GetApiUsersArgs, options: GetApiUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.countLimit != null) query.append("countLimit", String(args.countLimit)) for (const item of args.userId ?? []) query.append("userId", String(item)) if (args.name != null) query.append("name", String(args.name)) if (args.title != null) query.append("title", String(args.title)) if (args.department != null) query.append("department", String(args.department)) for (const item of args.userSource ?? []) query.append("userSource", String(item)) for (const item of args.nameSearchFields ?? []) query.append("nameSearchFields", String(item)) if (args.groupId != null) query.append("groupId", String(args.groupId)) if (args.onlyLoggedIn != null) query.append("onlyLoggedIn", String(args.onlyLoggedIn)) if (args.includeDisabled != null) query.append("includeDisabled", String(args.includeDisabled)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/users${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(userPagedResponse, await res.json()) };