UNPKG

@trivia-api/fetch

Version:

Functions to fetch data from The Trivia API.

45 lines (44 loc) 1.93 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getQuiz = void 0; const constants_1 = require("./constants"); const client_1 = require("@trivia-api/client"); const utils_1 = require("./utils"); /** * Creates a quiz, allowing you to group questions together and present the same * set of questions to multiple users. * * @param params - Parameters for creating a quiz * @param params.apiKey - API key with access to sessions * @param params.title - Title of the quiz * @param params.questionIds - IDs of the questions to include in the quiz * @param host - Optional parameter to send to a different API host * * @returns ID of the created quiz */ const getQuiz = ({ apiKey, id, language }, host = constants_1.TRIVIA_API_BASE_URL) => __awaiter(void 0, void 0, void 0, function* () { (0, utils_1.setHeaders)({ apiKey }); (0, utils_1.setHost)(host); try { const quiz = yield client_1.QuizzesService.getQuiz({ id, language }); return quiz; } catch (e) { if (e instanceof client_1.ApiError) { console.error(e.status); console.error(e.body); console.error(e.message); } throw e; } }); exports.getQuiz = getQuiz;