import {z} from 'zod' export type ChannelGroupPermissions = {permission: 'read' | 'write' | 'manage'}; export type UpdateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdArgs = {channel_name: string, subchannel_name: string, group_id: string, body: ChannelGroupPermissions}; export type UpdateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const channelGroup = z.object({group_id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), permission: z.enum(["read", "write", "manage"]).optional(), persistent: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const updateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdResponse = z.object({total_count: z.number().optional(), items: z.array(channelGroup).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 group subchannel permission * * PUT /channels/{channel_name}/subchannels/{subchannel_name}/groups/{group_id} */ export const updateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupId = async (args: UpdateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdArgs, options?: UpdateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdOptions) => { const {baseUrl = "https://api.anaconda.cloud/api", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_name))}/subchannels/${encodeURIComponent(String(args.subchannel_name))}/groups/${encodeURIComponent(String(args.group_id))}${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(updateApiChannelsChannelNameSubchannelsSubchannelNameGroupsGroupIdResponse, await res.json()) };