|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +`open-trivia-db` is a small and simple library for interacting with the [OpenTDB](https://opentdb.com/) API. |
| 9 | + |
| 10 | +Live Demo: https://replit.com/@Elitezenv/open-trivia-db-DEMO?v=1 |
| 11 | + |
| 12 | +## Updates (2.0.0) |
| 13 | +- The library received a massive rework through many quality of life changes. |
| 14 | + |
| 15 | +- 'Static' and 'Pretty' category names are no more, developers will no longer have to worry about converting through these formats. |
| 16 | + |
| 17 | +- All QuestionOption option types now have respective enums to work with. |
| 18 | + |
| 19 | +- `Category` is now just a class with various static methods for working with categories. |
| 20 | + |
| 21 | + |
| 22 | +## Example Code |
| 23 | +```js |
| 24 | +import { getQuestions, CategoryNames } from "open-trivia-db"; |
| 25 | + |
| 26 | +const questions = await getQuestions({ |
| 27 | + amount: 10, |
| 28 | + category: CategoryNames.Animals, |
| 29 | + difficulty: QuestionDifficulties.Easy, |
| 30 | +}) |
| 31 | +``` |
| 32 | + |
| 33 | +## Result |
| 34 | +```js |
| 35 | +[ |
| 36 | + { |
| 37 | + value: 'How many teeth does an adult rabbit have?', |
| 38 | + category: { id: 27, name: 'Animals', getData: [Function: getData] }, |
| 39 | + type: 'multiple', |
| 40 | + difficulty: 'easy', |
| 41 | + correctAnswer: '28', |
| 42 | + incorrectAnswers: [ '30', '26', '24' ], |
| 43 | + allAnswers: [ '24', '28', '30', '26' ], |
| 44 | + checkAnswer: [Function: checkAnswer] |
| 45 | + } |
| 46 | + ... |
| 47 | +] |
| 48 | +``` |
| 49 | + |
| 50 | +# Guide |
| 51 | +## Getting Questions |
| 52 | + |
| 53 | +Questions can be fetched via the `getQuestions()` function by supplying options such as `amount`, `category`, `difficulty`, `type`, `session` and `encode`. |
| 54 | + |
| 55 | +`type`: The kind of questions, such as multiple choice (`"multiple"`) or true/false (`"boolean"`). |
| 56 | + |
| 57 | +`session`: A session instance or session token. [Learn about sessions](#sessions) |
| 58 | + |
| 59 | +`encode`: The encoding of the questions such as `base64`, `urlLegacy`, `url3968` or `none` which is the default. |
| 60 | + |
| 61 | +You can apply options via their respective enums. |
| 62 | + |
| 63 | +The result will be an array of questions. |
| 64 | + |
| 65 | +```js |
| 66 | +import { |
| 67 | + CategoryNames, |
| 68 | + QuestionDifficulties, |
| 69 | + QuestionTypes, |
| 70 | + QuestionEncodings |
| 71 | +} from "open-trivia-db"; |
| 72 | + |
| 73 | +getQuestions({ |
| 74 | + amount: 50, |
| 75 | + category: CategoryNames["Entertainment: Japanese Anime & Manga"], |
| 76 | + difficulty: QuestionDifficulties.Hard, |
| 77 | + type: QuestionTypes.Multiple, |
| 78 | + encode: QuestionEncodings.None |
| 79 | +}) |
| 80 | +``` |
| 81 | + |
| 82 | +## Getting Categories and Category Data |
| 83 | +<hr> |
| 84 | + |
| 85 | +A category resolvable can either be a category name or id. Category id's range from 9-32 inclusive, for there are 23 categories. |
| 86 | + |
| 87 | +To jump between resolvables, use `Category.idByName()` and `Category.nameById()`. |
| 88 | + |
| 89 | +```js |
| 90 | +import { Category, CategoryNames } from "open-trivia-db"; |
| 91 | + |
| 92 | +Category.idByName('Art'); // 25 |
| 93 | +Category.nameById(25); // 'Art' |
| 94 | +``` |
| 95 | + |
| 96 | +### Getting a Category's Data |
| 97 | +Use `Category.getCategory()` to get a category's data such as name, id, and question counts. |
| 98 | + |
| 99 | +```js |
| 100 | +import { Category, CategoryNames } from "open-trivia-db" |
| 101 | + |
| 102 | +Category.getCategory(CategoryNames.Geography) |
| 103 | + .then(console.log) |
| 104 | +``` |
| 105 | + |
| 106 | +```js |
| 107 | +{ |
| 108 | + id: 22, |
| 109 | + name: 'Geography', |
| 110 | + questionCount: { |
| 111 | + total: 275, |
| 112 | + easy: 80, medium: 139, hard: 56 |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +You can also complete a category's data through a question via `Question.category.getData()` |
| 118 | + |
| 119 | +```js |
| 120 | +const targetQuestion = questions[0] // from getQuestions() |
| 121 | + |
| 122 | +targetQuestion.category.getData() |
| 123 | + .then(console.log) |
| 124 | +``` |
| 125 | + |
| 126 | +## Sessions |
| 127 | +A session ensures you are not supplied a question more than once throughout it's lifetime. |
| 128 | + |
| 129 | +Initialize a session and supply the instance into `getQuestions()`. Make sure to await or resolve `Session.start()`. |
| 130 | +```js |
| 131 | +import { Session } from "open-trivia-db" |
| 132 | + |
| 133 | +const mySession = new Session(); |
| 134 | +await mySession.start(); |
| 135 | + |
| 136 | +getQuestions({ |
| 137 | + session: mySession |
| 138 | +}) |
| 139 | +``` |
| 140 | + |
| 141 | +`getQuestions()` will return an error once your session has served every single question in OpenTDB. Don't worry, theres thousands of questions! You will likely never come accross a session's end. However, if you wish to reset your session, use `Session.reset()`. |
| 142 | + |
| 143 | +```js |
| 144 | +await mySession.reset(); |
| 145 | +``` |
| 146 | + |
| 147 | +<hr> |
0 commit comments