-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.ts
95 lines (81 loc) · 2.37 KB
/
routes.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
import type { DocumentData } from 'firebase/firestore'
// NOTE: nuxt generate時に呼ばれるときはalias pathが使えない https://github.com/nuxt/nuxt/issues/7017
import { db } from './app/api/apis'
import type { Query } from './app/api/firestore'
import { runtimePublicConfig } from './config'
interface Articles {
[key: string]: DocumentData[]
}
export const generateRoutes = async () => {
// /articles/_article.vue用の記事
const allArticles = await getArticles()
const articleRoutes = allArticles.flatMap((article) => {
const uuidRoute = {
route: `/articles/${article.id}`,
payload: article,
}
if (article.customId) {
return [
uuidRoute,
{
route: `/articles/${article.customId}`,
payload: article,
},
]
}
return uuidRoute
})
const allTags = await getArticleTags()
// /tags/_tag.vue用の記事一覧
const articleRecordByTag = await getArticleRecordByTag()
const tagRoutes = Object.entries(articleRecordByTag)
.map(([tag, articles]) => {
return {
route: `/tags/${tag}`,
payload: {
articlesByTag: articles,
recentArticles: articles.slice(0, 2),
allTags,
},
}
})
.filter(({ payload }) => {
return payload.articlesByTag.length > 0
})
return [...articleRoutes, ...tagRoutes]
}
const getArticleRecordByTag = async () => {
const allTags = await getArticleTags()
const articleRecord: Articles = {}
const articlesPath = `users/${runtimePublicConfig.AUTHOR_ID}/articles`
for (let i = 0; i < allTags.length; i++) {
const tag = allTags[i]
const queries: Query[] = [
{
fieldPath: 'tags',
filterStr: 'array-contains',
value: tag,
},
{
fieldPath: 'isPublished',
filterStr: '==',
value: true,
},
]
articleRecord[tag] = await db.getDocsData(articlesPath, queries)
}
return articleRecord
}
const getArticles = async () => {
const collectionPath = `users/${runtimePublicConfig.AUTHOR_ID}/articles`
const query: Query = {
fieldPath: 'isPublished',
filterStr: '==',
value: true,
}
return await db.getDocsData(collectionPath, [query])
}
const getArticleTags = async () => {
const articleTagsPath = `users/${runtimePublicConfig.AUTHOR_ID}/articleTags`
return await db.getDocIds(articleTagsPath)
}