fitness-libraries
Version:
Fitness libraries as inversify modules.
65 lines (51 loc) • 1.83 kB
text/typescript
import { inject, injectable, named } from 'inversify';
import { Api, DefaultApiException } from 'rest-api-handler';
import { WebApi } from 'strava-api-handler';
import { ArgumentsType } from '../../../utils';
import { SYMBOLS } from '../constants';
import StravaWebStorageService from './StravaWebStorageService';
()
class StravaWebApiHandler extends WebApi {
private init?: boolean;
public constructor(
(StravaWebStorageService) public storage: StravaWebStorageService,
(SYMBOLS.env) (SYMBOLS.login) public email: string,
(SYMBOLS.env) (SYMBOLS.password) public password: string,
) {
super();
}
private async sessionIni() {
if (this.init) {
return;
}
const session = await this.storage.get();
if (!session) {
return;
}
this.setSession(session);
this.init = true;
}
public async request(...parameters: ArgumentsType<typeof Api.prototype.request>) {
if (parameters[0].includes('login') || parameters[0].includes('session')) {
return super.request(...parameters);
}
await this.sessionIni();
if (!this.getSession()) {
await this.login();
}
try {
return await super.request(...parameters);
} catch (exception) {
if (exception instanceof DefaultApiException) {
await this.login();
return super.request(...parameters);
}
throw exception;
}
}
public async login(email: string = this.email, password: string = this.password) {
await super.login(email, password);
await this.storage.store(this.getSession());
}
}
export default StravaWebApiHandler;