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"
npm install --save sbtech-general-api-client
Package support two scenarios to configure token service:
- 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.
- 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
}
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
}