Api Class
Api Class
Section titled “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.
Instantiation
Section titled “Instantiation”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 classProperties
Section titled “Properties”- Type:
AxiosInstance - Description: A configured instance of
axios. This instance already comes with thebaseURLset tohttps://undernouzen.shopand anAuthorizationheader containing theApiTokenobtained fromcredentials.js. It is the basis for all HTTP requests made by theApiclass.
LanguageId (setter)
Section titled “LanguageId (setter)”api.LanguageId = 'es_es';- Description: Sets the preferred language for the API’s response messages. This value is included in requests as the
_langparameter. - Parameters:
value(string): The language code (e.g.,en_us,pt_br,es_es).
Methods
Section titled “Methods”The methods of the Api class provide the functionalities to interact with the specific endpoints of the UnderDeck REST API.
UserLogin(Username, Password)
Section titled “UserLogin(Username, Password)”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
Userobject 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>AUserobject if the login is successful,falseif the API returns an authentication failure, ornullin case of a request error.
UserAuth(Token)
Section titled “UserAuth(Token)”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>AUserobject if the authentication is successful,falseif the API returns an authentication failure, ornullin case of a request error.
Request(Data)
Section titled “Request(Data)”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
FormDatato 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 aFormData. If a value is an object withtype: 'file',data(Blob), andname(file name), it will be treated as a file for upload.
- Returns:
Promise<AxiosResponse>The complete response of the HTTP request, as returned by theaxiosinstance.