A library to handle the execution of HTTP requests with configurable options. Implements the Integration
interface from @rabid/pipeline-manager
.
npm install @rabid/pmi-http-request
import { HttpRequest } from '@rabid/pmi-http-request';
Create an instance of HttpRequest
by passing a configuration object containing the base URL, default headers, and response handler.
const config = {
baseUrl: 'https://api.example.com',
defaultHeaders: {
'Content-Type': 'application/json'
},
defaultResponseHandler: 'json'
};
const httpRequest = new HttpRequest(config);
Execute an HTTP request by calling the execute
method with the input object containing the method, path, headers, query, and body.
const input = {
method: 'POST',
path: '/data',
headers: {
'Authorization': 'Bearer token'
},
query: {
key: 'value'
},
body: JSON.stringify({ data: 'example' })
};
httpRequest.execute(input).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
Here is a complete example demonstrating how to use the HttpRequest
class:
import { HttpRequest } from '@rabid/pmi-http-request';
const config = {
baseUrl: 'https://api.example.com',
defaultHeaders: {
'Content-Type': 'application/json'
},
defaultResponseHandler: 'json'
};
const httpRequest = new HttpRequest(config);
const input = {
method: 'POST',
path: '/data',
headers: {
'Authorization': 'Bearer token'
},
query: {
key: 'value'
},
body: JSON.stringify({ data: 'example' })
};
httpRequest.execute(input).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});