@wistle/http-provider
v1.0.0
Published
This package is to handle http provider to make rest api
Downloads
7
Readme
@wistle/http-provider
HttpRequestProvider
HttpRequestProvider
is a flexible utility for making HTTP requests in Node.js using Axios. It supports advanced logging, handling PCI-sensitive information, configurable headers, and adding query parameters. It provides full control over request options and the ability to log requests and responses with different levels of verbosity.
Features
- Supports
GET
,POST
,PUT
,DELETE
, and other HTTP methods. - Handles logging for both sensitive (PCI) and non-sensitive data.
- Adds custom headers like
Content-Type
oroperation_id
. - Supports query parameters handling.
- Can resolve with the full response or just the
data
field. - Catches and logs errors properly.
Installation
npm install @wistle/http-provider
Usage
const { HttpRequest, HttpRequestProvider } = require('./dist');
// Mock logger to display log messages in the console
global['Logger'] = {
verbose: console.log,
safeVerbose: console.log,
error: console.error,
safeError: console.error,
info: console.info,
};
(async () => {
const operation_id = 'op123';
try {
// Example: POST request
const postRequest = new HttpRequest({
method: 'POST',
url: 'https://jsonplaceholder.typicode.com/posts',
data: { title: 'foo', body: 'bar', userId: 1 },
headers: { 'Content-Type': 'application/json' },
json: true,
});
console.log(`Making POST request to ${postRequest.url}`);
const postResponse = await HttpRequestProvider.makeRequest(operation_id, postRequest);
console.log('POST Response:', postResponse);
// Example: GET request with query parameters
const queryRequest = new HttpRequest({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/comments',
query: { postId: 1 },
json: true,
});
console.log(`Making GET request with query params to ${queryRequest.url}`);
const queryResponse = await HttpRequestProvider.makeRequest(operation_id, queryRequest);
console.log('GET with Query Params Response:', queryResponse);
} catch (error) {
console.error('Error occurred while making HTTP requests:', error);
}
})();
API
HttpRequestProvider.makeRequest(operation_id, options)
operation_id
: A unique ID to track requests for logging purposes.options
: An object with the following properties:method
: HTTP method (GET
,POST
,PUT
, etc.).url
: The request URL.data
: The request body (forPOST
,PUT
requests).headers
: Optional custom headers.query
: Object representing query parameters.json
: Boolean flag indicating if theContent-Type
should beapplication/json
.is_pci
: Boolean flag indicating if the request involves sensitive (PCI) data.is_internal_request
: Boolean flag to add internal headers.resolve_with_full_response
: Boolean flag to resolve the full Axios response or just thedata
field.
Example of HttpRequest
const getRequest = new HttpRequest({
method: 'GET',
url: 'https://jsonplaceholder.typicode.com/posts/1',
json: true,
});
Logging
HttpRequestProvider
uses a global Logger
object for logging. You can mock this logger in development or testing, or replace it with your logging framework in production.
Logger Methods
Logger.verbose()
Logger.safeVerbose()
Logger.error()
Logger.safeError()
Logger.info()
Error Handling
If the request fails, an error is thrown with detailed logs, including the status code and response from the server. If no response is available, a generic error will be thrown.
try {
const response = await HttpRequestProvider.makeRequest(operation_id, request);
} catch (error) {
console.error('Request failed:', error);
}
License
This project is licensed under the MIT License - see the [LICENSE] file for details.