npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@janiscommerce/microservice-call

v5.1.1

Published

Allows communication between services.

Downloads

2,079

Readme

Microservice Call

Build Status Coverage Status npm version

The MicroService Call module allows the communication between services.


Installation

npm install @janiscommerce/microservice-call

Endpoints

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, delete are 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.
  • namespace
    • type: String
    • The namespace of the microservice.
  • method
    • type: String
    • The method of microservice.
  • requestData
    • type: Object
    • The data that will send.
  • requestHeaders
    • type: Object
    • The headers of the request as key-value.
  • endpointParameters
    • type: Object
    • A key-value mapping between endpoint path variables and their replace value.
  • filters
    • type: Object
    • filters and/or orders available in destination Entity's Service.
    • example:
    { filters: { id: 'some-id', name:'some-name' }}
  • pageSize. Since 4.3.2
    • type: Number
    • The pageSize will be use to add the x-janis-page-size to the ApiList. The default value is 60.

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: Number
      • The status code of the error.
    • message:
      • type: String
      • The message of the error.
    • name:
      • type: String
      • The name of the Error
    • statusCode:
      • type: Number
      • The 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 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