@mojaloop/participants-bc-client-lib
TypeScript icon, indicating that this package has built-in type declarations

0.5.3 • Public • Published

Mojaloop vNext Participants Client Library

Git Commit Git Releases Npm Version NPM Vulnerabilities CircleCI

This is a readonly HTTP client library for the Participants bounded context.

It allows for the following readonly operations:

  • getAllParticipants: get all registered participants.
  • getParticipantsByIds: get multiple participants whose ids match the provided array of Ids.
  • getParticipantById: get a single participant by its Id.
  • getParticipantAccountsById: get the account list of a participant by its Id (This is the only call that includes balances - fetched from the Accounts and Balances Services by the Participants Service).

NOTE: This client requires an instance of a IAuthenticatedHttpRequester with adequate credentials from the Authentication BC to be passed in the constructor

Install

npm install @mojaloop/participants-bc-client-lib

Usage

Configure

"use strict";

import {ILogger, ConsoleLogger} from "@mojaloop/logging-bc-public-types-lib";
import {
	AuthenticatedHttpRequester,
	IAuthenticatedHttpRequester
} from "@mojaloop/security-bc-client-lib";
import {
    AccountsAndBalancesClient,
    IAccountDTO,
    IJournalEntryDTO
} from "@mojaloop/participants-bc-client-lib";

const logger: ILogger = new ConsoleLogger();

// IAuthenticatedHttpRequester consts
const AUTH_TOKEN_ENPOINT = "http://localhost:3201/token";
const USERNAME = "user";                // only needed for user logins (password grant)
const PASSWORD = "superPass";           // only needed for user logins (password grant)
const CLIENT_ID = "security-bc-ui";     // always required
const CLIENT_SECRET = "client_secret";  // only needed for app logins (client_credentials grant)

// create the instance of IAuthenticatedHttpRequester
const authRequester = new AuthenticatedHttpRequester(logger, AUTH_TOKEN_ENPOINT);

// set user credentials example (password grant)
authRequester.setUserCredentials(CLIENT_ID, USERNAME, PASSWORD);

// set app credentials example (client_credentials grant)
// authRequester.setAppCredentials(CLIENT_ID, CLIENT_SECRET);

// ParticipantsHttpClient constants
const PARTICIPANTS_BASE_URL: string = "http://localhost:3010";
const HTTP_CLIENT_TIMEOUT_MS: number = 10_000;

const participantsClient = new ParticipantsHttpClient(logger, PARTICIPANTS_BASE_URL, authRequester);

await participantsClient.getAllParticipants().then(value => {
	console.log(value);
}).catch(reason => {
	if(reason instanceof UnauthorizedError){
		console.log("Invalid credentials");
	}else if(reason instanceof ConnectionRefusedError){
		console.log("Cannot connect to destination endpoint");
	}else if(reason instanceof RequestTimeoutError) {
		console.log("Timeout while waiting for request to finish");
	}else if(reason instanceof MaxRetriesReachedError) {
		console.log("Request max retries reached, giving up");
	}else {
		console.error(reason);
	}
});

See Also

Readme

Keywords

Package Sidebar

Install

npm i @mojaloop/participants-bc-client-lib

Weekly Downloads

94

Version

0.5.3

License

Apache-2.0

Unpacked Size

30.6 kB

Total Files

12

Last publish

Collaborators

  • mojaloopci
  • pedrosousabarreto
  • mdebarros