import {z} from 'zod' export type SpacePermissions = {can_read?: boolean | undefined, can_write?: boolean | undefined, can_search?: boolean | undefined, can_delete?: boolean | undefined, filters?: ({[key: string]: unknown} | null) | undefined, rate_limits?: ({[key: string]: number} | null) | undefined}; export type IntegrationBindingCreate = {integration_type: string, integration_name: string, permissions: SpacePermissions, auth_config: {[key: string]: unknown}}; export type CreateApiApiV1SocketsSocketIdIntegrationsArgs = {socket_id: string, body: IntegrationBindingCreate}; export type CreateApiApiV1SocketsSocketIdIntegrationsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 }; /** * Create integration binding * * POST /api/v1/sockets/{socket_id}/integrations */ export const createApiApiV1SocketsSocketIdIntegrations = async (args: CreateApiApiV1SocketsSocketIdIntegrationsArgs, options: CreateApiApiV1SocketsSocketIdIntegrationsOptions) => { 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))}/integrations${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(integrationBinding, await res.json()) };