import {z} from 'zod' export type CommonTPID = 'hubspot' | 'zohocrm' | 'sfdc' | 'pipedrive' | 'slack' | 'closecrm' | 'ms_dynamics_365_sales'; export type ConnectionImport = {tp_id: CommonTPID, tp_access_token: string, tp_refresh_token: string, tp_customer_id: string, t_id: string, tp_account_url?: (string | null) | undefined, app_client_id: string, app_client_secret: string, app_id: string}; export type ImportConnectionsRequestBody = {connections: Array}; export type CreateApiConnectionImportArgs = {'x-revert-t-id': string, body: ImportConnectionsRequestBody}; export type CreateApiConnectionImportOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const commonResponseStatus = z.enum(["ok", "error"]); export const importConnectionsResponse = z.object({status: commonResponseStatus}); 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 }; /** * Import multiple connections for a specific environment. Use this to bulk import connections as a one-time exercise. * * POST /connection/import */ export const createApiConnectionImport = async (args: CreateApiConnectionImportArgs, options?: CreateApiConnectionImportOptions) => { const {baseUrl = "https://api.revert.dev", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-revert-t-id'] != null) headers.set("x-revert-t-id", String(args['x-revert-t-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/connection/import${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(importConnectionsResponse, await res.json()) };