import {z} from 'zod' export type GetApiV1UsersArgs = {'search.user_id'?: Array | undefined, 'search.agent_id'?: Array | undefined, 'search.customer_hierarchy_id'?: Array | undefined, 'search.login'?: Array | undefined, 'search.first_name'?: Array | undefined, 'search.last_name'?: Array | undefined, 'search.email'?: Array | undefined, 'search.active'?: boolean | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1UsersOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsRole = z.object({role_id: z.number().int().optional(), description: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppModelsUser = z.object({user_id: z.number().int().optional(), first_name: z.string().max(255).optional(), last_name: z.string().max(255).optional(), login_name: z.string().max(100).optional(), email: z.string().max(100).optional(), created_by: z.number().int().optional(), created_date: z.string().optional(), agent_id: z.number().int().optional(), customer_hierarchy_id: z.number().int().optional(), active: z.boolean().optional(), roles: z.array(revAppModelsRole).optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsUser = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsUser).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 }; /** * Search users * * GET /v1/Users */ export const getApiV1Users = async (args: GetApiV1UsersArgs, options?: GetApiV1UsersOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.user_id'] ?? []) query.append("search.user_id", String(item)) for (const item of args['search.agent_id'] ?? []) query.append("search.agent_id", String(item)) for (const item of args['search.customer_hierarchy_id'] ?? []) query.append("search.customer_hierarchy_id", String(item)) for (const item of args['search.login'] ?? []) query.append("search.login", String(item)) for (const item of args['search.first_name'] ?? []) query.append("search.first_name", String(item)) for (const item of args['search.last_name'] ?? []) query.append("search.last_name", String(item)) for (const item of args['search.email'] ?? []) query.append("search.email", String(item)) if (args['search.active'] != null) query.append("search.active", String(args['search.active'])) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsUser, await res.json()) };