UNPKG

strava-api-handler

Version:
82 lines (65 loc) 2.17 kB
'use strict'; var CookieApi = require('cookie-api-handler'); var restApiHandler = require('rest-api-handler'); var StravaException = require('./StravaException.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var CookieApi__default = /*#__PURE__*/_interopDefaultLegacy(CookieApi); class WebApi extends CookieApi__default["default"] { constructor(session) { super('https://www.strava.com', [new restApiHandler.DefaultResponseProcessor(restApiHandler.DefaultApiException)], { 'Content-Type': 'application/x-www-form-urlencoded' }); if (session) { this.addCookies({ _strava4_session: session }); } } setSession(session) { this.session = session; } getSession() { return this.session; } async getCsfrToken(path) { const { data } = await this.get(path); const token = /csrf-token" content="(([A-Za-z]|\+|\d|=|\/)*)/g.exec(data); if (!token) { throw new StravaException('CSFR token was not found'); } return token[1]; } async login(email, password) { const token = await this.getCsfrToken('login'); try { await this.request('session', 'POST', { body: CookieApi__default["default"].convertData({ utf8: '✓', authenticity_token: token, plan: '', email, password }, CookieApi__default["default"].FORMATS.URL_ENCODED), redirect: 'manual' }); // eslint-disable-next-line no-empty } catch {} const cookies = this.getCookies(); this.setSession(cookies ? cookies._strava4_session : undefined); } async setPrivacy(id, privacy) { const token = await this.getCsfrToken(`activities/${id}/edit`); await this.request(`activities/${id}`, 'POST', { body: CookieApi__default["default"].convertData({ 'utf8': '✓', '_method': 'patch', 'authenticity_token': token, 'activity[visibility]': privacy, 'commit': 'Save' }, CookieApi__default["default"].FORMATS.URL_ENCODED), redirect: 'manual' }); } } module.exports = WebApi;