mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
fix(api): 🐛 Fix string query values being incorrectly casted as booleans
This commit is contained in:
parent
937b2c3cde
commit
6063b3ff44
|
|
@ -173,11 +173,23 @@ export class RequestParser {
|
||||||
}
|
}
|
||||||
(result[arrayKey] as string[]).push(decodeURIComponent(value));
|
(result[arrayKey] as string[]).push(decodeURIComponent(value));
|
||||||
} else {
|
} else {
|
||||||
result[key as keyof T] = decodeURIComponent(
|
result[key as keyof T] = castBoolean(
|
||||||
value,
|
decodeURIComponent(value),
|
||||||
) as T[keyof T];
|
) as T[keyof T];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const castBoolean = (value: string) => {
|
||||||
|
if (["true"].includes(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (["false"].includes(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -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(
|
const response = await sendTestRequest(
|
||||||
new Request(
|
new Request(
|
||||||
new URL(
|
new URL(
|
||||||
`${meta.route}?limit=20&remote=true`,
|
`${meta.route}?remote=true&allow_local_only=false&only_media=false`,
|
||||||
config.http.base_url,
|
config.http.base_url,
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
|
|
@ -113,7 +113,7 @@ describe(meta.route, () => {
|
||||||
|
|
||||||
const objects = (await response.json()) as APIStatus[];
|
const objects = (await response.json()) as APIStatus[];
|
||||||
|
|
||||||
expect(objects.length).toBe(0);
|
expect(objects).toBeArrayOfSize(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("should paginate properly", async () => {
|
describe("should paginate properly", async () => {
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,9 @@ export const schema = z.object({
|
||||||
since_id: z.string().regex(idValidator).optional(),
|
since_id: z.string().regex(idValidator).optional(),
|
||||||
min_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),
|
limit: z.coerce.number().int().min(1).max(80).optional().default(20),
|
||||||
local: z.coerce.boolean().optional(),
|
local: z.boolean().optional(),
|
||||||
remote: z.coerce.boolean().optional(),
|
remote: z.boolean().optional(),
|
||||||
only_media: z.coerce.boolean().optional(),
|
only_media: z.boolean().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default apiRoute<typeof meta, typeof schema>(
|
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
|
// use authorId to grab user, then use user.instanceId to filter local/remote statuses
|
||||||
remote
|
remote
|
||||||
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NOT NULL)`
|
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NOT NULL)`
|
||||||
: local
|
: undefined,
|
||||||
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NULL)`
|
local
|
||||||
: undefined,
|
? sql`EXISTS (SELECT 1 FROM "User" WHERE "User"."id" = ${status.authorId} AND "User"."instanceId" IS NULL)`
|
||||||
|
: undefined,
|
||||||
only_media
|
only_media
|
||||||
? sql`EXISTS (SELECT 1 FROM "Attachment" WHERE "Attachment"."statusId" = ${status.id})`
|
? sql`EXISTS (SELECT 1 FROM "Attachment" WHERE "Attachment"."statusId" = ${status.id})`
|
||||||
: undefined,
|
: undefined,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue