UNPKG

fitness-libraries

Version:
81 lines (80 loc) 3.34 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const garmin_api_handler_1 = require("garmin-api-handler"); const inversify_1 = require("inversify"); const constants_1 = require("../constants"); const GarminStorageService_1 = __importDefault(require("./GarminStorageService")); let GarminHandler = class GarminHandler extends garmin_api_handler_1.GarminApi { storage; email; password; init; constructor(storage, email, password) { super(); this.storage = storage; this.email = email; this.password = password; } async sessionIni() { if (this.init) { return; } const session = await this.storage.get(); if (!session) { return; } this.setSession(session); this.init = true; } async login(email = this.email, password = this.password) { const response = await super.login(email, password); await this.storage.store(response); return response; } async request(...parameters) { if (parameters[0].includes('sso/login') || parameters[0].includes('ticket') || parameters[0] === 'https://connect.garmin.com/modern/') { return super.request(...parameters); } await this.sessionIni(); if (!this.getSession()) { await this.login(); } try { return await super.request(...parameters); } catch (exception) { if (exception instanceof garmin_api_handler_1.GarminApiException) { await this.login(); return super.request(...parameters); } throw exception; } } }; GarminHandler = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(GarminStorageService_1.default)), __param(1, (0, inversify_1.inject)(constants_1.SYMBOLS.env)), __param(1, (0, inversify_1.named)(constants_1.SYMBOLS.login)), __param(2, (0, inversify_1.inject)(constants_1.SYMBOLS.env)), __param(2, (0, inversify_1.named)(constants_1.SYMBOLS.password)), __metadata("design:paramtypes", [GarminStorageService_1.default, String, String]) ], GarminHandler); exports.default = GarminHandler;