import {z} from 'zod' export type GetApiV2UsersArgs = {cursor?: (string | null) | undefined, limit?: (number | null) | undefined, email?: (string | null) | undefined, remote_display_id?: (string | null) | undefined, name?: (string | null) | undefined, 'email[]'?: (Array | null) | undefined, 'status[]'?: (Array<'INVITED' | 'ACTIVE' | 'DISABLED' | 'PENDING_ACTIVATION' | 'INACTIVE' | 'ARCHIVED'> | null) | undefined, 'manager_id[]'?: (Array | null) | undefined, 'department_id[]'?: (Array | null) | undefined, 'location_id[]'?: (Array | null) | undefined, 'title_id[]'?: (Array | null) | undefined, 'cost_center_id[]'?: (Array | null) | undefined, 'legal_entity_id[]'?: (Array | null) | undefined, load_custom_fields?: (boolean | null) | undefined, 'custom_field[]'?: (Array | null) | undefined}; export type GetApiV2UsersOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const userStatus = z.enum(["INVITED", "ACTIVE", "DISABLED", "PENDING_ACTIVATION", "INACTIVE", "ARCHIVED"]); export const money = z.object({amount: z.number().int(), currency: z.string().nullable().optional()}); export const optionString = z.object({option_id: z.string().nullable().optional(), option_label: z.string().nullable().optional()}); export const customField = z.object({key: z.string(), value: z.union([z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("STRING"), data: z.string().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("DECIMAL"), data: z.number().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("BOOLEAN"), data: z.boolean().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("ENUM"), data: z.string().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("TIMESTAMP"), data: z.string().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("DATE"), data: z.string().nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("MONEY"), data: z.object({amount: z.number().int(), currency: z.string().nullable().optional()}).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("OPTION_STRING"), data: z.object({option_id: z.string().nullable().optional(), option_label: z.string().nullable().optional()}).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("STRING"), data: z.array(z.string()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("DECIMAL"), data: z.array(z.number()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("BOOLEAN"), data: z.array(z.boolean()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("ENUM"), data: z.array(z.string()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("TIMESTAMP"), data: z.array(z.string()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("DATE"), data: z.array(z.string()).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("MONEY"), data: z.array(money).nullable().optional()}), z.object({value_type: z.enum(["SINGLE_VALUE", "LIST"]), data_type: z.literal("OPTION_STRING"), data: z.array(optionString).nullable().optional()})])}); export const userResponse = z.object({id: z.string(), first_name: z.string(), last_name: z.string(), email: z.string(), status: userStatus.nullable().optional(), manager_id: z.string().nullable().optional(), department_id: z.string().nullable().optional(), location_id: z.string().nullable().optional(), title_id: z.string().nullable().optional(), cost_center_id: z.string().nullable().optional(), legal_entity_id: z.string().nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), remote_display_id: z.string().nullable().optional(), custom_fields: z.array(customField).nullable().optional()}); export const pageUserResponse = z.object({next_cursor: z.string().nullable().optional(), items: z.array(userResponse)}); 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 * * * GET /v2/users * * Also accepts auth: OAuth2 */ export const getApiV2Users = async (args: GetApiV2UsersArgs, options?: GetApiV2UsersOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.email != null) query.append("email", String(args.email)) if (args.remote_display_id != null) query.append("remote_display_id", String(args.remote_display_id)) if (args.name != null) query.append("name", String(args.name)) for (const item of args['email[]'] ?? []) query.append("email[]", String(item)) for (const item of args['status[]'] ?? []) query.append("status[]", String(item)) for (const item of args['manager_id[]'] ?? []) query.append("manager_id[]", String(item)) for (const item of args['department_id[]'] ?? []) query.append("department_id[]", String(item)) for (const item of args['location_id[]'] ?? []) query.append("location_id[]", String(item)) for (const item of args['title_id[]'] ?? []) query.append("title_id[]", String(item)) for (const item of args['cost_center_id[]'] ?? []) query.append("cost_center_id[]", String(item)) for (const item of args['legal_entity_id[]'] ?? []) query.append("legal_entity_id[]", String(item)) if (args.load_custom_fields != null) query.append("load_custom_fields", String(args.load_custom_fields)) for (const item of args['custom_field[]'] ?? []) query.append("custom_field[]", String(item)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) 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(pageUserResponse, await res.json()) };