Package Exports
- @kasa/koa-http-client
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kasa/koa-http-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@kasa/koa-http-client
A middleware that attachs HTTP client to communicate with the context during inter-service communications in Koa.
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({}));
app.use(async ctx => {
ctx.body = ctx.state.reqId;
});
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,
}
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.