import { Observable } from 'rxjs' import { CLIENT_ID, CUSTOMER_API } from '../../utils/ResourceConstants' import { createAjaxRoute, lessThanThreeErrors } from '../utils/apiHelpers' import { AccessToken, AccessTokenAndUserId, UpdateUserPropertiesOptions, User, } from './interfaces' export const login = (username: string, password: string) => { const formData = new URLSearchParams() formData.append('username', username) formData.append('password', password) formData.append('grant_type', 'password') formData.append('client_id', CLIENT_ID) formData.append('redirect_uri', 'http://localhost:3000/') return Observable.ajax({ url: `${CUSTOMER_API}/oauth/token`, method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, crossDomain: true, responseType: 'json', body: formData.toString(), }) .retryWhen(lessThanThreeErrors) .map((data: any) => { return data.response.access_token }) } export const fetchUser = (options: AccessToken) => { return createAjaxRoute({ url: `${CUSTOMER_API}/users/me`, method: 'GET', token: options.accessToken, }) .retryWhen(lessThanThreeErrors) .map((data: any): User => data.response) } export const updateUserProperties = (options: UpdateUserPropertiesOptions & AccessTokenAndUserId) => { return createAjaxRoute({ url: `${CUSTOMER_API}/users/${options.userId}/properties2`, method: 'PATCH', token: options.accessToken, body: JSON.stringify(options.operations), }) .retryWhen(lessThanThreeErrors) .map((data: any): {[name: string]: any} => data.response) } export const fetchGoogleScopes = (options: AccessToken) => { return createAjaxRoute({ url: `${CUSTOMER_API}/google/calendar/oauth/scopes`, method: 'GET', token: options.accessToken, }) .retryWhen(lessThanThreeErrors) .map((data: any) => data.response) }