Giter Club home page Giter Club logo

koa-http-client's Introduction

@kasa/koa-http-client

A middleware that attachs HTTP client to communicate with the context during inter-service communications in Koa.


Communication between microservices must be efficient. With lots of microservices to complete a single request(transaction) successfully, each communication should have a context informations like the request id, content language, or something else. Koa HTTP Client will help inter-service communication with automatically attached context informations.

Installation

# Using NPM
$ npm install --save @kasa/koa-http-client
# Using Yarn
$ yarn add @kasa/koa-http-client

Dependencies

Usage

Use koa-http-client as a middleware for a koa app.

const Koa = require('koa');
const requestId = require('@kasa/koa-http-client');
const httpClient = require('@kasa/koa-http-client');
const app = new Koa();

app.use(requestId());
app.use(httpClient({
  retry: 3
}));
app.use(async ctx => {
  const userService = ctx.http.extend({
    baseUrl: 'http://user.company.com:8080/'
  });

  let user;
  try {
    const { body } = await userService.get('/users/john');
    user = body.user;
  } catch (err) {
    // Do something with error (HTTP status code 4xx or 5xx)
  }
  // Do something with user data
});

app.listen(3000);

API

Creating an middleware

You can create a new http client middleware by passing the relevant options to httpClient;

const middleware = httpClient({
  forwardedHeaders: ['Accept-Language'],
  userAgent: 'user-service/0.1.0',
  requestIdHeader: 'X-Service-Request-Id',
  timeout: 20 * 1000,
  retry: 3,
});

Middleware Configuration

These are the available config options for the middleware. All is optional. The middleware attachs HTTP client with the context for each request.

{
  // HTTP headers to forward from Koa `ctx`
  forwardedHeaders: ['Accept-Language', 'Authorization'],

  // Client name to set `User-Agent` header
  userAgent: 'user-service/0.1.0',

  // HTTP header to get/set the request id
  requestIdHeader: 'X-Service-Request-Id',

  // Milliseconds to wait for the server to end the response before aborting the request
  timeout: 20 * 1000,

  // Count to retry on the request errors
  retry: 3,
}

Credits

  • sindresorhus/got: koa-http-client was developed based on got. koa-http-client utilizes got.extend feature well!

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or ask feature requests.

License

Provided under the terms of the MIT License.

Copyright ยฉ 2019, Kasa.

koa-http-client's People

Contributors

posquit0 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.