ModernAPIClient Documentation

Overview

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.

Initialization

const client = new ModernAPIClient('https://api.example.com');

Memory Optimization and Security

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.

Methods

1. postAction(controller, action, parameters, secure = false)

Sends a POST request to a specific controller and action within the API.

Parameters:

Returns: A Promise that resolves with the response data.

Example:

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));

2. postFileAction(controller, action, fileName, secure = false)

Sends a POST request to upload a file to a specific controller and action within the API.

Parameters:

Returns: A Promise that resolves with the response data.

Example:

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));

3. getAction(controller, action, parameters, secure = false)

Sends a GET request to a specific controller and action within the API.

Parameters:

Returns: A Promise that resolves with the response data.

Example:

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));

4. getStatic(staticRoute, parameters, secure = false)

Sends a GET request to a specific static route within the API.

Parameters:

Returns: A Promise that resolves with the response data.

Example:

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));

Secure Requests

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.

Example Workflow

// 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));

Conclusion

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.