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

@stone-payments/emd-generic-sdk

v2.3.3

Published

emd-generic-sdk

Downloads

8

Readme

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());