The ModernAPIClient class is designed to simplify API interactions in a web environment. It supports making GET and POST requests, including handling file uploads and securely managing request parameters.
const client = new ModernAPIClient('https://api.example.com');
When the ModernAPIClient is instantiated, it automatically decrypts and stores certain values (e.g., MemOptimizeMethod and MemStackFrame) that are used to secure API requests. These values are expected to be present as Base64-encoded hidden fields in the HTML page.
postAction(controller, action, parameters, secure = false)Sends a POST request to a specific controller and action within the API.
Parameters:
controller (string): The controller name.action (string): The action name.parameters (object): An object containing key-value pairs to be sent as the POST body.secure (boolean): If true, the request will be secured with additional parameters.Returns: A Promise that resolves with the response data.
const parameters = { param1: 'value1' };
client.postAction('exampleController', 'exampleAction', parameters)
.then(response => console.log('POST Action Result:', response))
.catch(error => console.error('Error in POST Action:', error));
postFileAction(controller, action, fileName, secure = false)Sends a POST request to upload a file to a specific controller and action within the API.
Parameters:
controller (string): The controller name.action (string): The action name.fileName (string): The file to be uploaded.secure (boolean): If true, the request will be secured with additional parameters.Returns: A Promise that resolves with the response data.
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
client.postFileAction('uploadController', 'uploadAction', file)
.then(response => console.log('File Upload Result:', response))
.catch(error => console.error('Error in File Upload:', error));
getAction(controller, action, parameters, secure = false)Sends a GET request to a specific controller and action within the API.
Parameters:
controller (string): The controller name.action (string): The action name.parameters (object): An object containing key-value pairs to be sent as query string parameters.secure (boolean): If true, the request will be secured with additional parameters.Returns: A Promise that resolves with the response data.
const parameters = { param1: 'value1' };
client.getAction('exampleController', 'exampleAction', parameters)
.then(response => console.log('GET Action Result:', response))
.catch(error => console.error('Error in GET Action:', error));
getStatic(staticRoute, parameters, secure = false)Sends a GET request to a specific static route within the API.
Parameters:
staticRoute (string): The static route.parameters (object): An object containing key-value pairs to be sent as query string parameters.secure (boolean): If true, the request will be secured with additional parameters.Returns: A Promise that resolves with the response data.
const parameters = { param1: 'value1' };
client.getStatic('/staticRoute', parameters)
.then(response => console.log('GET Static Result:', response))
.catch(error => console.error('Error in GET Static:', error));
For secure API interactions, set the secure parameter to true in any of the methods. This will add additional parameters, such as a timestamp and values from decrypted memory, to the request.
// Initialize the client with the base API URL
const client = new ModernAPIClient('https://api.example.com');
// Perform a secure POST request
const postParams = { param1: 'value1', param2: 'value2' };
client.postAction('myController', 'myAction', postParams, true)
.then(response => console.log('POST Response:', response))
.catch(error => console.error('POST Error:', error));
// Perform a secure GET request
const getParams = { param1: 'value1' };
client.getAction('myController', 'myGetAction', getParams, true)
.then(response => console.log('GET Response:', response))
.catch(error => console.error('GET Error:', error));
The ModernAPIClient class is a powerful tool for interacting with RESTful APIs in a web environment. It offers flexibility with its secure request options and supports a variety of request types, including file uploads. By following the examples provided, you can easily integrate this class into your web applications.