@janiscommerce/microservice-call
v5.1.1
Published
Allows communication between services.
Readme
Microservice Call
The MicroService Call module allows the communication between services.
Installation
npm install @janiscommerce/microservice-callEndpoints
MicroService Call uses Janis Discovery Service to obtain Api Endpoints using service, namespace and method.
Session
If an API Session is injected, it will inject janis-client and x-janis-user headers when possible.
Authentication
It will automatically inject the janis-api-key and janis-api-secret headers if JANIS_SERVICE_NAME and JANIS_SERVICE_SECRET environment variables are set.
🔑 Secrets
In case the JANIS_SERVICE_SECRET variable is not found, the package will get the secret using the JANIS_SERVICE_NAME environment variable.
If the secret is found it will be used in the janis-api-secret header.
The Secrets are stored in AWS Secrets Manager and obtained with the package @janiscommerce/aws-secrets-manager
API
No Safe Mode
These methods WILL THROW AN ERROR when response statusCode is 400+.
call(service, namespace, method, requestData, requestHeaders, endpointParameters)Make a request to an microservice.
Returns a
PromiseofMicroServiceCallResponse.list(service, namespace, requestData, endpointParameters, pageSize)Since 4.0.0
Make a
LISTrequest to an microservice by entity.Returns a
PromiseofMicroServiceCallResponse, thebodycontains the full list of entity's objects (no need for pagination)
Safe Mode
Since 4.0.0
These methods WILL NOT THROW AN ERROR when response statusCode is 400+.
safeCall(service, namespace, method, requestData, requestHeaders, endpointParameters)Make a request to an microservice.
Returns a
PromiseofMicroServiceCallResponse.safeList(service, namespace, requestData, endpointParameters, pageSize)Make a
LISTrequest to an microservice by entity.Returns a
PromiseofMicroServiceCallResponse, thebodycontains the full list of entity's objects (no need for pagination)
Extra
Since 4.0.0
shouldRetry(response)Indicates if should re-try the call. It is useful for Event-Listeners API to avoid unnecessary retries.
Params:
response{MicroServiceCallResponse | MicroServiceCallError}Returns a
Boolean.
:warning: After version 4.0.0,
get,post,put,path,deleteare REMOVED :warning:
Since 5.1.0
setUserId(userId)Function for add user id in api-key header
Params:
userId{String}Returns a
MicroServiceCallInstance.
Parameters
The Parameters used in the API functions.
service- type:
String - The name of the microservice.
- type:
namespace- type:
String - The namespace of the microservice.
- type:
method- type:
String - The method of microservice.
- type:
requestData- type:
Object - The data that will send.
- type:
requestHeaders- type:
Object - The headers of the request as key-value.
- type:
endpointParameters- type:
Object - A key-value mapping between endpoint path variables and their replace value.
- type:
filters- type:
Object - filters and/or orders available in destination Entity's Service.
- example:
{ filters: { id: 'some-id', name:'some-name' }}- type:
pageSize. Since 4.3.2- type:
Number - The pageSize will be use to add the
x-janis-page-sizeto the ApiList. The default value is60.
- type:
Response Object
Response of Microservice
MicroServiceCallResponse: type:ObjectstatusCode:- type:
Number - The status code of the response.
- type:
statusMessage:- type:
String - The status message of the response.
- type:
headers:- type:
Object - The headers of the response.
- type:
body:- type:
Object,ArrayorString(if it's "") - The body of the response
- type:
Errors
The errors are informed with a MicroServiceCallError.
MicroServiceCallError:code:- type:
Number - The status code of the error.
- type:
message:- type:
String - The message of the error.
- type:
name:- type:
String - The name of the Error
- type:
statusCode:- type:
Number - The status code of the response.
- type:
Codes
The codes are the following:
| Code | Description | |-----|-----------------------------| | 2 | Microservice Failed | | 3 | Request Library Errors | | 4 | Janis Secret is missing |
Usage
No Safe Mode
const MicroServiceCall = require('@janiscommerce/microservice-call');
const ms = new MicroServiceCall();
// Make a GET request to ms "sac" with the namespace "claim-type" and method "get".
try {
const response = await ms.call('sac', 'claim-type', 'get', null, null, {
foo: 'bar'
});
/*
Response example
{
headers: {}, // The headers of the response.
statusCode: 200,
statusMessage: 'Ok',
body: {
foo: 'bar',
id: 'foo-id',
other: 100
}
}
*/
} catch(error){
/*
Error Response Example:
{
name: 'MicroServiceCallError'
message: 'Could not found claim',
code: 2,
statusCode: 404
}
*/
if(ms.shouldRetry(error)) // false
throw new Error('Should Retry')
// Do something
}const MicroServiceCall = require('@janiscommerce/microservice-call');
const ms = new MicroServiceCall();
// Make a LIST request to ms "catalog" with the namespace "brand" with status filter
try {
const filters = {
status: 'active'
};
const response = await ms.list('catalog', 'brand', { filters });
/*
Response example
{
headers: {}, // The headers of the response.
statusCode: 200,
statusMessage: 'Ok',
body: [
{
id: 'brand-1',
referenceId: 'reference-id-1',
name: 'Brand One'
},
{
id: 'brand-2',
referenceId: 'reference-id-2',
name: 'Brand Two'
},
// 1997 objects ...
{
id: 'brand-2000',
referenceId: 'reference-id-2000',
name: 'Brand Two Thousands'
}
]
}
*/
} catch(err){
/*
Error Response Example:
{
name: 'MicroServiceCallError'
message: 'Database Fails',
code: 2,
statusCode: 500
}
*/
if(ms.shouldRetry(error)) // true
throw new Error('Service Call Fails. Should Retry')
// Do something
}Safe Mode
const MicroServiceCall = require('@janiscommerce/microservice-call');
const ms = new MicroServiceCall();
// Make a GET request to ms "pricing" with the namespace "base-price" and method "get".
const response = await ms.safeCall('pricing', 'base-price', 'get', null, null, {
foo: 'bar'
});
/*
Response example
{
headers: {}, // The headers of the response.
statusCode: 504,
statusMessage: null,
body: {
message: 'Timeout'
}
}
*/
if(ms.shouldRetry(response)) // true
throw new Error('Should Retry')
// Do something
// Make a POST request to ms "wms" with the namespace "stock" and method "post".
const response = await ms.safeCall('wms', 'stock', 'post', { name: 'stock-1', quantity: 1 });
/*
Response example
{
headers: {}, // The headers of the response.
statusCode: 200,
statusMessage: 'Ok',
body: {
id: 'stock-id-1'
}
}
*/
if(ms.shouldRetry(response)) // false
throw new Error('Should Retry')
// Do something
const MicroServiceCall = require('@janiscommerce/microservice-call');
const ms = new MicroServiceCall();
// Make a LIST request to ms "commerce" with the namespace "seller" with status filter
const filters = {
status: 'active'
};
const response = await ms.safeList('commerce', 'seller', { filters });
/*
Response example
{
headers: {}, // The headers of the response.
statusCode: 200,
statusMessage: 'Ok',
body: [
{
id: 'seller-1',
referenceId: 'reference-id-1',
name: 'Seller One'
},
{
id: 'seller-2',
referenceId: 'reference-id-2',
name: 'Seller Two'
},
// 1997 objects ...
{
id: 'seller-2000',
referenceId: 'reference-id-2000',
name: 'Seller Two Thousands'
}
]
}
*/
if(ms.shouldRetry(error)) // false
throw new Error('Service Call Fails. Should Retry')
// Do something
