import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType, } from "@superblocksteam/util"; import { fetchApi, fetchApiCommits, fetchApis, fetchApplication, fetchApplicationBranches, fetchApplicationCommits, fetchApplications, fetchApplicationWithComponents, fetchCurrentUser, pushApi, PushApiWithCommitConfig, pushApplication, PushMultiPageApplicationWithCommitConfig, registerComponents, uploadComponents, validateGitSetup, deployApplication, deployApi, } from "./client"; import { FeatureFlags } from "./flag"; import { UserMeDto, ViewMode } from "./types"; // Exporting here instead of index.ts because only sdk.ts is exposed outside in package.json export * from "./errors"; export class SuperblocksSdk { token = ""; superblocksBaseUrl = ""; cliVersion = ""; constructor( accessToken: string, superblocksBaseUrl: string, cliVersion: string, ) { this.token = accessToken; this.superblocksBaseUrl = superblocksBaseUrl; this.cliVersion = cliVersion; } async getFeatureFlagsForCurrentUser(): Promise { const user = await this.fetchCurrentUser(); return new FeatureFlags(user.flagBootstrap); } getFeatureFlagsForUser(user: UserMeDto): FeatureFlags { return new FeatureFlags(user.flagBootstrap); } async fetchApplication({ applicationId, branch, headers = {}, viewMode = "export-live", skipSigningVerification = false, }: { applicationId: string; branch?: string; headers?: Record; viewMode?: ViewMode; skipSigningVerification?: boolean; }) { return fetchApplication({ cliVersion: this.cliVersion, applicationId, branch, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, viewMode, skipSigningVerification, injectedHeaders: headers, }); } async fetchApplicationBranches(applicationId: string, headers = {}) { return fetchApplicationBranches({ cliVersion: this.cliVersion, applicationId, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, injectedHeaders: headers, }); } async fetchApplicationWithComponents({ applicationId, branch, headers = {}, viewMode = "export-live", commitId, skipSigningVerification = false, }: { applicationId: string; branch: string; headers?: Record; viewMode?: ViewMode; commitId?: string; skipSigningVerification?: boolean; }) { return fetchApplicationWithComponents({ cliVersion: this.cliVersion, applicationId, branch, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, viewMode, skipSigningVerification, commitId, injectedHeaders: headers, }); } async fetchApplicationCommits({ applicationId, branch, headers = {}, limit, offset, }: { applicationId: string; branch: string; headers?: Record; limit?: number; offset?: number; }) { return fetchApplicationCommits({ cliVersion: this.cliVersion, applicationId, branch, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, injectedHeaders: headers, limit, offset, }); } async fetchApiCommits({ apiId, branch, headers = {}, limit, offset, }: { apiId: string; branch: string; headers?: Record; limit?: number; offset?: number; }) { return fetchApiCommits({ cliVersion: this.cliVersion, applicationId: apiId, branch, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, injectedHeaders: headers, limit, offset, }); } async fetchApplications(headers?: Record) { return fetchApplications( this.cliVersion, this.token, this.superblocksBaseUrl, headers, ); } async fetchApi({ apiId, branch, viewMode = "export-live", commitId, skipSigningVerification = false, }: { apiId: string; viewMode?: ViewMode; branch?: string; commitId?: string; skipSigningVerification?: boolean; }) { return fetchApi( this.cliVersion, apiId, this.token, this.superblocksBaseUrl, viewMode, branch, commitId, skipSigningVerification, ); } async fetchApis() { return fetchApis(this.cliVersion, this.token, this.superblocksBaseUrl); } async validateGitSetup( resourceId: string, resourceType: SuperblocksResourceType, event: ComponentEvent, localGitRepoState: LocalGitRepoState, injectedHeaders?: Record, ) { return validateGitSetup( this.cliVersion, resourceId, resourceType, event, localGitRepoState, this.token, this.superblocksBaseUrl, injectedHeaders, ); } async registerComponents( applicationId: string, componentConfigs: Record, branch: string | null, injectedHeaders: Record, ) { return registerComponents( this.cliVersion, applicationId, componentConfigs, this.token, this.superblocksBaseUrl, branch, injectedHeaders, ); } async uploadComponents( applicationId: string, componentConfigs: Record, files: string[], branch: string | null, ) { return uploadComponents({ cliVersion: this.cliVersion, applicationId, componentConfigs, files, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, branch, }); } async fetchCurrentUser() { return fetchCurrentUser( this.cliVersion, this.token, this.superblocksBaseUrl, ); } async pushApplication({ applicationId, applicationConfig, branch, headers = {}, }: { applicationId: string; applicationConfig: PushMultiPageApplicationWithCommitConfig; branch: string; headers?: Record; }) { return pushApplication({ cliVersion: this.cliVersion, applicationId, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, branch, injectedHeaders: headers, applicationConfig, }); } async pushApi({ apiId, apiConfig, branch, headers = {}, }: { apiId: string; apiConfig: PushApiWithCommitConfig; branch: string; headers?: Record; }) { return pushApi({ cliVersion: this.cliVersion, apiId, apiConfig, token: this.token, superblocksBaseUrl: this.superblocksBaseUrl, branch, injectedHeaders: headers, }); } async deployApplication(applicationId: string, commitId?: string) { return deployApplication( this.cliVersion, applicationId, this.token, this.superblocksBaseUrl, commitId, ); } async deployApi(apiId: string, commitId?: string) { return deployApi( this.cliVersion, apiId, this.token, this.superblocksBaseUrl, commitId, ); } }