UNPKG

trainingpeaks-sdk

Version:
64 lines (63 loc) 2.2 kB
export const DEFAULT_BASE_URLS = { api: 'https://tpapi.trainingpeaks.com', home: 'https://home.trainingpeaks.com', app: 'https://app.trainingpeaks.com', }; export const DEFAULT_PATHS = { login: '/login', users: '/users', token: '/token', userProfile: '/user', tokenRefresh: '/token/refresh', userPreferences: '/preferences', fitness: '/fitness', athletes: '/athletes', workouts: '/workouts', }; export const createApiUrls = (config = {}) => { const baseUrls = { ...DEFAULT_BASE_URLS, ...config.baseUrls, }; const paths = { ...DEFAULT_PATHS, ...config.paths, }; const usersVersion = config.usersVersion ?? 'v3'; const fitnessVersion = config.fitnessVersion ?? 'v6'; const usersBase = `${baseUrls.api}${paths.users}/${usersVersion}`; const fitnessBase = `${baseUrls.api}${paths.fitness}/${fitnessVersion}`; const generatedEndpoints = { LOGIN_PAGE: `${baseUrls.home}${paths.login}`, TOKEN: `${usersBase}${paths.token}`, USER_PROFILE: `${usersBase}${paths.userProfile}`, TOKEN_REFRESH: `${usersBase}${paths.tokenRefresh}`, USER_PREFERENCES: `${usersBase}${paths.userPreferences}`, WORKOUTS_LIST: `${fitnessBase}${paths.athletes}`, }; const endpoints = { ...generatedEndpoints, ...config.endpoints, }; return { baseUrls: { API: baseUrls.api, LOGIN: baseUrls.home, USERS: usersBase, APP: baseUrls.app, }, endpoints, origins: { APP: baseUrls.app, APP_WITH_SLASH: `${baseUrls.app}/`, }, }; }; export const DEFAULT_API_URLS = createApiUrls(); export const API_BASE_URLS = DEFAULT_API_URLS.baseUrls; export const API_ORIGINS = DEFAULT_API_URLS.origins; export const API_ENDPOINTS = DEFAULT_API_URLS.endpoints; export const generateWorkoutListUrl = (athleteId, startDate, endDate, config = {}) => { const apiUrls = createApiUrls(config); return `${apiUrls.endpoints.WORKOUTS_LIST}/${encodeURIComponent(athleteId)}/workouts/${encodeURIComponent(startDate)}/${encodeURIComponent(endDate)}`; };