Skip to content

Documentation

Elitezen edited this page Nov 29, 2022 · 3 revisions

Documentation (2.0.0)


Functions

getQuestions

Fetches an array of questions that match the provided options.

getQuestions(options?:Partial<QuestionOptions>): Promise<Question[]>

await getQuestions({
   amount: 50,
   category: 'History',
   difficulty: 'hard',
   type: 'multiple',
   encoding: 'none',
   session: aSessionInstance
})

getCategory

Fetches a trivia category's data. Duplicate of Category.getCategory().

getCategory(arg: CategoryResolvable): Promise<CategoryData>

await getCategory(9)

Classes

Category

Properties:

  • static .allNames

    An array of all category names. Use Category.random() for a random pick.

    type: CategoryNameType[]

Methods:

  • static .decodeEncodedCategoryName(str: string): CategoryNameType | null

    Decodes a URLLegacy, URL3968 or Base64 category name.

    Category.decodeEncodedCategoryName('TXl0aG9sb2d5') // 'Mythology'
  • static .getCategory()

    Taken from the function with the same name.

  • static .idByName(name: CategoryNameType): number | null

    Returns a category id when given it's name.

    Category.idByName('General Knowledge') // 9
  • static .nameById(id: number: CategoryNameType) | null

    Returns a category name when given it's id.

    Category.nameById(9) // 'General Knowledge'
  • static .random(resolvableType?: ["name"] | "id"): CategoryNameType | number

    Picks a random category name or id.

Session

Properties:

  • token

    This session's current token

    type: string

Methods:

  • .assert(): void

    Checks if the session has been initialized or holds a token. Emits a warning if not. (Intended for internal use)

  • .start(): void

    Generates a session token and assigns it to the instance Session.token.

  • .reset(): void

    Resets the current session's data.


Typings

Enums

CategoryNames

enum CategoryNames {
  "General Knowledge" = 9,
  "Entertainment: Books",
  "Entertainment: Film",
  "Entertainment: Music",
  "Entertainment: Musicals & Theatres",
  "Entertainment: Television",
  "Entertainment: Video Games",
  "Entertainment: Board Games",
  "Science & Nature",
  "Science: Computers",
  "Science Mathematics",
  "Mythology",
  "Sports",
  "Geography",
  "History",
  "Politics",
  "Art",
  "Celebrities",
  "Animals",
  "Vehicles",
  "Entertainment: Comics",
  "Science: Gadgets",
  "Entertainment: Japanese Anime & Manga",
  "Entertainment: Cartoon & Animations",
}

QuestionDifficulties

enum QuestionDifficulties {
  Easy = "easy",
  Medium = "medium",
  Hard = "hard",
}

QuestionEncodings

enum QuestionEncodings {
  Base64 = "base64",
  None = "none",
  Url3986 = "url3986",
  UrlLegacy = "urlLegacy",
}

QuestionTypes

enum QuestionTypes {
  Multiple = "multiple",
  Boolean = "boolean",
}

Interfaces

Category Data

A category's API Data

Types Included: number, CategoryNameType

interface CategoryData {
  id: number;
  name: CategoryNameType;
  questionCount: {
    total: number;
    easy: number;
    medium: number;
    hard: number;
  };
}

MinifiedCategoryData

Category data that comes inside a Question

Types Included: CategoryData

interface MinifiedCategoryData {
  id: number;
  name: string;
  getData: () => Promise<CategoryData>; // Fetch full category data
}

Question

A fetched trivia question

Types Included: MinifiedCategoryData, QuestionTypeType, string, boolean

interface Question {
  category: MinifiedCategoryData; // A portion of the category's API data
  type: QuestionTypeType;
  difficulty: QuestionDifficultyType;
  value: string; // The question itself
  correctAnswer: string;
  incorrectAnswers: string[];
  allAnswers: string[]; // The correct and incorrect answers in one array, shuffled
  checkAnswer: (str: string, caseSensitive?: boolean) => boolean; // Returns true if the given string matches .correctAnswer
}

QuestionOptions

Data describing target questions

Types Included: CategoryResolvable, QuestionDifficultyType, QuestionDifficulties, QuestionTypeType, QuestionTypes, QuestionEncodingType, QuestionEncodings, Session, string, null

interface QuestionOptions ... {
  amount: number;
  category: CategoryResolvable;
  difficulty: QuestionDifficultyType | QuestionDifficulties;
  type: QuestionTypeType | QuestionTypes;
  encode: QuestionEncodingType | QuestionEncodings;
  session: Session | string | null;
}

Types

CategoryNameType

All string category names

Types Included: CategoryNames

type CategoryNameType = keyof typeof CategoryNames;

CategoryResolvable

A value that can be resolved into an OpenTDB category

Types Included: CategoryNameType, CategoryNames, number

type CategoryResolvable = CategoryNameType | CategoryNames | number;

QuestionDifficultyType

A string difficulty

type QuestionDifficultyType = "easy" | "medium" | "hard"

QuestionEncodingType

A string encoding format

type QuestionEncodingType = "none" | "base64" | "url3986" | "urlLegacy";

QuestionTypeType

A string question type (multiple choice/true or false)

type QuestionTypeType = "multiple" | "boolean";