/
query.ts
50 lines (44 loc) · 1.63 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
46
47
48
49
50
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { hash } from 'ohash'
import { useHead, useCookie } from '#app'
import { createQuery } from '../query/query'
import type { ParsedContent, QueryBuilder, QueryBuilderParams } from '../types'
import { jsonStringify } from '../utils/json'
import { withContentBase } from './utils'
/**
* Query fetcher
*/
export const queryFetch = <T = ParsedContent>(params: Partial<QueryBuilderParams>) => {
const apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`)
// Prefetch the query
if (!process.dev && process.server) {
useHead({
link: [
{ rel: 'prefetch', href: apiPath }
]
})
}
return $fetch<T | T[]>(apiPath as any, {
method: 'GET',
responseType: 'json',
params: {
_params: jsonStringify(params),
previewToken: useCookie('previewToken').value
}
})
}
/**
* Query contents from path
*/
export function queryContent<T = ParsedContent>(): QueryBuilder<T>;
export function queryContent<T = ParsedContent>(query: string, ...pathParts: string[]): QueryBuilder<T>;
export function queryContent<T = ParsedContent> (query: QueryBuilderParams): QueryBuilder<T>;
export function queryContent<T = ParsedContent> (query?: string | QueryBuilderParams, ...pathParts: string[]) {
if (typeof query === 'string') {
let path = withLeadingSlash(withoutTrailingSlash(joinURL(query, ...pathParts)))
// escape regex special chars
path = path.replace(/[-[\]{}()*+.,^$\s]/g, '\\$&')
return createQuery<T>(queryFetch).where({ _path: new RegExp(`^${path}`) })
}
return createQuery<T>(queryFetch, query)
}