npm package: @stone-payments/emd-generic-sdk
Description: emd-generic-sdk
Author: Stone Co.
Latest version: 2.3.3

# Emerald Generic SDK

## Install
```bash
npm install --save @stone-payments/emd-generic-sdk
```

## Import

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

## BaseUrl

### Setting the `baseUrl`

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

## Authorization

### Setting the authorization header

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

### Retrieving the authorization header

```javascript
sdk.authorization.get();
```

### Removing a header

```javascript
sdk.authorization.remove();
```

## Headers

### Setting a header

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

### Retrieving all headers

```javascript
sdk.headers.get();
```

### Removing a header

```javascript
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

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

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) 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.

```javascript
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**.

```javascript
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**.

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