/
ContentQuery.ts
206 lines (181 loc) · 5.35 KB
/
ContentQuery.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { hash } from 'ohash'
import { PropType, toRefs, defineComponent, h, useSlots } from 'vue'
import type { ParsedContent, QueryBuilder, SortParams } from '../types'
import { computed, useAsyncData, queryContent } from '#imports'
export default defineComponent({
props: {
/**
* The path of the content to load from content source.
*/
path: {
type: String,
required: false,
default: undefined
},
/**
* Select a subset of fields
*/
only: {
type: Array as PropType<Array<string>>,
required: false,
default: undefined
},
/**
* Remove a subset of fields
*/
without: {
type: Array as PropType<Array<string>>,
required: false,
default: undefined
},
/**
* Filter results
*/
where: {
type: Object as PropType<{ [key: string]: any }>,
required: false,
default: undefined
},
/**
* Sort results
*/
sort: {
type: Object as PropType<SortParams>,
required: false,
default: undefined
},
/**
* Limit number of results
*/
limit: {
type: Number as PropType<number>,
required: false,
default: undefined
},
/**
* Skip number of results
*/
skip: {
type: Number as PropType<number>,
required: false,
default: undefined
},
/**
* Filter contents based on locale
*/
locale: {
type: String as PropType<string>,
required: false,
default: undefined
},
/**
* A type of query to be made.
*/
find: {
type: String as PropType<'one' | 'surround'>,
required: false,
default: undefined
}
},
async setup (props) {
const {
path,
only,
without,
where,
sort,
limit,
skip,
locale,
find
} = toRefs(props)
/**
* Check if supplied path refers to partial content.
*
* Might be skipping `partial: true` marked in Markdown contents front-matter.
*/
const isPartial = computed(() => path.value.includes('/_'))
const { data, refresh } = await useAsyncData<ParsedContent | ParsedContent[]>(
`content-query-${hash(props)}`,
() => {
let queryBuilder: QueryBuilder
if (path.value) {
queryBuilder = queryContent(path.value)
} else {
queryBuilder = queryContent()
}
if (only.value) { queryBuilder = queryBuilder.only(only.value) }
if (without.value) { queryBuilder = queryBuilder.without(without.value) }
if (where.value) { queryBuilder = queryBuilder.where(where.value) }
if (sort.value) { queryBuilder = queryBuilder.sort(sort.value) }
if (limit.value) { queryBuilder = queryBuilder.limit(limit.value) }
if (skip.value) { queryBuilder = queryBuilder.skip(skip.value) }
if (locale.value) { queryBuilder = queryBuilder.where({ _locale: locale.value }) }
if (find.value === 'one') { return queryBuilder.findOne() as Promise<ParsedContent> }
if (find.value === 'surround') {
if (!path.value) {
// eslint-disable-next-line no-console
console.warn('[Content] Surround queries requires `path` prop to be set.')
// eslint-disable-next-line no-console
console.warn('[Content] Query without `path` will return regular `find()` results.')
return queryBuilder.find()
}
return queryBuilder.findSurround(path) as Promise<[ParsedContent | undefined, ParsedContent | undefined]>
}
return queryBuilder.find() as Promise<ParsedContent[]>
}
)
return {
isPartial,
data,
refresh
}
},
render (ctx) {
const slots = useSlots()
const {
// Setup
data,
refresh,
isPartial,
// Props
path,
only,
without,
where,
sort,
limit,
skip,
locale,
find
} = ctx
// We cannot use `...props` in a render function, Vue optimization might make it static.
const props = {
path,
only,
without,
where,
sort,
limit,
skip,
locale,
find
}
if (props.find === 'one') {
// Handle `findOne()` #empty/#not-found content
// If `type` is `one` and `data` is `undefined render the #not-found slot.
if (!data && slots?.['not-found']) { return slots['not-found']({ props, ...this.$attrs }) }
// Empty slots for `one` if type is "markdown" refers to an empty `body.children` key.
if (data._type && data._type === 'markdown' && !data?.body?.children.length) { return slots.empty({ props, ...this.$attrs }) }
} else if (!data || !data.length) {
// Handle `find()` and `findSurround()`
// These functions does not handle "empty" slots, as "empty" refers to missing content in `body` of the document.
if (slots?.['not-found']) { return slots['not-found']({ props, ...this.$attrs }) }
}
// Return default slot if present
if (slots?.default) { return slots.default({ data, refresh, isPartial, props, ...this.$attrs }) }
const emptyNode = (slot: string, data: any) => h('pre', null, JSON.stringify({ message: 'You should use slots with <ContentQuery>!', slot, data }, null, 2))
// Return empty node if not any slot not preset.
return emptyNode('default', { data, props, isPartial })
}
})