@inyourarea/http-client-builder
v0.4.2
Published
Helper library to build consistent HTTP clients using axios
Downloads
24
Readme
Http Client Builder
Introduction
This client builder has been created so that we can build out request libraries that can utilise consistent error handling, consistent logging strategies and consisteny HTTP headers.
For example usage see profile-service
Usage
import { ClientBuilder } from '@inyourarea/http-client-builder
new ClientBuilder({
service: 'userstore', // A unique identifer for the client
traceId: params.traceId, // OPTIONAL Trace-Id to send on the request
logger: params.logger, // OPTIONAL This can be any valid logger, e.g. console.log or a Pino logger
config: { baseURL: env.getUserstoreUrl() }, // OPTIONAL Axios config object
})
.addAuthErrorHandling() // Convert 401/403 errors into new errors
.add5xxErrorHandling() // Convert 5XX errors into new errors
.addRequestLogging() // Log requests and timing of requests
.addErrorLogging() // Log any request error that does not match axios validateStatus
.add404ErrorHandling() // Conver 404 errors into new errors
.addAuthorization(() => getSig(env.getUserStoreSecret())) // Add a dynamic auth on each request
.build();
Releasing
If you want to release this package, then you will need to version it by running npm version <major|minor|patch>
and then raise a PR to be merged into the master branch.
Remember that ANY breaking change MUST be considered major, unless the package version is below v1.0.0, in this case feel free to bump minor version. If the package obtains mass adoption, then we can consider releasing v1.0.0 which gives more flexibility in versioning.
Logging
Request Logging
Start - Pino Logger
{
"level": 30,
"time": 1625589124860,
"pid": 10,
"hostname": "169.254.213.165",
"traceId": "z24IDwPgQXWt/MbTtX8blg/79",
"event": "userstore-request",
"method": "GET",
"host": "https://userstore-prod.inyourarea.co.uk",
"path": "/issuer/iya/user/af4780fc-72e6-4797-8016-fc49d61afe6c"
}
End - Pino Logger
{
"level": 30,
"time": 1625590971689,
"pid": 10,
"hostname": "169.254.213.165",
"traceId": "z24IDwPgQXWt/MbTtX8blg/79",
"event": "userstore-response",
"method": "GET",
"host": "https://userstore-prod.inyourarea.co.uk",
"path": "/issuer/iya/user/af4780fc-72e6-4797-8016-fc49d61afe6c",
"status": 200,
"requestTime": 35
}
Error Logging
Pino Logger
{
"level": 50,
"time": 1625590093273,
"pid": 8,
"status": 401,
"hostname": "169.254.201.37",
"traceId": "qUphViW0T5yWRdoBgYs9NA/66",
"event": "userstore-request-error",
"message": "{\"code\":401,\"message\":\"Key is missing groups: authorized\"}"
}