import {z} from 'zod' export type CommonTPID = 'hubspot' | 'zohocrm' | 'sfdc' | 'pipedrive' | 'slack' | 'closecrm' | 'ms_dynamics_365_sales'; export type CommonAppConfig = {bot_token?: (string | null) | undefined, org_url?: (string | null) | undefined}; export type CreateApiInternalAccountCredentialsArgs = {body: {clientId?: (string | null) | undefined, clientSecret?: (string | null) | undefined, scopes?: (Array | null) | undefined, tpId: CommonTPID, isRevertApp: boolean, appId: string, appConfig?: CommonAppConfig | undefined}}; export type CreateApiInternalAccountCredentialsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const commonTPID = z.enum(["hubspot", "zohocrm", "sfdc", "pipedrive", "slack", "closecrm", "ms_dynamics_365_sales"]); export const commonRevertUser = z.object({id: z.string(), domain: z.string(), email: z.string(), accountId: z.string(), account: z.lazy(() => commonAccount.optional())}); export const commonEnvironment = z.object({development: z.boolean(), production: z.boolean()}); export const commonAccount: z.ZodType = z.object({id: z.string(), tenant_count: z.number().int(), private_token: z.string(), public_token: z.string(), domain: z.string(), skipWaitlist: z.boolean(), users: z.array(commonRevertUser).nullable().optional(), apps: z.array(z.lazy(() => commonApp)).nullable().optional(), isOnboardingCompleted: commonEnvironment.optional()}); export const commonConnection = z.object({tp_id: commonTPID, tp_access_token: z.string(), tp_refresh_token: z.string().nullable().optional(), tp_customer_id: z.string(), t_id: z.string(), tp_account_url: z.string().nullable().optional(), owner_account_public_token: z.string(), app_client_id: z.string().nullable().optional(), app_client_secret: z.string().nullable().optional(), app: z.lazy(() => commonApp.optional())}); export const commonAppConfig = z.object({bot_token: z.string().nullable().optional(), org_url: z.string().nullable().optional()}); export const commonApp: z.ZodType = z.object({id: z.string(), tp_id: commonTPID, scope: z.array(z.string()).nullable().optional(), available_scope: z.array(z.string()), app_client_id: z.string().nullable().optional(), app_client_secret: z.string().nullable().optional(), owner_account_public_token: z.string(), account: commonAccount.optional(), connections: z.array(commonConnection).nullable().optional(), is_revert_app: z.boolean(), app_config: commonAppConfig.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 account credentials (customer app token, secret, scope) * * POST /internal/account/credentials */ export const createApiInternalAccountCredentials = async (args: CreateApiInternalAccountCredentialsArgs, options?: CreateApiInternalAccountCredentialsOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/internal/account/credentials${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(commonApp, await res.json()) };