@stone-payments/emd-generic-sdk

2.3.3 • Public • Published

Emerald Generic SDK

Install

npm install --save @stone-payments/emd-generic-sdk

Import

import * as sdk from '@stone-payments/emd-generic-sdk';

BaseUrl

Setting the baseUrl

sdk.baseUrl.set('http://custom.api/');

Authorization

Setting the authorization header

sdk.authorization.set(sdk.BEARER, 't0k3n-7483-000');

Retrieving the authorization header

sdk.authorization.get();

Removing a header

sdk.authorization.remove();

Headers

Setting a header

sdk.headers.append('Content-Type', 'application/json');

Retrieving all headers

sdk.headers.get();

Removing a header

sdk.headers.remove('Content-Type');

Usage Examples

The Banking Javascript SDK uses window.fetch under the hood. The only difference is that it injects the headers, including authorization, to every request.

Remember to always get the baseUrl from the SDK, as it will be set by the application.

Making a request

const baseUrl = sdk.baseUrl.get();
sdk.request(`${baseUrl}v1/api/users`, { method: 'GET' });
const baseUrl = sdk.baseUrl.get();
const customHeaders = new Headers();

customHeaders.append('Content-Type', 'text/xml');

sdk.request(`${baseUrl}v1/api/users`, {
  method: 'GET',
  headers: customHeaders
});

Read the full Fetch API documentation on MDN.

Using Middlewares

Middlewares run before and after every request in the same order that they were declared. Every middleware must call either proceed or quit in order to work.

sdk.middleware.use({
  beforeRequest({ proceed, quit }) {
    if (conditionMet) {
      proceed();
    } else {
      quit();
    }
  },
  afterRequest({ proceed }) {
    logSomething();
    proceed();
  }
});

sdk.middleware.use({
  async beforeRequest({ proceed }) {
    await doSomethingAsync();
    proceed();
  }
});

sdk.middleware.use({
  async afterRequest({ proceed, response }) {
    if (response.status === 403) {
      const body = await response.clone().json();

      if (body.type === 'srn:error:challenge_required') {
        proceed(fetch('http://some.url'));
      } else {
        proceed();
      }
    }
    proceed();
  }
});

Complete Example

In the application

In the application, the package should be declared as a dependency.

import * as sdk from '@stone-payments/emd-generic-sdk';

sdk.baseUrl.set('https://randomuser.me/api/');
sdk.headers.append('Content-Type', 'application/json');
sdk.authorization.set(sdk.BEARER, authService.token);

sdk.middleware.use({
  beforeRequest ({ proceed }) {
    if (authService.isTokenExpired()) {
      authService.refreshToken();
      sdk.authorization.set(sdk.BEARER, authService.token);
    }
    proceed();
  }
});

In the component

In the component, the package should be declared as a peerDependency.

import * as sdk from '@stone-payments/emd-generic-sdk';

const baseUrl = sdk.baseUrl.get();

const customHeaders = new Headers();
customHeaders.append('Page', '5');

sdk.request(`${baseUrl}?gender=male`, {
  method: 'GET',
  headers: customHeaders
})
  .then(response => response.json());

Readme

Keywords

none

Package Sidebar

Install

npm i @stone-payments/emd-generic-sdk

Weekly Downloads

0

Version

2.3.3

License

none

Unpacked Size

96.9 kB

Total Files

68

Last publish

Collaborators

  • sling-web