0.1.10 • Published 6 years ago

sbtech-general-api-client v0.1.10

Weekly downloads
61
License
-
Repository
-
Last release
6 years ago

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 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.

import {tokenService} from 'sbtech-general-api-client'
let tokenConfig = {
    tokenRenewUrl: 'example.com/token/renew'
}
tokenService.init(tokenConfig)
interface TokenConfig {
    tokenRenewInterval: number
    tokenRenewUrl: string
    tokenRenewProvider: TokenRequestProvider //callback to retrieve token from legacy site, can be deprecated in future
}

Usage

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.

interface RequestOptions extends RequestInit {
    responseAs?: 'json' | 'text' | 'response' // default: json
    retryCheckers?: RetryChecker[] // default: checkExpiredToken
    maxRetriesAmount?: number // default: 3
    attachToken?: boolean // default: true
}
0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.3-alpha1

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago