import {z} from 'zod' export type SocketTrigger = {event: string, conditions?: (Array<{[key: string]: unknown}> | null) | undefined}; export type SocketAction = {type: string, parameters: {[key: string]: unknown}, condition?: ({[key: string]: unknown} | null) | undefined}; export type SocketConfiguration = {triggers?: Array | undefined, actions?: Array | undefined, error_handling?: ({[key: string]: unknown} | null) | undefined, execution_mode?: string | undefined, priority?: number | undefined}; export type SpaceSocketUpdate = {socket_name?: (string | null) | undefined, description?: (string | null) | undefined, socket_config?: (SocketConfiguration | null) | undefined, is_active?: (boolean | null) | undefined}; export type UpdateApiApiV1SocketsSocketIdArgs = {socket_id: string, body: SpaceSocketUpdate}; export type UpdateApiApiV1SocketsSocketIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const socketTrigger = z.object({event: z.string(), conditions: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); export const socketAction = z.object({type: z.string(), parameters: z.record(z.string(), z.unknown()), condition: z.record(z.string(), z.unknown()).nullable().optional()}); export const socketConfiguration = z.object({triggers: z.array(socketTrigger).optional(), actions: z.array(socketAction).optional(), error_handling: z.record(z.string(), z.unknown()).nullable().optional(), execution_mode: z.string().optional(), priority: z.number().int().gte(1).lte(10).optional()}); export const spacePermissions = z.object({can_read: z.boolean().optional(), can_write: z.boolean().optional(), can_search: z.boolean().optional(), can_delete: z.boolean().optional(), filters: z.record(z.string(), z.unknown()).nullable().optional(), rate_limits: z.record(z.string(), z.number().int()).nullable().optional()}); export const integrationBinding = z.object({integration_type: z.string(), integration_name: z.string(), permissions: spacePermissions, auth_config: z.record(z.string(), z.unknown()), binding_id: z.string(), socket_id: z.string(), is_active: z.boolean().optional(), status: z.string().optional(), created_at: z.string(), last_used: z.string().nullable().optional(), access_token_preview: z.string().nullable().optional()}); export const spaceSocket = z.object({socket_name: z.string().min(1).max(100), description: z.string().max(500).nullable().optional(), socket_config: socketConfiguration, socket_id: z.string(), space_id: z.string(), user_id: z.string(), is_active: z.boolean().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), integration_bindings: z.array(integrationBinding).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 socket * * PUT /api/v1/sockets/{socket_id} */ export const updateApiApiV1SocketsSocketId = async (args: UpdateApiApiV1SocketsSocketIdArgs, options: UpdateApiApiV1SocketsSocketIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sockets/${encodeURIComponent(String(args.socket_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(spaceSocket, await res.json()) };