incyclist-services 1.9.25 → 1.9.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/activities/upload/index.js +1 -0
- package/lib/cjs/activities/upload/trainingpeaks.js +220 -0
- package/lib/cjs/api/rest/RestApiClient.js +5 -2
- package/lib/cjs/api/rest/api.js +45 -4
- package/lib/cjs/apps/base/api/base.js +2 -2
- package/lib/cjs/apps/base/api/index.js +1 -0
- package/lib/cjs/apps/base/api/strava/api.js +81 -3
- package/lib/cjs/apps/base/api/strava/consts.js +35 -0
- package/lib/cjs/apps/base/api/trainingpeaks/api.js +255 -0
- package/lib/cjs/apps/base/api/trainingpeaks/index.js +18 -0
- package/lib/cjs/apps/base/api/trainingpeaks/types.js +10 -0
- package/lib/cjs/apps/base/api/trainingpeaks/utils.js +20 -0
- package/lib/cjs/apps/base/api/types.js +2 -0
- package/lib/cjs/apps/base/app/ConnectedAppService.js +116 -0
- package/lib/cjs/apps/base/app/OAuthAppConnection.js +106 -0
- package/lib/cjs/apps/base/app/index.js +14 -109
- package/lib/cjs/apps/base/utils.js +19 -0
- package/lib/cjs/apps/index.js +1 -0
- package/lib/cjs/apps/service.js +37 -17
- package/lib/cjs/apps/strava/StravaAppConnection.js +5 -4
- package/lib/cjs/apps/trainingpeaks/TrainingPeaksAppConnection.js +91 -0
- package/lib/cjs/apps/trainingpeaks/index.js +18 -0
- package/lib/cjs/apps/trainingpeaks/types.js +2 -0
- package/lib/cjs/ride/route/RLVDisplayService.js +2 -2
- package/lib/cjs/routes/base/parsers/gpx.js +5 -0
- package/lib/cjs/routes/list/service.js +1 -1
- package/lib/cjs/routes/sync/base/provider.js +217 -0
- package/lib/cjs/routes/sync/index.js +1 -0
- package/lib/cjs/routes/sync/komoot/provider.js +143 -335
- package/lib/cjs/routes/sync/strava/provider.js +128 -0
- package/lib/cjs/workouts/calendar/sync/index.js +1 -0
- package/lib/cjs/workouts/calendar/sync/trainingpeaks/provider.js +254 -0
- package/lib/esm/activities/upload/index.js +1 -0
- package/lib/esm/activities/upload/trainingpeaks.js +217 -0
- package/lib/esm/api/rest/RestApiClient.js +5 -2
- package/lib/esm/api/rest/api.js +45 -4
- package/lib/esm/apps/base/api/base.js +2 -2
- package/lib/esm/apps/base/api/index.js +1 -0
- package/lib/esm/apps/base/api/strava/api.js +81 -3
- package/lib/esm/apps/base/api/trainingpeaks/api.js +251 -0
- package/lib/esm/apps/base/api/trainingpeaks/index.js +2 -0
- package/lib/esm/apps/base/api/trainingpeaks/types.js +6 -0
- package/lib/esm/apps/base/api/trainingpeaks/utils.js +15 -0
- package/lib/esm/apps/base/app/ConnectedAppService.js +113 -0
- package/lib/esm/apps/base/app/OAuthAppConnection.js +102 -0
- package/lib/esm/apps/base/app/index.js +2 -110
- package/lib/esm/apps/base/utils.js +15 -0
- package/lib/esm/apps/index.js +1 -0
- package/lib/esm/apps/service.js +37 -17
- package/lib/esm/apps/strava/StravaAppConnection.js +5 -4
- package/lib/esm/apps/trainingpeaks/TrainingPeaksAppConnection.js +88 -0
- package/lib/esm/apps/trainingpeaks/index.js +2 -0
- package/lib/esm/apps/trainingpeaks/types.js +1 -0
- package/lib/esm/ride/route/RLVDisplayService.js +2 -2
- package/lib/esm/routes/base/parsers/gpx.js +5 -0
- package/lib/esm/routes/list/service.js +1 -1
- package/lib/esm/routes/sync/index.js +1 -0
- package/lib/esm/routes/sync/komoot/provider.js +143 -336
- package/lib/esm/workouts/calendar/sync/index.js +1 -0
- package/lib/esm/workouts/calendar/sync/trainingpeaks/provider.js +251 -0
- package/lib/types/activities/upload/index.d.ts +1 -0
- package/lib/types/activities/upload/trainingpeaks.d.ts +31 -0
- package/lib/types/api/rest/RestApiClient.d.ts +1 -1
- package/lib/types/api/rest/api.d.ts +6 -0
- package/lib/types/apps/base/api/base.d.ts +1 -1
- package/lib/types/apps/base/api/index.d.ts +1 -0
- package/lib/types/apps/base/api/intervals/api.d.ts +2 -1
- package/lib/types/apps/base/api/komoot/api.d.ts +2 -1
- package/lib/types/apps/base/api/strava/api.d.ts +15 -8
- package/lib/types/apps/base/api/strava/types.d.ts +101 -0
- package/lib/types/apps/base/api/trainingpeaks/api.d.ts +39 -0
- package/lib/types/apps/base/api/trainingpeaks/index.d.ts +2 -0
- package/lib/types/apps/base/api/trainingpeaks/types.d.ts +56 -0
- package/lib/types/apps/base/api/trainingpeaks/utils.d.ts +2 -0
- package/lib/types/apps/base/app/ConnectedAppService.d.ts +23 -0
- package/lib/types/apps/base/app/OAuthAppConnection.d.ts +32 -0
- package/lib/types/apps/base/app/index.d.ts +2 -22
- package/lib/types/apps/base/index.d.ts +1 -0
- package/lib/types/apps/base/types.d.ts +7 -0
- package/lib/types/apps/base/utils.d.ts +1 -0
- package/lib/types/apps/index.d.ts +1 -0
- package/lib/types/apps/intervals/types.d.ts +2 -6
- package/lib/types/apps/service.d.ts +3 -2
- package/lib/types/apps/strava/types.d.ts +2 -6
- package/lib/types/apps/trainingpeaks/TrainingPeaksAppConnection.d.ts +13 -0
- package/lib/types/apps/trainingpeaks/index.d.ts +2 -0
- package/lib/types/apps/trainingpeaks/types.d.ts +2 -0
- package/lib/types/apps/types.d.ts +2 -1
- package/lib/types/routes/base/parsers/gpx.d.ts +1 -0
- package/lib/types/routes/sync/index.d.ts +1 -0
- package/lib/types/routes/sync/komoot/provider.d.ts +11 -37
- package/lib/types/routes/sync/types.d.ts +5 -0
- package/lib/types/workouts/calendar/sync/index.d.ts +1 -0
- package/lib/types/workouts/calendar/sync/trainingpeaks/provider.d.ts +31 -0
- package/package.json +1 -1
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./strava"), exports);
|
|
18
18
|
__exportStar(require("./intervals"), exports);
|
|
19
19
|
__exportStar(require("./komoot"), exports);
|
|
20
|
+
__exportStar(require("./trainingpeaks"), exports);
|
|
20
21
|
__exportStar(require("./types"), exports);
|
|
21
22
|
__exportStar(require("./factory"), exports);
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
3
|
+
var useValue = arguments.length > 2;
|
|
4
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
5
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
6
|
+
}
|
|
7
|
+
return useValue ? value : void 0;
|
|
8
|
+
};
|
|
9
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
10
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
11
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
12
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
13
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
14
|
+
var _, done = false;
|
|
15
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
16
|
+
var context = {};
|
|
17
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
18
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
19
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
20
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
21
|
+
if (kind === "accessor") {
|
|
22
|
+
if (result === void 0) continue;
|
|
23
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
24
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
25
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
26
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
27
|
+
}
|
|
28
|
+
else if (_ = accept(result)) {
|
|
29
|
+
if (kind === "field") initializers.unshift(_);
|
|
30
|
+
else descriptor[key] = _;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
34
|
+
done = true;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.TrainingPeaksUpload = void 0;
|
|
38
|
+
const api_1 = require("../../api");
|
|
39
|
+
const TrainingPeaksAppConnection_1 = require("../../apps/trainingpeaks/TrainingPeaksAppConnection");
|
|
40
|
+
const Injection_1 = require("../../base/decorators/Injection");
|
|
41
|
+
const service_1 = require("../../base/service");
|
|
42
|
+
const types_1 = require("../../base/types");
|
|
43
|
+
const trainingpeaks_1 = require("../../apps/base/api/trainingpeaks");
|
|
44
|
+
const utils_1 = require("../../apps/base/api/trainingpeaks/utils");
|
|
45
|
+
const factory_1 = require("./factory");
|
|
46
|
+
const SUPPORTED_FORMATS = new Set(['tcx', 'fit']);
|
|
47
|
+
const EXISTING_ACTIVITY = 'existing';
|
|
48
|
+
let TrainingPeaksUpload = (() => {
|
|
49
|
+
let _classDecorators = [types_1.Singleton];
|
|
50
|
+
let _classDescriptor;
|
|
51
|
+
let _classExtraInitializers = [];
|
|
52
|
+
let _classThis;
|
|
53
|
+
let _classSuper = service_1.IncyclistService;
|
|
54
|
+
let _instanceExtraInitializers = [];
|
|
55
|
+
let _getTrainingPeaksAppConnection_decorators;
|
|
56
|
+
let _getFileSystem_decorators;
|
|
57
|
+
var TrainingPeaksUpload = class extends _classSuper {
|
|
58
|
+
static { _classThis = this; }
|
|
59
|
+
static {
|
|
60
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
61
|
+
_getTrainingPeaksAppConnection_decorators = [Injection_1.Injectable];
|
|
62
|
+
_getFileSystem_decorators = [Injection_1.Injectable];
|
|
63
|
+
__esDecorate(this, null, _getTrainingPeaksAppConnection_decorators, { kind: "method", name: "getTrainingPeaksAppConnection", static: false, private: false, access: { has: obj => "getTrainingPeaksAppConnection" in obj, get: obj => obj.getTrainingPeaksAppConnection }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
64
|
+
__esDecorate(this, null, _getFileSystem_decorators, { kind: "method", name: "getFileSystem", static: false, private: false, access: { has: obj => "getFileSystem" in obj, get: obj => obj.getFileSystem }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
65
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
66
|
+
TrainingPeaksUpload = _classThis = _classDescriptor.value;
|
|
67
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
68
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
69
|
+
}
|
|
70
|
+
isInitialized = __runInitializers(this, _instanceExtraInitializers);
|
|
71
|
+
connection = new TrainingPeaksAppConnection_1.TrainingPeaksAppConnection();
|
|
72
|
+
constructor() {
|
|
73
|
+
super('TrainingPeaksUpload');
|
|
74
|
+
this.isInitialized = false;
|
|
75
|
+
this.init();
|
|
76
|
+
}
|
|
77
|
+
init() {
|
|
78
|
+
if (this.isInitialized)
|
|
79
|
+
return true;
|
|
80
|
+
this.isInitialized = this.getTrainingPeaksAppConnection().init();
|
|
81
|
+
return this.isInitialized;
|
|
82
|
+
}
|
|
83
|
+
isConnected() {
|
|
84
|
+
return this.getTrainingPeaksAppConnection().isConnected();
|
|
85
|
+
}
|
|
86
|
+
async upload(activity, format = 'TCX') {
|
|
87
|
+
if (!this.ensureInitialized()) {
|
|
88
|
+
this.logEvent({ message: 'TrainingPeaks Upload skipped', reason: 'not initialized' });
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (!this.isConnected())
|
|
92
|
+
return false;
|
|
93
|
+
try {
|
|
94
|
+
const lcFormat = format.toLowerCase();
|
|
95
|
+
this.logEvent({ message: 'TrainingPeaks Upload', format: lcFormat.toUpperCase() });
|
|
96
|
+
const fileName = this.getActivityFile(activity, lcFormat);
|
|
97
|
+
const data = await this.readFileAsBase64(fileName, lcFormat);
|
|
98
|
+
const res = await this.getApi().upload({
|
|
99
|
+
fileName: this.getUploadFileName(fileName, lcFormat),
|
|
100
|
+
data,
|
|
101
|
+
title: activity.title,
|
|
102
|
+
type: 'bike'
|
|
103
|
+
});
|
|
104
|
+
this.logEvent({ message: 'TrainingPeaks Upload success', activityId: res.trainingPeaksId });
|
|
105
|
+
this.setLink(activity, { activity_id: res.trainingPeaksId, url: this.getUrl() });
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
if (err instanceof trainingpeaks_1.TrainingPeaksDuplicateError)
|
|
110
|
+
return await this.onDuplicate(activity);
|
|
111
|
+
this.logEvent({ message: 'TrainingPeaks Upload failure', error: err.message });
|
|
112
|
+
this.setLink(activity, { error: err.message });
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async onDuplicate(activity) {
|
|
117
|
+
const id = await this.findExistingWorkout(activity);
|
|
118
|
+
this.logEvent({ message: 'TrainingPeaks Upload duplicate', activityId: id, found: id !== undefined });
|
|
119
|
+
this.setLink(activity, id
|
|
120
|
+
? { activity_id: id, url: this.getUrl() }
|
|
121
|
+
: { activity_id: EXISTING_ACTIVITY, url: this.getUrl() });
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
async findExistingWorkout(activity) {
|
|
125
|
+
try {
|
|
126
|
+
const start = new Date(activity.startTime);
|
|
127
|
+
if (Number.isNaN(start.valueOf()))
|
|
128
|
+
return undefined;
|
|
129
|
+
const workout = await this.getApi().findWorkout(start);
|
|
130
|
+
return workout ? String(workout.Id) : undefined;
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
this.logEvent({ message: 'TrainingPeaks Upload duplicate: workout not found', error: err.message });
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
getUrl() {
|
|
138
|
+
return this.getApi().getCalendarUrl();
|
|
139
|
+
}
|
|
140
|
+
getActivityFile(activity, format) {
|
|
141
|
+
if (!SUPPORTED_FORMATS.has(format))
|
|
142
|
+
throw new Error(`format ${format.toUpperCase()} is not supported by TrainingPeaks`);
|
|
143
|
+
const fileName = activity[`${format}FileName`];
|
|
144
|
+
if (!fileName)
|
|
145
|
+
throw new Error(`no ${format.toUpperCase()} file available for this activity`);
|
|
146
|
+
return fileName;
|
|
147
|
+
}
|
|
148
|
+
async readFileAsBase64(fileName, format) {
|
|
149
|
+
const fs = this.getFileSystem();
|
|
150
|
+
if (!(await fs.existsFile(fileName)))
|
|
151
|
+
throw new Error('activity file not found');
|
|
152
|
+
const content = await fs.readFile(fileName, 'base64');
|
|
153
|
+
if (!content)
|
|
154
|
+
throw new Error('activity file is empty');
|
|
155
|
+
const base64 = this.toBase64(content, format);
|
|
156
|
+
const contentType = this.getContentType(content);
|
|
157
|
+
const isXml = format === 'fit' ? undefined : this.isXml(base64);
|
|
158
|
+
this.logEvent({ message: 'TrainingPeaks Upload file', contentType, bytes: Math.floor(base64.length * 3 / 4), isXml });
|
|
159
|
+
if (isXml === false)
|
|
160
|
+
throw new Error(`activity file does not contain xml (content type: ${contentType})`);
|
|
161
|
+
return base64;
|
|
162
|
+
}
|
|
163
|
+
getContentType(content) {
|
|
164
|
+
if (typeof content !== 'object' || content === null)
|
|
165
|
+
return typeof content;
|
|
166
|
+
return content.constructor?.name ?? 'object';
|
|
167
|
+
}
|
|
168
|
+
toBase64(content, format) {
|
|
169
|
+
if (typeof content === 'string') {
|
|
170
|
+
const compact = content.replaceAll(/\s/g, '');
|
|
171
|
+
if ((0, utils_1.isBase64)(compact))
|
|
172
|
+
return compact;
|
|
173
|
+
if (format === 'fit')
|
|
174
|
+
throw new Error('binary file could not be read');
|
|
175
|
+
return Buffer.from(content, 'utf-8').toString('base64');
|
|
176
|
+
}
|
|
177
|
+
return this.toBuffer(content).toString('base64');
|
|
178
|
+
}
|
|
179
|
+
toBuffer(content) {
|
|
180
|
+
if (ArrayBuffer.isView(content))
|
|
181
|
+
return Buffer.from(content.buffer, content.byteOffset, content.byteLength);
|
|
182
|
+
if (content instanceof ArrayBuffer)
|
|
183
|
+
return Buffer.from(content);
|
|
184
|
+
const serialized = content;
|
|
185
|
+
if (serialized?.type === 'Buffer' && Array.isArray(serialized.data))
|
|
186
|
+
return Buffer.from(serialized.data);
|
|
187
|
+
throw new Error('activity file has an unsupported content type');
|
|
188
|
+
}
|
|
189
|
+
isXml(base64) {
|
|
190
|
+
return Buffer.from(base64.substring(0, 88), 'base64').toString('utf-8').trimStart().startsWith('<');
|
|
191
|
+
}
|
|
192
|
+
getUploadFileName(fileName, format) {
|
|
193
|
+
const name = fileName.split(/[\\/]/).at(-1) ?? '';
|
|
194
|
+
const dot = name.lastIndexOf('.');
|
|
195
|
+
const base = (dot >= 0 ? name.substring(0, dot) : name).replaceAll(/[\\/?%*:|"<>.\s]/g, '_');
|
|
196
|
+
return `${base || 'activity'}.${format}`;
|
|
197
|
+
}
|
|
198
|
+
setLink(activity, link) {
|
|
199
|
+
activity.links = { ...activity.links, trainingpeaks: link };
|
|
200
|
+
}
|
|
201
|
+
ensureInitialized() {
|
|
202
|
+
if (!this.isInitialized)
|
|
203
|
+
this.init();
|
|
204
|
+
return this.isInitialized;
|
|
205
|
+
}
|
|
206
|
+
getTrainingPeaksAppConnection() {
|
|
207
|
+
return this.connection;
|
|
208
|
+
}
|
|
209
|
+
getFileSystem() {
|
|
210
|
+
return (0, api_1.getBindings)().fs;
|
|
211
|
+
}
|
|
212
|
+
getApi() {
|
|
213
|
+
return this.getTrainingPeaksAppConnection().getApi();
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
return TrainingPeaksUpload = _classThis;
|
|
217
|
+
})();
|
|
218
|
+
exports.TrainingPeaksUpload = TrainingPeaksUpload;
|
|
219
|
+
const factory = new factory_1.ActivityUploadFactory();
|
|
220
|
+
factory.add('trainingpeaks', new TrainingPeaksUpload());
|
|
@@ -4,8 +4,11 @@ exports.RestApiClient = void 0;
|
|
|
4
4
|
const api_1 = require("./api");
|
|
5
5
|
class RestApiClient {
|
|
6
6
|
static _api;
|
|
7
|
-
static getClient() {
|
|
8
|
-
|
|
7
|
+
static getClient(enableLogging) {
|
|
8
|
+
const api = this.api();
|
|
9
|
+
if (enableLogging)
|
|
10
|
+
api.enableLogging();
|
|
11
|
+
return api.client();
|
|
9
12
|
}
|
|
10
13
|
static api() {
|
|
11
14
|
if (!this._api)
|
package/lib/cjs/api/rest/api.js
CHANGED
|
@@ -11,6 +11,12 @@ class ApiClient {
|
|
|
11
11
|
axios;
|
|
12
12
|
logger;
|
|
13
13
|
requests;
|
|
14
|
+
requestInterceptorId;
|
|
15
|
+
responseInterceptorId;
|
|
16
|
+
constructor() {
|
|
17
|
+
this.logger = new gd_eventlog_1.EventLogger('API');
|
|
18
|
+
this.requests = [];
|
|
19
|
+
}
|
|
14
20
|
init(props) {
|
|
15
21
|
const { requestLog = true, version, appVersion, channel = 'desktop', platform } = props;
|
|
16
22
|
const headers = {
|
|
@@ -29,10 +35,25 @@ class ApiClient {
|
|
|
29
35
|
}
|
|
30
36
|
this.axios = axios_1.default.create({ headers });
|
|
31
37
|
if (requestLog) {
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
this.enableLogging();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
enableLogging() {
|
|
42
|
+
const client = this.client();
|
|
43
|
+
if (this.requestInterceptorId !== undefined)
|
|
44
|
+
return;
|
|
45
|
+
this.requestInterceptorId = client.interceptors.request.use((req) => this.onRequest(req));
|
|
46
|
+
this.responseInterceptorId = client.interceptors.response.use((res) => this.onResponse(res), (err) => this.onError(err));
|
|
47
|
+
}
|
|
48
|
+
disableLogging() {
|
|
49
|
+
const client = this.client();
|
|
50
|
+
if (this.requestInterceptorId !== undefined) {
|
|
51
|
+
client.interceptors.request.eject(this.requestInterceptorId);
|
|
52
|
+
this.requestInterceptorId = undefined;
|
|
53
|
+
}
|
|
54
|
+
if (this.responseInterceptorId !== undefined) {
|
|
55
|
+
client.interceptors.response.eject(this.responseInterceptorId);
|
|
56
|
+
this.responseInterceptorId = undefined;
|
|
36
57
|
}
|
|
37
58
|
}
|
|
38
59
|
client() {
|
|
@@ -74,5 +95,25 @@ class ApiClient {
|
|
|
74
95
|
}
|
|
75
96
|
return res;
|
|
76
97
|
}
|
|
98
|
+
onError(err) {
|
|
99
|
+
try {
|
|
100
|
+
const { config, response } = err ?? {};
|
|
101
|
+
const { headers = {}, url, method, params } = config ?? {};
|
|
102
|
+
const tid = headers['x-transaction-id'];
|
|
103
|
+
const idx = tid ? this.requests.findIndex(r => r.tid === tid) : -1;
|
|
104
|
+
if (idx !== -1) {
|
|
105
|
+
const tsStart = this.requests[idx].ts;
|
|
106
|
+
this.requests.splice(idx, 1);
|
|
107
|
+
this.logger.logEvent({
|
|
108
|
+
message: 'api call', method, url, tid, status: response?.status ?? 'no response',
|
|
109
|
+
duration: Date.now() - tsStart, params, error: err.message
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
this.logger.logEvent({ message: 'error', fn: 'onError', error: e.message, stack: e.stack });
|
|
115
|
+
}
|
|
116
|
+
return Promise.reject(err);
|
|
117
|
+
}
|
|
77
118
|
}
|
|
78
119
|
exports.ApiClient = ApiClient;
|
|
@@ -45,9 +45,9 @@ class AppApiBase {
|
|
|
45
45
|
const form = await fp.createForm({ url: this.getBaseUrl() + url, ...requestOpts }, uploadInfo);
|
|
46
46
|
return form;
|
|
47
47
|
}
|
|
48
|
-
getApi() {
|
|
48
|
+
getApi(enableLogging = false) {
|
|
49
49
|
if (!this.api) {
|
|
50
|
-
this.api = api_1.RestApiClient.getClient();
|
|
50
|
+
this.api = api_1.RestApiClient.getClient(enableLogging);
|
|
51
51
|
return this.api;
|
|
52
52
|
}
|
|
53
53
|
return this.api;
|
|
@@ -7,6 +7,7 @@ const base_1 = require("../base");
|
|
|
7
7
|
const types_1 = require("./types");
|
|
8
8
|
const gd_eventlog_1 = require("gd-eventlog");
|
|
9
9
|
const sleep_1 = require("../../../../utils/sleep");
|
|
10
|
+
const consts_1 = require("./consts");
|
|
10
11
|
const API_BASE_URL = 'https://www.strava.com/api/v3';
|
|
11
12
|
const OAUTH_URL = 'https://www.strava.com/oauth';
|
|
12
13
|
const AUTH_SERVER_URL = 'https://auth.incyclist.com';
|
|
@@ -21,6 +22,7 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
21
22
|
}
|
|
22
23
|
init(config) {
|
|
23
24
|
this.config = config;
|
|
25
|
+
this.observer = this.observer ?? new observer_1.Observer();
|
|
24
26
|
return this.observer;
|
|
25
27
|
}
|
|
26
28
|
update(config) {
|
|
@@ -46,6 +48,16 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
46
48
|
const response = await this.createUpload(request);
|
|
47
49
|
return await this.waitForUploadResponse(response);
|
|
48
50
|
}
|
|
51
|
+
async getLoggedInAthlete() {
|
|
52
|
+
await this.verifyAuthentication();
|
|
53
|
+
try {
|
|
54
|
+
const response = await this.get('/athlete');
|
|
55
|
+
return response.data;
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
return { props: {}, error: 'getLoggedInAthlete failed: ' + err.message };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
49
61
|
async getLoggedInAthleteActivities(props = {}) {
|
|
50
62
|
await this.verifyAuthentication();
|
|
51
63
|
try {
|
|
@@ -57,6 +69,31 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
57
69
|
return { props, error: 'getLoggedInAthleteActivities failed: ' + err.message };
|
|
58
70
|
}
|
|
59
71
|
}
|
|
72
|
+
async getLoggedInAthleteRoutes(props = {}, filters) {
|
|
73
|
+
await this.verifyAuthentication();
|
|
74
|
+
try {
|
|
75
|
+
const hasParams = Object.keys(props).length > 0;
|
|
76
|
+
const params = Object.keys(props).map(k => `${k}=${props[k]}`).join('&');
|
|
77
|
+
const url = `/athlete/routes${hasParams ? '?' + params : ''}`;
|
|
78
|
+
const response = await this.get(url);
|
|
79
|
+
const all = response.data;
|
|
80
|
+
this.logger.logEvent({ message: 'getLoggedInAthleteRoutes success', routes: all.length });
|
|
81
|
+
return this.applyFilter(all, filters);
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
return { props, error: 'getLoggedInAthleteRoutes failed: ' + err.message };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async getRouteAsGPX(id) {
|
|
88
|
+
await this.verifyAuthentication();
|
|
89
|
+
try {
|
|
90
|
+
const response = await this.get(`/routes/${id}/export_gpx`);
|
|
91
|
+
return response.data;
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
return { props: { id }, error: 'getRouteAsGPX failed: ' + err.message };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
60
97
|
async getActivityById(id, include_all_efforts = false) {
|
|
61
98
|
await this.verifyAuthentication();
|
|
62
99
|
try {
|
|
@@ -78,9 +115,19 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
78
115
|
return { id, error: 'getActivityStream failed: ' + err.message };
|
|
79
116
|
}
|
|
80
117
|
}
|
|
81
|
-
async
|
|
82
|
-
|
|
83
|
-
|
|
118
|
+
async getRoutesByAthleteId(id, props = {}, filters) {
|
|
119
|
+
await this.verifyAuthentication();
|
|
120
|
+
try {
|
|
121
|
+
const hasParams = Object.keys(props).length > 0;
|
|
122
|
+
const params = Object.keys(props).map(k => `${k}=${props[k]}`).join('&');
|
|
123
|
+
const url = `/athletes/${id}/routes${hasParams ? '?' + params : ''}`;
|
|
124
|
+
const response = await this.get(url);
|
|
125
|
+
const all = response.data;
|
|
126
|
+
return this.applyFilter(all, filters);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
return { props, error: 'getRoutesByAthleteId failed: ' + err.message };
|
|
130
|
+
}
|
|
84
131
|
}
|
|
85
132
|
async verifyToken() {
|
|
86
133
|
if (!this.isTokenStillValid()) {
|
|
@@ -186,6 +233,34 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
186
233
|
this.logger.logEvent({ message: 'Strava refresh Token result', status: 'error', error: errorStr });
|
|
187
234
|
}
|
|
188
235
|
}
|
|
236
|
+
applyFilter(routes, filters) {
|
|
237
|
+
if (!filters)
|
|
238
|
+
return routes;
|
|
239
|
+
const sportTypeFilter = this.getSportTypeFilter(filters?.sport);
|
|
240
|
+
return routes.filter(route => {
|
|
241
|
+
let valid = true;
|
|
242
|
+
if (sportTypeFilter.length > 0)
|
|
243
|
+
valid = valid && (sportTypeFilter.includes(route.type));
|
|
244
|
+
if (filters?.after)
|
|
245
|
+
valid = valid && (new Date(route.created_at).valueOf() > filters.after.valueOf());
|
|
246
|
+
if (filters?.lastUpdateAfter)
|
|
247
|
+
valid = valid && (new Date(route.updated_at).valueOf() > filters.lastUpdateAfter.valueOf());
|
|
248
|
+
return valid;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
getSportTypeFilter(filterStr) {
|
|
252
|
+
if (!filterStr)
|
|
253
|
+
return [];
|
|
254
|
+
const sportTypeFilter = filterStr?.split(',') ?? [];
|
|
255
|
+
const transformed = [];
|
|
256
|
+
for (const sport of sportTypeFilter) {
|
|
257
|
+
const stravaType = consts_1.StringToStravaRouteType[sport];
|
|
258
|
+
if (stravaType) {
|
|
259
|
+
transformed.push(stravaType);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return transformed;
|
|
263
|
+
}
|
|
189
264
|
async get(url, config) {
|
|
190
265
|
await this.verifyToken();
|
|
191
266
|
const props = config ?? {};
|
|
@@ -224,5 +299,8 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
224
299
|
return API_BASE_URL;
|
|
225
300
|
}
|
|
226
301
|
}
|
|
302
|
+
getApi() {
|
|
303
|
+
return super.getApi(true);
|
|
304
|
+
}
|
|
227
305
|
}
|
|
228
306
|
exports.StravaApi = StravaApi;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringToStravaRouteType = exports.StravaRouteTypeToString = exports.StringToStravaRouteSubType = exports.StravaRouteSubTypeToString = void 0;
|
|
4
|
+
exports.StravaRouteSubTypeToString = {
|
|
5
|
+
1: 'racebike',
|
|
6
|
+
2: 'mtb',
|
|
7
|
+
3: 'cross',
|
|
8
|
+
4: 'trail',
|
|
9
|
+
5: 'mixed'
|
|
10
|
+
};
|
|
11
|
+
exports.StringToStravaRouteSubType = {
|
|
12
|
+
racebike: 1,
|
|
13
|
+
mtb: 2,
|
|
14
|
+
cross: 3,
|
|
15
|
+
trail: 4,
|
|
16
|
+
mixed: 5
|
|
17
|
+
};
|
|
18
|
+
exports.StravaRouteTypeToString = {
|
|
19
|
+
1: 'ride',
|
|
20
|
+
2: 'run',
|
|
21
|
+
3: 'walk',
|
|
22
|
+
4: 'hike',
|
|
23
|
+
5: 'trailrun',
|
|
24
|
+
6: 'gravel',
|
|
25
|
+
7: 'mtb'
|
|
26
|
+
};
|
|
27
|
+
exports.StringToStravaRouteType = {
|
|
28
|
+
ride: 1,
|
|
29
|
+
run: 2,
|
|
30
|
+
walk: 3,
|
|
31
|
+
hike: 4,
|
|
32
|
+
trailrun: 5,
|
|
33
|
+
gravel: 6,
|
|
34
|
+
mtb: 7
|
|
35
|
+
};
|