import {z} from 'zod' export type GetApiV2GroupsArgs = {page_size?: number | undefined, skip?: number | undefined, filter?: string | undefined, order_by?: string | undefined}; export type GetApiV2GroupsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const group = z.object({id: z.string().optional(), create_time: z.string().optional(), update_time: z.string().optional(), tenant_id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), source: z.string().optional()}); export const getApiV2GroupsResponse = z.object({users: z.array(group).optional(), 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 groups for a tenant * * GET /v2/groups */ export const getApiV2Groups = async (args: GetApiV2GroupsArgs, options?: GetApiV2GroupsOptions) => { 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/groups${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(getApiV2GroupsResponse, await res.json()) };