import {z} from 'zod' export type GetApiResourcesGroupsV2Args = {_groupsRelations?: ('roles' | 'users' | 'rolesAndUsers') | undefined, _limit?: number | undefined, _offset?: number | undefined, _sortBy?: ('id' | 'name' | 'createdAt' | 'updatedAt') | undefined, _order?: ('ASC' | 'DESC') | undefined}; export type GetApiResourcesGroupsV2Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const roleDto = z.object({id: z.string(), vendorId: z.string(), tenantId: z.string(), key: z.string(), name: z.string(), description: z.string(), isDefault: z.boolean(), firstUserRole: z.boolean(), level: z.number(), createdAt: z.string(), updatedAt: z.string()}); export const groupUserDto = z.object({id: z.string(), email: z.string(), name: z.string(), profilePictureUrl: z.string(), createdAt: z.string(), activatedForTenant: z.boolean()}); export const groupResponse = z.object({id: z.string(), name: z.string(), color: z.string(), description: z.string(), metadata: z.string(), roles: z.array(roleDto), users: z.array(groupUserDto), managedBy: z.enum(["frontegg", "scim2"]), createdAt: z.string(), updatedAt: z.string()}); 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 all groups paginated * * GET /resources/groups/v2 */ export const getApiResourcesGroupsV2 = async (args: GetApiResourcesGroupsV2Args, options?: GetApiResourcesGroupsV2Options) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args._groupsRelations != null) query.append("_groupsRelations", String(args._groupsRelations)) if (args._limit != null) query.append("_limit", String(args._limit)) if (args._offset != null) query.append("_offset", String(args._offset)) if (args._sortBy != null) query.append("_sortBy", String(args._sortBy)) if (args._order != null) query.append("_order", String(args._order)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/groups/v2${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(groupResponse, await res.json()) };