13.1.4 • Published 2 years ago

@alliedpayment/http-client v13.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

http-client

HTTP client for Allied REST api.

Public / Private Key Required.

Usage

Basic

import client from '@alliedpayment/http-client';
const client = require("@alliedpayment/http-client");

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

Proxy

const client = require("@alliedpayment/http-client");
client.options.proxy =  {
    host: 'localhost',
    port: 8888,
    protocol: 'http',
  },
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);
13.1.4

2 years ago

10.0.0

2 years ago

13.0.4

2 years ago

13.1.1

2 years ago

13.1.2

2 years ago

13.0.3

2 years ago

13.1.0

2 years ago

13.0.1

2 years ago

7.2.0

2 years ago

7.1.0

2 years ago

12.0.0

2 years ago

8.0.0

2 years ago

12.0.1

2 years ago

11.0.0

2 years ago

11.0.1

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.0.0

2 years ago

3.0.0

2 years ago

7.0.0

2 years ago

7.0.1

2 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.4.2

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago