import {z} from 'zod' export type GetApiUsersArgs = {userId?: string | undefined, email?: string | undefined, userName?: string | undefined, fullName?: string | undefined, role?: string | undefined, status?: ('active' | 'deactivated') | undefined, limit?: number | undefined, page?: number | undefined, sortCol?: string | undefined, sortOrder?: ('asc' | 'desc') | undefined}; export type GetApiUsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({userId: z.string(), email: z.string(), userName: z.string(), fullName: z.string().optional(), role: z.string().optional(), status: z.enum(["active", "deactivated"]).optional()}); export const getApiUsersResponse = z.object({criteria: z.object({}).optional(), metadata: z.object({totalCount: z.number().int().optional()}).optional(), results: z.array(user).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 }; /** * Get public account data directly from the database * * GET /users */ export const getApiUsers = async (args: GetApiUsersArgs, options?: GetApiUsersOptions) => { const {baseUrl = "https://catalog.archives.gov/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.userId != null) query.append("userId", String(args.userId)) if (args.email != null) query.append("email", String(args.email)) if (args.userName != null) query.append("userName", String(args.userName)) if (args.fullName != null) query.append("fullName", String(args.fullName)) if (args.role != null) query.append("role", String(args.role)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiUsersResponse, await res.json()) };