MSDN Documentation

Networking API Reference

This section details the APIs available for managing network operations within the MSDN platform. These APIs allow your applications to communicate with remote services, exchange data, and manage network connectivity.

NetworkClient Class

The NetworkClient class provides a high-level interface for performing HTTP requests.

Methods

  • GET(url, options)

    Performs an HTTP GET request.

    Parameters
    Name Type Description
    url string The URL to request.
    options object (optional) Configuration options for the request.
    Returns

    A Promise that resolves with the response object.

    Example Usage

    
    import { NetworkClient } from '@msdn/networking';
    
    const client = new NetworkClient();
    
    client.GET('/api/users')
        .then(response => {
            console.log('Users:', response.data);
        })
        .catch(error => {
            console.error('Failed to fetch users:', error);
        });
                                
  • POST(url, data, options)

    Performs an HTTP POST request.

    Parameters
    Name Type Description
    url string The URL to request.
    data any The data to send in the request body.
    options object (optional) Configuration options for the request.
    Returns

    A Promise that resolves with the response object.

  • PUT(url, data, options)

    Performs an HTTP PUT request.

    Parameters
    Name Type Description
    url string The URL to request.
    data any The data to send in the request body.
    options object (optional) Configuration options for the request.
    Returns

    A Promise that resolves with the response object.

  • DELETE(url, options)

    Performs an HTTP DELETE request.

    Parameters
    Name Type Description
    url string The URL to request.
    options object (optional) Configuration options for the request.
    Returns

    A Promise that resolves with the response object.

Request Options

The options object can include the following properties:

Name Type Default Description
headers object {} Custom headers to send with the request.
timeout number 30000 Request timeout in milliseconds.
responseType string 'json' Expected response type (e.g., 'json', 'text', 'blob').

Response Object

The response object returned by the network methods contains the following properties:

Name Type Description
data any The parsed response data.
status number The HTTP status code.
statusText string The HTTP status text.
headers object The response headers.

Error Handling

Network requests can fail for various reasons, such as network errors, timeouts, or server-side issues. The Promises returned by NetworkClient methods will reject with an error object. This object typically includes:

  • message: A descriptive error message.
  • status: The HTTP status code if the error is server-related.
  • response: The partial response object if available.

Advanced Usage: Uploading Files

For more complex scenarios like file uploads, you can leverage the underlying Fetch API directly or use specific utility functions provided by MSDN's file handling modules.


// Example of a POST request with a file (simplified)
async function uploadFile(file) {
    const formData = new FormData();
    formData.append('file', file);

    try {
        const response = await fetch('/api/upload', {
            method: 'POST',
            body: formData
        });
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        const result = await response.json();
        console.log('Upload successful:', result);
        return result;
    } catch (error) {
        console.error('File upload failed:', error);
    }
}