-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Elitezen
committed
Nov 28, 2022
1 parent
745f979
commit 7ef5dcf
Showing
21 changed files
with
917 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/node_modules | ||
/bin | ||
/dist | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { CategoryNameType } from "../typings/types"; | ||
import getCategory from "../functions/getCategory"; | ||
/** | ||
* @class Class for anything trivia category related. | ||
*/ | ||
export default class Category { | ||
/** | ||
* An array of all category names. Use `Category.random()` for a random pick. | ||
*/ | ||
static allNames: CategoryNameType[]; | ||
/** | ||
* Decodes a URLLegacy, URL3968 or Base64 category name. | ||
* @param {string} str string to decode. | ||
* @returns {string} The decoded category name. | ||
*/ | ||
static decodeEncodedCategoryName(str: string): CategoryNameType | null; | ||
/** | ||
* Fetches a trivia category's data. Duplicate of `getCategory()`. | ||
* @param {CategoryResolvable} arg An argument resolving to a trivia category. | ||
* @returns {Promise<CategoryData>} The data of the category. | ||
*/ | ||
static getCategory: typeof getCategory; | ||
/** | ||
* Returns a category id when given it's name. | ||
* @param {CategoryNameType} name The name of the category. | ||
* @returns {number | null} The id if resolvable. | ||
*/ | ||
static idByName(name: CategoryNameType): number | null; | ||
/** | ||
* Returns a category name when given it's id. | ||
* @param {number} id The id of the category. | ||
* @returns {CategoryNameType | null} The name if resolvable. | ||
*/ | ||
static nameById(id: number): CategoryNameType | null; | ||
/** | ||
* Picks a random category name or id. | ||
* @param {'name' | 'id'} resolvableType The kind of resolvable to return (default `'name'`). | ||
* @returns {CategoryNameType | number} A random category id or name. | ||
*/ | ||
static random(resolvableType: "name"): CategoryNameType; | ||
static random(resolvableType: "id"): number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var enums_1 = require("../typings/enums"); | ||
var getCategory_1 = require("../functions/getCategory"); | ||
var Util_1 = require("./Util"); | ||
/** | ||
* @class Class for anything trivia category related. | ||
*/ | ||
var Category = /** @class */ (function () { | ||
function Category() { | ||
} | ||
/** | ||
* Decodes a URLLegacy, URL3968 or Base64 category name. | ||
* @param {string} str string to decode. | ||
* @returns {string} The decoded category name. | ||
*/ | ||
Category.decodeEncodedCategoryName = function (str) { | ||
return ([ | ||
Util_1.default.decodeBase64(str), | ||
Util_1.default.decodeUrl3968(str), | ||
Util_1.default.decodeUrlLegacy(str), | ||
].find(function (str) { return Category.allNames.includes(str); }) || null); | ||
}; | ||
/** | ||
* Returns a category id when given it's name. | ||
* @param {CategoryNameType} name The name of the category. | ||
* @returns {number | null} The id if resolvable. | ||
*/ | ||
Category.idByName = function (name) { | ||
var id = Category.allNames.indexOf(name); | ||
return id > -1 ? id + 9 : null; | ||
}; | ||
/** | ||
* Returns a category name when given it's id. | ||
* @param {number} id The id of the category. | ||
* @returns {CategoryNameType | null} The name if resolvable. | ||
*/ | ||
Category.nameById = function (id) { | ||
var name = Category.allNames[id - 9]; | ||
return name !== undefined ? name : null; | ||
}; | ||
Category.random = function (resolvableType) { | ||
if (resolvableType === undefined) | ||
resolvableType = "name"; | ||
var name = Category.allNames[Math.floor(Math.random() * Category.allNames.length)]; | ||
if (resolvableType === "id") | ||
return Category.idByName(name); | ||
return name; | ||
}; | ||
/** | ||
* An array of all category names. Use `Category.random()` for a random pick. | ||
*/ | ||
Category.allNames = Object.keys(enums_1.CategoryNames).filter(function (key) { return isNaN(+key); }); | ||
/** | ||
* Fetches a trivia category's data. Duplicate of `getCategory()`. | ||
* @param {CategoryResolvable} arg An argument resolving to a trivia category. | ||
* @returns {Promise<CategoryData>} The data of the category. | ||
*/ | ||
Category.getCategory = getCategory_1.default; | ||
return Category; | ||
}()); | ||
exports.default = Category; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { CategoryData, Question, RawCategoryResponse, RawQuestion } from "../typings/interfaces"; | ||
/** | ||
* @class Class for transforming raw API data to developer friendly data. | ||
* @private | ||
*/ | ||
export default class Constructor { | ||
static category(rawCategoryData: RawCategoryResponse): CategoryData; | ||
static questions(rawQuestions: RawQuestion[]): Question[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Category_1 = require("./Category"); | ||
var Util_1 = require("./Util"); | ||
/** | ||
* @class Class for transforming raw API data to developer friendly data. | ||
* @private | ||
*/ | ||
var Constructor = /** @class */ (function () { | ||
function Constructor() { | ||
} | ||
Constructor.category = function (rawCategoryData) { | ||
return { | ||
id: rawCategoryData.category_id, | ||
name: Category_1.default.nameById(rawCategoryData.category_id), | ||
questionCount: { | ||
total: rawCategoryData.category_question_count.total_question_count, | ||
easy: rawCategoryData.category_question_count.total_easy_question_count, | ||
medium: rawCategoryData.category_question_count.total_medium_question_count, | ||
hard: rawCategoryData.category_question_count.total_hard_question_count, | ||
}, | ||
}; | ||
}; | ||
Constructor.questions = function (rawQuestions) { | ||
return rawQuestions.map(function (question) { | ||
return { | ||
value: question.question, | ||
category: { | ||
id: Category_1.default.idByName(Category_1.default.decodeEncodedCategoryName(question.category)), | ||
name: question.category, | ||
getData: function () { | ||
return Category_1.default.getCategory(this.id); | ||
}, | ||
}, | ||
type: question.type, | ||
difficulty: question.difficulty, | ||
correctAnswer: question.correct_answer, | ||
incorrectAnswers: question.incorrect_answers, | ||
allAnswers: Util_1.default.shuffleArray(__spreadArray([ | ||
question.correct_answer | ||
], question.incorrect_answers, true)), | ||
checkAnswer: function (str, caseSensitive) { | ||
if (caseSensitive === void 0) { caseSensitive = false; } | ||
if (!caseSensitive) { | ||
return str.toLowerCase() === this.correctAnswer.toLowerCase(); | ||
} | ||
return str === this.correctAnswer; | ||
}, | ||
}; | ||
}); | ||
}; | ||
return Constructor; | ||
}()); | ||
exports.default = Constructor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { ErrorResponse } from "../typings/interfaces"; | ||
/** | ||
* @class OpenTDB error constructor | ||
* @private | ||
*/ | ||
export default class OpenTDBError extends TypeError { | ||
constructor(error: ErrorResponse); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @class OpenTDB error constructor | ||
* @private | ||
*/ | ||
var OpenTDBError = /** @class */ (function (_super) { | ||
__extends(OpenTDBError, _super); | ||
function OpenTDBError(error) { | ||
var _this = _super.call(this, error.text) || this; | ||
_this.name = "OpenTDBError [".concat(error.header, "]"); | ||
return _this; | ||
} | ||
return OpenTDBError; | ||
}(TypeError)); | ||
exports.default = OpenTDBError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @class Class for working with trivia API sessions. | ||
*/ | ||
export default class Session { | ||
/** | ||
* This session's current token | ||
*/ | ||
token: string | null; | ||
constructor(); | ||
/** | ||
* Checks if the session has been initialized or holds a token. Emits a warning if not. | ||
*/ | ||
assert(): void; | ||
/** | ||
* Generates a session token and assigns it to the instance (`Session.token`). | ||
*/ | ||
start(): Promise<string>; | ||
/** | ||
* Resets the current session's data. | ||
*/ | ||
reset(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var enums_1 = require("../typings/enums"); | ||
var OpenTDBError_1 = require("./OpenTDBError"); | ||
var Util_1 = require("./Util"); | ||
/** | ||
* @class Class for working with trivia API sessions. | ||
*/ | ||
var Session = /** @class */ (function () { | ||
function Session() { | ||
this.token = null; | ||
} | ||
/** | ||
* Checks if the session has been initialized or holds a token. Emits a warning if not. | ||
*/ | ||
Session.prototype.assert = function () { | ||
if (this.token === null) { | ||
process.emitWarning("This session currently has no token. Use Session.start() and resolve the promise before using."); | ||
} | ||
}; | ||
/** | ||
* Generates a session token and assigns it to the instance (`Session.token`). | ||
*/ | ||
Session.prototype.start = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var request, err_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, Util_1.default.fetch(enums_1.Routes.SessionStart)]; | ||
case 1: | ||
request = _a.sent(); | ||
this.token = request.token; | ||
return [2 /*return*/, this.token]; | ||
case 2: | ||
err_1 = _a.sent(); | ||
throw new OpenTDBError_1.default(err_1); | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Resets the current session's data. | ||
*/ | ||
Session.prototype.reset = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var url, request, err_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
if (this.token === null) | ||
return [2 /*return*/]; | ||
url = Util_1.default.createQueriedLink(enums_1.Routes.SessionReset, { | ||
command: "reset", | ||
token: this.token, | ||
}); | ||
return [4 /*yield*/, Util_1.default.fetch(url)]; | ||
case 1: | ||
request = _a.sent(); | ||
this.token = request.token; | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
err_2 = _a.sent(); | ||
throw new OpenTDBError_1.default(err_2); | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return Session; | ||
}()); | ||
exports.default = Session; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { ErrorResponse } from "../typings/interfaces"; | ||
import type { ErrorCode, ExtendedDictionary } from "../typings/types"; | ||
/** | ||
* @class Class for utility functions. | ||
* @private | ||
*/ | ||
export default class Util { | ||
static assignDefaults<T extends object>(defaults: T, current?: T | {}): T & ({} | T); | ||
static base64Decoder: { | ||
atob(str: string): string; | ||
decode<T>(value: unknown): T; | ||
decodeString<T_1 extends string>(str: string): T_1; | ||
decodeStringArray(arr: string[]): unknown[]; | ||
decodeObjectValues(obj: object): any; | ||
}; | ||
static decodeBase64: <T extends string>(str: string) => T; | ||
static decodeUrlLegacy<T extends string>(str: string): T; | ||
static decodeUrl3968<T extends string>(str: string): T; | ||
static createQueriedLink(baseURL: string, options: ExtendedDictionary<null>, concatSymbol?: string): string; | ||
static fetch<T>(url: string, checkForResponseCode?: boolean): Promise<T>; | ||
static getErrorByCode(code: ErrorCode): ErrorResponse; | ||
static shuffleArray<T>(arg: T[]): T[]; | ||
} |
Oops, something went wrong.