import {z} from 'zod' export type SlackInstallation = {access_token: string, app_id: string, authed_user: {access_token?: string | undefined, expires_in?: number | undefined, id: string, refresh_token?: string | undefined, scope?: string | undefined, token_type?: string | undefined}, bot_user_id?: string | undefined, enterprise?: {id: string, name: string} | undefined, expires_in?: number | undefined, id?: string | undefined, incoming_webhook?: {channel: string, configuration_url: string, url: string} | undefined, is_enterprise_install?: boolean | undefined, refresh_token?: string | undefined, scope?: string | undefined, team: {id: string, name?: string | undefined}, token_type?: string | undefined}; export type UpdateApiIntegrationsSlackInstallationsArgs = {body: SlackInstallation}; export type UpdateApiIntegrationsSlackInstallationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const slackInstallation = z.object({access_token: z.string(), app_id: z.string(), authed_user: z.object({access_token: z.string().optional(), expires_in: z.number().int().optional(), id: z.string(), refresh_token: z.string().optional(), scope: z.string().optional(), token_type: z.string().optional()}), bot_user_id: z.string().optional(), enterprise: z.object({id: z.string(), name: z.string()}).optional(), expires_in: z.number().int().optional(), id: z.string().optional(), incoming_webhook: z.object({channel: z.string(), configuration_url: z.string(), url: z.string()}).optional(), is_enterprise_install: z.boolean().optional(), refresh_token: z.string().optional(), scope: z.string().optional(), team: z.object({id: z.string(), name: z.string().optional()}), token_type: z.string().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 }; /** * Save a Slack installation * * PUT /integrations/slack/installations */ export const updateApiIntegrationsSlackInstallations = async (args: UpdateApiIntegrationsSlackInstallationsArgs, options?: UpdateApiIntegrationsSlackInstallationsOptions) => { const {baseUrl = "https://api.magicbell.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/integrations/slack/installations${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(slackInstallation, await res.json()) };