import {z} from 'zod' export type GetApiResourcesRolesV2Args = {_limit?: number | undefined, _sortBy: 'key' | 'name' | 'description' | 'isDefault' | 'firstUserRole' | 'level' | 'updatedAt' | 'createdAt' | 'permissions' | 'userTenants' | 'groups', _levels?: Array | undefined, _tenantIds?: Array | undefined, _offset?: number | undefined, _order?: ('ASC' | 'DESC') | undefined, _filter?: string | undefined, 'frontegg-tenant-id'?: string | undefined}; export type GetApiResourcesRolesV2Options = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const roleResponse = 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(), permissions: z.array(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 roles v2 * * GET /resources/roles/v2 */ export const getApiResourcesRolesV2 = async (args: GetApiResourcesRolesV2Args, options?: GetApiResourcesRolesV2Options) => { 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._limit != null) query.append("_limit", String(args._limit)) if (args._sortBy != null) query.append("_sortBy", String(args._sortBy)) for (const item of args._levels ?? []) query.append("_levels", String(item)) for (const item of args._tenantIds ?? []) query.append("_tenantIds", String(item)) if (args._offset != null) query.append("_offset", String(args._offset)) if (args._order != null) query.append("_order", String(args._order)) if (args._filter != null) query.append("_filter", String(args._filter)) if (args['frontegg-tenant-id'] != null) headers.set("frontegg-tenant-id", String(args['frontegg-tenant-id'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/roles/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(roleResponse, await res.json()) };