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/router-fetcher

v2.1.2

Published

Module to make request to the router

Downloads

5,990

Readme

router-fetcher

Build Status Coverage Status npm version

Maps services names, namespaces and methods defined in the API's schemas, to endpoints and HTTP methods for APIs's calls from any service.

Instalation

npm install @janiscommerce/router-fetcher

Configuration

Router-Fetcher uses a setting JSON file.

It's located in path/to/root/[MS_PATH]/config/.janiscommercerc.json

Needs the following fields

  • routerConfig, object with URL to get Endpoints and Schemas.

Example

In path/to/root/[MS_PATH]/config/.janiscommercerc.json.

{
	"routerConfig": {
		"endpoint": "http://valid-router:7999/api/endpoint",
		"schema": "http://valid-router:7999/api/services/{serviceName}/schema"
	}
}

API

  • new RouterFetcher()

    Router-Fetcher Constructors

  • getEndpoint(service, namespace, method, httpMethod)

    Get the endpoint data doing one request to the router.

    • service:
      • type String
      • The name of the microservice.
    • namespace
      • type String
      • The namespace of the microservice.
    • method
      • type String
      • The method of microservice.
    • httpMethod
      • type String
      • Verb of the request.

    Returns a Promise of RouterResponse object

  • getSchema(service)

    Get the schema data of a service doing one request to the router.

    • service:
      • type String
      • The name of the microservice.

    Returns a Promise of RouterResponse object

Response Object

Response of Router for endpoints

  • RouterResponse
    • endpoint
      • type: String
      • The endpoint of microservice.
    • httpMethod
      • type: String
      • The httpMethod of endpoint.

For Schemas requests, the response will be an object with the OpenAPI specification

Errors

The errors are informed with a RouterFetcherError.

  • RouterFetcherError:
    • code:
      • type: Number
      • The status code of the error
    • message:
      • type: String
      • The status message of the response.
    • name:
      • type: String
      • value:
        • RouterFetcherError. If the response code is >= 400.
        • Other, Request Library Error.

Codes

The codes are the following:

|Code |Description | |-----|-----------------------------| |2 |Schema not found | |3 |Invalid Router Config Path | |4 |Endpoint not found | |5 |Request Library Errors |

Usage

Make a request to "SAC" microservice with the namespace "claim-type" and method "list" and get its endpoints.

const RouterFetcher = require('@janiscommerce/router-fetcher');

const routerFetcher = new RouterFetcher();

try {

    const response = await routerFetcher.getEndpoint('sac', 'claim-type', 'list');

    /*
        Response example
        {
            "endpoint": "https://sac.janis.in/claim-types",
            "httpMethod: "GET"
        }
    */

} catch (err) {
    /*
        Error Response Example:
        {
            name: 'RouterFetcherError'
            message: 'Endpoint not found',
            code: 3
        }
    */
    if (err.name === `RouterFetcherError`) {
        // The code of the router response is >= 400.

    } else {
        // Fatal error of request library https://www.npmjs.com/package/request
    }
}

Make a request to "SAC" microservice and obtain its schemas.

const RouterFetcher = require('@janiscommerce/router-fetcher');

const routerFetcher = new RouterFetcher();

try {

    const response = await routerFetcher.getSchema('sac');

    /*
        Response example
        {
            servers: ["core"],
            tags: ["sac"],
            paths: {
                /sac/claim-type/list:
                    x-janis-namespace: claim-type,
                    x-janis-method: "list",
                    get: {
                        responses: {
                        '200': description: OK,
                        '400': description: Invalid parameters,
                        '401': description: Invalid authentication,
                        '403': description: Invalid permissions
                        }
                    }
                }
        }
    */

} catch(err) {
    /*
        Error Response Example:
        {
            name: 'RouterFetcherError'
            message: 'Schema not found',
            code: 2
        }
    */
    if(err.name === `RouterFetcherError`) {
        // The code of the router response is >= 400.

    } else {
        // Fatal error of request library https://www.npmjs.com/package/request
    }
}