fix(api): 🐛 Fix string query values being incorrectly casted as booleans

This commit is contained in:
Jesse Wierzbinski 2024-04-15 15:02:25 -10:00
parent 937b2c3cde
commit 6063b3ff44
No known key found for this signature in database
3 changed files with 24 additions and 11 deletions

View file

@ -173,11 +173,23 @@ export class RequestParser {
}
(result[arrayKey] as string[]).push(decodeURIComponent(value));
} else {
result[key as keyof T] = decodeURIComponent(
value,
result[key as keyof T] = castBoolean(
decodeURIComponent(value),
) as T[keyof T];
}
}
return result;
}
}
const castBoolean = (value: string) => {
if (["true"].includes(value)) {
return true;
}
if (["false"].includes(value)) {
return false;
}
return value;
};

View file

@ -93,11 +93,11 @@ describe(meta.route, () => {
}
});
test("should only fetch remote statuses (0)", async () => {
test("should only fetch remote statuses", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&remote=true`,
`${meta.route}?remote=true&allow_local_only=false&only_media=false`,
config.http.base_url,
),
{
@ -113,7 +113,7 @@ describe(meta.route, () => {
const objects = (await response.json()) as APIStatus[];
expect(objects.length).toBe(0);
expect(objects).toBeArrayOfSize(1);
});
describe("should paginate properly", async () => {

View file

@ -26,9 +26,9 @@ export const schema = z.object({
since_id: z.string().regex(idValidator).optional(),
min_id: z.string().regex(idValidator).optional(),
limit: z.coerce.number().int().min(1).max(80).optional().default(20),
local: z.coerce.boolean().optional(),
remote: z.coerce.boolean().optional(),
only_media: z.coerce.boolean().optional(),
local: z.boolean().optional(),
remote: z.boolean().optional(),
only_media: z.boolean().optional(),
});
export default apiRoute<typeof meta, typeof schema>(
@ -53,9 +53,10 @@ export default apiRoute<typeof meta, typeof schema>(
// use authorId to grab user, then use user.instanceId to filter local/remote statuses
remote
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NOT NULL)`
: local
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NULL)`
: undefined,
: undefined,
local
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NULL)`
: undefined,
only_media
? sql`EXISTS (SELECT 1 FROM "Attachment" WHERE "Attachment"."statusId" = ${status.id})`
: undefined,