Re-export classes, fix imports and manage session
							parent
							
								
									2af6c263c1
								
							
						
					
					
						commit
						ededed1cc8
					
				
							
								
								
									
										133
									
								
								src/index.ts
								
								
								
								
							
							
						
						
									
										133
									
								
								src/index.ts
								
								
								
								
							| 
						 | 
					@ -4,7 +4,6 @@
 | 
				
			||||||
import shared from "./scripts/shared.js";
 | 
					import shared from "./scripts/shared.js";
 | 
				
			||||||
import search from "./scripts/search.js";
 | 
					import search from "./scripts/search.js";
 | 
				
			||||||
import { authenticate, urlExists, isF95URL } from "./scripts/network-helper.js";
 | 
					import { authenticate, urlExists, isF95URL } from "./scripts/network-helper.js";
 | 
				
			||||||
import { getUserData as retrieveUserData } from "./scripts/scrape-data/scrape-user.js";
 | 
					 | 
				
			||||||
import fetchLatestHandiworkURLs from "./scripts/fetch-data/fetch-latest.js";
 | 
					import fetchLatestHandiworkURLs from "./scripts/fetch-data/fetch-latest.js";
 | 
				
			||||||
import fetchPlatformData from "./scripts/fetch-data/fetch-platform-data.js";
 | 
					import fetchPlatformData from "./scripts/fetch-data/fetch-platform-data.js";
 | 
				
			||||||
import getHandiworkInformation from "./scripts/scrape-data/handiwork-parse.js";
 | 
					import getHandiworkInformation from "./scripts/scrape-data/handiwork-parse.js";
 | 
				
			||||||
| 
						 | 
					@ -13,21 +12,34 @@ import { IBasic } from "./scripts/interfaces.js";
 | 
				
			||||||
// Classes from file
 | 
					// Classes from file
 | 
				
			||||||
import Credentials from "./scripts/classes/credentials.js";
 | 
					import Credentials from "./scripts/classes/credentials.js";
 | 
				
			||||||
import LoginResult from "./scripts/classes/login-result.js";
 | 
					import LoginResult from "./scripts/classes/login-result.js";
 | 
				
			||||||
import UserData from "./scripts/classes/user-data.js";
 | 
					import UserProfile from "./scripts/classes/mapping/user-profile.js";
 | 
				
			||||||
import LatestSearchQuery from "./scripts/classes/query/latest-search-query.js";
 | 
					import LatestSearchQuery from "./scripts/classes/query/latest-search-query.js";
 | 
				
			||||||
import HandiworkSearchQuery from "./scripts/classes/query/handiwork-search-query.js";
 | 
					import HandiworkSearchQuery from "./scripts/classes/query/handiwork-search-query.js";
 | 
				
			||||||
import HandiWork from "./scripts/classes/handiwork/handiwork.js";
 | 
					import HandiWork from "./scripts/classes/handiwork/handiwork.js";
 | 
				
			||||||
 | 
					import { UserNotLogged } from "./scripts/classes/errors.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#region Global variables
 | 
					//#region Global variables
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
 | 
					const USER_NOT_LOGGED = "User not authenticated, unable to continue";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#endregion
 | 
					//#endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#region Export classes
 | 
					//#region Re-export classes
 | 
				
			||||||
// module.exports.GameInfo = GameInfo;
 | 
					export { default as Animation } from "./scripts/classes/handiwork/animation.js";
 | 
				
			||||||
// module.exports.LoginResult = LoginResult;
 | 
					export { default as Asset } from "./scripts/classes/handiwork/asset.js";
 | 
				
			||||||
// module.exports.UserData = UserData;
 | 
					export { default as Comic } from "./scripts/classes/handiwork/comic.js";
 | 
				
			||||||
// module.exports.PrefixParser = PrefixParser;
 | 
					export { default as Game } from "./scripts/classes/handiwork/game.js";
 | 
				
			||||||
//#endregion Export classes
 | 
					export { default as Handiwork } from "./scripts/classes/handiwork/handiwork.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export { default as PlatformUser } from "./scripts/classes/mapping/platform-user.js";
 | 
				
			||||||
 | 
					export { default as Post } from "./scripts/classes/mapping/post.js";
 | 
				
			||||||
 | 
					export { default as Thread } from "./scripts/classes/mapping/thread.js";
 | 
				
			||||||
 | 
					export { default as UserProfile } from "./scripts/classes/mapping/user-profile.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export { default as HandiworkSearchQuery } from "./scripts/classes/query/handiwork-search-query.js";
 | 
				
			||||||
 | 
					export { default as LatestSearchQuery } from "./scripts/classes/query/latest-search-query.js";
 | 
				
			||||||
 | 
					export { default as ThreadSearchQuery } from "./scripts/classes/query/thread-search-query.js";
 | 
				
			||||||
 | 
					//#endregion Re-export classes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#region Export properties
 | 
					//#region Export properties
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -40,24 +52,32 @@ shared.logger.level = "warn"; // By default log only the warn messages
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Indicates whether a user is logged in to the F95Zone platform or not.
 | 
					 * Indicates whether a user is logged in to the F95Zone platform or not.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export function isLogged(): boolean {
 | 
					export function isLogged(): boolean { return shared.isLogged; };
 | 
				
			||||||
    return shared.isLogged;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
//#endregion Export properties
 | 
					//#endregion Export properties
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#region Export methods
 | 
					//#region Export methods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Log in to the F95Zone platform.
 | 
					 * Log in to the F95Zone platform.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 * This **must** be the first operation performed before accessing any other script functions.
 | 
					 * This **must** be the first operation performed before accessing any other script functions.
 | 
				
			||||||
 * @returns {Promise<LoginResult>} Result of the operation
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function login(username: string, password: string): Promise<LoginResult> {
 | 
					export async function login(username: string, password: string): Promise<LoginResult> {
 | 
				
			||||||
    /* istanbul ignore next */
 | 
					    // Try to load a previous session
 | 
				
			||||||
    if (shared.isLogged) {
 | 
					    await shared.session.load();
 | 
				
			||||||
        shared.logger.info(`${username} already authenticated`);
 | 
					
 | 
				
			||||||
        return new LoginResult(true, `${username} already authenticated`);
 | 
					    // If the session is valid, return
 | 
				
			||||||
 | 
					    if (shared.session.isValid(username, password)) {
 | 
				
			||||||
 | 
					        shared.logger.info(`Loading previous session for ${username}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Load platform data
 | 
				
			||||||
 | 
					        await fetchPlatformData();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        shared.setIsLogged(true);
 | 
				
			||||||
 | 
					        return new LoginResult(true, `${username} already authenticated (session)`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Creating credentials and fetch unique platform token
 | 
				
			||||||
    shared.logger.trace("Fetching token...");
 | 
					    shared.logger.trace("Fetching token...");
 | 
				
			||||||
    const creds = new Credentials(username, password);
 | 
					    const creds = new Credentials(username, password);
 | 
				
			||||||
    await creds.fetchToken();
 | 
					    await creds.fetchToken();
 | 
				
			||||||
| 
						 | 
					@ -66,15 +86,16 @@ export async function login(username: string, password: string): Promise<LoginRe
 | 
				
			||||||
    const result = await authenticate(creds);
 | 
					    const result = await authenticate(creds);
 | 
				
			||||||
    shared.setIsLogged(result.success);
 | 
					    shared.setIsLogged(result.success);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Load platform data
 | 
					 | 
				
			||||||
    if (result.success) {
 | 
					    if (result.success) {
 | 
				
			||||||
 | 
					        // Load platform data
 | 
				
			||||||
        await fetchPlatformData();
 | 
					        await fetchPlatformData();
 | 
				
			||||||
        shared.session.create(username, password, creds.token);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* istambul ignore next */
 | 
					        // Recreate the session, overwriting the old one
 | 
				
			||||||
    if (result.success) shared.logger.info("User logged in through the platform");
 | 
					        shared.session.create(username, password, creds.token);
 | 
				
			||||||
    else shared.logger.warn(`Error during authentication: ${result.message}`);
 | 
					        await shared.session.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        shared.logger.info("User logged in through the platform");
 | 
				
			||||||
 | 
					    } else shared.logger.warn(`Error during authentication: ${result.message}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -88,11 +109,8 @@ export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise<boolean>
 | 
				
			||||||
    // Local variables
 | 
					    // Local variables
 | 
				
			||||||
    let hasUpdate = false;
 | 
					    let hasUpdate = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* istanbul ignore next */
 | 
					    // Check if the user is logged
 | 
				
			||||||
    if (!shared.isLogged) {
 | 
					    if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
 | 
				
			||||||
        shared.logger.warn(USER_NOT_LOGGED);
 | 
					 | 
				
			||||||
        return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // F95 change URL at every game update,
 | 
					    // F95 change URL at every game update,
 | 
				
			||||||
    // so if the URL is different an update is available
 | 
					    // so if the URL is different an update is available
 | 
				
			||||||
| 
						 | 
					@ -108,35 +126,28 @@ export async function checkIfHandiworkHasUpdate(hw: HandiWork): Promise<boolean>
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Starting from the name, it gets all the information about the game you are looking for.
 | 
					 * Search for one or more handiworks identified by a specific query.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 * You **must** be logged in to the portal before calling this method.
 | 
					 * You **must** be logged in to the portal before calling this method.
 | 
				
			||||||
 * @param {String} name Name of the game searched
 | 
					 * 
 | 
				
			||||||
 * @param {Boolean} mod Indicate if you are looking for mods or games
 | 
					 * @param {HandiworkSearchQuery} query Parameters used for the search.
 | 
				
			||||||
 * @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to
 | 
					 * @param {Number} limit Maximum number of results. Default: 10
 | 
				
			||||||
 * an identified game (in the case of homonymy of titles)
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function getHandiwork<T extends IBasic>(query: HandiworkSearchQuery, limit: number = 30): Promise<T[]> {
 | 
					export async function searchHandiwork<T extends IBasic>(query: HandiworkSearchQuery, limit: number = 10): Promise<T[]> {
 | 
				
			||||||
    /* istanbul ignore next */
 | 
					    // Check if the user is logged
 | 
				
			||||||
    if (!shared.isLogged) {
 | 
					    if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
 | 
				
			||||||
        shared.logger.warn(USER_NOT_LOGGED);
 | 
					 | 
				
			||||||
        return null;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return await search<T>(query, limit);
 | 
					    return await search<T>(query, limit);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Starting from the url, it gets all the information 
 | 
					 * Given the url, it gets all the information about the handiwork requested.
 | 
				
			||||||
 * about the handiwork requested.
 | 
					 | 
				
			||||||
 * 
 | 
					 * 
 | 
				
			||||||
 * You **must** be logged in to the portal before calling this method.
 | 
					 * You **must** be logged in to the portal before calling this method.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function getHandiworkFromURL<T extends IBasic>(url: string): Promise<T> {
 | 
					export async function getHandiworkFromURL<T extends IBasic>(url: string): Promise<T> {
 | 
				
			||||||
    /* istanbul ignore next */
 | 
					    // Check if the user is logged
 | 
				
			||||||
    if (!shared.isLogged) {
 | 
					    if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
 | 
				
			||||||
        shared.logger.warn(USER_NOT_LOGGED);
 | 
					 | 
				
			||||||
        return null;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Check URL validity
 | 
					    // Check URL validity
 | 
				
			||||||
    const exists = await urlExists(url);
 | 
					    const exists = await urlExists(url);
 | 
				
			||||||
| 
						 | 
					@ -149,30 +160,37 @@ export async function getHandiworkFromURL<T extends IBasic>(url: string): Promis
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Gets the data of the currently logged in user.
 | 
					 * Gets the data of the currently logged in user.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 * You **must** be logged in to the portal before calling this method.
 | 
					 * You **must** be logged in to the portal before calling this method.
 | 
				
			||||||
 * @returns {Promise<UserData>} Data of the user currently logged in
 | 
					 * 
 | 
				
			||||||
 | 
					 * @returns {Promise<UserProfile>} Data of the user currently logged in
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function getUserData(): Promise<UserData> {
 | 
					export async function getUserData(): Promise<UserProfile> {
 | 
				
			||||||
    /* istanbul ignore next */
 | 
					    // Check if the user is logged
 | 
				
			||||||
    if (!shared.isLogged) {
 | 
					    if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
 | 
				
			||||||
        shared.logger.warn(USER_NOT_LOGGED);
 | 
					 | 
				
			||||||
        return null;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return await retrieveUserData();
 | 
					    // Create and fetch profile data
 | 
				
			||||||
 | 
					    const profile = new UserProfile();
 | 
				
			||||||
 | 
					    await profile.fetch();
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return profile;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Gets the latest updated games that match the specified parameters.
 | 
					 * Gets the latest updated games that match the specified parameters.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 * You **must** be logged in to the portal before calling this method.
 | 
					 * You **must** be logged in to the portal before calling this method.
 | 
				
			||||||
 * @param {LatestSearchQuery} query
 | 
					 * 
 | 
				
			||||||
 * Parameters used for the search.
 | 
					 * @param {LatestSearchQuery} query Parameters used for the search.
 | 
				
			||||||
 * @param {Number} limit Maximum number of results
 | 
					 * @param {Number} limit Maximum number of results. Default: 10
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export async function getLatestUpdates<T extends IBasic>(query: LatestSearchQuery, limit: number): Promise<T[]> {
 | 
					export async function getLatestUpdates<T extends IBasic>(query: LatestSearchQuery, limit: number = 10): Promise<T[]> {
 | 
				
			||||||
    // Check limit value
 | 
					    // Check limit value
 | 
				
			||||||
    if (limit <= 0) throw new Error("limit must be greater than 0");
 | 
					    if (limit <= 0) throw new Error("limit must be greater than 0");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Check if the user is logged
 | 
				
			||||||
 | 
					    if (!shared.isLogged) throw new UserNotLogged(USER_NOT_LOGGED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Fetch the results
 | 
					    // Fetch the results
 | 
				
			||||||
    const urls = await fetchLatestHandiworkURLs(query, limit);
 | 
					    const urls = await fetchLatestHandiworkURLs(query, limit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -180,4 +198,5 @@ export async function getLatestUpdates<T extends IBasic>(query: LatestSearchQuer
 | 
				
			||||||
    const promiseList = urls.map((u: string) => getHandiworkInformation<T>(u));
 | 
					    const promiseList = urls.map((u: string) => getHandiworkInformation<T>(u));
 | 
				
			||||||
    return await Promise.all(promiseList);
 | 
					    return await Promise.all(promiseList);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//#endregion
 | 
					//#endregion
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue