@rabid/pmi-http-request
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

@rabid/pmi-http-request

A library to handle the execution of HTTP requests with configurable options. Implements the Integration interface from @rabid/pipeline-manager.

Installation

npm install @rabid/pmi-http-request

Usage

Importing the Library

import { HttpRequest } from '@rabid/pmi-http-request';

Creating an Instance

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

Executing an HTTP Request

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

Example

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

Readme

Keywords

none

Package Sidebar

Install

npm i @rabid/pmi-http-request

Weekly Downloads

17

Version

2.0.0

License

Affero GPL-3.0

Unpacked Size

8.45 kB

Total Files

5

Last publish

Collaborators

  • jmdiego