# sbtech-general-api-client

> General api client for all SBTech APIs

Latest version **0.1.10** (published 2018-03-26) · 0 weekly downloads

## Install

```sh
npm install sbtech-general-api-client
pnpm add sbtech-general-api-client
yarn add sbtech-general-api-client
bun add sbtech-general-api-client
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.10 |
| Published | 2018-03-26 |
| First published | 2017-06-02 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 75.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | sbtech |

## Links

- npm: https://www.npmjs.com/package/sbtech-general-api-client
- npm.io page: https://npm.io/package/sbtech-general-api-client

## Dependencies (1)

- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) ^2.0.3

## Recent versions

- 0.1.10 (latest) — 2018-03-26
- 0.1.9 — 2018-03-26
- 0.1.8 — 2018-03-22
- 0.1.7 — 2017-06-22
- 0.1.6 — 2017-06-22
- 0.1.5 — 2017-06-14
- 0.1.4 — 2017-06-06
- 0.1.3 — 2017-06-06
- 0.1.3-alpha1 — 2017-06-06
- 0.1.2 — 2017-06-06
- 0.1.1 — 2017-06-02
- 0.1.0 — 2017-06-02

## README

SBTech general API client contains tools which make communicating with the SBTech API's easier. 

> It is a base package for all SBTech API clients, you can use it to implement more specific API client (e.q. SportsData API, Login API, Betslip API and etc).

It contains:
- customized http client named ```sbtFetch``` (wrapper on native browser fetch)
- token service 

```sbtFetch``` extends functionality of native fetch by next features:
- attach JWT token to each request
- renew JWT token by timer
- renew JWT token and retry request if response contains error "token expired"

## Installation

```npm
npm install --save sbtech-general-api-client
```

## Configure token service
Package support two scenarios to configure token service:
1. It can be auto configured if it running on the same page with widget-platform. It means that page has global variable SBTech with platform configuration.
1. If API client is used without widget-platform it can be configured manually by a consumer of API client.

```js
import {tokenService} from 'sbtech-general-api-client'
let tokenConfig = {
    tokenRenewUrl: 'example.com/token/renew'
}
tokenService.init(tokenConfig)
```
```js
interface TokenConfig {
    tokenRenewInterval: number
    tokenRenewUrl: string
    tokenRenewProvider: TokenRequestProvider //callback to retrieve token from legacy site, can be deprecated in future
}
```

## Usage
```js
import {sbtFetch} from 'sbtech-general-api-client'

let defaultRequestOptions = {
    maxRetriesAmount: 5,
    headers: {
        myCustomHeader: 'headerValue1'
    }
}
let api = sbtFetch('example.com/api', defaultRequestOptions) // setup default options (optional)
api('events?', {attachToken: false}).get() // options can be overridden
    .then((json)=>{
        // ...
    })
```
RequestOptions extends native fetch options object.
```js
interface RequestOptions extends RequestInit {
    responseAs?: 'json' | 'text' | 'response' // default: json
    retryCheckers?: RetryChecker[] // default: checkExpiredToken
    maxRetriesAmount?: number // default: 3
    attachToken?: boolean // default: true
}
```

---
_Source: https://npm.io/package/sbtech-general-api-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
