Skip to content

Commit

Permalink
Fix types for trivia api responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
jollytoad committed Mar 13, 2024
1 parent 61064c5 commit f1de03f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion routes/quiz/_components/Quiz.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Delayed } from "../../../components/Delayed.tsx";
import type { TextChoiceQuestion } from "../_trivia_api_types.ts";
import { QuizAnswer } from "./QuizAnswer.tsx";
import { QuizScore } from "./QuizScore.tsx";

Expand All @@ -25,7 +26,7 @@ async function Question() {
);

if (response.ok) {
const questions = await response.json();
const questions = await response.json() as TextChoiceQuestion[];

const { id, question: { text }, correctAnswer, incorrectAnswers } =
questions[0];
Expand Down
14 changes: 14 additions & 0 deletions routes/quiz/_trivia_api_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface TextChoiceQuestion {
type: "text_choice";
id: string;
question: {
text: string;
};
correctAnswer: string;
incorrectAnswers: string[];
category: string;
tags: string[];
difficulty: string;
regions: string[];
isNiche: boolean;
}
3 changes: 2 additions & 1 deletion routes/quiz/answer/:id/:answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QuizAnswer } from "../../_components/QuizAnswer.tsx";
import { notFound } from "@http/fns/response/not_found";
import { getQuizSession, updateQuizScore } from "../../_lib/session.ts";
import { QuizScore } from "../../_components/QuizScore.tsx";
import type { TextChoiceQuestion } from "../../_trivia_api_types.ts";

export default handleFragment(
async function AnswerResult({ req, match }) {
Expand All @@ -24,7 +25,7 @@ export default handleFragment(
"Content-Type": "application/json",
},
},
)).json();
)).json() as TextChoiceQuestion;

const correct = answer === question.correctAnswer;

Expand Down

0 comments on commit f1de03f

Please sign in to comment.