-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
query.ts
45 lines (37 loc) · 1.26 KB
/
query.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
import { createError } from 'h3'
import { serverQueryContent } from '../storage'
import { getContentQuery } from '../../utils/query'
import { useRuntimeConfig, cachedEventHandler } from '#imports'
export default cachedEventHandler(async (event) => {
const query = getContentQuery(event)
const { advanceQuery } = useRuntimeConfig().public.content.experimental
if (query.first) {
let contentQuery = serverQueryContent(event, query)
if (!advanceQuery) {
contentQuery = contentQuery.withDirConfig()
}
const content = await contentQuery.findOne()
const _result = advanceQuery ? content?.result : content
// @ts-ignore
const missing = !_result && !content?.dirConfig?.navigation?.redirect && !content?._dir?.navigation?.redirect
// If no documents matchs and using findOne()
if (missing) {
throw createError({
statusMessage: 'Document not found!',
statusCode: 404,
data: {
description: 'Could not find document for the given query.',
query
}
})
}
return content
}
if (query.count) {
return serverQueryContent(event, query).count()
}
return serverQueryContent(event, query).find()
}, {
maxAge: 31536000,
shouldBypassCache: () => !!import.meta.dev
})