import { Observable } from 'rxjs' export interface ICreateAjaxRoute { url: string method: string token?: string body?: any headers?: any, } export const createAjaxRoute = (routeOpts: ICreateAjaxRoute) => { const opts = { url: routeOpts.url, method: routeOpts.method, headers: { 'Content-Type': 'application/json', Authorization: `bearer ${routeOpts.token}`, ...routeOpts.headers, }, crossDomain: true, responseType: 'json', body: routeOpts.body, } if (routeOpts.token) { opts.headers.Authorization = `bearer ${routeOpts.token}` } return Observable.ajax(opts) } export const lessThanThreeErrors = (errors: any) => { return errors .scan((count: number, error: any) => { if (error.status === 403 || count >= 3) { throw error } return count + 1 }, 0) .mergeMap((count: number) => Observable.timer(2 ^ count * 1000)) }