import {z} from 'zod' export type GroupUpdate = {name: string, groups_users: Array<{id?: string | undefined, user_id?: string | undefined, is_admin?: boolean | undefined, delete?: boolean | undefined}>, secrets?: Array<{resource_id: string, user_id: string, data: string}> | undefined}; export type UpdateApiGroupsGroupIdDryRunJsonArgs = {groupId: string, body: GroupUpdate}; export type UpdateApiGroupsGroupIdDryRunJsonOptions = {baseUrl?: string | undefined, bearerHttpAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const header = z.object({id: z.string(), status: z.enum(["success", "error"]), servertime: z.number().int(), action: z.string(), message: z.string(), url: z.string(), code: z.number().int()}); export const groupUpdateDryRun = z.object({"dry-run": z.object({SecretsNeeded: z.array(z.object({Secret: z.object({resource_id: z.string(), user_id: z.string()})})), Secrets: z.array(z.object({Secret: z.object({id: z.string(), resource_id: z.string(), user_id: z.string(), data: z.string()})}))})}); export const updateApiGroupsGroupIdDryRunJsonResponse = z.object({header: header, body: groupUpdateDryRun}); 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 }; /** * Dry run a group update. * * PUT /groups/{groupId}/dry-run.json */ export const updateApiGroupsGroupIdDryRunJson = async (args: UpdateApiGroupsGroupIdDryRunJsonArgs, options?: UpdateApiGroupsGroupIdDryRunJsonOptions) => { const {baseUrl = "https://passbolt.local", fetch: fetchFn = fetch, bearerHttpAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerHttpAuthentication !== undefined) headers.set('authorization', `Bearer ${bearerHttpAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/groups/${encodeURIComponent(String(args.groupId))}/dry-run.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiGroupsGroupIdDryRunJsonResponse, await res.json()) };