import {z} from 'zod' export type DepartmentPageSelection = Record; export type ViewType = 0 | 1; export type GetApiV1UsersArgs = {Id?: number | undefined, DisplayName?: string | undefined, Nickname?: string | undefined, Email?: string | undefined, PhoneNumber?: string | undefined, Department?: string | undefined, GroupIds?: string | undefined, IncludeSettings?: boolean | undefined, Q?: string | undefined, Fields?: string | undefined, State?: number | undefined, DepartmentId?: number | undefined, DepartmentScope?: DepartmentPageSelection | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1UsersOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const mfaMethod = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const userSettings = z.object({userId: z.number().int().optional(), defaultTimezoneId: z.number().int().optional(), defaultLanguage: z.string().nullable().optional(), defaultCurrency: z.string().nullable().optional(), allowSuperadminAccess: z.number().int().optional(), shortcutsEnabled: z.number().int().optional(), nickname: z.string().nullable().optional(), signature: z.string().nullable().optional(), defaultCallExtension: z.string().nullable().optional(), mfaEnabled: z.boolean().optional(), mfaMethod: mfaMethod.optional(), previewSettings: z.string().nullable().optional()}); export const userResponseModel = z.object({id: z.number().int().optional(), email: z.string().nullable().optional(), displayName: z.string().nullable().optional(), nickname: z.string().nullable().optional(), photo: z.string().nullable().optional(), color: z.string().nullable().optional(), loginType: z.literal("AA==").optional(), active: z.number().int().optional(), ipAddress: z.string().nullable().optional(), gsmNumber: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), status: z.string().optional(), department: z.string().nullable().optional(), departmentIds: z.string().nullable().optional(), isAdmin: z.boolean().optional(), profileId: z.number().int().nullable().optional(), profileName: z.string().nullable().optional(), lastActivityDate: z.string().optional(), groupIds: z.string().nullable().optional(), groups: z.string().nullable().optional(), autoTicketMaxAssign: z.number().int().optional(), autoDealMaxAssign: z.number().int().optional(), autoChatMaxAssign: z.number().int().optional(), userSettings: userSettings.optional()}); export const userResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(userResponseModel).nullable().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 }; /** * List all existing users * * GET /v1/users * * Also accepts auth: API Key */ export const getApiV1Users = async (args: GetApiV1UsersArgs, options: GetApiV1UsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.DisplayName != null) query.append("DisplayName", String(args.DisplayName)) if (args.Nickname != null) query.append("Nickname", String(args.Nickname)) if (args.Email != null) query.append("Email", String(args.Email)) if (args.PhoneNumber != null) query.append("PhoneNumber", String(args.PhoneNumber)) if (args.Department != null) query.append("Department", String(args.Department)) if (args.GroupIds != null) query.append("GroupIds", String(args.GroupIds)) if (args.IncludeSettings != null) query.append("IncludeSettings", String(args.IncludeSettings)) if (args.Q != null) query.append("Q", String(args.Q)) if (args.Fields != null) query.append("Fields", String(args.Fields)) if (args.State != null) query.append("State", String(args.State)) if (args.DepartmentId != null) query.append("DepartmentId", String(args.DepartmentId)) if (args.DepartmentScope != null) query.append("DepartmentScope", JSON.stringify(args.DepartmentScope)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) 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(userResponseModelQueryResult, await res.json()) };