Skip to content

Commit 221ca18

Browse files
beeequeueBrutuZ
authored andcommitted
improve include query param parsing and handling
1 parent 5e73cb2 commit 221ca18

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/routes/v2/include.test-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ export const testIncludeQueryParam = (
5353
expect(response.headers.get("content-type")).toContain("application/json")
5454
})
5555

56+
test("duplicate sources (anilist,anilist,themoviedb)", async () => {
57+
await db
58+
.insertInto("relations")
59+
.values({ anilist: 1337, thetvdb: 1337, themoviedb: 1337, imdb: "tt1337" })
60+
.execute()
61+
62+
const query = new URLSearchParams({
63+
source,
64+
id: prefixify(source, "1337"),
65+
include: [Source.AniList, Source.AniList, Source.TheMovieDB].join(","),
66+
})
67+
const response = await app.fetch(new Request(`http://localhost${path}?${query.toString()}`))
68+
69+
await expect(response.json()).resolves.toStrictEqual(
70+
arrayify({ anilist: 1337, themoviedb: 1337 }),
71+
)
72+
expect(response.status).toBe(200)
73+
expect(response.headers.get("content-type")).toContain("application/json")
74+
})
75+
5676
test("all the sources", async () => {
5777
await db
5878
.insertInto("relations")

src/routes/v2/include.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@ import * as v from "valibot"
22

33
import { db, Source, type SourceValue } from "../../db/db.ts"
44

5+
const allSources = Object.values(Source)
6+
57
export const includeSchema = v.object({
68
include: v.optional(
79
v.pipe(
810
v.string(),
911
v.regex(/^[\-a-z,]+$/, "Invalid `include` query"),
12+
v.transform((value) => value.split(",")),
13+
v.transform((sources) => (sources.length > 1 ? Array.from(new Set(sources)) : sources)),
1014
v.minLength(1),
11-
v.maxLength(200),
15+
v.maxLength(allSources.length),
1216
),
1317
),
1418
})
1519

1620
export type IncludeQuery = v.InferOutput<typeof includeSchema>
1721

18-
const sources = Object.values(Source)
19-
const selectAll = sources.map((column) => db.dynamic.ref<SourceValue>(column))
20-
export const buildSelectFromInclude = (include: string | null | undefined) => {
22+
const selectAll = allSources.map((column) => db.dynamic.ref<SourceValue>(column))
23+
export const buildSelectFromInclude = (include: string[] | null | undefined) => {
2124
if (include == null) {
2225
return selectAll
2326
}
2427

2528
return include
26-
.split(",")
27-
.filter((inclusion) => sources.includes(inclusion as SourceValue))
29+
.filter((inclusion) => allSources.includes(inclusion as SourceValue))
2830
.map((column) => db.dynamic.ref<SourceValue>(column))
2931
}

0 commit comments

Comments
 (0)