|
1 | | - |
2 | | - |
3 | | -**open-trivia-db** is a small, simple and fast wrapper for [Open Trivia Database](https://opentdb.com/) - A Free to use, user-contributed trivia question database. Built with TypeScript, works with VanillaJS. |
4 | | - |
5 | | -Documentation: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation |
6 | | - |
7 | | -Live Demo: https://replit.com/@Elitezenv/open-trivia-db-DEMO?v=1 |
8 | | - |
9 | | -Support me: https://www.paypal.com/paypalme/alejandromuratalla |
10 | | - |
11 | | -## Updates |
12 | | -### 1.0.2 |
13 | | -- Switched from https module to Node Fetch API (now requires Node 18) |
14 | | - |
15 | | -# Installation |
16 | | -Ensure you are using Node version 14 or higher and that your enviroment contains the `https` module. |
17 | | -```sh-session |
18 | | -npm i open-trivia-db // Requires NodeJS 18 or higher |
19 | | - |
20 | | -npm i [email protected] // Below NodeJS 18 |
21 | | -``` |
22 | | - |
23 | | -# Example Usage |
24 | | -The following examples make use of the [Async/Await](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) syntax. Ensure you are inside an async function, otherwise use promise callbacks. |
25 | | - |
26 | | -## Fetching Questions |
27 | | -You can provide `QuestionOptions` to describe the type of questions you want to recieve. |
28 | | -```js |
29 | | -import { Category, getQuestions } from 'open-trivia-db'; |
30 | | - |
31 | | -const questions = await getQuestions({ |
32 | | - amount: 50, // 1 - 50 |
33 | | - difficulty: 'easy', // or 'medium' or 'hard' |
34 | | - type: 'multiple', // or 'boolean (true/false) |
35 | | - category: Category.allNames.SCIENCE_COMPUTERS |
36 | | -}); |
37 | | -``` |
38 | | -### Output |
39 | | -<details> |
40 | | - <summary>Click to view</summary> |
41 | | - |
42 | | - ```js |
43 | | -[ |
44 | | - { |
45 | | - value: 'What is the code name for the mobile operating system Android 7.0?', |
46 | | - category: 'Science: Computers', |
47 | | - type: 'multiple', |
48 | | - difficulty: 'easy', |
49 | | - correctAnswer: 'Nougat', |
50 | | - incorrectAnswers: [ 'Ice Cream Sandwich', 'Jelly Bean', 'Marshmallow' ], |
51 | | - allAnswers: [ 'Nougat', 'Jelly Bean', 'Marshmallow', 'Ice Cream Sandwich' ], |
52 | | - checkAnswer: [Function: checkAnswer] |
53 | | - } |
54 | | - |
55 | | - ... |
56 | | -] |
57 | | -``` |
58 | | - |
59 | | -</details> |
60 | | -<hr> |
61 | | - |
62 | | -## Working With Categories |
63 | | - |
64 | | -### Creating Categories with Resolvables |
65 | | - |
66 | | -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. |
67 | | -```js |
68 | | -let myCategory = new Category(9); |
69 | | - |
70 | | -myCategory = new Category('GENERAL_KNOWLEDGE'); |
71 | | - |
72 | | -myCategory = new Category(Category.allNames.GENERAL_KNOWLEDGE); |
73 | | -``` |
74 | | - |
75 | | -<hr> |
76 | | - |
77 | | -### Fetching a Category's API Data |
78 | | - |
79 | | -```js |
80 | | -const data = await myCategory.getData(); |
81 | | -``` |
82 | | - |
83 | | -### Output |
84 | | -<details> |
85 | | - <summary>Click to view</summary> |
86 | | - |
87 | | - ```js |
88 | | - { |
89 | | - id: 9, |
90 | | - name: 'General Knowledge', |
91 | | - questionCounts: { |
92 | | - total: 298, |
93 | | - forEasy: 116, |
94 | | - forMedium: 123, |
95 | | - forHard: 59 |
96 | | - } |
97 | | - } |
98 | | - ``` |
99 | | - |
100 | | -</details> |
101 | | -<hr> |
102 | | - |
103 | | -### Fetching Questions From a Category |
104 | | -```js |
105 | | -const questions = await myCategory.fetchQuestions({ |
106 | | - amount: 1, |
107 | | - difficulty: 'hard' |
108 | | -}); |
109 | | - |
110 | | -// Same outputs as getQuestions() |
111 | | -``` |
112 | | - |
113 | | -You can always get information relating to a category by simply passing a resolvable into `getQuestions()` and `getCategoryData()` |
114 | | - |
115 | | -```js |
116 | | -getQuestions({ |
117 | | - category: 9 |
118 | | -}); |
119 | | - |
120 | | -getCategoryData('GENERAL_KNOWLEDGE'); |
121 | | - |
122 | | -// Same as myCategory.fetchQuestions() and .getData() |
123 | | -``` |
124 | | - |
125 | | -<hr> |
126 | | - |
127 | | -## Using Sessions |
128 | | -A session ensures you do not get duplicate questions. |
129 | | - |
130 | | -```js |
131 | | -import { Category, Session, getQuestions } from 'open-trivia-db'; |
132 | | - |
133 | | -const session = new Session(); |
134 | | -await session.start(); |
135 | | - |
136 | | - |
137 | | -const batch1 = await getQuestions({ |
138 | | - amount: 10, |
139 | | - category: Category.random(), |
140 | | - difficulty: 'hard', |
141 | | - session |
142 | | -}); |
143 | | - |
144 | | -const batch2 = await getQuestions({ |
145 | | - amount: 10, |
146 | | - category: Category.random(), |
147 | | - difficulty: 'hard', |
148 | | - session |
149 | | -}); |
150 | | - |
151 | | - |
152 | | -const completeBatch = [...batch1, ...batch2]; // All unique! |
153 | | -session.end(); |
154 | | -``` |
155 | | - |
156 | | -**Note:** In respect to the API, it is recommended you generate and save 1 session token for use when testing. |
157 | | - |
158 | | -# Documentation |
159 | | -Documentation has been moved to a GitHub Wiki page: |
160 | | - |
161 | | -https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation |
162 | | - |
163 | | -# Support Me |
164 | | -Any tip is greatly appreciated 😀 |
165 | | -https://www.paypal.com/paypalme/alejandromuratalla |
0 commit comments