Skip to content

Commit 584ea35

Browse files
committed
fix: use default season on empty input
1 parent 881f615 commit 584ea35

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/routes/rules.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { db, selectSlimQuestion } from "../db";
66
import { questions, metadata as dbMetadata } from "../db/schema";
77
import { slimQuestionSchema } from "../schemas";
88
import tags from "../tags";
9-
import { errorString, trycatch } from "../utils";
9+
import { errorString, isEmptyOrNullish, trycatch } from "../utils";
1010
import { z } from "zod";
1111
import { fetchRules } from "../apis/rules";
1212

@@ -51,7 +51,7 @@ rules.get(
5151
})
5252
),
5353
async (c) => {
54-
54+
5555
const { season: inputSeason } = c.req.valid("query");
5656
const { rule: inputRule } = c.req.valid("param");
5757
const [metadataError, metadata] = await trycatch(() =>
@@ -65,7 +65,9 @@ rules.get(
6565
if (!metadata) {
6666
return c.text("Metadata object is empty.", 500);
6767
}
68-
const season = inputSeason ?? metadata.currentSeason;
68+
const season = isEmptyOrNullish(inputSeason)
69+
? metadata.currentSeason
70+
: inputSeason;
6971
const [ruleQuestionsError, ruleQuestions] = await trycatch(() =>
7072
selectSlimQuestion()
7173
.where(
@@ -83,7 +85,9 @@ rules.get(
8385
console.error(`Unable to resolve rule data (${rulesError})`)
8486
return c.json(ruleQuestions);
8587
}
86-
const targetRule = rules.ruleGroups.flatMap(group => group.rules).find(({rule}) => rule === `<${inputRule}>`);
88+
const targetRule = rules.ruleGroups
89+
.flatMap(group => group.rules)
90+
.find(({ rule }) => rule === `<${inputRule}>`);
8791
if (!targetRule) {
8892
return c.json(ruleQuestions);
8993
}

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ export const trycatch = async <T>(
1616
export const errorString = (error: Error) => {
1717
return `${error.name} ${error.message} ${error.stack}`
1818
}
19+
20+
export type Nullish = null | undefined;
21+
22+
export const isEmptyOrNullish = (input: string | undefined | null): input is Nullish => input === null || input === undefined || input?.trim() === "";

0 commit comments

Comments
 (0)