import type { Channel } from './api/channels' import type { BuildRequestOptions as InternalBuildRequestOptions } from './build/request' import type { DecryptResult } from './bundle/decrypt' import type { EncryptResult } from './bundle/encrypt' import type { BuildZipResult } from './build/zip-command' import type { ZipResult } from './bundle/zip' import type { StarAllRepositoryResult } from './github' import type { ProbeInternalResult } from './probe' import type { AppOptions } from './schemas/app' import type { OptionsUpload } from './schemas/bundle' import type { OptionsSetChannel } from './schemas/channel' import type { AccountIdOptions, AddAppOptions, AddChannelOptions, AddOrganizationOptions, AppInfo, BundleCompatibilityOptions, BundleInfo, BuildMonitorOptions, CleanupOptions, CurrentBundleOptions, DecryptBundleOptions, DeleteOldKeyOptions, DeleteOrganizationOptions, DeviceStats, DoctorOptions, EncryptBundleOptions, GenerateKeyOptions, GetAccountOptions, GetStatsOptions, InviteOrganizationMemberOptions, ListOrganizationsOptions, LoginOptions, ObserveOptions, OrganizationInfo, ProbeOptions, PromoteChannelOptions, RemoveOrganizationMemberOptions, RequestBuildOptions, SaveKeyOptions, SDKResult, SetSettingOptions, StarAllRepositoriesOptions, StarRepoOptions, UpdateAccountOptions, UpdateAppOptions, UpdateChannelOptions, UpdateOrganizationMemberRoleOptions, UpdateOrganizationOptions, UploadOptions, UploadResult, ZipBuildOptions, ZipBundleOptions, } from './schemas/sdk' import type { AccountProfile } from './schemas/account' import type { CapgoHostOptions, Organization } from './utils' import { buildCliRequestHeaders } from './analytics/cli-headers' import { checkAppExistsAndHasPermissionOrgErr } from './api/app' import { getActiveAppVersions } from './api/versions' import { addAppInternal } from './app/add' import { deleteAppInternal } from './app/delete' import { getInfoInternal } from './app/info' import { listAppInternal } from './app/list' import { setAppInternal } from './app/set' import { setSettingInternal } from './app/setting' import { parseStoreReleaseNotesLocalizedJson, requestBuildInternal } from './build/request' import { zipBuildInternal } from './build/zip-command' import { cleanupBundleInternal } from './bundle/cleanup' import { checkCompatibilityInternal } from './bundle/compatibility' import { decryptZipInternal } from './bundle/decrypt' import { deleteBundleInternal } from './bundle/delete' import { encryptZipInternal } from './bundle/encrypt' import { uploadBundleInternal } from './bundle/upload' import { zipBundleInternal } from './bundle/zip' import { addChannelInternal } from './channel/add' import { currentBundleInternal } from './channel/currentBundle' import { deleteChannelInternal } from './channel/delete' import { listChannelsInternal } from './channel/list' import { promoteChannelInternal } from './channel/promote' import { setChannelInternal } from './channel/set' import { resolveCapacitorConfigTargetPath, withConfigWriteTarget } from './config' import { starAllRepositories as starAllRepositoriesInternal, starRepository } from './github' import { createKeyInternal, deleteOldPrivateKeyInternal, saveKeyInternal } from './key' import { loginInternal } from './login' import { fetchObserve } from './observe/api' import { fetchBuildMonitor } from './build/monitor' import { addOrganizationInternal } from './organization/add' import { deleteOrganizationInternal } from './organization/delete' import { listOrganizationsInternal } from './organization/list' import { inviteMemberInternal, removeMemberInternal, setMemberRoleInternal } from './organization/member-manage' import { listMembersInternal } from './organization/members' import { setOrganizationInternal } from './organization/set' import { organizationMemberRoleOptionsSchema, organizationMemberSdkOptionsSchema, promoteChannelOptionsSchema, requestBuildOptionsSchema, updateAccountOptionsSchema, updateAppOptionsSchema, updateChannelOptionsSchema, uploadOptionsSchema, zipBuildOptionsSchema } from './schemas/sdk' import { CliUserError } from './shared/cli-user-error' import { getUserIdInternal } from './user/account' import { getAccountInternal, setAccountInternal } from './user/settings' import { createCapgoClient, findSavedKey, getConfig, getLocalConfig, withCapgoHosts } from './utils' import { parseSecurityPolicyError } from './utils/security_policy_errors' import { normalizeAutoBumpInput } from './versionHelpers' export type DoctorInfo = Awaited> type CompatibilityReport = Awaited>['finalCompatibility'] export type BundleCompatibilityEntry = CompatibilityReport[number] // ============================================================================ // Re-export all SDK types from schemas // ============================================================================ export type { UpdateProbeResult } from './app/updateProbe' /** * Create an SDK error result from an error, with security policy awareness. * This parses the error to check if it's a security policy error and provides * human-readable messages for 2FA, password policy, and API key requirements. */ function createErrorResult(error: unknown): SDKResult { const errorMessage = error instanceof Error ? error.message : String(error) const errorContext = error instanceof CliUserError ? error.context : undefined const parsed = parseSecurityPolicyError(error) return { success: false, error: errorMessage, errorContext, isSecurityPolicyError: parsed.isSecurityPolicyError, securityPolicyMessage: parsed.isSecurityPolicyError ? parsed.message : undefined, } } /** * Runs config-writing SDK methods against an app-specific source while Capacitor * continues to load the active root config in dynamic monorepos. */ async function withCapacitorConfigTarget(capacitorConfig: string | undefined, action: () => Promise): Promise { if (capacitorConfig === undefined) return action() return withConfigWriteTarget(resolveCapacitorConfigTargetPath(capacitorConfig), action) } // ============================================================================ // SDK Class - Main Entry Point // ============================================================================ /** * Capgo SDK for programmatic access to all CLI functionality. * Use this class to integrate Capgo operations directly into your application. * Config-writing methods accept `capacitorConfig` to target an app-specific * source config in a dynamic monorepo. * * @example * ```typescript * // Initialize SDK * const sdk = new CapgoSDK({ apikey: 'your-api-key' }) * * // Upload a bundle * const result = await sdk.uploadBundle({ * appId: 'com.example.app', * path: './dist', * bundle: '1.0.0', * channel: 'production' * }) * * if (result.success) { * console.log('Upload successful!') * } * ``` */ export class CapgoSDK { private readonly apikey?: string private readonly apiHost?: string private readonly filesHost?: string constructor(options?: { apikey?: string /** Capgo API base URL for self-hosting or tests, e.g. `http://127.0.0.1:8787`. */ apiHost?: string /** Capgo files API base URL when it differs from apiHost. */ filesHost?: string }) { this.apikey = options?.apikey this.apiHost = options?.apiHost this.filesHost = options?.filesHost } /** * Hosts for one SDK call: per-call apiHost / filesHost, else the constructor's. Each call runs in its own host scope, so SDK instances * on different backends can run concurrently without sharing hosts or keys. */ callHosts(args: unknown[]): { apiHost?: string, filesHost?: string } { const callOptions = args.find((arg): arg is CapgoHostOptions => typeof arg === 'object' && arg !== null && ('apiHost' in arg || 'filesHost' in arg)) const call: CapgoHostOptions = callOptions ?? {} if (call.apiHost) return { apiHost: call.apiHost, filesHost: call.filesHost } return { apiHost: this.apiHost, filesHost: call.filesHost ?? this.filesHost } } /** Per-call apiHost overrides the SDK-wide one. */ private resolveApiHost(options?: CapgoHostOptions): string | undefined { return options?.apiHost ?? this.apiHost } // ========================================================================== // App Management Methods // ========================================================================== /** * Save an API key locally or in the home directory */ async login(options: LoginOptions): Promise { try { await loginInternal(options.apikey, { local: options.local ?? false, apiHost: this.resolveApiHost(options), }, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Run Capgo Doctor diagnostics and return the report */ async doctor(options?: DoctorOptions): Promise> { try { const info = await getInfoInternal({ packageJson: options?.packageJson }, true) return { success: true, data: info, } } catch (error) { return createErrorResult(error) } } /** * Add a new app to Capgo Cloud * * @example * ```typescript * const result = await sdk.addApp({ * appId: 'com.example.app', * name: 'My App', * icon: './icon.png' * }) * ``` */ async addApp(options: AddAppOptions): Promise { try { const internalOptions: AppOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), name: options.name, icon: options.icon, } await addAppInternal(options.appId, internalOptions, undefined, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Update an existing app in Capgo Cloud * * Note: This method requires CLI function refactoring to work without exit(). * Currently it will throw an error. * * @example * ```typescript * const result = await sdk.updateApp({ * appId: 'com.example.app', * name: 'Updated App Name', * retention: 30 * }) * ``` */ async updateApp(options: UpdateAppOptions): Promise { try { const parsed = updateAppOptionsSchema.parse(options) const internalOptions: AppOptions = { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), name: parsed.name, icon: parsed.icon, retention: parsed.retention, exposeMetadata: parsed.exposeMetadata, preview: parsed.preview, allowDeviceCustomId: parsed.allowDeviceCustomId, blockProviderInfraRequests: parsed.blockProviderInfraRequests, buildTimeoutMinutes: parsed.buildTimeoutMinutes, iosStoreUrl: parsed.iosStoreUrl, androidStoreUrl: parsed.androidStoreUrl, defaultUploadChannel: parsed.defaultUploadChannel, defaultDownloadChannel: parsed.defaultDownloadChannel, disableDownloadChannels: parsed.disableDownloadChannels, collectCountry: parsed.collectCountry, collectPlatform: parsed.collectPlatform, collectOsVersion: parsed.collectOsVersion, collectPluginVersion: parsed.collectPluginVersion, collectVersionBuild: parsed.collectVersionBuild, collectIsEmulator: parsed.collectIsEmulator, collectIsProd: parsed.collectIsProd, collectInstallSource: parsed.collectInstallSource, } await setAppInternal(parsed.appId, internalOptions, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Delete an app from Capgo Cloud * * @param appId - The app ID to delete * @param skipConfirmation - Skip owner confirmation check (use with caution) * * @example * ```typescript * const result = await sdk.deleteApp('com.example.app') * ``` */ async deleteApp(appId: string, skipConfirmation = false): Promise { try { const internalOptions = { apikey: this.apikey || findSavedKey(true), apiHost: this.apiHost, } await deleteAppInternal(appId, internalOptions, false, skipConfirmation) return { success: true } } catch (error) { return createErrorResult(error) } } /** * List all apps for the authenticated account * * @example * ```typescript * const result = await sdk.listApps() * if (result.success) { * result.data?.forEach(app => { * console.log(`${app.name} (${app.appId})`) * }) * } * ``` */ async listApps(): Promise> { try { const internalOptions = { apikey: this.apikey || findSavedKey(true), apiHost: this.apiHost, } // silent=true: the SDK is a programmatic API and (via the MCP server) runs over // a stdio JSON-RPC channel — clack `intro`/`log` output on stdout corrupts the // protocol framing and drops the transport. Every other SDK method is silent too. const apps = await listAppInternal(internalOptions, true) const appInfos: AppInfo[] = apps.map(app => ({ appId: app.app_id, name: app.name || 'Unknown', iconUrl: app.icon_url || undefined, createdAt: new Date(app.created_at || ''), })) return { success: true, data: appInfos, } } catch (error) { return createErrorResult(error) } } /** * Whether `appId` exists in Capgo Cloud AND the configured key has at least read * access to it. Uses the TARGETED existence+permission check (the same one * `app set` / `channel` / `bundle` use) — NOT listApps — so it never triggers the * full-table RBAC RLS scan that times out on large databases and falsely reports an * owned app as missing. Returns false for a missing app or one the key can't access. */ async appHasAccess(appId: string): Promise> { try { const apikey = this.apikey || findSavedKey(true) if (!apikey) return { success: true, data: false } const client = await createCapgoClient(apikey, this.apiHost) // silent: no UI output (this runs over a stdio MCP channel). skip2FACheck: this // is a read-only "is it registered" probe — the real credential ops enforce 2FA. await checkAppExistsAndHasPermissionOrgErr(client, apikey, appId, 'app.read', true, true) return { success: true, data: true } } catch { return { success: true, data: false } } } /** * Retrieve the account ID associated with the configured API key */ async getAccountId(options?: AccountIdOptions): Promise> { try { const resolvedOptions = { apikey: options?.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), } const userId = await getUserIdInternal(resolvedOptions, true) return { success: true, data: userId, } } catch (error) { return createErrorResult(error) } } /** * Star the Capgo repository on GitHub * * @example * ```typescript * const result = await sdk.starRepo({ repository: 'Cap-go/capacitor-updater' }) * if (result.success) { * console.log(`${result.data?.repository} starred`) * } * ``` */ async starRepo(options?: StarRepoOptions): Promise> { try { const { repository, alreadyStarred } = starRepository(options?.repository) return { success: true, data: { repository, alreadyStarred }, } } catch (error) { return createErrorResult(error) } } /** * Star the Capgo-related repositories on GitHub * * @example * ```typescript * const result = await sdk.starAllRepositories() * if (result.success) { * for (const entry of result.data ?? []) { * console.log(entry.repository, entry.status) * } * } * ``` */ async starAllRepositories(options?: StarAllRepositoriesOptions): Promise> { try { const data = await starAllRepositoriesInternal(options) return { success: true, data, } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Bundle Management Methods // ========================================================================== async checkBundleCompatibility(options: BundleCompatibilityOptions): Promise> { try { const requestOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), channel: options.channel, text: options.textOutput ?? false, packageJson: options.packageJson, nodeModules: options.nodeModules, apiHost: this.resolveApiHost(options), } const compatibility = await checkCompatibilityInternal(options.appId, requestOptions, true) return { success: true, data: compatibility.finalCompatibility, } } catch (error) { return createErrorResult(error) } } async encryptBundle(options: EncryptBundleOptions): Promise> { try { const result = await encryptZipInternal(options.zipPath, options.checksum, { key: options.keyPath, keyData: options.keyData, json: options.json, packageJson: options.packageJson, }, true) return { success: true, data: result, } } catch (error) { return createErrorResult(error) } } async decryptBundle(options: DecryptBundleOptions): Promise> { try { const result = await decryptZipInternal(options.zipPath, options.ivSessionKey, { key: options.keyPath, keyData: options.keyData, checksum: options.checksum, packageJson: options.packageJson, }, true) return { success: true, data: result, } } catch (error) { return createErrorResult(error) } } async zipBundle(options: ZipBundleOptions): Promise> { try { const result = await zipBundleInternal(options.appId, { apikey: this.apikey || findSavedKey(true), path: options.path, bundle: options.bundle, name: options.name, codeCheck: options.codeCheck, json: options.json, keyV2: options.keyV2, keyDataV2: options.keyDataV2, key: options.key, packageJson: options.packageJson, nodeModules: options.nodeModules, forUpload: options.forUpload, deltaPack: options.deltaPack, encryptDelta: options.encryptDelta, disableBrotli: options.disableBrotli, forceCrc32Checksum: options.forceCrc32Checksum, }, true) return { success: true, data: result, } } catch (error) { return createErrorResult(error) } } /** * Zip the native project exactly like `requestBuild` does, without uploading it. * Offline: no API key and no network calls. */ async zipBuild(options: ZipBuildOptions): Promise> { try { const parsed = zipBuildOptionsSchema.parse(options) const result = await zipBuildInternal(parsed.appId, { platform: parsed.platform, path: parsed.path, nodeModules: parsed.nodeModules, syncIosVersion: parsed.syncIosVersion, syncAndroidVersion: parsed.syncAndroidVersion, output: parsed.output, }, true) return { success: true, data: result } } catch (error) { return createErrorResult(error) } } /** * Upload a bundle to Capgo Cloud * * @example * ```typescript * const result = await sdk.uploadBundle({ * appId: 'com.example.app', * path: './dist', * bundle: '1.0.0', * channel: 'production', * comment: 'New features added' * }) * ``` */ async uploadBundle(options: UploadOptions): Promise { try { const parsed = uploadOptionsSchema.parse(options) return await withCapacitorConfigTarget(parsed.capacitorConfig, async () => { // Convert SDK options to internal format const internalOptions: OptionsUpload = { apikey: parsed.apikey || this.apikey || findSavedKey(true), ...this.callHosts([parsed]), path: parsed.path, mode: parsed.mode, bundle: parsed.bundle, channel: parsed.channel, rollout: parsed.rollout, rolloutPercentageBps: parsed.rolloutPercentageBps, rolloutCacheTtlSeconds: parsed.rolloutCacheTtlSeconds, external: parsed.external, key: parsed.encrypt !== false, // default true unless explicitly false keyV2: parsed.encryptionKey, timeout: parsed.timeout, tus: parsed.useTus, comment: parsed.comment, minUpdateVersion: parsed.minUpdateVersion, autoMinUpdateVersion: parsed.autoMinUpdateVersion, autoSetBundle: parsed.autoSetBundle, autoBump: normalizeAutoBumpInput(parsed.autoBump), selfAssign: parsed.selfAssign, packageJson: parsed.packageJsonPaths, ignoreMetadataCheck: parsed.ignoreCompatibilityCheck, acceptIncompatible: parsed.acceptIncompatible, codeCheck: !parsed.disableCodeCheck, // disable if requested, otherwise check zip: parsed.useZip, // use legacy zip upload if requested delta: parsed.delta, deltaOnly: parsed.deltaOnly, encryptDelta: parsed.encryptDelta, } // Call internal upload function but suppress CLI behaviors const uploadResponse = await uploadBundleInternal(parsed.appId, internalOptions, true) return { success: uploadResponse.success, bundleId: uploadResponse.bundle, checksum: uploadResponse.checksum ?? null, encryptionMethod: uploadResponse.encryptionMethod, sessionKey: uploadResponse.sessionKey, ivSessionKey: uploadResponse.ivSessionKey, storageProvider: uploadResponse.storageProvider, skipped: uploadResponse.skipped, reason: uploadResponse.reason, } }) } catch (error) { return createErrorResult(error) } } /** * List bundles for an app * * @example * ```typescript * const result = await sdk.listBundles('com.example.app') * if (result.success) { * result.data?.forEach(bundle => { * console.log(`${bundle.version} - ${bundle.uploadedAt}`) * }) * } * ``` */ async listBundles(appId: string): Promise> { try { const apikey = this.apikey || findSavedKey(true) const versions = await getActiveAppVersions(apikey, appId, { apikey, apiHost: this.apiHost, }) const bundles: BundleInfo[] = versions.map(bundle => ({ id: bundle.id.toString(), version: bundle.name, uploadedAt: new Date(bundle.created_at || ''), size: 0, // Size not available in current schema encrypted: bundle.session_key !== null, })) return { success: true, data: bundles, } } catch (error) { return createErrorResult(error) } } /** * Delete a specific bundle * * Note: This method requires CLI function refactoring to work without exit(). * * @example * ```typescript * const result = await sdk.deleteBundle('com.example.app', '1.0.0') * ``` */ async deleteBundle(appId: string, bundleId: string): Promise { try { const internalOptions = { apikey: this.apikey || findSavedKey(true), apiHost: this.apiHost, bundle: bundleId, } await deleteBundleInternal(bundleId, appId, internalOptions, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Cleanup old bundles, keeping only recent versions * * @example * ```typescript * const result = await sdk.cleanupBundles({ * appId: 'com.example.app', * keep: 5, * force: true * }) * ``` */ async cleanupBundles(options: CleanupOptions): Promise> { try { const internalOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), bundle: options.bundle || '', version: '', keep: options.keep || 4, force: options.force || false, ignoreChannel: options.ignoreChannel || false, } const result = await cleanupBundleInternal(options.appId, internalOptions, true) return { success: true, data: result, } } catch (error) { return createErrorResult(error) } } /** * Request a native build for your app with store publishing * * SECURITY GUARANTEE: * Credentials provided to this method are NEVER stored on Capgo servers. * They are used only during the build process and automatically deleted * after completion (maximum 24 hours retention). Build outputs may optionally * be uploaded for time-limited download links. * * @example * ```typescript * const result = await sdk.requestBuild({ * appId: 'com.example.app', * path: './my-project', * lane: 'ios', // Must be exactly "ios" or "android" * credentials: { * BUILD_CERTIFICATE_BASE64: 'base64-cert...', * CAPGO_IOS_PROVISIONING_MAP: '{"com.example.app":{"profile":"base64...","name":"match AppStore com.example.app"}}', * P12_PASSWORD: 'cert-password', * APPLE_KEY_ID: 'KEY123', * APPLE_ISSUER_ID: 'issuer-uuid', * APPLE_KEY_CONTENT: 'base64-p8...', * APP_STORE_CONNECT_TEAM_ID: 'team-id' * } * }) * * if (result.success) { * console.log('Job ID:', result.data.jobId) * } * ``` */ async requestBuild(options: RequestBuildOptions): Promise> { try { const parsed = requestBuildOptionsSchema.parse(options) // Convert BuildCredentials object to flattened CLI-compatible format const creds = parsed.credentials const internalOptions: InternalBuildRequestOptions = { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), path: parsed.path, nodeModules: parsed.nodeModules, platform: parsed.platform, userId: parsed.userId, useStoredCredentials: parsed.useStoredCredentials, // Flatten BuildCredentials to individual fields buildCertificateBase64: creds?.BUILD_CERTIFICATE_BASE64, p12Password: creds?.P12_PASSWORD, appleKeyId: creds?.APPLE_KEY_ID, appleIssuerId: creds?.APPLE_ISSUER_ID, appleKeyContent: creds?.APPLE_KEY_CONTENT, appStoreConnectTeamId: creds?.APP_STORE_CONNECT_TEAM_ID, appleId: creds?.FASTLANE_USER, appleAppSpecificPassword: creds?.FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD, appleAppId: creds?.APPLE_APP_ID, iosScheme: creds?.CAPGO_IOS_SCHEME, iosTarget: creds?.CAPGO_IOS_TARGET, xcodeVersion: parsed.xcodeVersion ?? creds?.CAPGO_IOS_XCODE_VERSION, iosDistribution: creds?.CAPGO_IOS_DISTRIBUTION as 'app_store' | 'ad_hoc' | undefined, iosProvisioningMap: creds?.CAPGO_IOS_PROVISIONING_MAP, androidKeystoreFile: creds?.ANDROID_KEYSTORE_FILE, keystoreKeyAlias: creds?.KEYSTORE_KEY_ALIAS, keystoreKeyPassword: creds?.KEYSTORE_KEY_PASSWORD, keystoreStorePassword: creds?.KEYSTORE_STORE_PASSWORD, playConfigJson: creds?.PLAY_CONFIG_JSON, androidTrack: parsed.androidTrack ?? (creds?.PLAY_STORE_TRACK as 'internal' | 'alpha' | 'beta' | 'production' | undefined), androidReleaseStatus: parsed.androidReleaseStatus ?? (creds?.PLAY_STORE_RELEASE_STATUS as 'draft' | 'completed' | 'inProgress' | 'halted' | undefined), submitToStoreReview: parsed.submitToStoreReview ?? (creds?.CAPGO_STORE_SUBMIT_REVIEW === undefined ? undefined : creds.CAPGO_STORE_SUBMIT_REVIEW === 'true'), storeReleaseName: parsed.storeReleaseName ?? creds?.CAPGO_STORE_RELEASE_NAME, storeReleaseNotes: parsed.storeReleaseNotes ?? creds?.CAPGO_STORE_RELEASE_NOTES, storeReleaseNotesLocalized: parsed.storeReleaseNotesLocalized ?? parseStoreReleaseNotesLocalizedJson(creds?.CAPGO_STORE_RELEASE_NOTES_LOCALIZED), iosTestflightGroups: parsed.iosTestflightGroups ?? creds?.CAPGO_IOS_TESTFLIGHT_GROUPS, iosAutomaticRelease: parsed.iosAutomaticRelease ?? (creds?.CAPGO_IOS_AUTOMATIC_RELEASE === undefined ? undefined : creds.CAPGO_IOS_AUTOMATIC_RELEASE === 'true'), // Prescan escape hatch: SDK callers own their output channel and cannot // pass CLI flags, so expose the gate controls directly. prescan: parsed.prescan, prescanIgnoreFatal: parsed.prescanIgnoreFatal, prescanSkip: parsed.prescanSkip, prescanWarn: parsed.prescanWarn, cache: parsed.cache, cacheKey: parsed.cacheKey, } const result = await requestBuildInternal(parsed.appId, internalOptions, true) if (result.success && result.jobId) { return { success: true, data: { jobId: result.jobId, uploadUrl: result.uploadUrl || '', status: result.status || 'unknown', }, } } return { success: false, error: result.error || 'Unknown error during build request', } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Channel Management Methods // ========================================================================== async getCurrentBundle(appId: string, channelId: string, options?: CurrentBundleOptions): Promise> { try { const requestOptions = { apikey: options?.apikey || this.apikey || findSavedKey(true), quiet: true, apiHost: this.resolveApiHost(options), } const bundle = await currentBundleInternal(channelId, appId, requestOptions as any, true) return { success: true, data: bundle, } } catch (error) { return createErrorResult(error) } } /** * Create a new channel for app distribution * * @example * ```typescript * const result = await sdk.addChannel({ * channelId: 'production', * appId: 'com.example.app', * default: true * }) * ``` */ async addChannel(options: AddChannelOptions): Promise { try { const internalOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), default: options.default, selfAssign: options.selfAssign, } await addChannelInternal(options.channelId, options.appId, internalOptions, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Update channel settings * * @example * ```typescript * const result = await sdk.updateChannel({ * channelId: 'production', * appId: 'com.example.app', * bundle: '1.0.0' * }) * ``` */ async updateChannel(options: UpdateChannelOptions): Promise { try { const parsed = updateChannelOptionsSchema.parse(options) const internalOptions: OptionsSetChannel = { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), bundle: parsed.bundle ?? undefined, state: parsed.state, downgrade: parsed.downgrade, ios: parsed.ios, android: parsed.android, electron: parsed.electron, pause: parsed.pause, resume: parsed.resume, selfAssign: parsed.selfAssign, disableAutoUpdate: parsed.disableAutoUpdate ?? undefined, updatePackage: parsed.updatePackage, dev: parsed.dev, emulator: parsed.emulator, device: parsed.device, prod: parsed.prod, rolloutBundle: parsed.rolloutBundle, rolloutPercentage: parsed.rolloutPercentage, rolloutPercentageBps: parsed.rolloutPercentageBps, rolloutEnable: parsed.rolloutEnable, rolloutDisable: parsed.rolloutDisable, rolloutPause: parsed.rolloutPause, rolloutResume: parsed.rolloutResume, rolloutRollback: parsed.rolloutRollback, rolloutPromote: parsed.rolloutPromote, rolloutCacheTtlSeconds: parsed.rolloutCacheTtlSeconds, autoPauseEnabled: parsed.autoPauseEnabled, autoPauseDisabled: parsed.autoPauseDisabled, autoPauseWindowMinutes: parsed.autoPauseWindowMinutes, autoPauseFailureRateBps: parsed.autoPauseFailureRateBps, autoPauseConfidence: parsed.autoPauseConfidence, autoPauseMinAttempts: parsed.autoPauseMinAttempts, autoPauseMinFailures: parsed.autoPauseMinFailures, autoPauseAction: parsed.autoPauseAction, autoPauseCooldownMinutes: parsed.autoPauseCooldownMinutes, latest: false, latestRemote: false, packageJson: undefined, ignoreMetadataCheck: false, acceptIncompatible: parsed.acceptIncompatible, } await setChannelInternal(parsed.channelId, parsed.appId, internalOptions, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Promote the bundle currently linked to one channel to another channel * * @example * ```typescript * const result = await sdk.promoteChannel({ * appId: 'com.example.app', * fromChannel: 'staging', * toChannel: 'production', * }) * // result.data?.bundle === '1.2.3' * ``` */ async promoteChannel(options: PromoteChannelOptions): Promise> { try { const parsed = promoteChannelOptionsSchema.parse(options) const data = await promoteChannelInternal(parsed.fromChannel, parsed.toChannel, parsed.appId, { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), ignoreMetadataCheck: parsed.ignoreMetadataCheck, acceptIncompatible: parsed.acceptIncompatible, sendUpdateNotification: parsed.sendUpdateNotification, }, true) return { success: true, data } } catch (error) { return createErrorResult(error) } } /** * Delete a channel * * @example * ```typescript * const result = await sdk.deleteChannel('staging', 'com.example.app') * ``` */ async deleteChannel(channelId: string, appId: string, deleteBundle = false): Promise { try { const internalOptions = { apikey: this.apikey || findSavedKey(true), apiHost: this.apiHost, deleteBundle, successIfNotFound: false, } await deleteChannelInternal(channelId, appId, internalOptions, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * List all channels for an app * * @example * ```typescript * const result = await sdk.listChannels('com.example.app') * if (result.success) { * result.data?.forEach(channel => { * console.log(`${channel.name} - ${channel.isDefault ? 'default' : 'normal'}`) * }) * } * ``` */ async listChannels(appId: string): Promise> { try { const internalOptions = { apikey: this.apikey || findSavedKey(true), apiHost: this.apiHost, } const channels = await listChannelsInternal(appId, internalOptions, true) return { success: true, data: channels, } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Organization Management Methods // ========================================================================== /** * Generate Capgo encryption keys (private/public pair) */ async generateEncryptionKeys(options?: GenerateKeyOptions): Promise { try { return await withCapacitorConfigTarget(options?.capacitorConfig, async () => { await createKeyInternal({ force: options?.force, setupChannel: options?.setupChannel, }, true) return { success: true } }) } catch (error) { return createErrorResult(error) } } /** * Save a public encryption key into the Capacitor config */ async saveEncryptionKey(options?: SaveKeyOptions): Promise { try { return await withCapacitorConfigTarget(options?.capacitorConfig, async () => { await saveKeyInternal({ key: options?.keyPath, keyData: options?.keyData, setupChannel: options?.setupChannel, }, true) return { success: true } }) } catch (error) { return createErrorResult(error) } } /** * Delete legacy (v1) encryption keys from the project */ async deleteLegacyEncryptionKey(options?: DeleteOldKeyOptions): Promise> { try { return await withCapacitorConfigTarget(options?.capacitorConfig, async () => { const deleted = await deleteOldPrivateKeyInternal({ force: options?.force, setupChannel: options?.setupChannel, }, true) return { success: true, data: { deleted }, } }) } catch (error) { return createErrorResult(error) } } async listOrganizations(options?: ListOrganizationsOptions): Promise> { try { const requestOptions = { apikey: options?.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), } const organizations = await listOrganizationsInternal(requestOptions, true) const data: OrganizationInfo[] = organizations.map((org: Organization) => ({ id: String((org as any).id ?? (org as any).gid ?? ''), name: (org as any).name ?? 'Unknown', role: (org as any).role ?? undefined, appCount: typeof (org as any).app_count === 'number' ? (org as any).app_count : undefined, email: (org as any).management_email ?? undefined, createdAt: (org as any).created_at ? new Date((org as any).created_at) : undefined, })) return { success: true, data, } } catch (error) { return createErrorResult(error) } } async addOrganization(options: AddOrganizationOptions): Promise> { try { const requestOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), name: options.name, email: options.email, } const org = await addOrganizationInternal(requestOptions, true) const info: OrganizationInfo = { id: String((org as any).id ?? (org as any).gid ?? ''), name: (org as any).name ?? options.name, role: 'owner', appCount: 0, email: (org as any).management_email ?? options.email, createdAt: (org as any).created_at ? new Date((org as any).created_at) : undefined, } return { success: true, data: info, } } catch (error) { return createErrorResult(error) } } async updateOrganization(options: UpdateOrganizationOptions): Promise> { try { const requestOptions = { apikey: options.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), name: options.name, email: options.email, website: options.website, enforce2fa: options.enforce2fa, passwordPolicy: options.passwordPolicy, minLength: options.minLength, requireUppercase: options.requireUppercase, requireNumber: options.requireNumber, requireSpecial: options.requireSpecial, requireApikeyExpiration: options.requireApikeyExpiration, maxApikeyExpirationDays: options.maxApikeyExpirationDays, enforceHashedApiKeys: options.enforceHashedApiKeys, enforceEncryptedBundles: options.enforceEncryptedBundles, requiredEncryptionKey: options.requiredEncryptionKey, emailPreferences: options.emailPreferences, } const updated = await setOrganizationInternal(options.orgId, requestOptions, true) const info: OrganizationInfo = { id: updated.orgId, name: updated.name, email: updated.email, } return { success: true, data: info, } } catch (error) { return createErrorResult(error) } } async deleteOrganization(orgId: string, options?: DeleteOrganizationOptions): Promise> { try { const requestOptions = { apikey: options?.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), autoConfirm: options?.autoConfirm ?? true, } const deleted = await deleteOrganizationInternal(orgId, requestOptions, true) return { success: true, data: { deleted }, } } catch (error) { return createErrorResult(error) } } /** * List the members of an organization with their role, invite state, and 2FA status */ async listOrganizationMembers(orgId: string, options?: ListOrganizationsOptions): Promise>>> { try { const data = await listMembersInternal(orgId, { apikey: options?.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), }, true) return { success: true, data } } catch (error) { return createErrorResult(error) } } /** * Invite a user to an organization by email * * @example * ```typescript * await sdk.inviteOrganizationMember({ orgId, email: 'teammate@example.com', role: 'org_admin' }) * ``` */ async inviteOrganizationMember(options: InviteOrganizationMemberOptions): Promise { try { const parsed = organizationMemberRoleOptionsSchema.parse(options) await inviteMemberInternal(parsed.orgId, { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), email: parsed.email, role: parsed.role, }, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Change the role of an organization member or pending invite */ async updateOrganizationMemberRole(options: UpdateOrganizationMemberRoleOptions): Promise { try { const parsed = organizationMemberRoleOptionsSchema.parse(options) await setMemberRoleInternal(parsed.orgId, { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), email: parsed.email, role: parsed.role, }, true) return { success: true } } catch (error) { return createErrorResult(error) } } /** * Remove a member from an organization */ async removeOrganizationMember(options: RemoveOrganizationMemberOptions): Promise { try { const parsed = organizationMemberSdkOptionsSchema.parse(options) await removeMemberInternal(parsed.orgId, { apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), email: parsed.email, }, true) return { success: true } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Account // ========================================================================== /** * Get the profile and email notification settings of the account that owns the API key */ async getAccount(options?: GetAccountOptions): Promise> { try { const data = await getAccountInternal({ apikey: options?.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(options), }, true) return { success: true, data } } catch (error) { return createErrorResult(error) } } /** * Update the profile and email notification settings of the account that owns the API key * * @example * ```typescript * await sdk.updateAccount({ firstName: 'Ada', emailPreferences: { weekly_stats: false } }) * ``` */ async updateAccount(options: UpdateAccountOptions): Promise> { try { const parsed = updateAccountOptionsSchema.parse(options) const data = await setAccountInternal({ apikey: parsed.apikey || this.apikey || findSavedKey(true), apiHost: this.resolveApiHost(parsed), firstName: parsed.firstName, lastName: parsed.lastName, country: parsed.country, discordUsername: parsed.discordUsername, formatLocale: parsed.formatLocale, notifications: parsed.notifications, newsletter: parsed.newsletter, emailPreferences: parsed.emailPreferences, }, true) return { success: true, data } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Device Stats & Debugging // ========================================================================== /** * Get device statistics/logs from Capgo backend * * This method works similarly to waitLog, allowing you to poll for device activity. * Use the `after` parameter to get only new stats since a previous call. * * @example * ```typescript * // Get recent stats for an app * const result = await sdk.getStats({ * appId: 'com.example.app', * rangeStart: new Date().toISOString(), * limit: 100 * }) * * if (result.success && result.data) { * result.data.forEach(stat => { * console.log(`${stat.deviceId}: ${stat.action}`) * }) * } * * // Poll for new stats (similar to waitLog) * let after = new Date().toISOString() * const poll = async () => { * const result = await sdk.getStats({ * appId: 'com.example.app', * after * }) * * if (result.success && result.data && result.data.length > 0) { * // Update 'after' to newest timestamp * const newest = result.data.reduce((max, d) => { * const t = new Date(d.createdAt).getTime() * return Math.max(max, t) * }, new Date(after).getTime()) * after = new Date(newest).toISOString() * * // Process new stats * result.data.forEach(stat => console.log(stat)) * } * } * ``` */ async getStats(options: GetStatsOptions): Promise> { try { const apikey = options.apikey || this.apikey || findSavedKey(true) const localConfig = await getLocalConfig() const query = { appId: options.appId, devicesId: options.deviceIds, search: options.search, order: options.order, rangeStart: options.after || options.rangeStart, rangeEnd: options.rangeEnd, limit: options.limit, } const response = await fetch(`${localConfig.hostApi}/private/stats`, { method: 'POST', headers: buildCliRequestHeaders({ 'Content-Type': 'application/json', 'capgkey': apikey }), body: JSON.stringify(query), }) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } const data = await response.json() as Array<{ app_id: string device_id: string action: string version_id: number version?: number created_at: string }> const stats: DeviceStats[] = data.map(d => ({ appId: d.app_id, deviceId: d.device_id, action: d.action, versionId: d.version_id, version: d.version, createdAt: d.created_at, })) return { success: true, data: stats, } } catch (error) { return createErrorResult(error) } } /** Read any authorized cloud build, including builds started by CI or the hosted MCP. */ async getBuildStatus(options: BuildMonitorOptions): Promise>> { return this.readBuildMonitor('status', options) } /** Bounded JSON snapshot; disconnecting never cancels a build. */ async getBuildLogs(options: BuildMonitorOptions): Promise>> { return this.readBuildMonitor('logs', options) } private async readBuildMonitor(kind: 'status' | 'logs', options: BuildMonitorOptions): Promise>> { try { return { success: true, data: await fetchBuildMonitor(kind, { ...options, apikey: options.apikey || this.apikey, apiHost: this.resolveApiHost(options), }) } } catch (error) { return createErrorResult(error) } } /** * Query Capgo Observe (launch, issues, routes, device timelines). * Start with view=summary; follow findings.next. Use --json / MCP for agents. */ async observe(options: ObserveOptions): Promise>> { try { const data = await fetchObserve({ ...options, apikey: options.apikey || this.apikey, apiHost: this.resolveApiHost(options), }) return { success: true, data } } catch (error) { return createErrorResult(error) } } // ========================================================================== // Miscellaneous Helpers // ========================================================================== async setAppSetting(path: string, options: SetSettingOptions): Promise { try { return await withCapacitorConfigTarget(options.capacitorConfig, async () => { await setSettingInternal(path, { apikey: options.apikey || this.apikey || findSavedKey(true), bool: options.bool, string: options.string, }, true) return { success: true } }) } catch (error) { return createErrorResult(error) } } // ========================================================================== // Probe (no auth required - hits public /updates endpoint) // ========================================================================== async probe(options: ProbeOptions): Promise> { try { const { probeInternal } = await import('./probe') const result = await probeInternal({ platform: options.platform }) if (result.error) { return { success: false, error: result.error } } return { success: true, data: result } } catch (error) { return createErrorResult(error) } } } // Every public SDK method runs in its own Capgo host scope (see callHosts). for (const name of Object.getOwnPropertyNames(CapgoSDK.prototype)) { if (name === 'constructor' || name === 'callHosts' || name === 'resolveApiHost') continue const method = (CapgoSDK.prototype as unknown as Record)[name] if (typeof method !== 'function') continue Object.defineProperty(CapgoSDK.prototype, name, { configurable: true, writable: true, value(this: CapgoSDK, ...args: unknown[]) { return withCapgoHosts(this.callHosts(args), () => method.apply(this, args)) }, }) } // ============================================================================ // Functional API - Convenience Wrappers // ============================================================================ /** * Upload a bundle to Capgo Cloud (functional API) * * @example * ```typescript * const result = await uploadBundle({ * appId: 'com.example.app', * path: './dist', * bundle: '1.0.0', * apikey: 'your-api-key' * }) * ``` */ export async function uploadBundle(options: UploadOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.uploadBundle(options) } export async function login(options: LoginOptions): Promise { const sdk = new CapgoSDK({ apiHost: options.apiHost, }) return sdk.login(options) } export async function doctor(options?: DoctorOptions): Promise> { const sdk = new CapgoSDK() return sdk.doctor(options) } export async function checkBundleCompatibility(options: BundleCompatibilityOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.checkBundleCompatibility(options) } export async function encryptBundle(options: EncryptBundleOptions): Promise> { const sdk = new CapgoSDK() return sdk.encryptBundle(options) } export async function decryptBundle(options: DecryptBundleOptions): Promise> { const sdk = new CapgoSDK() return sdk.decryptBundle(options) } export async function zipBundle(options: ZipBundleOptions): Promise> { const sdk = new CapgoSDK() return sdk.zipBundle(options) } export async function zipBuild(options: ZipBuildOptions): Promise> { const sdk = new CapgoSDK() return sdk.zipBuild(options) } export async function starRepo(options?: StarRepoOptions): Promise> { const sdk = new CapgoSDK() return sdk.starRepo(options) } export async function starAllRepositories(options?: StarAllRepositoriesOptions): Promise> { const sdk = new CapgoSDK() return sdk.starAllRepositories(options) } export async function generateEncryptionKeys(options?: GenerateKeyOptions): Promise { const sdk = new CapgoSDK() return sdk.generateEncryptionKeys(options) } export async function saveEncryptionKey(options?: SaveKeyOptions): Promise { const sdk = new CapgoSDK() return sdk.saveEncryptionKey(options) } export async function deleteLegacyEncryptionKey(options?: DeleteOldKeyOptions): Promise> { const sdk = new CapgoSDK() return sdk.deleteLegacyEncryptionKey(options) } export async function getCurrentBundle(appId: string, channelId: string, options?: CurrentBundleOptions): Promise> { const sdk = new CapgoSDK({ apikey: options?.apikey, apiHost: options?.apiHost, }) return sdk.getCurrentBundle(appId, channelId, options) } export async function promoteChannel(options: PromoteChannelOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.promoteChannel(options) } export async function updateAppSetting(path: string, options: SetSettingOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, }) return sdk.setAppSetting(path, options) } export async function getAccountId(options?: AccountIdOptions): Promise> { const sdk = new CapgoSDK({ apikey: options?.apikey, apiHost: options?.apiHost, }) return sdk.getAccountId(options) } export async function listOrganizations(options?: ListOrganizationsOptions): Promise> { const sdk = new CapgoSDK({ apikey: options?.apikey, apiHost: options?.apiHost, }) return sdk.listOrganizations(options) } export async function addOrganization(options: AddOrganizationOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.addOrganization(options) } export async function updateOrganization(options: UpdateOrganizationOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.updateOrganization(options) } export async function deleteOrganization(orgId: string, options?: DeleteOrganizationOptions): Promise> { const sdk = new CapgoSDK({ apikey: options?.apikey, apiHost: options?.apiHost, }) return sdk.deleteOrganization(orgId, options) } export async function inviteOrganizationMember(options: InviteOrganizationMemberOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }) return sdk.inviteOrganizationMember(options) } export async function updateOrganizationMemberRole(options: UpdateOrganizationMemberRoleOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }) return sdk.updateOrganizationMemberRole(options) } export async function removeOrganizationMember(options: RemoveOrganizationMemberOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }) return sdk.removeOrganizationMember(options) } export async function getAccount(options?: GetAccountOptions): Promise> { const sdk = new CapgoSDK({ apikey: options?.apikey, apiHost: options?.apiHost }) return sdk.getAccount(options) } export async function updateAccount(options: UpdateAccountOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }) return sdk.updateAccount(options) } /** * Add a new app to Capgo Cloud (functional API) * * @example * ```typescript * const result = await addApp({ * appId: 'com.example.app', * name: 'My App', * apikey: 'your-api-key' * }) * ``` */ export async function addApp(options: AddAppOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.addApp(options) } /** * List bundles for an app (functional API) * * @example * ```typescript * const result = await listBundles('com.example.app', { apikey: 'your-api-key' }) * ``` */ export async function listBundles( appId: string, options?: { apikey?: string, apiHost?: string }, ): Promise> { const sdk = new CapgoSDK(options) return sdk.listBundles(appId) } /** * Add a new channel (functional API) * * @example * ```typescript * const result = await addChannel({ * channelId: 'production', * appId: 'com.example.app', * apikey: 'your-api-key' * }) * ``` */ export async function addChannel(options: AddChannelOptions): Promise { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.addChannel(options) } /** * Request a native build for your app (functional API) * * SECURITY GUARANTEE: * Credentials are NEVER stored on Capgo servers. They are used only during * the build process and automatically deleted after completion. * Build outputs may optionally be uploaded for time-limited download links. * * @example * ```typescript * const result = await requestBuild({ * appId: 'com.example.app', * path: './my-project', * lane: 'ios', // Must be exactly "ios" or "android" * credentials: { * // iOS credentials (use standard environment variable names) * BUILD_CERTIFICATE_BASE64: 'base64-encoded-cert', * BUILD_PROVISION_PROFILE_BASE64: 'base64-encoded-profile', * P12_PASSWORD: 'cert-password', * APPLE_KEY_ID: 'KEY123', * APPLE_ISSUER_ID: 'issuer-uuid', * APPLE_KEY_CONTENT: 'base64-encoded-p8', * APP_STORE_CONNECT_TEAM_ID: 'team-id', * // Android credentials (use standard environment variable names) * ANDROID_KEYSTORE_FILE: 'base64-encoded-keystore', * KEYSTORE_KEY_ALIAS: 'my-key-alias', * KEYSTORE_KEY_PASSWORD: 'key-password', * KEYSTORE_STORE_PASSWORD: 'store-password', * PLAY_CONFIG_JSON: 'base64-encoded-service-account-json' * }, * apikey: 'your-api-key' * }) * * if (result.success) { * console.log('Job ID:', result.data.jobId) * console.log('Status:', result.data.status) * } * ``` */ export async function requestBuild(options: RequestBuildOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.requestBuild(options) } /** * Get device statistics/logs from Capgo backend (functional API) * * This function works similarly to waitLog, allowing you to poll for device activity. * * @example * ```typescript * // Get recent stats for an app * const result = await getStats({ * appId: 'com.example.app', * apikey: 'your-api-key', * rangeStart: new Date().toISOString(), * limit: 100 * }) * * if (result.success && result.data) { * result.data.forEach(stat => { * console.log(`${stat.deviceId}: ${stat.action}`) * }) * } * ``` */ export async function getStats(options: GetStatsOptions): Promise> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.getStats(options) } /** * Query Capgo Observe (functional API). * Start with view=summary and follow findings.next. view=device is the session timeline. */ export async function getObserve(options: ObserveOptions): Promise>> { const sdk = new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost, }) return sdk.observe(options) } export async function getBuildStatus(options: BuildMonitorOptions): Promise>> { return new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }).getBuildStatus(options) } export async function getBuildLogs(options: BuildMonitorOptions): Promise>> { return new CapgoSDK({ apikey: options.apikey, apiHost: options.apiHost }).getBuildLogs(options) } export async function probeUpdates(options: ProbeOptions): Promise> { const sdk = new CapgoSDK() return sdk.probe(options) } // ============================================================================ // Utility Functions // ============================================================================ /** * Get Capacitor configuration * * @example * ```typescript * const config = await getCapacitorConfig() * if (config) { * console.log(config.appId) * } * ``` */ export async function getCapacitorConfig() { try { return await getConfig() } catch { return null } } // ============================================================================ // Re-export useful types // ============================================================================ export type { BuildCredentials } from './build/request' export type { BuildZipResult } from './build/zip-command' export type { CapacitorConfig } from './config' export type { ProbeInternalResult } from './probe' export type { AccountIdOptions, AddAppOptions, AddChannelOptions, AddOrganizationOptions, AppInfo, BundleCompatibilityOptions, BundleInfo, BuildMonitorOptions, CleanupOptions, CurrentBundleOptions, DecryptBundleOptions, DeleteOldKeyOptions, DeleteOrganizationOptions, DeviceStats, DoctorOptions, EncryptBundleOptions, GenerateKeyOptions, GetAccountOptions, GetStatsOptions, InviteOrganizationMemberOptions, ListOrganizationsOptions, LoginOptions, ObserveOptions, OrganizationInfo, ProbeOptions, PromoteChannelOptions, RemoveOrganizationMemberOptions, RequestBuildOptions, SaveKeyOptions, SDKResult, SetSettingOptions, StarAllRepositoriesOptions, StarRepoOptions, StatsOrder, UpdateAccountOptions, UpdateAppOptions, UpdateChannelOptions, UpdateOrganizationMemberRoleOptions, UpdateOrganizationOptions, UploadOptions, UploadResult, ZipBuildOptions, ZipBundleOptions, } from './schemas/sdk' export type { AccountProfile } from './schemas/account' export type { AppRow, AppVersionRow, ChannelRow, Organization } from './types/api' export { createCapgoClient, withCapgoHosts } from './utils' export { formatApiErrorForCli, getSecurityPolicyMessage, isSecurityPolicyError, parseSecurityPolicyError, SECURITY_POLICY_ERRORS, SECURITY_POLICY_MESSAGES, } from './utils/security_policy_errors' export type { ParsedSecurityError, SecurityPolicyErrorCode } from './utils/security_policy_errors'