0.0.9 • Published 6 years ago

@attivio/fetch-utils v0.0.9

Weekly downloads
36
License
SEE LICENSE IN LI...
Repository
github
Last release
6 years ago

@attivio/fetch-utils

TravisCI npmjs

This ia a library to use when accessing Attivio REST APIs with SAML-based authentication from a serverless web application such as a React single-page application. It contains a single function, FetchUtils.fetch() which you can use instead of calling the built-in fetch() API directly. This method does a few useful things for you:

  • It sets the headers and parameters to the fetch() call in a consistent manner
  • It handles cases where the fetch() call fails because of a potentially missing authentication token, redirecting to the special login API call on the servlet to trigger the authentication
  • It provides a consistent way to receive results and error messages

Using the Library

To use @attivio/fetch-utils:

  1. Add it as a dependency to your project:
npm install --save @attivio/fetch-utils
  1. Import the FetchUtils class into your application code where you will need to access the Attivio REST APIs:
import FetchUtils from '@attivio/fetch-utils';
  1. Finally, use it to access the REST APIs you need:
const baseUri = 'http://myhost:8080/searchui/';
const restEndpoint = 'rest/serverDetailsApi/user';
const callback = (result: any | null, error: string | null) => {
  if (result) {
    console.log('The API call returned this result', result);
  } else if (error) {
    console.log('The API call returned this error', error);
  }
};

FetchUtils.fetch(baseUri, restEndpoint, null, callback, 'GET', 'An error occured.');

Using the FetchUtils.fetch() Method

The FetchUtils.fetch() method takes the following parameters:

ParameterTypeDescription
baseUristringthe base URI for the Attivio servlet, up to and including the servlet context part of the path, including the trailing slash (e.g., /searchui/)
endpointUristringthe part of the URI specific to the REST endpoint you want to access
payloadanya JavaScript object containing the palyload to pass as parameters to the REST call; will be converted into a JSON string -- pass null if no payload is needed
callbackfunction (see below)the function called when the fetch() call returns a result or an error
methodHttpMethod (see below)the HTTP method to use when making the fetch request
defaultErrorMessagestringthe message that should be passed to the callback if an error occurs but no message can be obtained from the fetch() call

The callback function is called asynchronosly, once there is either a successful result or an error from the fetch() call. Its parameters are:

ParameterTypeDescription
resultany | nulla JavaScript object containing the result of a successful fetch() call; if an error occurred, this will be null
errorstring | nullthe error message describing what went wrong if the fetch() call didn't succeed; if it did succeed, this will be null

The type HttpMethod is one of the following strings: 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', or 'PATCH'.

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago