This package has been deprecated

Author message:

This package has been rebranded as open-trivia-db to better represent it's purpose. This module will no longer be maintained, use open-trivia-db instead.

easy-trivia
TypeScript icon, indicating that this package has built-in type declarations

2.1.5 • Public • Published

Easy Trivia

OpenTriviaDB

Easy Trivia is a small, simple and fast wrapper for Open Trivia Database - A Free to use, user-contributed trivia question database. Built with TypeScript, works with VanillaJS.

Join the Discord for updates: https://discord.gg/wtwM4HhbAr

discord-trivia is almost ready: https://github.com/Elitezen/discord-trivia

Support me: https://www.paypal.com/paypalme/alejandromuratalla

2.1.2 Changelog

View the list of changes in Easy Trivia 2.1.2:

https://github.com/Elitezen/easy-trivia/wiki/Changelog

Installation

Ensure you are using Node version 14 or higher and that your enviroment contains the https module.

npm i easy-trivia

Example Usage

The following examples make use of the Async/Await syntax. Ensure you are inside an async function, otherwise use promise callbacks.

Fetching Questions

You can provide QuestionOptions to describe the type of questions you want to recieve.

import { Category, getQuestions } from 'easy-trivia';

const questions = await getQuestions({
   amount: 50, // 1 - 50
   difficulty: 'easy', // or 'medium' or 'hard'
   type: 'multiple', // or 'boolean (true/false)
   category: Category.allNames.SCIENCE_COMPUTERS
});

Output

Click to view
[
  {
 	value: 'What is the code name for the mobile operating system Android 7.0?',
  	category: 'Science: Computers',
  	type: 'multiple',
  	difficulty: 'easy',
  	correctAnswer: 'Nougat',
  	incorrectAnswers: [ 'Ice Cream Sandwich', 'Jelly Bean', 'Marshmallow' ],
  	allAnswers: [ 'Nougat', 'Jelly Bean', 'Marshmallow', 'Ice Cream Sandwich' ],
  	checkAnswer: [Function: checkAnswer]
  }

 ...
]

Working With Categories

Creating Categories with Resolvables

You can generate a category class by providing a CategoryResolvable which includes a category's name or id. An instance of Category will allow you to fetch category data and questions relating to the provided resolvable.

let myCategory = new Category(9);

myCategory = new Category('GENERAL_KNOWLEDGE');

myCategory = new Category(Category.allNames.GENERAL_KNOWLEDGE);

Fetching a Category's API Data

const data = await myCategory.getData();

Output

Click to view
  {
  	id: 9,
  	name: 'General Knowledge',
  	questionCounts: { 
  		total: 298, 
  		forEasy: 116, 
  		forMedium: 123, 
  		forHard: 59 
  	}
  }

Fetching Questions From a Category

const questions = await myCategory.fetchQuestions({
	amount: 1,
	difficulty: 'hard'
});

// Same outputs as getQuestions()

You can always get information relating to a category by simply passing a resolvable into getQuestions() and getCategoryData()

getQuestions({
	category: 9
});

getCategoryData('GENERAL_KNOWLEDGE');

// Same as myCategory.fetchQuestions() and .getData()

Using Sessions

A session ensures you do not get duplicate questions.

import { Categories, Session, getQuestions } from 'easy-trivia';

const session = new Session();
await session.start();


const batch1 = await getQuestions({
  amount: 10,
  category: Category.random(),
  difficulty: 'hard',
  session
});

const batch2 = await getQuestions({
  amount: 10,
  category: Category.random(),
  difficulty: 'hard',
  session
});


const completeBatch = [...batch1, ...batch2]; // All unique!
session.end();

Note: In respect to the API, it is recommended you generate and save 1 session token for use when testing.

Documentation

Documentation has been moved to a GitHub Wiki page:

https://github.com/Elitezen/easy-trivia/wiki/Documentation

Support Me

Any tip is greatly appreciated 😀 https://www.paypal.com/paypalme/alejandromuratalla

Package Sidebar

Install

npm i easy-trivia

Weekly Downloads

2

Version

2.1.5

License

ISC

Unpacked Size

160 kB

Total Files

57

Last publish

Collaborators

  • elitezen