# open-trivia-db

> A small and simple wrapper for the Open Trivia Database API.

Latest version **2.1.6** (published 2024-05-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install open-trivia-db
pnpm add open-trivia-db
yarn add open-trivia-db
bun add open-trivia-db
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.6 |
| Published | 2024-05-30 |
| First published | 2022-05-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.0.0 |
| Dependencies | 0 |
| Unpacked size | 61.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Elitezen |
| Maintainers | elitezen |
| Keywords | opentriviadatabase, opentdb, opentriviadb, trivia, games, fun, api, typescript, small, quiz |

## Links

- npm: https://www.npmjs.com/package/open-trivia-db
- Repository: https://github.com/Elitezen/open-trivia-db-wrapper
- Homepage: https://github.com/Elitezen/open-trivia-db-wrapper#readme
- Issues: https://github.com/Elitezen/open-trivia-db-wrapper/issues
- npm.io page: https://npm.io/package/open-trivia-db

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.1.6 (latest) — 2024-05-30
- 2.1.5 — 2023-03-20
- 2.1.4 — 2023-03-19
- 2.1.3 — 2023-03-17
- 2.1.1 — 2023-01-12
- 2.1.0 — 2023-01-12
- 2.0.0 — 2022-11-28
- 1.0.2 — 2022-08-02
- 1.0.1 — 2022-06-06
- 1.0.0 — 2022-05-14

## README

![OpenTriviaDB](https://i.imgur.com/QBhF5aY.png)

![version](https://img.shields.io/npm/v/open-trivia-db)
![downloads](https://img.shields.io/npm/dm/open-trivia-db)
![minisize](https://img.shields.io/bundlephobia/min/open-trivia-db)
![types](https://img.shields.io/npm/types/open-trivia-db)

`open-trivia-db` is a small and simple library for interacting with the [OpenTDB](https://opentdb.com/) API.

**Live Demo**: https://replit.com/@Elitezenv/open-trivia-db-DEMO?v=1

**Documentation**: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation

## Discord.JS Add On

Planning a trivia command for your Discord bot?
`discord-trivia` v2 is coming!: https://github.com/Elitezen/discord-trivia

# 2.1.5
- Fixed the issue of the dist/ folder not being included.

## Example Code
```js
import { getQuestions, CategoryNames } from "open-trivia-db";

const questions = await getQuestions({
  amount: 10,
  category: CategoryNames.Animals,
  difficulty: QuestionDifficulties.Easy,
})
```

## Result
```js
[
  {
    value: 'How many teeth does an adult rabbit have?',
    category: { id: 27, name: 'Animals', getData: [Function: getData] },
    type: 'multiple',
    difficulty: 'easy',
    correctAnswer: '28',
    incorrectAnswers: [ '30', '26', '24' ],
    allAnswers: [ '24', '28', '30', '26' ],
    checkAnswer: [Function: checkAnswer]
  }
  ...
]
```

# Guide
## Getting Questions

Questions can be fetched via the `getQuestions()` function by supplying options such as `amount`, `category`, `difficulty`, `type`, `session` and `encode`.

`type`: The kind of questions, such as multiple choice (`"multiple"`) or true/false (`"boolean"`).

`session`: A session instance or session token. [Learn about sessions](#sessions)

`encode`: The encoding of the questions such as `base64`, `urlLegacy`, `url3968` or `none` which is the default.

You can apply options via their respective enums.

The result will be an array of questions.

```js
import { 
  CategoryNames, 
  QuestionDifficulties, 
  QuestionTypes, 
  QuestionEncodings 
} from "open-trivia-db";

getQuestions({
  amount: 50,
  category: CategoryNames["Entertainment: Japanese Anime & Manga"],
  difficulty: QuestionDifficulties.Hard,
  type: QuestionTypes.Multiple,
  encode: QuestionEncodings.None
})
```

## Getting Categories and Category Data
<hr>

A category resolvable can either be a category name or id. Category id's range from 9-32 inclusive, for there are 23 categories.

To jump between resolvables, use `Category.idByName()` and `Category.nameById()`.

```js
import { Category, CategoryNames } from "open-trivia-db";

Category.idByName('Art'); // 25
Category.nameById(25); // 'Art'
```

### Getting a Category's Data
Use `Category.getCategory()` to get a category's data such as name, id, and question counts.

```js
import { Category, CategoryNames } from "open-trivia-db"

Category.getCategory(CategoryNames.Geography)
  .then(console.log)
```

```js
{
  id: 22,
  name: 'Geography',
  questionCount: { 
    total: 275, 
    easy: 80, medium: 139, hard: 56 
  }
}
```

You can also complete a category's data through a question via `Question.category.getData()`

```js
const targetQuestion = questions[0] // from getQuestions()

targetQuestion.category.getData()
  .then(console.log)
```

## Sessions
A session ensures you are not supplied a question more than once throughout it's lifetime.

Initialize a session and supply the instance into `getQuestions()`. Make sure to await or resolve `Session.start()`.
```js
import { Session } from "open-trivia-db"

const mySession = new Session();
await mySession.start();

getQuestions({
  session: mySession
})
```

`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()`.

```js
await mySession.reset();
```

<hr>

**Documentation**: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation

---
_Source: https://npm.io/package/open-trivia-db · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
