import {z} from 'zod' export type AccessGroupCreate = {data: {type: 'accessGroup', attributes: {groupName: string}}}; export type CreateApiApiV2UiAccessgroupsArgs = {body: AccessGroupCreate}; export type CreateApiApiV2UiAccessgroupsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accessGroupPostPatchResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string()}), data: z.object({id: z.number().int(), type: z.literal("accessGroup"), attributes: z.object({groupName: z.string()}), links: z.object({self: z.string()}), relationships: z.object({agentMembers: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("agent"), id: z.number().int()})).optional()}), userMembers: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("user"), id: z.number().int()})).optional()})})}), included: z.array(z.discriminatedUnion("type", [z.object({id: z.number().int(), type: z.literal("user"), attributes: z.object({name: z.string(), email: z.string().optional(), isValid: z.boolean().optional(), isComputedPassword: z.boolean().optional(), lastLoginDate: z.number().int().optional(), registeredSince: z.number().int().optional(), sessionLifetime: z.number().int().optional(), globalPermissionGroupId: z.number().int().optional()})}), z.object({id: z.number().int(), type: z.literal("agent"), attributes: z.object({agentName: z.string(), uid: z.string(), os: z.union([z.literal(0), z.literal(1), z.literal(2)]), devices: z.string(), cmdPars: z.string(), ignoreErrors: z.union([z.literal(0), z.literal(1), z.literal(2)]), isActive: z.boolean(), isTrusted: z.boolean(), token: z.string(), lastAct: z.string(), lastTime: z.number().int(), lastIp: z.string(), userId: z.number().int().nullable(), cpuOnly: z.boolean(), clientSignature: z.string()})})])).optional()}); 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 AccessGroups * * POST /api/v2/ui/accessgroups */ export const createApiApiV2UiAccessgroups = async (args: CreateApiApiV2UiAccessgroupsArgs, options: CreateApiApiV2UiAccessgroupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/accessgroups${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(accessGroupPostPatchResponse, await res.json()) };