incyclist-services 1.9.30 → 1.9.32
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 +2 -0
- package/lib/cjs/activities/upload/komoot.js +2 -2
- package/lib/cjs/activities/upload/ridewithgps.js +158 -0
- package/lib/cjs/activities/upload/trainerday.js +216 -0
- package/lib/cjs/activities/upload/trainingpeaks.js +2 -2
- package/lib/cjs/apps/base/api/index.js +2 -0
- package/lib/cjs/apps/base/api/oauth.js +170 -0
- package/lib/cjs/apps/base/api/ridewithgps/api.js +129 -0
- package/lib/cjs/apps/base/api/ridewithgps/index.js +19 -0
- package/lib/cjs/apps/base/api/ridewithgps/types.js +18 -0
- package/lib/cjs/apps/base/api/ridewithgps/utils.js +42 -0
- package/lib/cjs/apps/base/api/trainerday/api.js +58 -0
- package/lib/cjs/apps/base/api/trainerday/index.js +18 -0
- package/lib/cjs/apps/base/api/trainerday/types.js +4 -0
- package/lib/cjs/apps/base/api/trainingpeaks/api.js +17 -133
- package/lib/cjs/apps/base/app/OAuthAppConnection.js +21 -4
- package/lib/cjs/apps/base/utils.js +34 -1
- package/lib/cjs/apps/index.js +2 -0
- package/lib/cjs/apps/ridewithgps/RideWithGpsAppConnection.js +83 -0
- package/lib/cjs/apps/ridewithgps/index.js +18 -0
- package/lib/cjs/apps/ridewithgps/types.js +2 -0
- package/lib/cjs/apps/service.js +13 -1
- package/lib/cjs/apps/trainerday/TrainerDayAppConnection.js +91 -0
- package/lib/cjs/apps/trainerday/index.js +18 -0
- package/lib/cjs/apps/trainerday/types.js +2 -0
- package/lib/cjs/coaches/coach.js +14 -6
- package/lib/cjs/devices/ride/service.js +42 -0
- package/lib/cjs/ride/base/base.js +14 -0
- package/lib/cjs/ride/route/RLVDisplayService.js +18 -4
- package/lib/cjs/routes/sync/base/provider.js +51 -9
- package/lib/cjs/routes/sync/index.js +1 -0
- package/lib/cjs/routes/sync/komoot/provider.js +1 -27
- package/lib/cjs/routes/sync/ridewithgps/provider.js +182 -0
- package/lib/cjs/routes/sync/strava/provider.js +1 -28
- package/lib/cjs/utils/base64.js +52 -0
- package/lib/cjs/utils/index.js +2 -0
- package/lib/cjs/utils/utf8.js +66 -0
- package/lib/cjs/utils/xml.js +16 -1
- package/lib/cjs/video/VideoConversion.js +3 -0
- package/lib/cjs/workouts/base/parsers/trainerday/converter.js +139 -0
- package/lib/cjs/workouts/base/parsers/trainerday/types.js +2 -0
- package/lib/cjs/workouts/calendar/sync/base/provider.js +150 -0
- package/lib/cjs/workouts/calendar/sync/base/types.js +2 -0
- package/lib/cjs/workouts/calendar/sync/index.js +1 -0
- package/lib/cjs/workouts/calendar/sync/trainerday/provider.js +142 -0
- package/lib/cjs/workouts/calendar/sync/trainingpeaks/provider.js +26 -101
- package/lib/esm/activities/upload/index.js +2 -0
- package/lib/esm/activities/upload/komoot.js +2 -2
- package/lib/esm/activities/upload/ridewithgps.js +155 -0
- package/lib/esm/activities/upload/trainerday.js +213 -0
- package/lib/esm/activities/upload/trainingpeaks.js +1 -1
- package/lib/esm/apps/base/api/index.js +2 -0
- package/lib/esm/apps/base/api/oauth.js +166 -0
- package/lib/esm/apps/base/api/ridewithgps/api.js +125 -0
- package/lib/esm/apps/base/api/ridewithgps/index.js +3 -0
- package/lib/esm/apps/base/api/ridewithgps/types.js +12 -0
- package/lib/esm/apps/base/api/ridewithgps/utils.js +34 -0
- package/lib/esm/apps/base/api/trainerday/api.js +54 -0
- package/lib/esm/apps/base/api/trainerday/index.js +2 -0
- package/lib/esm/apps/base/api/trainerday/types.js +1 -0
- package/lib/esm/apps/base/api/trainerday/utils.js +117 -0
- package/lib/esm/apps/base/api/trainingpeaks/api.js +15 -131
- package/lib/esm/apps/base/app/OAuthAppConnection.js +21 -4
- package/lib/esm/apps/base/utils.js +29 -0
- package/lib/esm/apps/index.js +2 -0
- package/lib/esm/apps/ridewithgps/RideWithGpsAppConnection.js +80 -0
- package/lib/esm/apps/ridewithgps/index.js +2 -0
- package/lib/esm/apps/ridewithgps/types.js +1 -0
- package/lib/esm/apps/service.js +13 -1
- package/lib/esm/apps/trainerday/TrainerDayAppConnection.js +88 -0
- package/lib/esm/apps/trainerday/index.js +2 -0
- package/lib/esm/apps/trainerday/types.js +1 -0
- package/lib/esm/coaches/coach.js +14 -6
- package/lib/esm/devices/ride/service.js +42 -0
- package/lib/esm/ride/base/base.js +14 -0
- package/lib/esm/ride/route/RLVDisplayService.js +18 -4
- package/lib/esm/routes/sync/base/provider.js +51 -9
- package/lib/esm/routes/sync/index.js +1 -0
- package/lib/esm/routes/sync/komoot/provider.js +1 -27
- package/lib/esm/routes/sync/ridewithgps/provider.js +178 -0
- package/lib/esm/routes/sync/strava/provider.js +1 -28
- package/lib/esm/utils/base64.js +45 -0
- package/lib/esm/utils/index.js +2 -0
- package/lib/esm/utils/utf8.js +59 -0
- package/lib/esm/utils/xml.js +14 -0
- package/lib/esm/video/VideoConversion.js +3 -0
- package/lib/esm/workouts/base/parsers/trainerday/converter.js +133 -0
- package/lib/esm/workouts/base/parsers/trainerday/types.js +1 -0
- package/lib/esm/workouts/calendar/sync/base/provider.js +147 -0
- package/lib/esm/workouts/calendar/sync/base/types.js +1 -0
- package/lib/esm/workouts/calendar/sync/index.js +1 -0
- package/lib/esm/workouts/calendar/sync/trainerday/provider.js +139 -0
- package/lib/esm/workouts/calendar/sync/trainingpeaks/provider.js +26 -101
- package/lib/types/activities/upload/index.d.ts +2 -0
- package/lib/types/activities/upload/ridewithgps.d.ts +30 -0
- package/lib/types/activities/upload/trainerday.d.ts +32 -0
- package/lib/types/activities/upload/types.d.ts +1 -0
- package/lib/types/apps/base/api/index.d.ts +2 -0
- package/lib/types/apps/base/api/oauth.d.ts +35 -0
- package/lib/types/apps/base/api/ridewithgps/api.d.ts +18 -0
- package/lib/types/apps/base/api/ridewithgps/index.d.ts +3 -0
- package/lib/types/apps/base/api/ridewithgps/types.d.ts +104 -0
- package/lib/types/apps/base/api/ridewithgps/utils.d.ts +9 -0
- package/lib/types/apps/base/api/trainerday/api.d.ts +9 -0
- package/lib/types/apps/base/api/trainerday/index.d.ts +2 -0
- package/lib/types/apps/base/api/trainerday/types.d.ts +62 -0
- package/lib/types/apps/base/api/trainerday/utils.d.ts +13 -0
- package/lib/types/apps/base/api/trainingpeaks/api.d.ts +2 -24
- package/lib/types/apps/base/api/trainingpeaks/types.d.ts +3 -13
- package/lib/types/apps/base/api/types.d.ts +21 -0
- package/lib/types/apps/base/app/OAuthAppConnection.d.ts +6 -1
- package/lib/types/apps/base/types.d.ts +1 -1
- package/lib/types/apps/base/utils.d.ts +7 -0
- package/lib/types/apps/index.d.ts +2 -0
- package/lib/types/apps/ridewithgps/RideWithGpsAppConnection.d.ts +13 -0
- package/lib/types/apps/ridewithgps/index.d.ts +2 -0
- package/lib/types/apps/ridewithgps/types.d.ts +2 -0
- package/lib/types/apps/service.d.ts +1 -0
- package/lib/types/apps/trainerday/TrainerDayAppConnection.d.ts +13 -0
- package/lib/types/apps/trainerday/index.d.ts +2 -0
- package/lib/types/apps/trainerday/types.d.ts +2 -0
- package/lib/types/apps/types.d.ts +2 -0
- package/lib/types/coaches/coach.d.ts +1 -0
- package/lib/types/devices/ride/service.d.ts +5 -1
- package/lib/types/ride/base/base.d.ts +1 -0
- package/lib/types/ride/base/types.d.ts +7 -0
- package/lib/types/ride/display/service.d.ts +1 -1
- package/lib/types/ride/route/RLVDisplayService.d.ts +3 -2
- package/lib/types/routes/sync/base/provider.d.ts +5 -1
- package/lib/types/routes/sync/index.d.ts +1 -0
- package/lib/types/routes/sync/ridewithgps/provider.d.ts +41 -0
- package/lib/types/utils/base64.d.ts +4 -0
- package/lib/types/utils/index.d.ts +2 -0
- package/lib/types/utils/utf8.d.ts +4 -0
- package/lib/types/utils/xml.d.ts +1 -0
- package/lib/types/video/VideoConversion.d.ts +1 -0
- package/lib/types/workouts/base/parsers/trainerday/converter.d.ts +7 -0
- package/lib/types/workouts/base/parsers/trainerday/types.d.ts +5 -0
- package/lib/types/workouts/calendar/sync/base/provider.d.ts +26 -0
- package/lib/types/workouts/calendar/sync/base/types.d.ts +3 -0
- package/lib/types/workouts/calendar/sync/index.d.ts +1 -0
- package/lib/types/workouts/calendar/sync/trainerday/provider.d.ts +19 -0
- package/lib/types/workouts/calendar/sync/trainingpeaks/provider.d.ts +7 -14
- package/package.json +2 -2
|
@@ -18,5 +18,7 @@ __exportStar(require("./strava"), exports);
|
|
|
18
18
|
__exportStar(require("./intervals"), exports);
|
|
19
19
|
__exportStar(require("./komoot"), exports);
|
|
20
20
|
__exportStar(require("./trainingpeaks"), exports);
|
|
21
|
+
__exportStar(require("./trainerday"), exports);
|
|
21
22
|
__exportStar(require("./types"), exports);
|
|
22
23
|
__exportStar(require("./factory"), exports);
|
|
24
|
+
__exportStar(require("./ridewithgps"), exports);
|
|
@@ -100,7 +100,7 @@ let KomootUpload = (() => {
|
|
|
100
100
|
if (!data) {
|
|
101
101
|
throw new Error(`file could not be read: ${fileName}`);
|
|
102
102
|
}
|
|
103
|
-
const res = await this.getApi().uploadTour(Buffer.from(data), uploadFormat, sport, activity.title);
|
|
103
|
+
const res = await this.getApi().uploadTour(typeof data === 'string' ? Buffer.from(data, 'base64') : Buffer.from(data), uploadFormat, sport, activity.title);
|
|
104
104
|
this.logEvent({ message: 'Komoot Upload success', activityId: res.id });
|
|
105
105
|
activity.links.komoot = {
|
|
106
106
|
activity_id: res.id.toString(),
|
|
@@ -142,7 +142,7 @@ let KomootUpload = (() => {
|
|
|
142
142
|
const exists = await fs.existsFile(fileName);
|
|
143
143
|
if (!exists)
|
|
144
144
|
return null;
|
|
145
|
-
const content = await fs.readFile(fileName);
|
|
145
|
+
const content = await fs.readFile(fileName, 'base64');
|
|
146
146
|
return content || null;
|
|
147
147
|
}
|
|
148
148
|
catch (err) {
|
|
@@ -0,0 +1,158 @@
|
|
|
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.RideWithGpsUpload = exports.RIDEWITHGPS_URL = void 0;
|
|
38
|
+
const ridewithgps_1 = require("../../apps/base/api/ridewithgps");
|
|
39
|
+
const RideWithGpsAppConnection_1 = require("../../apps/ridewithgps/RideWithGpsAppConnection");
|
|
40
|
+
const Injection_1 = require("../../base/decorators/Injection");
|
|
41
|
+
const service_1 = require("../../base/service");
|
|
42
|
+
const types_1 = require("../../base/types");
|
|
43
|
+
const factory_1 = require("./factory");
|
|
44
|
+
exports.RIDEWITHGPS_URL = 'https://ridewithgps.com';
|
|
45
|
+
const FORMATS = ['tcx', 'fit'];
|
|
46
|
+
let RideWithGpsUpload = (() => {
|
|
47
|
+
let _classDecorators = [types_1.Singleton];
|
|
48
|
+
let _classDescriptor;
|
|
49
|
+
let _classExtraInitializers = [];
|
|
50
|
+
let _classThis;
|
|
51
|
+
let _classSuper = service_1.IncyclistService;
|
|
52
|
+
let _instanceExtraInitializers = [];
|
|
53
|
+
let _getRideWithGpsAppConnection_decorators;
|
|
54
|
+
var RideWithGpsUpload = class extends _classSuper {
|
|
55
|
+
static { _classThis = this; }
|
|
56
|
+
static {
|
|
57
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
58
|
+
_getRideWithGpsAppConnection_decorators = [Injection_1.Injectable];
|
|
59
|
+
__esDecorate(this, null, _getRideWithGpsAppConnection_decorators, { kind: "method", name: "getRideWithGpsAppConnection", static: false, private: false, access: { has: obj => "getRideWithGpsAppConnection" in obj, get: obj => obj.getRideWithGpsAppConnection }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
60
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
61
|
+
RideWithGpsUpload = _classThis = _classDescriptor.value;
|
|
62
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
63
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
64
|
+
}
|
|
65
|
+
isInitialized = __runInitializers(this, _instanceExtraInitializers);
|
|
66
|
+
connection = new RideWithGpsAppConnection_1.RideWithGpsAppConnection();
|
|
67
|
+
constructor() {
|
|
68
|
+
super('RideWithGpsUpload');
|
|
69
|
+
this.isInitialized = false;
|
|
70
|
+
this.init();
|
|
71
|
+
}
|
|
72
|
+
init() {
|
|
73
|
+
if (this.isInitialized)
|
|
74
|
+
return true;
|
|
75
|
+
this.isInitialized = this.getRideWithGpsAppConnection().init();
|
|
76
|
+
return this.isInitialized;
|
|
77
|
+
}
|
|
78
|
+
isConnected() {
|
|
79
|
+
return this.getRideWithGpsAppConnection().isConnected();
|
|
80
|
+
}
|
|
81
|
+
async upload(activity, format = 'TCX') {
|
|
82
|
+
if (!this.ensureInitialized()) {
|
|
83
|
+
this.logEvent({ message: 'RideWithGPS Upload skipped', reason: 'not initialized' });
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (!this.isConnected())
|
|
87
|
+
return false;
|
|
88
|
+
try {
|
|
89
|
+
const res = await this.uploadFirstAccepted(activity, format.toLowerCase());
|
|
90
|
+
this.logEvent({ message: 'RideWithGPS Upload success', activityId: res.tripId, duplicate: res.duplicate });
|
|
91
|
+
this.setLink(activity, this.buildLink(res));
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
const error = err?.message ?? String(err);
|
|
96
|
+
this.logEvent({ message: 'RideWithGPS Upload failure', error });
|
|
97
|
+
this.setLink(activity, { error });
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
getUrl(activityId) {
|
|
102
|
+
return `${exports.RIDEWITHGPS_URL}/trips/${activityId}`;
|
|
103
|
+
}
|
|
104
|
+
async uploadFirstAccepted(activity, preferred) {
|
|
105
|
+
const formats = [preferred, ...FORMATS.filter(f => f !== preferred)].filter(f => FORMATS.includes(f) && activity[`${f}FileName`]);
|
|
106
|
+
if (formats.length === 0)
|
|
107
|
+
throw new Error('no TCX or FIT file available for this activity');
|
|
108
|
+
let lastError;
|
|
109
|
+
for (const format of formats) {
|
|
110
|
+
try {
|
|
111
|
+
this.logEvent({ message: 'RideWithGPS Upload', format: format.toUpperCase() });
|
|
112
|
+
return await this.getApi().uploadActivity({
|
|
113
|
+
fileName: activity[`${format}FileName`],
|
|
114
|
+
name: activity.title?.trim() || undefined,
|
|
115
|
+
activityType: this.getActivityType(activity)
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
if (this.isFinal(err))
|
|
120
|
+
throw err;
|
|
121
|
+
this.logEvent({ message: 'RideWithGPS Upload not accepted', format: format.toUpperCase(), error: err.message });
|
|
122
|
+
lastError = err;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
throw lastError;
|
|
126
|
+
}
|
|
127
|
+
isFinal(err) {
|
|
128
|
+
return err instanceof ridewithgps_1.RideWithGpsTimeDataMissingError || err instanceof ridewithgps_1.RideWithGpsAuthError || err instanceof ridewithgps_1.RideWithGpsTimeoutError;
|
|
129
|
+
}
|
|
130
|
+
getActivityType(activity) {
|
|
131
|
+
const isFreeRide = activity.routeType === 'Free-Ride' || activity.routeType === 'None';
|
|
132
|
+
return isFreeRide ? 'cycling:indoor' : 'cycling:virtual';
|
|
133
|
+
}
|
|
134
|
+
buildLink(res) {
|
|
135
|
+
if (res.tripId)
|
|
136
|
+
return { activity_id: res.tripId, url: this.getUrl(res.tripId) };
|
|
137
|
+
return { activity_id: 'existing', url: `${exports.RIDEWITHGPS_URL}/trips` };
|
|
138
|
+
}
|
|
139
|
+
setLink(activity, link) {
|
|
140
|
+
activity.links = { ...activity.links, ridewithgps: link };
|
|
141
|
+
}
|
|
142
|
+
ensureInitialized() {
|
|
143
|
+
if (!this.isInitialized)
|
|
144
|
+
this.init();
|
|
145
|
+
return this.isInitialized;
|
|
146
|
+
}
|
|
147
|
+
getRideWithGpsAppConnection() {
|
|
148
|
+
return this.connection;
|
|
149
|
+
}
|
|
150
|
+
getApi() {
|
|
151
|
+
return this.getRideWithGpsAppConnection().getApi();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
return RideWithGpsUpload = _classThis;
|
|
155
|
+
})();
|
|
156
|
+
exports.RideWithGpsUpload = RideWithGpsUpload;
|
|
157
|
+
const factory = new factory_1.ActivityUploadFactory();
|
|
158
|
+
factory.add('ridewithgps', new RideWithGpsUpload());
|
|
@@ -0,0 +1,216 @@
|
|
|
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.TrainerDayUpload = exports.TRAINERDAY_ACTIVITY_URL = void 0;
|
|
38
|
+
const api_1 = require("../../api");
|
|
39
|
+
const TrainerDayAppConnection_1 = require("../../apps/trainerday/TrainerDayAppConnection");
|
|
40
|
+
const Injection_1 = require("../../base/decorators/Injection");
|
|
41
|
+
const service_1 = require("../../base/service");
|
|
42
|
+
const types_1 = require("../../base/types");
|
|
43
|
+
const factory_1 = require("./factory");
|
|
44
|
+
const base64_1 = require("../../utils/base64");
|
|
45
|
+
const utf8_1 = require("../../utils/utf8");
|
|
46
|
+
exports.TRAINERDAY_ACTIVITY_URL = 'https://app.trainerday.com/activities';
|
|
47
|
+
const SUPPORTED_FORMATS = new Set(['tcx', 'fit']);
|
|
48
|
+
let TrainerDayUpload = (() => {
|
|
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 _getTrainerDayAppConnection_decorators;
|
|
56
|
+
let _getFileSystem_decorators;
|
|
57
|
+
var TrainerDayUpload = 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
|
+
_getTrainerDayAppConnection_decorators = [Injection_1.Injectable];
|
|
62
|
+
_getFileSystem_decorators = [Injection_1.Injectable];
|
|
63
|
+
__esDecorate(this, null, _getTrainerDayAppConnection_decorators, { kind: "method", name: "getTrainerDayAppConnection", static: false, private: false, access: { has: obj => "getTrainerDayAppConnection" in obj, get: obj => obj.getTrainerDayAppConnection }, 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
|
+
TrainerDayUpload = _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 TrainerDayAppConnection_1.TrainerDayAppConnection();
|
|
72
|
+
constructor() {
|
|
73
|
+
super('TrainerDayUpload');
|
|
74
|
+
this.isInitialized = false;
|
|
75
|
+
this.init();
|
|
76
|
+
}
|
|
77
|
+
init() {
|
|
78
|
+
if (this.isInitialized)
|
|
79
|
+
return true;
|
|
80
|
+
this.isInitialized = this.getTrainerDayAppConnection().init();
|
|
81
|
+
return this.isInitialized;
|
|
82
|
+
}
|
|
83
|
+
isConnected() {
|
|
84
|
+
return this.getTrainerDayAppConnection().isConnected();
|
|
85
|
+
}
|
|
86
|
+
async upload(activity, format = 'TCX') {
|
|
87
|
+
if (!this.ensureInitialized()) {
|
|
88
|
+
this.logEvent({ message: 'TrainerDay 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: 'TrainerDay Upload', format: lcFormat.toUpperCase() });
|
|
96
|
+
const dataType = this.getDataType(lcFormat);
|
|
97
|
+
const fileName = this.getActivityFile(activity, lcFormat);
|
|
98
|
+
const data = await this.readFile(fileName, dataType);
|
|
99
|
+
const res = await this.getApi().uploadActivity({
|
|
100
|
+
name: this.getName(activity),
|
|
101
|
+
dataType,
|
|
102
|
+
data,
|
|
103
|
+
externalId: activity.id,
|
|
104
|
+
sportType: activity.sport === 'rowing' ? undefined : 'bike'
|
|
105
|
+
});
|
|
106
|
+
const activityId = res?.activityId === undefined || res.activityId === null ? '' : String(res.activityId);
|
|
107
|
+
if (!activityId)
|
|
108
|
+
throw new Error('TrainerDay did not return an activity id');
|
|
109
|
+
this.logEvent({ message: 'TrainerDay Upload success', activityId });
|
|
110
|
+
this.setLink(activity, { activity_id: activityId, url: this.getUrl(activityId) });
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
const error = err?.message ?? String(err);
|
|
115
|
+
this.logEvent({ message: 'TrainerDay Upload failure', error });
|
|
116
|
+
this.setLink(activity, { error });
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
getUrl(activityId) {
|
|
121
|
+
return `${exports.TRAINERDAY_ACTIVITY_URL}/${activityId}`;
|
|
122
|
+
}
|
|
123
|
+
getDataType(format) {
|
|
124
|
+
if (!SUPPORTED_FORMATS.has(format))
|
|
125
|
+
throw new Error(`format ${format.toUpperCase()} is not supported by TrainerDay`);
|
|
126
|
+
return format;
|
|
127
|
+
}
|
|
128
|
+
getActivityFile(activity, format) {
|
|
129
|
+
const fileName = activity[`${format}FileName`];
|
|
130
|
+
if (!fileName)
|
|
131
|
+
throw new Error(`no ${format.toUpperCase()} file available for this activity`);
|
|
132
|
+
return fileName;
|
|
133
|
+
}
|
|
134
|
+
getName(activity) {
|
|
135
|
+
const title = activity.title?.trim();
|
|
136
|
+
if (title)
|
|
137
|
+
return title;
|
|
138
|
+
const routeName = (activity.route?.title ?? activity.route?.name)?.trim();
|
|
139
|
+
if (routeName)
|
|
140
|
+
return routeName;
|
|
141
|
+
const start = new Date(activity.startTime);
|
|
142
|
+
return Number.isNaN(start.valueOf()) ? 'Incyclist ride' : `Incyclist ride ${start.toISOString().substring(0, 10)}`;
|
|
143
|
+
}
|
|
144
|
+
async readFile(fileName, format) {
|
|
145
|
+
const fs = this.getFileSystem();
|
|
146
|
+
if (!(await fs.existsFile(fileName)))
|
|
147
|
+
throw new Error('activity file not found');
|
|
148
|
+
const content = await fs.readFile(fileName, 'base64');
|
|
149
|
+
if (!content)
|
|
150
|
+
throw new Error('activity file is empty');
|
|
151
|
+
const contentType = this.getContentType(content);
|
|
152
|
+
const data = this.toData(content, format);
|
|
153
|
+
const isXml = format === 'fit' ? undefined : this.isXml(data);
|
|
154
|
+
this.logEvent({ message: 'TrainerDay Upload file', contentType, bytes: this.getSize(data), isXml });
|
|
155
|
+
if (isXml === false)
|
|
156
|
+
throw new Error(`activity file does not contain xml (content type: ${contentType})`);
|
|
157
|
+
return data;
|
|
158
|
+
}
|
|
159
|
+
getContentType(content) {
|
|
160
|
+
if (typeof content !== 'object' || content === null)
|
|
161
|
+
return typeof content;
|
|
162
|
+
return content.constructor?.name ?? 'object';
|
|
163
|
+
}
|
|
164
|
+
toData(content, format) {
|
|
165
|
+
if (typeof content === 'string') {
|
|
166
|
+
const compact = content.replaceAll(/\s/g, '');
|
|
167
|
+
if (compact.length % 4 === 0 && (0, base64_1.isBase64)(compact))
|
|
168
|
+
return compact;
|
|
169
|
+
if (format === 'fit')
|
|
170
|
+
throw new Error('binary file could not be read');
|
|
171
|
+
return new TextEncoder().encode(content);
|
|
172
|
+
}
|
|
173
|
+
return this.toBytes(content);
|
|
174
|
+
}
|
|
175
|
+
toBytes(content) {
|
|
176
|
+
if (ArrayBuffer.isView(content))
|
|
177
|
+
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
|
|
178
|
+
if (content instanceof ArrayBuffer)
|
|
179
|
+
return new Uint8Array(content);
|
|
180
|
+
const serialized = content;
|
|
181
|
+
if (serialized?.type === 'Buffer' && Array.isArray(serialized.data))
|
|
182
|
+
return Uint8Array.from(serialized.data);
|
|
183
|
+
throw new Error('activity file has an unsupported content type');
|
|
184
|
+
}
|
|
185
|
+
getSize(data) {
|
|
186
|
+
return typeof data === 'string' ? (0, base64_1.base64Size)(data) : data.byteLength;
|
|
187
|
+
}
|
|
188
|
+
isXml(data) {
|
|
189
|
+
const head = typeof data === 'string'
|
|
190
|
+
? (0, base64_1.fromBase64)(data.substring(0, 88))
|
|
191
|
+
: data.subarray(0, 66);
|
|
192
|
+
return (0, utf8_1.utf8Decode)(head).replace(/^\u{FEFF}/u, '').trimStart().startsWith('<');
|
|
193
|
+
}
|
|
194
|
+
setLink(activity, link) {
|
|
195
|
+
activity.links = { ...activity.links, trainerday: link };
|
|
196
|
+
}
|
|
197
|
+
ensureInitialized() {
|
|
198
|
+
if (!this.isInitialized)
|
|
199
|
+
this.init();
|
|
200
|
+
return this.isInitialized;
|
|
201
|
+
}
|
|
202
|
+
getTrainerDayAppConnection() {
|
|
203
|
+
return this.connection;
|
|
204
|
+
}
|
|
205
|
+
getFileSystem() {
|
|
206
|
+
return (0, api_1.getBindings)().fs;
|
|
207
|
+
}
|
|
208
|
+
getApi() {
|
|
209
|
+
return this.getTrainerDayAppConnection().getApi();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
return TrainerDayUpload = _classThis;
|
|
213
|
+
})();
|
|
214
|
+
exports.TrainerDayUpload = TrainerDayUpload;
|
|
215
|
+
const factory = new factory_1.ActivityUploadFactory();
|
|
216
|
+
factory.add('trainerday', new TrainerDayUpload());
|
|
@@ -41,7 +41,7 @@ const Injection_1 = require("../../base/decorators/Injection");
|
|
|
41
41
|
const service_1 = require("../../base/service");
|
|
42
42
|
const types_1 = require("../../base/types");
|
|
43
43
|
const trainingpeaks_1 = require("../../apps/base/api/trainingpeaks");
|
|
44
|
-
const
|
|
44
|
+
const base64_1 = require("../../utils/base64");
|
|
45
45
|
const factory_1 = require("./factory");
|
|
46
46
|
const SUPPORTED_FORMATS = new Set(['tcx', 'fit']);
|
|
47
47
|
const EXISTING_ACTIVITY = 'existing';
|
|
@@ -168,7 +168,7 @@ let TrainingPeaksUpload = (() => {
|
|
|
168
168
|
toBase64(content, format) {
|
|
169
169
|
if (typeof content === 'string') {
|
|
170
170
|
const compact = content.replaceAll(/\s/g, '');
|
|
171
|
-
if ((0,
|
|
171
|
+
if ((0, base64_1.isBase64)(compact))
|
|
172
172
|
return compact;
|
|
173
173
|
if (format === 'fit')
|
|
174
174
|
throw new Error('binary file could not be read');
|
|
@@ -19,3 +19,5 @@ __exportStar(require("./strava"), exports);
|
|
|
19
19
|
__exportStar(require("./komoot"), exports);
|
|
20
20
|
__exportStar(require("./intervals"), exports);
|
|
21
21
|
__exportStar(require("./trainingpeaks"), exports);
|
|
22
|
+
__exportStar(require("./trainerday"), exports);
|
|
23
|
+
__exportStar(require("./ridewithgps"), exports);
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OAuthAppApiBase = void 0;
|
|
4
|
+
const gd_eventlog_1 = require("gd-eventlog");
|
|
5
|
+
const observer_1 = require("../../../base/types/observer");
|
|
6
|
+
const valid_1 = require("../../../utils/valid");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
8
|
+
const base_1 = require("./base");
|
|
9
|
+
const AUTH_SERVER_URL = 'https://auth.incyclist.com';
|
|
10
|
+
class OAuthAppApiBase extends base_1.AppApiBase {
|
|
11
|
+
props;
|
|
12
|
+
config;
|
|
13
|
+
logger;
|
|
14
|
+
observer;
|
|
15
|
+
authFailed = false;
|
|
16
|
+
refreshPromise;
|
|
17
|
+
constructor(loggerName, props) {
|
|
18
|
+
super();
|
|
19
|
+
this.props = props;
|
|
20
|
+
this.logger = new gd_eventlog_1.EventLogger(loggerName);
|
|
21
|
+
this.observer = new observer_1.Observer();
|
|
22
|
+
}
|
|
23
|
+
init(config) {
|
|
24
|
+
this.config = config;
|
|
25
|
+
if (config)
|
|
26
|
+
this.authFailed = false;
|
|
27
|
+
return this.observer;
|
|
28
|
+
}
|
|
29
|
+
update(config) {
|
|
30
|
+
if (!this.config)
|
|
31
|
+
throw new Error('update not allowed before init');
|
|
32
|
+
this.config = { ...this.config, ...config };
|
|
33
|
+
this.authFailed = false;
|
|
34
|
+
}
|
|
35
|
+
isAuthenticated() {
|
|
36
|
+
return (0, valid_1.valid)(this.config?.accessToken);
|
|
37
|
+
}
|
|
38
|
+
async request(method, url, data, config, options = {}) {
|
|
39
|
+
await this.verifyAuthentication();
|
|
40
|
+
const request = {
|
|
41
|
+
method,
|
|
42
|
+
url: this.getBaseUrl() + url,
|
|
43
|
+
data,
|
|
44
|
+
headers: this.getAuthHeaders(),
|
|
45
|
+
validateStatus: (status) => status >= 200 && status < 300,
|
|
46
|
+
...config
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
return await this.getApi().request(request);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
if (options.checkRevoked !== false)
|
|
53
|
+
await this.checkRevoked(err);
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async checkRevoked(err) {
|
|
58
|
+
const { refreshPath, authCheckPath, name } = this.props;
|
|
59
|
+
if (refreshPath || !authCheckPath || this.authFailed || err?.response?.status !== 401)
|
|
60
|
+
return;
|
|
61
|
+
if (!(await this.isTokenRejected(authCheckPath)) || this.authFailed)
|
|
62
|
+
return;
|
|
63
|
+
this.authFailed = true;
|
|
64
|
+
this.logger.logEvent({ message: `${name} access token rejected`, status: 'error', error: 'HTTP 401, reconnect required' });
|
|
65
|
+
this.observer.emit('auth.failed', name);
|
|
66
|
+
}
|
|
67
|
+
async isTokenRejected(path) {
|
|
68
|
+
try {
|
|
69
|
+
await this.getApi().request({
|
|
70
|
+
method: 'get',
|
|
71
|
+
url: this.getBaseUrl() + path,
|
|
72
|
+
headers: this.getAuthHeaders(),
|
|
73
|
+
validateStatus: (status) => status >= 200 && status < 300
|
|
74
|
+
});
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
return err?.response?.status === 401;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async get(url, config) {
|
|
82
|
+
return await this.request('get', url, undefined, config);
|
|
83
|
+
}
|
|
84
|
+
getAuthHeaders() {
|
|
85
|
+
if (!this.config?.accessToken)
|
|
86
|
+
throw new Error('not authenticated');
|
|
87
|
+
return { Authorization: 'Bearer ' + this.config.accessToken };
|
|
88
|
+
}
|
|
89
|
+
mapUploadRejection(err) {
|
|
90
|
+
const status = err.response?.status;
|
|
91
|
+
if (status) {
|
|
92
|
+
const detail = (0, utils_1.getErrorDetail)(err.response.data);
|
|
93
|
+
return new Error(`Upload rejected by ${this.props.name} (HTTP ${status})${detail ? ': ' + detail : ''}`);
|
|
94
|
+
}
|
|
95
|
+
return err;
|
|
96
|
+
}
|
|
97
|
+
async verifyAuthentication() {
|
|
98
|
+
if (!this.isAuthenticated())
|
|
99
|
+
throw new Error('not authenticated');
|
|
100
|
+
await this.verifyToken();
|
|
101
|
+
}
|
|
102
|
+
async verifyToken() {
|
|
103
|
+
if (this.isTokenStillValid())
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
106
|
+
await this.refreshTokenOnce();
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
throw new Error('token refresh failed');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
refreshTokenOnce() {
|
|
113
|
+
this.refreshPromise ??= this.refreshToken().finally(() => { this.refreshPromise = undefined; });
|
|
114
|
+
return this.refreshPromise;
|
|
115
|
+
}
|
|
116
|
+
isTokenStillValid() {
|
|
117
|
+
if (!this.props.refreshPath)
|
|
118
|
+
return true;
|
|
119
|
+
const expiration = this.config?.expiration;
|
|
120
|
+
if (!expiration)
|
|
121
|
+
return false;
|
|
122
|
+
return Date.now() < expiration.valueOf();
|
|
123
|
+
}
|
|
124
|
+
async refreshToken() {
|
|
125
|
+
const { name, refreshPath } = this.props;
|
|
126
|
+
this.logger.logEvent({ message: `${name} refresh token start` });
|
|
127
|
+
const url = this.getAuthServerUrl() + refreshPath;
|
|
128
|
+
try {
|
|
129
|
+
const response = await this.getApi().request({
|
|
130
|
+
method: 'post',
|
|
131
|
+
url,
|
|
132
|
+
data: { refresh_token: this.config.refreshToken }
|
|
133
|
+
});
|
|
134
|
+
const data = response.data;
|
|
135
|
+
this.config.accessToken = data.access_token;
|
|
136
|
+
this.config.refreshToken = data.refresh_token;
|
|
137
|
+
this.config.expiration = this.getExpiration(data);
|
|
138
|
+
this.logger.logEvent({ message: `${name} refresh token result`, status: 'success' });
|
|
139
|
+
this.observer.emit('token.updated', this.config);
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
const error = err.response ? `HTTP error ${err.response.status}: ${err.response.statusText}, url:${url}` : err.message;
|
|
143
|
+
this.logger.logEvent({ message: `${name} refresh token result`, status: 'error', error });
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
getSetting(key, defValue) {
|
|
148
|
+
try {
|
|
149
|
+
const settings = this.getUserSettings();
|
|
150
|
+
if (settings?.isInitialized === false)
|
|
151
|
+
return defValue;
|
|
152
|
+
return settings.get(key, defValue) ?? defValue;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return defValue;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
getAuthServerUrl() {
|
|
159
|
+
return this.getSetting(this.props.authUrlSetting, null)
|
|
160
|
+
?? this.getSetting('oauthUrl', null)
|
|
161
|
+
?? AUTH_SERVER_URL;
|
|
162
|
+
}
|
|
163
|
+
getBaseUrl() {
|
|
164
|
+
return this.getSetting(this.props.apiUrlSetting, this.props.defaultApiUrl) ?? this.props.defaultApiUrl;
|
|
165
|
+
}
|
|
166
|
+
getApi() {
|
|
167
|
+
return super.getApi(true);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.OAuthAppApiBase = OAuthAppApiBase;
|