import {z} from 'zod' export type GlobalPermissionGroupPatch = {data: {type: 'globalPermissionGroup', attributes: {name?: string | undefined, permissions?: {[key: string]: boolean} | undefined}}}; export type PatchApiApiV2UiGlobalpermissiongroupsIdArgs = {id: number, body: GlobalPermissionGroupPatch}; export type PatchApiApiV2UiGlobalpermissiongroupsIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const globalPermissionGroupPostPatchResponse = 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("globalPermissionGroup"), attributes: z.object({name: z.string(), permissions: z.record(z.string(), z.boolean())}), links: z.object({self: z.string()}), relationships: z.object({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.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()})})).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 }; /** * Update GlobalPermissionGroups * * PATCH /api/v2/ui/globalpermissiongroups/{id} */ export const patchApiApiV2UiGlobalpermissiongroupsId = async (args: PatchApiApiV2UiGlobalpermissiongroupsIdArgs, options: PatchApiApiV2UiGlobalpermissiongroupsIdOptions) => { 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/globalpermissiongroups/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(globalPermissionGroupPostPatchResponse, await res.json()) };