-
Notifications
You must be signed in to change notification settings - Fork 3
Documentation (1.0.2)
-
- CategoryIdResolvable
- CategoryNameResolvable
- CategoryName
- CategoryNameVersion
- CategoryNamePretty
- CategoryNameStrict
- CategoryResolvable
- CategoryResolvableType
- NumberResolvable
- OpenTDBResponseCode
- Questions
- QuestionDifficulty
- QuestionOptions
- QuestionOptionsDefaults
- QuestionType
- QuestionEncoding
- QuestionVersion
getQuestions(options?: QuestionOptions): Promise<Question[]>
Fetches an array of questions based on provided options.
options - The metadeta describing target questions.
-
options.amount
The amount of questions to fetch (min. 1, max. 50) -
options.category
The category of questions. -
options.difficulty
The difficulty of questions. -
options.type
The type of questions (true/false or multiple choice) -
options.encode
The encoding of question values. -
options.token
The session token.
Returns: Promise<Question[]>
An Array of questions.
const questions = await getQuestions({
amount: 50,
difficulty: 'easy',
type: 'multiple',
category: Category.allNames.SCIENCE_COMPUTERS
});
getCategoryData(arg: CategoryResolvable): Promise<CategoryData>
Fetches a trivia category's data.
arg - An argument resolving to a trivia category.
Returns: Promise<CategoryData>
The data of the category.
const data = await getCategoryData("HISTORY"):
new Category(arg:
CategoryResolvable):
Category
For trivia category related data retrieving.
arg - An argument resolving to a trivia category.
Properties:
-
readonly id
This category's API id.
type: number
-
readonly strictName
This category's 'strict' (constant) name.
type: CategoryName<"Strict">
-
readonly prettyName
This category's 'pretty' (display) name.
type: CategoryName<"Pretty">
-
static readonly allNames
All OpenTDB category names in 'strict' (constant) form.
type: CategoryNamesStrict
-
static readonly allPrettyNames
All OpenTDB category names in 'pretty' (display) form
type: CategoryNamesPretty
Methods:
-
static idToStrictName(arg: NumberResolvable): CategoryName<"Strict"> | null
Takes a category's id and returns it's 'strict' (constant) name.
arg - A number or string number.
Returns: number | null
The category id
Category.idToStrictName(9); // 'GENERAL_KNOWLEDGE'
-
static idToPrettyName(arg: NumberResolvable): CategoryName<"Pretty"> | null
Takes a category's id and returns it's 'pretty' (display) name.
arg - A number or string number.
Returns: CategoryName<"Pretty"> | null
The category name
Category.idToPrettyName(9); // 'General Knowledge'
-
static nameToId(arg: CategoryNameResolvable): number | null
Takes a category's name and returns the respective id
arg - A category name
Returns: number | null
The category id
Category.nameToId('GENERAL_KNOWLEDGE'); Category.nameToId('General Knowledge'); // 9
-
static isIdResolvable(arg: NumberResolvable | CategoryResolvable): boolean
Returns whether or not the given number can be resolved into a category id.
arg - A category resolvable or id
Returns: boolean
Category.isIdResolvable(9); Category.isIdResolvable('32'); // true Category.isIdResolvable('GENERAL_KNOWLEDGE'); Category.isIdResolvable({}); // false
-
static isNameResolvable(arg: string | CategoryResolvable): boolean
Returns whether or not the given string can be resolved into a category name.
arg - A category resolvable or name
Returns: boolean
Category.isNameResolvable('GENERAL_KNOWLEDGE'); Category.isNameResolvable('General Knowledge'); // true Category.isNameResolvable('Cheeseburger'); Category.isNameResolvable({}); // false
-
static prettyToStrictName(arg: CategoryName<"Pretty">): CategoryName<"Strict">
Converts a category's pretty name into it's strict version
arg - The category's pretty name
Returns: CategoryName<"Strict">
Category.prettyToStrictName('General Knowledge'); // 'GENERAL_KNOWLEDGE'
-
static strictToPrettyName(arg: CategoryName<"Strict">): CategoryName<"Pretty">
Converts a category's strict name into it's pretty version
arg - The category's strict name
Returns: CategoryName<"Pretty">
Category.prettyToStrictName('GENERAL_KNOWLEDGE'); // 'General Knowledge'
-
static random(arg?: CategoryResolvableType): number | CategoryName<"Pretty">
Chooses a random category and returns it's id.
arg - What type of resolvable to return
Returns: number | CategoryName<"Pretty">
Category.random(); Category.random('ID'); // 15 Category.random('NAME'); // 'SCIENCE_COMPUTERS'
-
static resolve(arg: CategoryResolvable): Category | null
Resolves a given category resolvable and returns a
Category
class ornull
.arg - The argument to resolve.
Returns: Category | null
An instance of Category
Category.resolve('General Knowledge'); Category.resolve(9); // Category Category.resolve(0); Category.resolve(); // null
-
getData(): Promise<CategoryData>
Fetches the data about this category. Wrapper for
getCategoryData
Returns: Promise<CategoryData>
A new promise of the category data
const data = await genKnowledgeCategory.getData(); { id: 9, name: 'General Knowledge', questionCounts: { total: 298, forEasy: 116, forMedium: 123, forHard: 59 } }
-
fetchQuestions(options?: Omit<QuestionOptions, "category">): Promise<QuestionOptions[]>
Fetches questions for this category. Wrapper for
getQuestions
Returns: Promise<QuestionOptions[]>
An array of questions
myCategory.fetchQuestions({ amount: 50, difficulty: 'easy', type: 'multiple', }); // Same outputs as getQuestions()
Class for starting OpenTDB API sessions
Properties:
-
token
The current session token
type: string | null
-
start(): Promise string
Starts a new trivia session and assigns the new token to
Session#token
.Returns: Promise
The session token.
-
reset(): Promise string
Resets the current trivia session.
Returns: Promise string
The new session token.
-
end(): void
Sets
Session#token
to null
Category data parsed for the end developer
interface CategoryData {
id: number;
name: CategoryName<"Pretty">;
questionCounts: {
total: number;
forEasy: number;
forMedium: number;
forHard: number;
};
}
Describes a common response body from OpenTDB.
interface OpenTDBResponseDefault<T> {
response_code: number;
results: T[];
}
A category data response from OpenTDB
interface OpenTDBResponseCategoryData {
category_id: number;
category_question_count: RawCategoryData;
}
A new session response from OpenTDB
interface OpenTDBResponseSession {
response_code: number;
response_message?: string;
token: string;
}
The common entries between a RawQuestion and Question
interface QuestionBase {
category: CategoryName;
type: QuestionType;
difficulty: QuestionDifficulty;
}
Category data as recieved from OpenTDB
export interface RawCategoryData {
total_question_count: number;
total_easy_question_count: number;
total_medium_question_count: number;
total_hard_question_count: number;
}
A question object as received from OpenTDB.
RawQuestion extends QuestionBase {
question: string;
correct_answer: string;
incorrect_answers: string[];
}
A raw question parsed for the end developer.
interface Question extends QuestionBase {
value: string;
correctAnswer: string;
incorrectAnswers: string[];
allAnswers: string[];
checkAnswer(arg: string): boolean;
}
Describes the metadata for target questions.
interface QuestionOptions {
amount?: NumberResolvable | null;
category?: CategoryResolvable | null;
difficulty?: QuestionDifficulty | null;
type?: QuestionType | null;
encode?: QuestionEncoding | null;
session?: Session | string | null;
}
A value which can be resolved into a category id
type CategoryIdResolvable = NumberResolvable;
A value which can be resolved into a category name
type CategoryNameResolvable = CategoryName<"Pretty" | "Strict"> | string;
A name of a category in either format.
type CategoryName<T extends CategoryNameVersion = "Strict"> =
T extends "Pretty" ? CategoryNamePretty : CategoryNameStrict;
'Strict' or 'Pretty' name format
type CategoryNameVersion = keyof typeof CategoryNameVersions;
All pretty category names
type CategoryNamePretty = keyof typeof CategoryNamesPretty;
All strict category names
type CategoryNameStrict = keyof typeof CategoryNamesStrict;
Any value that can be resolved into a category
type CategoryResolvable = CategoryNameResolvable | CategoryIdResolvable | Category;
The kind of resolvable
type CategoryResolvableType = "ID" | "NAME";
Any value that can represent a number
type NumberResolvable = `${number}` | number;
All valid OpenTDB API response codes
type OpenTDBResponseCode = 0 | 1 | 2 | 3 | 4;
A question in either format
type Questions<T extends QuestionVersion = "Final"> = T extends "Raw"
? RawQuestion
: Question;
All difficulties
type QuestionDifficulty = keyof typeof QuestionDifficulties;
A type which requires the bare minimum to establish QuestionOptions defaults
type QuestionOptionsDefaults = Pick<
QuestionOptions,
"amount" | "encode"
>;
A question type
type QuestionType = keyof typeof QuestionTypes;
A question encoding
type QuestionEncoding = keyof typeof QuestionEncodings;
A question version
type QuestionVersion = keyof typeof QuestionVersions;