diff --git a/src/scripts/classes/credentials.ts b/src/scripts/classes/credentials.ts index b35c6c8..d65c629 100644 --- a/src/scripts/classes/credentials.ts +++ b/src/scripts/classes/credentials.ts @@ -1,22 +1,35 @@ "use strict"; // Modules from file -const { getF95Token } = require("../network-helper.js"); +import { getF95Token } from "../network-helper.js"; -class Credentials { - constructor(username, password) { +/** + * Represents the credentials used to access the platform. + */ +export class Credentials { + /** + * Username + */ + username: string; + /** + * Password of the user. + */ + password: string; + /** + * One time token used during login. + */ + token: string = null; + + constructor(username: string, password: string) { this.username = username; this.password = password; - this.token = null; } /** * @public * Fetch and save the token used to log in to F95Zone. */ - async fetchToken() { + async fetchToken(): Promise { this.token = await getF95Token(); } -} - -module.exports = Credentials; \ No newline at end of file +} \ No newline at end of file diff --git a/src/scripts/classes/game-info.ts b/src/scripts/classes/game-info.ts index 78883d0..8bd8930 100644 --- a/src/scripts/classes/game-info.ts +++ b/src/scripts/classes/game-info.ts @@ -1,97 +1,82 @@ "use strict"; -class GameInfo { - constructor() { - //#region Properties - /** - * Unique ID of the game on the platform. - * @type Number - */ - this.id = -1; - /** - * Game name - * @type String - */ - this.name = null; - /** - * Game author - * @type String - */ - this.author = null; - /** - * URL to the game's official conversation on the F95Zone portal - * @type String - */ - this.url = null; - /** - * Game description - * @type String - */ - this.overview = null; - /** - * Game language. - * @type String - */ - this.language = null; - /** - * List of supported OS. - * @type - */ - this.supportedOS = []; - /** - * Specify whether the game has censorship - * measures regarding NSFW scenes. - * @type Boolean - */ - this.censored = null; - /** - * List of tags associated with the game - * @type String[] - */ - this.tags = []; - /** - * Graphics engine used for game development - * @type String - */ - this.engine = null; - /** - * Development of the game - * @type String - */ - this.status = null; - /** - * Game description image URL - * @type String - */ - this.previewSrc = null; - /** - * Game version - * @type String - */ - this.version = null; - /** - * Last time the game underwent updates - * @type Date - */ - this.lastUpdate = null; - /** - * Specifies if the game is original or a mod - * @type Boolean - */ - this.isMod = false; - /** - * Changelog for the last version. - * @type String - */ - this.changelog = null; - //#endregion Properties - } +/** + * Information of a game/mod on the platform. + */ +export class GameInfo { + //#region Properties + /** + * Unique ID of the game on the platform. + */ + id = -1; + /** + * Game name + */ + name: string = null; + /** + * Game author + */ + author: string = null; + /** + * URL to the game's official conversation on the F95Zone portal + */ + url: string = null; + /** + * Game description + */ + overview: string = null; + /** + * Game language + */ + language: string = null; + /** + * List of supported OS + */ + supportedOS: string[] = []; + /** + * Specify whether the game has censorship + * measures regarding NSFW scenes + */ + censored: boolean = null; + /** + * List of tags associated with the game + */ + tags: string[] = []; + /** + * Graphics engine used for game development + */ + engine: string = null; + /** + * Development of the game + */ + status: string = null; + /** + * Game description image URL + */ + previewSrc: string = null; + /** + * Game version + */ + version: string = null; + /** + * Last time the game underwent updates + */ + lastUpdate: Date = null; + /** + * Specifies if the game is original or a mod + */ + isMod = false; + /** + * Changelog for the last version + */ + changelog: string = null; + //#endregion Properties /** * Converts the object to a dictionary used for JSON serialization. */ /* istanbul ignore next */ - toJSON() { + toJSON(): Record { return { id: this.id, name: this.name, @@ -117,13 +102,12 @@ class GameInfo { * @param {String} json JSON string used to create the new object * @returns {GameInfo} */ - static fromJSON(json) { + static fromJSON(json: string): GameInfo { // Convert string const temp = Object.assign(new GameInfo(), JSON.parse(json)); - + // JSON cannot transform a string to a date implicitly temp.lastUpdate = new Date(temp.lastUpdate); return temp; } } -module.exports = GameInfo; diff --git a/src/scripts/classes/login-result.ts b/src/scripts/classes/login-result.ts index 51fd9bc..9886909 100644 --- a/src/scripts/classes/login-result.ts +++ b/src/scripts/classes/login-result.ts @@ -3,18 +3,18 @@ /** * Object obtained in response to an attempt to login to the portal. */ -class LoginResult { - constructor(success, message) { - /** - * Result of the login operation - * @type Boolean - */ +export class LoginResult { + /** + * Result of the login operation + */ + success: boolean; + /** + * Login response message + */ + message: string; + + constructor(success: boolean, message: string) { this.success = success; - /** - * Login response message - * @type String - */ this.message = message; } } -module.exports = LoginResult; diff --git a/src/scripts/classes/prefix-parser.ts b/src/scripts/classes/prefix-parser.ts index fb3b9df..859326d 100644 --- a/src/scripts/classes/prefix-parser.ts +++ b/src/scripts/classes/prefix-parser.ts @@ -1,15 +1,12 @@ "use strict"; // Modules from file -const shared = require("../shared.js"); +import shared = require("../shared.js"); /** * Convert prefixes and platform tags from string to ID and vice versa. */ -class PrefixParser { - constructor() { - } - +export class PrefixParser { //#region Private methods /** * @private @@ -18,23 +15,19 @@ class PrefixParser { * @param {Any} value Value associated with the key * @returns {String|undefined} Key found or undefined */ - _getKeyByValue(object, value) { + _getKeyByValue(object: { [x: string]: unknown; }, value: unknown): string | undefined { return Object.keys(object).find(key => object[key] === value); } /** * @private * Makes an array of strings uppercase. - * @param {String[]} a - * @returns {String[]} */ - _toUpperCaseArray(a) { + _toUpperCaseArray(a: string[]): string[] { /** * Makes a string uppercase. - * @param {String} s - * @returns {String} */ - function toUpper(s) { + function toUpper(s: string): string { return s.toUpperCase(); } return a.map(toUpper); @@ -43,10 +36,8 @@ class PrefixParser { /** * @private * Check if `dict` contains `value` as a value. - * @param {Object.} dict - * @param {String} value */ - _valueInDict(dict, value) { + _valueInDict(dict: { [s: number]: string; }, value: string): boolean { const array = Object.values(dict); const upperArr = this._toUpperCaseArray(array); const element = value.toUpperCase(); @@ -57,10 +48,9 @@ class PrefixParser { /** * @public * Convert a list of prefixes to their respective IDs. - * @param {String[]} prefixes */ - prefixesToIDs(prefixes) { - const ids = []; + prefixesToIDs(prefixes: string[]) : number[] { + const ids: number[] = []; for(const p of prefixes) { // Check what dict contains the value let dict = null; @@ -80,10 +70,9 @@ class PrefixParser { /** * @public * It converts a list of IDs into their respective prefixes. - * @param {number[]} ids */ - idsToPrefixes(ids) { - const prefixes = []; + idsToPrefixes(ids: number[]): string[] { + const prefixes:string[] = []; for(const id of ids) { // Check what dict contains the key let dict = null; @@ -98,6 +87,4 @@ class PrefixParser { } return prefixes; } -} - -module.exports = PrefixParser; \ No newline at end of file +} \ No newline at end of file diff --git a/src/scripts/classes/session.ts b/src/scripts/classes/session.ts index fcf1e24..14e9b15 100644 --- a/src/scripts/classes/session.ts +++ b/src/scripts/classes/session.ts @@ -1,53 +1,53 @@ "use strict"; // Core modules -const fs = require("fs"); -const promisify = require("util").promisify; +import * as fs from "fs"; +import { promisify } from "util"; // Public modules from npm -const md5 = require("md5"); +import * as md5 from "md5"; // Promisifed functions const areadfile = promisify(fs.readFile); const awritefile = promisify(fs.writeFile); const aunlinkfile = promisify(fs.unlink); -class Session { - constructor(path) { - /** - * Max number of days the session is valid. - */ - this.SESSION_TIME = 1; +export class Session { + //#region Properties + /** + * Max number of days the session is valid. + */ + private readonly SESSION_TIME: number = 1; + /** + * Path of the session map file on disk. + */ + private path: string = null; + /** + * Indicates if the session is mapped on disk. + */ + private isMapped = null; + /** + * Date of creation of the session. + */ + private created = null; + /** + * MD5 hash of the username and the password. + */ + private hash = null; + //#endregion Properties - /** - * Path of the session map file on disk. - */ - this._path = path; - - /** - * Indicates if the session is mapped on disk. - */ - this._isMapped = fs.existsSync(this._path); - - /** - * Date of creation of the session. - */ - this._created = new Date(Date.now()); - - /** - * MD5 hash of the username and the password. - */ - this._hash = null; + constructor(path: string) { + this.path = path; + this.isMapped = fs.existsSync(this.path); + this.created = new Date(Date.now()); + this.hash = null; } //#region Private Methods /** - * @private * Get the difference in days between two dates. - * @param {Date} a - * @param {Date} b */ - _dateDiffInDays(a, b) { + private dateDiffInDays(a: Date, b: Date) { const MS_PER_DAY = 1000 * 60 * 60 * 24; // Discard the time and time-zone information. @@ -58,72 +58,70 @@ class Session { } /** - * @private * Convert the object to a dictionary serializable in JSON. */ - _toJSON() { + private toJSON(): Record { return { - created: this._created, - hash: this._hash, + created: this.created, + hash: this.hash, }; } //#endregion Private Methods //#region Public Methods - create(username, password) { + /** + * Create a new session + */ + create(username: string, password: string):void { // First, create the hash of the credentials const value = `${username}%%%${password}`; - this._hash = md5(value); + this.hash = md5(value); // Update the creation date - this._created = new Date(Date.now()); + this.created = new Date(Date.now()); } /** - * @public * Save the session to disk. */ - async save() { + async save() : Promise { // Update the creation date - this._created = new Date(Date.now()); + this.created = new Date(Date.now()); // Convert data - const json = this._toJSON(); + const json = this.toJSON(); const data = JSON.stringify(json); // Write data - await awritefile(this._path, data); + await awritefile(this.path, data); } /** - * @public * Load the session from disk. */ - async load() { + async load(): Promise { // Read data - const data = await areadfile(this._path); + const data = await areadfile(this.path, { encoding: 'utf-8', flag: 'r' }); const json = JSON.parse(data); // Assign values - this._created = json.created; - this._hash = json.hash; + this.created = json.created; + this.hash = json.hash; } /** - * @public * Delete the session from disk. */ - async delete() { - await aunlinkfile(this._path); + async delete(): Promise { + await aunlinkfile(this.path); } /** - * @public * Check if the session is valid. */ - isValid(username, password) { + isValid(username:string, password:string) : boolean { // Get the number of days from the file creation - const diff = this._dateDiffInDays(new Date(Date.now()), this._created); + const diff = this.dateDiffInDays(new Date(Date.now()), this.created); // The session is valid if the number of days is minor than SESSION_TIME let valid = diff < this.SESSION_TIME; @@ -131,11 +129,9 @@ class Session { if(valid) { // Check the hash const value = `${username}%%%${password}`; - valid = md5(value) === this._hash; + valid = md5(value) === this.hash; } return valid; } //#endregion Public Methods -} - -module.exports = Session; \ No newline at end of file +} \ No newline at end of file diff --git a/src/scripts/classes/user-data.ts b/src/scripts/classes/user-data.ts index 233c641..42267bc 100644 --- a/src/scripts/classes/user-data.ts +++ b/src/scripts/classes/user-data.ts @@ -3,24 +3,17 @@ /** * Class containing the data of the user currently connected to the F95Zone platform. */ -class UserData { - constructor() { - /** - * User name. - * @type String - */ - this.username = ""; - /** - * Path to the user's profile picture. - * @type String - */ - this.avatarSrc = null; - /** - * List of followed game thread URLs. - * @type String[] - */ - this.watchedGameThreads = []; - } +export class UserData { + /** + * User name. + */ + username: string = null; + /** + * Path to the user's profile picture. + */ + avatarSrc: string = null; + /** + * List of followed game thread URLs. + */ + watchedGameThreads: string[] = []; } - -module.exports = UserData;