import {z} from 'zod' export type AuthPermissionObjectMerge = {id: string}; export type AuthGroupPermissionWrite = {id?: string | undefined, object?: AuthPermissionObjectMerge | undefined, privilege?: ('viewer' | 'editor') | undefined}; export type CreateApiAccountsAccountPkGroupsGroupPkPermissionsArgs = {account_pk: string, group_pk: string, body: AuthGroupPermissionWrite}; export type CreateApiAccountsAccountPkGroupsGroupPkPermissionsOptions = {baseUrl?: string | undefined, Apikey0?: string | undefined, fetch?: typeof fetch | undefined}; export const authPermissionObjectMerge = z.object({id: z.string()}); export const authGroupPermissionWrite = z.object({id: z.string().optional(), object: authPermissionObjectMerge.optional(), privilege: z.enum(["viewer", "editor"]).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 }; /** * POST /accounts/{account_pk}/groups/{group_pk}/permissions */ export const createApiAccountsAccountPkGroupsGroupPkPermissions = async (args: CreateApiAccountsAccountPkGroupsGroupPkPermissionsArgs, options?: CreateApiAccountsAccountPkGroupsGroupPkPermissionsOptions) => { const {baseUrl = "https://api.connect.cloudblue.com/public/v1", fetch: fetchFn = fetch, Apikey0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Apikey0 !== undefined) headers.set("Authorization", Apikey0) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_pk))}/groups/${encodeURIComponent(String(args.group_pk))}/permissions${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(authGroupPermissionWrite, await res.json()) };