@janiscommerce/microservice-call v5.1.1
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 `Promise` of `MicroServiceCallResponse`.list(service, namespace, requestData, endpointParameters, pageSize)_Since 4.0.0_ Make a `LIST` request to an microservice by entity. Returns a `Promise` of `MicroServiceCallResponse`, the `body` contains 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 `Promise` of `MicroServiceCallResponse`.safeList(service, namespace, requestData, endpointParameters, pageSize)Make a `LIST` request to an microservice by entity. Returns a `Promise` of `MicroServiceCallResponse`, the `body` contains 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.
servicetype:StringThe name of the microservice.namespacetype:StringThe namespace of the microservice.methodtype:StringThe method of microservice.requestDatatype:ObjectThe data that will send.requestHeaderstype:ObjectThe headers of the request as key-value.endpointParameterstype:ObjectA key-value mapping between endpoint path variables and their replace value.filterstype:Objectfilters and/or orders available in destination Entity's Service. * example:```js { filters: { id: 'some-id', name:'some-name' }} ```pageSize. Since 4.3.2 type:NumberThe pageSize will be use to add thex-janis-page-sizeto the ApiList. The default value is60.
Response Object
Response of Microservice
MicroServiceCallResponse: type:Object* `statusCode`: * type: `Number` * The status code of the response. * `statusMessage`: * type: `String` * The status message of the response. * `headers`: * type: `Object` * The headers of the response. * `body`: * type: `Object`, `Array` or `String` (if it's "") * The body of the response
Errors
The errors are informed with a MicroServiceCallError.
MicroServiceCallError:code: type:NumberThe status code of the error.message: type:StringThe message of the error.name: type:StringThe name of the ErrorstatusCode: type:NumberThe status code of the response.
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 somethingconst 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 something2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago