Skip to content

Api Class

The Api class in UnderDeckLib is the abstraction layer responsible for all communications with the UnderDeck REST API. It uses the axios library to perform HTTP requests, ensuring efficient and standardized interaction with backend services. This class handles the configuration of base URLs, authorization headers, and the processing of request parameters, including file uploads.

By centralizing API calls, the Api class simplifies development, allowing the UnderDeck class and other parts of the library to focus on business logic without worrying about the low-level details of HTTP communication.

The Api class is instantiated internally by the UnderDeck class and generally does not need to be instantiated directly by the library user. You access its functionalities through the Api property of the UnderDeck instance:

import UnderDeck from 'underdecklib';
const client = new UnderDeck();
const api = client.Api; // Accessing the instance of the Api class
  • Type: AxiosInstance
  • Description: A configured instance of axios. This instance already comes with the baseURL set to https://undernouzen.shop and an Authorization header containing the ApiToken obtained from credentials.js. It is the basis for all HTTP requests made by the Api class.
api.LanguageId = 'es_es';
  • Description: Sets the preferred language for the API’s response messages. This value is included in requests as the _lang parameter.
  • Parameters:
    • value (string): The language code (e.g., en_us, pt_br, es_es).

The methods of the Api class provide the functionalities to interact with the specific endpoints of the UnderDeck REST API.

const user = await api.UserLogin("my_user", "my_password");
if (user) {
console.log("Login successful for:", user.username);
}
  • Description: Performs the login process for a user on the UnderDeck platform. Sends the provided credentials to the API and, upon success, returns a User object with the authenticated user’s data.
  • Parameters:
    • Username (string): The user’s username or email.
    • Password (string): The user’s password.
  • Returns: Promise<Models.User|boolean|null> A User object if the login is successful, false if the API returns an authentication failure, or null in case of a request error.
const user = await api.UserAuth("my_authentication_token");
if (user) {
console.log("Authentication successful for:", user.username);
}
  • Description: Authenticates a user on the UnderDeck platform using a pre-existing authentication token. This method is useful for maintaining user sessions or for token-based authentication.
  • Parameters:
    • Token (string): The user’s authentication token.
  • Returns: Promise<Models.User|boolean|null> A User object if the authentication is successful, false if the API returns an authentication failure, or null in case of a request error.
const response = await api.Request({
Method: 'GetPlugins',
ClientToken: 'your_client_token'
});
console.log("API Response:", response.data);
// Example with file upload
// const fileBuffer = Buffer.from("file content");
// const fileData = {
// type: 'file',
// data: new Blob([fileBuffer]),
// name: 'my_file.txt'
// };
// const uploadResponse = await api.Request({
// Method: 'UploadFile',
// ClientToken: 'your_client_token',
// file: fileData
// });
// console.log("Upload response:", uploadResponse.data);
  • Description: A generic and flexible method for making requests to the UnderDeck API. It allows sending arbitrary data to different API endpoints, including support for file uploads. The method constructs a FormData to send the data, which is essential for requests involving files.
  • Parameters:
    • Data (object): An object containing the request parameters. The keys and values of this object will be transformed into fields of a FormData. If a value is an object with type: 'file', data (Blob), and name (file name), it will be treated as a file for upload.
  • Returns: Promise<AxiosResponse> The complete response of the HTTP request, as returned by the axios instance.