-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
query.ts
106 lines (87 loc) · 3.22 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
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
import { getQuery, H3Event, createError } from 'h3'
import { QueryBuilderParams, QueryBuilderWhere } from '../types'
import { jsonParse, jsonStringify } from './json'
const parseJSONQueryParams = (body: string) => {
try {
return jsonParse(body)
} catch (e) {
throw createError({ statusCode: 400, message: 'Invalid _params query' })
}
}
export const encodeQueryParams = (params: QueryBuilderParams) => {
let encoded = jsonStringify(params)
encoded = typeof Buffer !== 'undefined' ? Buffer.from(encoded).toString('base64') : btoa(encoded)
encoded = encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
// split to chunks of 100 chars
const chunks = encoded.match(/.{1,100}/g) || []
return chunks.join('/')
}
export const decodeQuryParams = (encoded: string) => {
// remove chunks
encoded = encoded.replace(/\//g, '')
// revert base64
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/')
encoded = encoded.padEnd(encoded.length + (4 - (encoded.length % 4)) % 4, '=')
return parseJSONQueryParams(typeof Buffer !== 'undefined' ? Buffer.from(encoded, 'base64').toString() : atob(encoded))
}
const memory: Record<string, QueryBuilderParams> = {}
export const getContentQuery = (event: H3Event): QueryBuilderParams => {
const { params } = event.context.params || {}
if (params) {
return decodeQuryParams(params.replace(/.json$/, ''))
}
const qid = event.context.params.qid?.replace(/.json$/, '')
const query: any = getQuery(event) || {}
// Using /api/_content/query/:qid?_params=....
if (qid && query._params) {
memory[qid] = parseJSONQueryParams(query._params)
if (memory[qid].where && !Array.isArray(memory[qid].where)) {
memory[qid].where = [memory[qid].where as any as QueryBuilderWhere]
}
return memory[qid]
}
if (memory[qid]) {
return memory[qid]
}
// Using /api/_content/query?_params={{JSON_FORMAT}}
if (query._params) {
return parseJSONQueryParams(query._params)
}
// Using /api/_content/query?path=...&only=...
// Support both ?only=path,title and ?only=path&only=title
if (typeof query.only === 'string' && query.only.includes(',')) {
query.only = (query.only as string).split(',').map(s => s.trim())
}
if (typeof query.without === 'string' && query.without.includes(',')) {
query.without = (query.without as string).split(',').map(s => s.trim())
}
const where = query.where || {}
// ?partial=true|false&draft=true|false&empty=true|false
for (const key of ['draft', 'partial', 'empty']) {
// ?partial=true|false
if (query[key] && ['true', 'false'].includes(query[key])) {
where[key] = query[key] === 'true'
delete query[key]
}
}
// ?sortyBy=size:1
if (query.sort) {
query.sort = String(query.sort).split(',').map((s) => {
const [key, order] = s.split(':')
return [key, +order]
})
}
// ?[query]=...
const reservedKeys = ['partial', 'draft', 'only', 'without', 'where', 'sort', 'limit', 'skip']
for (const key of Object.keys(query)) {
if (reservedKeys.includes(key)) { continue }
query.where = query.where || {}
query.where[key] = query[key]
}
if (Object.keys(where).length > 0) {
query.where = [where]
} else {
delete query.where
}
return query
}