Documentation
Class: API
Description:
This class provides a set of functions for interacting with the application's backend API. It uses the axios
library for making HTTP requests and socket.io-client
for establishing a WebSocket connection.
Methods
Method: authorize
Description: Authenticates the user with a specific provider (GitHub, GitLab, or Bitbucket).
Parameters:
provider
(string
): The provider to authenticate with.payload
(object
): An object containing authentication details specific to the provider.
Returns:
Promise<object>
: A promise that resolves with the authentication response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the authentication process fails.
Example:
const provider = "github";
const payload = {
username: "your_username",
password: "your_password"
};
API.authorize(provider, payload)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: head
Description: Fetches the latest head information from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the head data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.head()
.then(headData => {
console.log(headData);
})
.catch(error => {
console.error(error.message);
});
Method: getPipelines
Description: Retrieves a list of pipelines from the API based on a provided configuration object.
Parameters:
config
(object
): An object containing filtering parameters for the pipelines.
Returns:
Promise<object>
: A promise that resolves with the list of pipelines from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const config = {
branch: "master",
status: "running"
};
API.getPipelines(config)
.then(pipelines => {
console.log(pipelines);
})
.catch(error => {
console.error(error.message);
});
Method: getUser
Description: Fetches the currently authenticated user's information from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the user data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getUser()
.then(userData => {
console.log(userData);
})
.catch(error => {
console.error(error.message);
});
Method: getPipeline
Description: Fetches a single pipeline from the API based on its ID and an optional configuration object.
Parameters:
pipeId
(string
): The ID of the pipeline to retrieve.config
(object
): An optional configuration object containing filtering parameters.idx
(number | null
): An optional index for the pipeline. Default:null
.
Returns:
Promise<object>
: A promise that resolves with the pipeline data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const pipeId = "1234567890";
const config = {
branch: "master"
};
API.getPipeline(pipeId, config)
.then(pipeline => {
console.log(pipeline);
})
.catch(error => {
console.error(error.message);
});
Method: stopPipeline
Description: Stops a pipeline based on its ID.
Parameters:
pipeId
(string
): The ID of the pipeline to stop.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const pipeId = "1234567890";
API.stopPipeline(pipeId)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: hasRepositories
Description: Checks if the authenticated user has any repositories associated with their account.
Parameters:
- None
Returns:
Promise<boolean>
: A promise that resolves totrue
if the user has repositories,false
otherwise, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.hasRepositories()
.then(hasRepos => {
console.log(hasRepos);
})
.catch(error => {
console.error(error.message);
});
Method: getRepositories
Description: Fetches a list of repositories from the API based on an optional configuration object.
Parameters:
config
(object
): An optional configuration object containing filtering parameters for the repositories.
Returns:
Promise<object>
: A promise that resolves with the list of repositories from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const config = {
name: "my_repo"
};
API.getRepositories(config)
.then(repositories => {
console.log(repositories);
})
.catch(error => {
console.error(error.message);
});
Method: getRepository
Description: Fetches a single repository from the API based on its ID and an optional configuration object.
Parameters:
repoId
(string
): The ID of the repository to retrieve.config
(object
): An optional configuration object containing filtering parameters.idx
(number | null
): An optional index for the repository. Default:null
.
Returns:
Promise<object>
: A promise that resolves with the repository data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repoId = "1234567890";
const config = {
branch: "master"
};
API.getRepository(repoId, config)
.then(repository => {
console.log(repository);
})
.catch(error => {
console.error(error.message);
});
Method: getRepoLanguages
Description: Fetches the languages used in a repository.
Parameters:
repoId
(string
): The ID of the repository.
Returns:
Promise<object>
: A promise that resolves with the languages data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repoId = "1234567890";
API.getRepoLanguages(repoId)
.then(languages => {
console.log(languages);
})
.catch(error => {
console.error(error.message);
});
Method: updateRepo
Description: Updates a repository with new information.
Parameters:
repo
(object
): The repository object containing the updated information.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const updatedRepo = {
id: "1234567890",
name: "my_updated_repo"
};
API.updateRepo(updatedRepo)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: updateSchedule
Description: Updates the schedule for a repository.
Parameters:
repo
(object
): The repository object.schedule
(object
): The schedule object containing the new scheduling information.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repo = {
id: "1234567890"
};
const schedule = {
cron: "* * * * *"
};
API.updateSchedule(repo, schedule)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: getPlan
Description: Fetches the current user's plan information.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the plan data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getPlan()
.then(plan => {
console.log(plan);
})
.catch(error => {
console.error(error.message);
});
Method: getPlanUser
Description: Fetches the user associated with a specific plan.
Parameters:
plan_id
(string
): The ID of the plan.
Returns:
Promise<object>
: A promise that resolves with the user data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const planId = "1234567890";
API.getPlanUser(planId)
.then(user => {
console.log(user);
})
.catch(error => {
console.error(error.message);
});
Method: getRemoteRepo
Description: Fetches a repository from a remote provider.
Parameters:
repoId
(string
): The ID of the repository.branches
(boolean
): Whether to include branch information.
Returns:
Promise<object>
: A promise that resolves with the remote repository data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repoId = "1234567890";
const branches = true;
API.getRemoteRepo(repoId, branches)
.then(repo => {
console.log(repo);
})
.catch(error => {
console.error(error.message);
});
Method: updateBranchStatus
Description: Updates the status of branches for a repository.
Parameters:
repoId
(string
): The ID of the repository.branches
(object
): An object containing branch status information.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repoId = "1234567890";
const branches = {
master: "active",
develop: "inactive"
};
API.updateBranchStatus(repoId, branches)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: getDocumentFile
Description: Fetches a document file from a repository.
Parameters:
repoId
(string
): The ID of the repository.file
(string
): The name of the file.ref
(string
): The reference (branch, tag, or commit) to retrieve the file from.
Returns:
Promise<object>
: A promise that resolves with the document file data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repoId = "1234567890";
const file = "README.md";
const ref = "master";
API.getDocumentFile(repoId, file, ref)
.then(fileData => {
console.log(fileData);
})
.catch(error => {
console.error(error.message);
});
Method: getRepoFiles
Description: Fetches a list of files from a repository, optionally specifying a reference, reference type (hash, tag, or branch), and whether to recursively list files in subfolders.
Parameters:
repo
(object
): The repository object.ref
(string
): The reference (branch, tag, or commit) to retrieve files from.refType
(string
): The type of reference. Default:"hash"
.recursive
(boolean
): Whether to recursively list files in subfolders. Default:true
.
Returns:
Promise<object>
: A promise that resolves with the list of files from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const repo = {
id: "1234567890"
};
const ref = "master";
const refType = "branch";
const recursive = false;
API.getRepoFiles(repo, ref, refType, recursive)
.then(files => {
console.log(files);
})
.catch(error => {
console.error(error.message);
});
Method: getLogs
Description: Fetches logs from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the logs data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getLogs()
.then(logs => {
console.log(logs);
})
.catch(error => {
console.error(error.message);
});
Method: getStats
Description: Fetches statistics from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the stats data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getStats()
.then(stats => {
console.log(stats);
})
.catch(error => {
console.error(error.message);
});
Method: getMagicLink
Description: Sends a magic link to the user's email address for authentication.
Parameters:
elements
(object
): An object containing the user's login email.
Returns:
Promise<object>
: A promise that resolves with the magic link response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const elements = {
login_email: {
value: "your_email@example.com"
}
};
API.getMagicLink(elements)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: verifyMagicLink
Description: Verifies a magic link token.
Parameters:
token
(string
): The magic link token.
Returns:
Promise<object>
: A promise that resolves with the verification response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const token = "your_magic_link_token";
API.verifyMagicLink(token)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: connectProvider
Description: Connects a provider to the user's account.
Parameters:
request
(object
): An object containing the connection request details.provider
(string
): The provider to connect. Default:Provider.GITHUB
.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const request = {
// connection details
};
const provider = Provider.GITLAB;
API.connectProvider(request, provider)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: sendEmail
Description: Sends an email using the API.
Parameters:
payload
(object
): The email payload containing the recipient, subject, and message.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const payload = {
recipient: "recipient_email@example.com",
subject: "Email Subject",
message: "Email Message"
};
API.sendEmail(payload)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: launchQuickstart
Description: Launches a quickstart using the API.
Parameters:
payload
(object
): The payload containing the quickstart request details.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const payload = {
// quickstart request details
};
API.launchQuickstart(payload)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: getAccessTokens
Description: Fetches access tokens for a specific provider.
Parameters:
provider
(string
): The provider to retrieve access tokens for.
Returns:
Promise<object>
: A promise that resolves with the access tokens data from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const provider = "github";
API.getAccessTokens(provider)
.then(tokens => {
console.log(tokens);
})
.catch(error => {
console.error(error.message);
});
Method: getLanguages
Description: Fetches a list of supported languages from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the list of languages from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getLanguages()
.then(languages => {
console.log(languages);
})
.catch(error => {
console.error(error.message);
});
Method: setAccountToken
Description: Sets an account token for a specific provider.
Parameters:
token
(string
): The account token.provider
(string
): The provider to set the token for.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const token = "your_account_token";
const provider = "github";
API.setAccountToken(token, provider)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: getProductDetails
Description: Retrieves details of Stripe products.
Parameters:
productIds
(array
): An array of product IDs.
Returns:
Promise<object>
: A promise that resolves with the product details from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const productIds = ["prod_1234567890", "prod_0987654321"];
API.getProductDetails(productIds)
.then(productDetails => {
console.log(productDetails);
})
.catch(error => {
console.error(error.message);
});
Method: getProductPricing
Description: Retrieves pricing information for Stripe products.
Parameters:
prices
(array
): An array of price IDs.quantity
(number
): The quantity of the product. Default:1
.
Returns:
Promise<object>
: A promise that resolves with the pricing information from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const prices = ["price_1234567890", "price_0987654321"];
const quantity = 2;
API.getProductPricing(prices, quantity)
.then(pricing => {
console.log(pricing);
})
.catch(error => {
console.error(error.message);
});
Method: createCheckoutSession
Description: Creates a Stripe checkout session.
Parameters:
type
(string
): The type of checkout session (e.g., "subscription").trial
(object
): The trial period information (if applicable).
Returns:
Promise<object>
: A promise that resolves with the checkout session ID from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const type = "subscription";
const trial = {
// trial period information
};
API.createCheckoutSession(type, trial)
.then(sessionId => {
console.log(sessionId);
})
.catch(error => {
console.error(error.message);
});
Method: getSessionStatus
Description: Retrieves the status of a Stripe checkout session.
Parameters:
sessionId
(string
): The ID of the checkout session.
Returns:
Promise<object>
: A promise that resolves with the session status from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const sessionId = "cs_1234567890";
API.getSessionStatus(sessionId)
.then(status => {
console.log(status);
})
.catch(error => {
console.error(error.message);
});
Method: getPlans
Description: Fetches a list of available plans from the API.
Parameters:
- None
Returns:
Promise<object>
: A promise that resolves with the list of plans from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
API.getPlans()
.then(plans => {
console.log(plans);
})
.catch(error => {
console.error(error.message);
});
Method: cancelPlan
Description: Cancels a plan.
Parameters:
planId
(string
): The ID of the plan to cancel.
Returns:
Promise<object>
: A promise that resolves with the response from the API, ornull
if there is an error.
Throws:
Error
: An error object if the API call fails.
Example:
const planId = "1234567890";
API.cancelPlan(planId)
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error.message);
});
Method: _api
Description: This is a private method used for making API requests. It handles the construction of the request, authorization, and error handling.
Parameters:
endpoint
(string
): The API endpoint to call.method
(string
): The HTTP method to use (e.g., "GET", "POST", "PUT", "DELETE"). Default:"GET"
.args
(object | null
): Optional arguments to be passed to the request. Default:null
.auth
(string
): The authorization header to use. Default:Bearer ${getJwt()}
.
Returns:
Promise<object>
: A promise that resolves with the response data from the API.
Throws:
Error
: An error object if the API call fails.
Example: (This method is private and not intended to be called directly by users.)
Important Considerations:
- Ensure that environment variables such as
REACT_APP_API_URL
,REACT_APP_GITHUB_CLIENT
, etc. are properly configured. - The
logout
function is called if an unauthorized (401) response is received. - The
_api
method provides basic error handling. Consider adding more specific error handling for different error codes.
Potential Pitfalls:
- Missing or incorrect configuration of environment variables can cause API calls to fail.
- Incorrect authorization headers can lead to unauthorized access errors.
- Network connectivity issues can disrupt API calls.
- The code relies on the availability of the
axios
andsocket.io-client
libraries. Make sure these are included in your project.
==========