trainingpeaks-sdk
Version:
TypeScript SDK for TrainingPeaks API integration
100 lines (99 loc) • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAuthRepository = void 0;
const http_errors_1 = require("../../adapters/errors/http-errors.js");
const mappers_1 = require("../../adapters/mappers/index.js");
const domain_1 = require("../../domain/index.js");
const api_urls_1 = require("./constants/api-urls.js");
const endpoints_1 = require("./endpoints/index.js");
const createErrorContext = (url, method, requestData) => ({
url,
method,
...(requestData && { requestData }),
});
const createLogin = (deps) => {
return async (credentials) => {
if (!(0, domain_1.validateCredentials)(credentials)) {
(0, http_errors_1.throwValidationError)('Invalid credentials provided', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'POST', {
username: credentials.username,
}));
}
const response = await (0, endpoints_1.getLoginPage)(deps.httpClient);
if (!response.success) {
(0, http_errors_1.throwHttpErrorFromResponse)(response, 'Get request verification token', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'GET'));
}
if (!response.data) {
(0, http_errors_1.throwMissingDataError)('No response data received from login page', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'GET'));
}
const requestVerificationToken = (0, endpoints_1.getRequestVerificationToken)(response.data);
if (!requestVerificationToken) {
(0, http_errors_1.throwValidationError)('Request verification token not found', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'GET'));
}
const submitLoginResponse = await (0, endpoints_1.submitLogin)(deps.httpClient, requestVerificationToken, credentials);
if (!submitLoginResponse.success) {
(0, http_errors_1.throwHttpErrorFromResponse)(submitLoginResponse, 'Submit login', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'POST', {
username: credentials.username,
}));
}
const tpAuthCookie = submitLoginResponse.cookies?.find((cookie) => cookie.includes('Production_tpAuth'));
if (!tpAuthCookie) {
(0, http_errors_1.throwCookieNotFoundError)('Production_tpAuth', createErrorContext(api_urls_1.API_ENDPOINTS.LOGIN_PAGE, 'POST', {
username: credentials.username,
}));
}
const authTokenResponse = await (0, endpoints_1.getAuthToken)(deps.httpClient, submitLoginResponse.cookies || []);
if (!authTokenResponse.success) {
(0, http_errors_1.throwHttpErrorFromResponse)(authTokenResponse, 'Get auth token', createErrorContext(api_urls_1.API_ENDPOINTS.TOKEN, 'GET'));
}
if (!authTokenResponse.data) {
(0, http_errors_1.throwMissingDataError)('No auth token data received', createErrorContext(api_urls_1.API_ENDPOINTS.TOKEN, 'GET'));
}
const authTokenData = authTokenResponse.data;
const authToken = authTokenData.token;
const userResponse = await (0, endpoints_1.getUser)(deps.httpClient, {
accessToken: authToken.access_token,
tokenType: authToken.token_type,
expiresIn: authToken.expires_in,
expires: new Date(authToken.expires),
refreshToken: authToken.refresh_token,
scope: authToken.scope,
});
if (!userResponse.success) {
(0, http_errors_1.throwHttpErrorFromResponse)(userResponse, 'Get user information', createErrorContext(api_urls_1.API_ENDPOINTS.USER_PROFILE, 'GET'));
}
if (!userResponse.data) {
(0, http_errors_1.throwMissingDataError)('No user data received', createErrorContext(api_urls_1.API_ENDPOINTS.USER_PROFILE, 'GET'));
}
const userData = userResponse.data;
const user = (0, mappers_1.mapTPUserToUser)(userData.user);
const token = (0, mappers_1.mapTPTokenToAuthToken)(authToken);
if ((0, domain_1.isTokenExpired)(token)) {
(0, http_errors_1.throwTokenExpiredError)(createErrorContext(api_urls_1.API_ENDPOINTS.TOKEN, 'GET'));
}
const session = {
token,
user,
};
await deps.sessionStorage.set(session);
return session;
};
};
const createLogout = (deps) => {
return async () => {
try {
await deps.sessionStorage.clear();
}
catch (error) {
deps.logger.warn('Failed to clear session storage during logout', {
error,
});
}
};
};
const createAuthRepository = (deps) => {
return {
login: createLogin(deps),
logout: createLogout(deps),
};
};
exports.createAuthRepository = createAuthRepository;