@alliedpayment/http-client

HTTP client for Allied REST api.

Usage no npm install needed!

<script type="module">
  import alliedpaymentHttpClient from 'https://cdn.skypack.dev/@alliedpayment/http-client';
</script>

README

http-client

HTTP client for Allied REST api.

Public / Private Key Required.

Usage

Basic

const HttpClient = require("@alliedpayment/http-client");
const client = new HttpClient();

const main = async () => {
  try {
    const res = await client.get("url/to/resource");
    console.log(`response status code ${res.status}`);
    return res.data; // response body
  } catch (error) {
    console.log("failed to get resource", error);
  }
};

main();

Request Options

// underlying client is axios and config object is publicly available to support all axios supported request configs
// https://axios-http.com/docs/req_config

// example
const options = {
  timeout: 1000 * 30, // 30 second/s request timeout
  headers: { cookie: "smoke=true" },
};
const res = await client.get("url/to/resource", options);

Request Delegates

You can pass a delegate function to wrap around the http request.

const wrapper = async (promise) => {
  return await promise; // do nothing delegate
};
const res = await client.get("url/to/resource", options, wrapper);

Custom Response Parser

const customParser = async (promise) => {
  try {
    const result = await promise;
    return `parse result for one: ${result.data.one}`;
  } catch (err) {
    log.error(err);
    return err.message;
  }
};

const res = await client.get("url/to/resource", options, customParser);

Custom Logging

const customLogger = async (promise) => {
  console.log("i get logged before the request");
  const result = await promise;
  console.log("i get logged after the request", result);
  return result;
};

const res = await client.get("url/to/resource", options, customLogger);

Custom Error Handling

const customErrorHandling = async (promise) => {
  try {
    console.log("i can try and catch exceptions and handle errors as needed");
    return await promise;
  } catch (err) {
    log.error(err);
    return null;
  }
};

const res = await client.get("url/to/resource", options, customErrorHandling);