import {z} from 'zod' export type GetApiV2UsersArgs = {email?: string | undefined, slack_user_id?: string | undefined, page_size?: number | undefined, after?: string | undefined}; export type GetApiV2UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationMetaResult = z.object({after: z.string().optional(), page_size: z.number().int()}); export const rBACRoleV2 = z.object({description: z.string().optional(), id: z.string(), name: z.string(), slug: z.string()}); export const userWithRolesV2 = z.object({base_role: rBACRoleV2, custom_roles: z.array(rBACRoleV2), email: z.string().optional(), id: z.string(), name: z.string(), role: z.enum(["viewer", "responder", "administrator", "owner", "unset"]), slack_user_id: z.string().optional()}); export const usersV2ListResponseBody = z.object({pagination_meta: paginationMetaResult, users: z.array(userWithRolesV2)}); 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 Users V2 * * GET /v2/users */ export const getApiV2Users = async (args: GetApiV2UsersArgs, options?: GetApiV2UsersOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.email != null) query.append("email", String(args.email)) if (args.slack_user_id != null) query.append("slack_user_id", String(args.slack_user_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.after != null) query.append("after", String(args.after)) const url = `${baseUrl.replace(/\/+$/, '')}/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(usersV2ListResponseBody, await res.json()) };