import {z} from 'zod' export type GetApiV2UsersArgs = {page_size?: number | undefined, skip?: number | undefined, filter?: string | undefined, order_by?: string | undefined}; export type GetApiV2UsersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.string().optional(), create_time: z.string().optional(), update_time: z.string().optional(), last_auth_time: z.string().optional(), tenant_id: z.string().optional(), external_id: z.string().optional(), email_address: z.string().optional(), username: z.string().optional(), display_name: z.string().optional(), state: z.enum(["ACTIVE", "SUSPENDED", "DELETED"]).optional(), source: z.string().optional(), has_active_passkey: z.boolean().optional()}); export const getApiV2UsersResponse = z.object({users: z.array(user), total_size: z.number().int()}); 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 users for a tenant * * GET /v2/users */ export const getApiV2Users = async (args: GetApiV2UsersArgs, options?: GetApiV2UsersOptions) => { const {baseUrl = "https://api.byndid.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) 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(getApiV2UsersResponse, await res.json()) };