@trivia-api/fetch
Version:
Functions to fetch data from The Trivia API.
55 lines (54 loc) • 2.44 kB
JavaScript
;
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.getQuestions = void 0;
const constants_1 = require("./constants");
const client_1 = require("@trivia-api/client");
const utils_1 = require("./utils");
/**
* Get random questions from The Trivia API
* @param params - To filter the questions returned
* @param params.apiKey - Optional parameter to send an API key
* @param host - Optional parameter to send to a different API host
* @returns
*/
const getQuestions = (params = {}, host = constants_1.TRIVIA_API_BASE_URL) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
(0, utils_1.setHeaders)({ apiKey: params.apiKey });
(0, utils_1.setHost)(host);
const _params = {
limit: params.limit,
categories: (_a = params.categories) === null || _a === void 0 ? void 0 : _a.join(","),
difficulties: (_b = params.difficulties) === null || _b === void 0 ? void 0 : _b.join(","),
region: params.region,
tags: (_c = params.tags) === null || _c === void 0 ? void 0 : _c.join(","),
session: params.session,
types: (_d = params.types) === null || _d === void 0 ? void 0 : _d.join(","),
language: params.language,
preview: typeof params.preview === "boolean"
? params.preview
? "true"
: "false"
: undefined,
};
try {
const response = yield client_1.QuestionsService.getRandomQuestions(_params);
return response;
}
catch (err) {
if (err instanceof client_1.ApiError) {
console.error(err.status);
console.error(err.statusText);
}
throw err;
}
});
exports.getQuestions = getQuestions;