import {z} from 'zod' export type CreateGroupDto = {color?: string | undefined, description?: string | undefined, metadata?: string | undefined, name: string}; export type CreateApiResourcesGroupsV1Args = {body: CreateGroupDto}; export type CreateApiResourcesGroupsV1Options = {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 }; /** * Create group * * POST /resources/groups/v1 */ export const createApiResourcesGroupsV1 = async (args: CreateApiResourcesGroupsV1Args, options?: CreateApiResourcesGroupsV1Options) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/groups/v1${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) 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()) };