1.17.0 • Published 2 years ago

@fengcart/http v1.17.0

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

@fengcart/http

HTTP client implementations with standardized request success/error logging.

Usage

yarn add @fengcart/http

Implementations

RequestClient (deprecated)

Our RequestClient implementation is deprecated since the underlying request package itself was deprecated. Use the axiosCreate function instead.

As of Feb 11th 2020, request is fully deprecated. No new changes are expected to land. In fact, none have landed for some time. This package is also deprecated because it depends on request.

https://github.com/request/request-promise#deprecated

AxiosClient (deprecated)

AxiosClient is a custom HTTP client built around the axios package. This implementation is deprecated in favor of the axiosCreate factory function.

The AxiosClient provides only a single method: request(config: AxiosRequestConfig).

const client: AxiosClient = new AxiosClient({ baseURL: 'https://www.bk.com', timeout: 1000 });

// GET https://www.bk.com/faq
const result = await client.request({
  method: 'GET',
  pathParams: { example: 'faq' }
  responseType: 'document',
  templatizedPath: '/:example',
});

axiosCreate

The axiosCreate function is a small wrapper around the standard axios.create factory function. This function returns a new axios instance with our standardized logging interceptors applied.

const client: AxiosInstance = axiosCreate({ baseURL: 'https://www.bk.com', timeout: 1000 });

// GET https://www.bk.com/faq
const result = await client.get('/faq', { responseType: 'document' });

HTTP request logging

HTTP requests made with @fengcart/http clients will log response outcome and relevant metadata. This logging is critically important for observability, and is also useful for quickly identifying vendor API service degredations.

HTTP requests that return a successful response will automatically log "HTTP_REQUEST_SUCCESS".

INFO	HTTP_REQUEST_SUCCESS
{
  "level": 30,
  "level_label": "info",
  "time": 1629991203772,
  "brand": "bk",
  "stage": "prod",
  "name": "sls-rbi-prod-bk-whitelabel-graphql",
  "awsRequestId": "example-aws-request-id",
  "apiRequestId": "example-api-request-id",
  "x-correlation-trace-id": "example-correlation-trace-id",
  "x-correlation-id": "example-correlation-id",
  "request": {
    "host": "www.bk.com",
    "method": "GET",
    "path": "/faq",
    "protocol": "https",
    "templatizedPath": "/:example"
  },
  "response": {
    "status": 200,
    "statusCategory": "2XX"
  },
  "retryCurrentAttempt": 1,
  "success": true,
  "uri": "https://www.bk.com/faq",
  "msg": "HTTP_REQUEST_SUCCESS"
}

HTTP requests that throw an error will automatically log "HTTP_REQUEST_ERROR".

WARN	HTTP_REQUEST_FAILURE
{
  "level": 40,
  "level_label": "warn",
  "time": 1629991646729,
  "brand": "bk",
  "stage": "qa",
  "name": "sls-rbi-prod-bk-whitelabel-graphql",
  "awsRequestId": "example-aws-request-id",
  "apiRequestId": "example-api-request-id",
  "x-correlation-trace-id": "example-correlation-trace-id",
  "x-correlation-id": "example-correlation-id",
  "metadata": {},
  "request": {
    "method": "GET",
    "templatizedPath": "/api/v2/menu-service/plus/:storeId",
    "host": "use1-prod-bk.rbictg.com",
    "path": "/api/v2/menu-service/plus/21011",
    "protocol": "https"
  },
  "response": {
    "body": {
      "error": "no PLUs data found for 21011"
    },
    "headers": {
      "content-type": "application/json",
      "content-length": "40",
      "connection": "close",
      "date": "Thu, 26 Aug 2021 15:27:26 GMT",
      "x-amzn-requestid": "exmaple-aws-request-id",
      "x-amz-apigw-id": "example-aws-apig-id",
      "x-amzn-trace-id": "example-aws-trace-id",
    },
    "status": 404,
    "statusCategory": "4XX"
  },
  "retryCurrentAttempt": 0,
  "success": false,
  "uri": "https://use1-prod-BK.rbictg.com/api/v2/menu-service/plus/21011",
  "msg": "HTTP_REQUEST_FAILURE"
}