sbtech-general-api-client
TypeScript icon, indicating that this package has built-in type declarations

0.1.10 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i sbtech-general-api-client

Weekly Downloads

36

Version

0.1.10

License

none

Unpacked Size

77.5 kB

Total Files

55

Last publish

Collaborators

  • sbtech