@supersourcing/request-map
v1.0.1
Published
axios wrapper with in-built service discovery for inter-service http requests
Readme
request-map
A library for handling inter-service http requests with built-in service discovery, timeouts, and retries.
Installation
npm install @mayank_supersourcing/request-map
Usage
Sending HTTP Requests
Initialize the request map with the following options:
const RequestMap = require('@mayank_supersourcing/request-map'); const request = new RequestMap({ environment: 'dev' });The
environmentoption in the config is optional and defaults to 'dev'. The environment is used to determine which service discovery endpoint to use. The environment can be set todev,staging, orprod.Send request to a service:
- GET
async () => { const ctx = { traceId: 'dkcnjfhueir4o3', user: {id: 'dsfkcnsik23e', name: 'John Doe'}, headers: {authorization: user.token} }; const requestConfig = { params: {id: 'dsfkcnsik23e'} }; const service = 'user-management-service'; const path = '/users'; const response = await request.get(ctx, service, path, requestConfig); console.log(response); }- POST
async () => { const ctx = { traceId: 'dkcnjfhueir4o3', user: {id: 'dsfkcnsik23e', name: 'John Doe'}, headers: {authorization: user.token} }; const requestConfig = { params: {id: 'dsfkcnsik23e'} }; const data ={ userId: 'asfdkf', } const service = 'user-management-service'; const path = '/users'; const response = await request.get(ctx, service, path, data, requestConfig); console.log(response); }Most of the http methods have the following arguments.
ctx: required. the context object that will be passed to the service. This object should be used to set thetraceIdanduserheaders in the request.service: required name of the service to send the request to. The actual URL of the service will be determined by the service discovery client based on the environment and service name.path: required and is the path of the endpoint to send the request to.data: optional data to send in the request body.requestConfig: optional object that can be used to set the following options:params: an object that will be used to set the query parameters in the request.data: an object that will be used to set the body of the request.headers: an object that will be used to set the headers of the request.
Contributing
Please raise an issue or submit a pull request if you would like to contribute to this project.
