import {z} from 'zod' export type ConnectorResourceSelectionUpdateParams = {resource_ids?: Array | undefined, resource_aliases?: (Array<'all_mail' | 'all_notes' | 'public_channels' | 'private_channels' | 'direct_messages' | 'group_messages'>) | undefined}; export type UpdateApiV1IntegrationsInstallationsInstallationIdResourcesArgs = {installation_id: string, body: ConnectorResourceSelectionUpdateParams}; export type UpdateApiV1IntegrationsInstallationsInstallationIdResourcesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const connectorResourceSelectionUpdate = z.object({installation_id: z.string(), store_id: z.string(), selected_resource_ids: z.array(z.string()), selected_resource_aliases: z.array(z.enum(["all_mail", "all_notes", "public_channels", "private_channels", "direct_messages", "group_messages"])).optional(), queued_resource_ids: z.array(z.string()).optional(), skipped_resources: z.record(z.string(), z.string()).optional()}); export const connectorResourceSelectionUpdateResponse = z.object({data: connectorResourceSelectionUpdate}); 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 }; /** * Replace a connected account's selected resources * * PUT /v1/integrations/installations/{installation_id}/resources */ export const updateApiV1IntegrationsInstallationsInstallationIdResources = async (args: UpdateApiV1IntegrationsInstallationsInstallationIdResourcesArgs, options?: UpdateApiV1IntegrationsInstallationsInstallationIdResourcesOptions) => { const {baseUrl = "https://api.mixedbread.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/integrations/installations/${encodeURIComponent(String(args.installation_id))}/resources${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(connectorResourceSelectionUpdateResponse, await res.json()) };