-
Notifications
You must be signed in to change notification settings - Fork 171
/
validate-collection-params.ts
99 lines (93 loc) · 2.75 KB
/
validate-collection-params.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Context } from "@nuxt/types"
import { Pinia } from "pinia"
import { SupportedMediaType } from "~/constants/media"
import {
CollectionParams,
CreatorCollection,
SourceCollection,
} from "~/types/search"
import { useProviderStore } from "~/stores/provider"
import { warn } from "~/utils/console"
import { useFeatureFlagStore } from "~/stores/feature-flag"
/**
* Validate the params for a collection page.
*
* @param firstParam - The first param in the path, either "tag" or "source".
* If the first param is "source", the collection is either a source collection
* or a creator collection.
* @param mediaType - The media type of the collection.
* @param params - `params.pathMatch` is the part of the path after the collection name:
* `/sourceName` or `/sourceName/creator/creatorName`.
* @param $pinia - Pinia instance, necessary to check the feature flag, and validate the
* sources using providerStore.
*/
export function validateCollectionParams({
firstParam,
mediaType,
params,
$pinia,
}: {
firstParam: "tag" | "source"
mediaType: SupportedMediaType
params: Context["params"]
$pinia: Pinia
}): CollectionParams | null {
// This page is shown only when the feature flag is `on`.
if (!useFeatureFlagStore($pinia).isOn("additional_search_views")) {
return null
}
return _validate({ firstParam, mediaType, params, $pinia })
}
function parseCollectionPath(
pathMatch: string
): SourceCollection | CreatorCollection | null {
// Build collection params.
// pathMatch is the part of the path after the collection name:
//`/sourceName` or `/sourceName/creator/creatorName`.
const pathMatchParts = pathMatch
.split("/")
.map((part) => part.trim())
.filter((part) => part !== "")
if (pathMatchParts.length === 1) {
return { collection: "source", source: pathMatchParts[0] }
} else if (pathMatchParts.length === 3 && pathMatchParts[1] === "creator") {
return {
collection: "creator",
creator: pathMatchParts[2],
source: pathMatchParts[0],
}
}
return null
}
export function _validate({
firstParam,
mediaType,
params,
$pinia,
}: {
firstParam: "tag" | "source"
mediaType: SupportedMediaType
params: Context["params"]
$pinia: Pinia
}): CollectionParams | null {
if (firstParam === "tag") {
return params.tag ? { collection: "tag", tag: params.tag } : null
}
const collectionParams = parseCollectionPath(params.pathMatch)
if (!collectionParams) {
return null
}
// Validate source param
if (
!useProviderStore($pinia).isSourceNameValid(
mediaType,
collectionParams.source
)
) {
warn(
`Invalid source name "${collectionParams.source}" for a ${collectionParams.collection} collection page.`
)
return null
}
return collectionParams
}