import {z} from 'zod' export type GetApiScimV2UsersArgs = {filter?: string | undefined, count?: number | undefined, startIndex?: number | undefined}; export type GetApiScimV2UsersOptions = {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 getApiScimV2UsersResponse = z.object({schemas: z.array(z.string()), Resources: z.array(sCIMUser), totalResults: z.number().int(), startIndex: z.number().int(), itemsPerPage: z.number().int()}); 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 }; /** * List All Users * * GET /scim/v2/Users */ export const getApiScimV2Users = async (args: GetApiScimV2UsersArgs, options?: GetApiScimV2UsersOptions) => { const {baseUrl = "https://api.byndid.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.count != null) query.append("count", String(args.count)) if (args.startIndex != null) query.append("startIndex", String(args.startIndex)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/scim/v2/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(getApiScimV2UsersResponse, await res.json()) };