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

@comparaonline/protocaller

v2.5.0

Published

Package that contains callers for different protocols

Downloads

381

Readme

Protocaller

NPM Lib that wraps rxjs for calling request through axios and soap

Getting started 🚀

The directory is as follows:

.
└── src                                     ##MAIN CONTENT
|    ├── __tests__         
|    ├── config            
|    ├── errors            
|    ├── interfaces        
|    ├── lib               
|    ├── rest              
|    ├── soap  # Part of example server
|    ├── test
|    ├── base-request.ts
|    ├── caller-factory.ts
|    ├── enums.ts
|    ├── notifier.ts
|    └── index.ts
├── package.json
├── tsconfig.json
├── tslint.json
└── README.md

As you may see, the lib is locate at the src directory

Pre-requisites 📋

Be familiarized with RXJS lib for better understanding about the behavior of this lib

Installing

Using npm:

$ npm install @compara/protocaller

Using yarn:

$ yarn add @compara/protocaller

Examples

Rest Calls

To make rest request you will need to build a rest client, to do that, you have three possible options, Providing attributes step by step (Declarative approach), Providing an entire config and a mixture between Declarative approach and Config option:

Providing attributes step by step (Declarative approach):

  • Using an Oauth Credentials type
  import {
    CredentialsType,
    OauthConfig,
    RequestData,
    RestClientBuilder,
    RestServiceConfig
  } from "@comparaonline/protocaller"

  const OAuthConf = <OauthConfig>{
    "type": 'oauth',
    "realm": "some_realm", // this field is optional.
    "consumer": {
      "key": "some_key",
      "secret": "some_secret"
    },
    "token": {
      "key": "some_key_again",
      "secret": "some_secret_again"
    },
    signature_method: 'HMAC-SHA1' // this field is optional. 'HMAC-SHA1' is the default value 
  }
  const restClientBuilder = new RestClientBuilder()
  const reqData = <RequestData>{
    httpMethod: 'GET'
  }
  

  try {
    const client = restClientBuilder.
    setCredentials(CredentialsType.OAUTH, oAuthConf).
    setUrl('http://localhost:8080/test').
    setRetryOptions({
      retryAttempts: 3,
      exponentialDelay: 2
    }).
    build();
  
    const result = await client.call(reqData).toPromise()
    //DO SOMETHING...
  } catch (e) {
    //DO SOMETHING...
  }
  • Using JWT Credentials type
  import {
  CredentialsType,
  RequestData,
  RestClientBuilder,
  RestServiceConfig
} from "@comparaonline/protocaller"
import {JWTConfig} from "./jwt-config";

const restClientBuilder = new RestClientBuilder()
const reqData = <RequestData>{
  httpMethod: 'GET'
}

const jwtConf = <JWTConfig>{
    jwt:'some_jwt'
};
try {
  const client = restClientBuilder.setCredentials(CredentialsType.JWT, jwtConfig).setUrl('http://localhost:8080/test').setRetryOptions({
    retryAttempts: 3,
    exponentialDelay: 2
  }).build();

  const result = await client.call(reqData).toPromise()
  //DO SOMETHING...
} catch (e) {
  //DO SOMETHING...
}
  • With no Credentials
  import {
  RequestData,
  RestClientBuilder,
  RestServiceConfig
  } from "@comparaonline/protocaller"

  const restClientBuilder = new RestClientBuilder()
  const reqData = <RequestData>{
    httpMethod: 'GET'
  }
  
  try {
    const client = restClientBuilder.
    setUrl('http://localhost:8080/test').
    setRetryOptions({
      retryAttempts: 3,
      exponentialDelay: 2
    }).
    build();
  
    const result = await client.call(reqData).toPromise()
    //DO SOMETHING...
  } catch (e) {
    //DO SOMETHING...
  }

Providing an entire Config

  • Config with JWT
  import {
    CredentialsType,
    RequestData,
    RestClientBuilder,
    RestServiceConfig
  } from "@comparaonline/protocaller" 
  
  const config = <RestServiceConfig>{
    credentials: {
      type: 'jwt',
      jwt: 'some_jwt'
    },
    retryOptions: {
      retryAttempts: 2,
      exponentialDelay: 2
    },
    timeout: 3000,
    url: 'http://localhost:8080/test'
  }
  const restClientBuilder = new RestClientBuilder()
  const reqData = <RequestData>{
    httpMethod: 'GET',
    headers: {
      'some_header': 1
    }
  }
  
  try {
    const client = restClientBuilder.setConfig(config).build();
  
    const result = await client.call(reqData).toPromise()
    //DO SOMETHING...
  } catch (e) {
    //DO SOMETHING...
  }
  • Config with Oauth
  import {
    CredentialsType,
    RequestData,
    RestClientBuilder,
    RestServiceConfig
  } from "@comparaonline/protocaller"

  const config = <RestServiceConfig>{
    credentials: {
      "type": 'oauth',
      "realm": "some_realm", // this field is optional.
      "consumer": {
        "key": "some_key",
        "secret": "some_secret"
      },
      "token": {
        "key": "some_key_again",
        "secret": "some_secret_again"
      },
      signature_method: 'HMAC-SHA1' // this field is optional. 'HMAC-SHA1' is the default value 
    },
    retryOptions: {
      retryAttempts: 2,
      exponentialDelay: 2
    },
    timeout: 3000,
    url: 'http://localhost:8080/test'
  }
  const restClientBuilder = new RestClientBuilder()
  const reqData = <RequestData>{
    httpMethod: 'GET',
    headers: {
      'some_header': 1
    }
  }

  try {
    const client = restClientBuilder.
    setConfig(config).
    build();
  
    const result = await client.call(reqData).toPromise()
    //DO SOMETHING...
  } catch (e) {
    //DO SOMETHING...
  }

Mixture between Declarative approach and Config

  import {
    CredentialsType,
    OauthConfig,
    RequestData,
    RestClientBuilder,
    RestServiceConfig
  } from "@comparaonline/protocaller"

  const oAuthConf =  <OauthConfig>{
    type: "oauth",
    realm: "some_realm", // this field is optional.
    consumer: {
      key: "some_key",
      secret: "some_secret"
    },
    token: {
      key: "some_key_again",
      secret: "some_secret_again"
    },
  }

  const config = <RestServiceConfig>{
    retryOptions: {
      retryAttempts: 2,
      exponentialDelay: 2
    },
    timeout: 3000,
    url: 'http://localhost:8080/test'
  }
  const restClientBuilder = new RestClientBuilder()
  const reqData = <RequestData>{
    httpMethod: 'GET',
    headers: {
      'some_header': 1
    }
  }

  try {
    const client = restClientBuilder.
    setConfig(config).
    setCredentials(CredentialsType.OAUTH, oAuthConf).
    build();
  
    const result = await client.call(reqData).toPromise()
    //DO SOMETHING...
  } catch (e) {
    //DO SOMETHING...
  }

With this last options, notice that you are able to leverage the restClientBuilder pattern. To explain a bit, you should call first setConfig(config) method because it replaces the entire object of configuration inside the restClientBuilder and then, start to call another methods to override the desired attribute.

In addition, as you may see, exists a lot of ways to declare credentials configs, i.e:

  • With types :
    const cred = <credential_class_type>{
      // Some configs here
    }
  • With types as attributes:
    const cred = {
      // ...
      // Some configs here
      type: CredentialsType.OAUTH || CredentialsType.JWT,
      // Some configs here
      // ...
    }
  • With restClientBuilder:
      const restClientBuilder = new RestClientBuilder()
        
      try {
        const client = restClientBuilder.
        setConfig(config).
        setCredentials(CredentialsType.OAUTH || CredentialsType.JWT, some_conf).
        build();
        //DO SOMETHING...
      } catch (e) {
        //DO SOMETHING...
      }

There is no "Correct" way, it is just matter of what you like better.

NOTES

  • retryOptions: Its mission is simple, provides configurations to retry a request. You may notice that there are two options, retryAttempts: this tells to the client how many requests should make in case of error. exponentialDelay: this tells to the client how much should wait to make next rest call. retryOptions is an optional attribute, by default is set as following:
 {  
    retryAttempts: 2,
    exponentialDelay: 2
 }
  • Always calls build method, to generate the client.

Soap Calls

*In construction

Third parties 🛠️

  • Axios - Library to built http request
  • Rxjs - Library to handle streams of data