-
Notifications
You must be signed in to change notification settings - Fork 15
/
query_converter.html
89 lines (84 loc) · 2.92 KB
/
query_converter.html
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
<!DOCTYPE html>
<html lang="en" class="bg-color">
<head>
<title>Query Converter</title>
<meta charset="utf-8" />
<!-- <link rel="icon" href="/favicon.png" /> -->
<meta name="viewport" content="width=device-width" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.11.2/qs.min.js" integrity="sha512-vCegEXqPUYpZsTGz2lk0jaQ1psxtFeniVJACAXhMVxuoYa/N4nZkjoVFOxLwP7uGeQOoemiz7DQrIpRTj4IBPw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.tailwindcss.com"></script>
<script>
function convertToString() {
const queryObjectField = document.querySelector('#queryObject')
const queryStringField = document.querySelector('#queryString')
if (queryObjectField.value === '') {
window.alert(`No value provided!`)
return
}
let json
try {
json = JSON.parse(queryObjectField.value)
} catch (e) {
window.alert(`Parse Error:\n` + e.message)
return
}
const stringValue = Qs.stringify(json, { encode: false })
queryStringField.value = stringValue
}
function convertToObject() {
const queryObjectField = document.querySelector('#queryObject')
const queryStringField = document.querySelector('#queryString')
const objectValue = Qs.parse(queryStringField.value)
let json
try {
json = JSON.stringify(objectValue, null, 2)
} catch (e) {
window.alert(`Parse Error:\n` + e.message)
return
}
queryObjectField.value = json
}
</script>
<style type="text/tailwindcss">
.textarea {
@apply font-mono w-[35vw] h-[20rem] p-4 border-sky-900 bg-sky-950 rounded;
}
.button {
@apply bg-indigo-800 py-2 px-4 w-full rounded border-0 flex-shrink ml-auto mb-4 whitespace-pre;
}
.bg-color {
@apply bg-gray-900 text-white;
}
.section {
@apply flex flex-col justify-end;
}
</style>
</head>
<body>
<header class="p-12">
<h1 class="text-2xl">Directus Query Object & Query String Converter</h1>
<p class="text-lg">Convert between formats for a Directus Query</p>
</header>
<main class="flex space-x-8 px-12">
<section class="section">
<textarea id="queryObject" class="textarea">{
"fields": "title,slug",
"filter": {
"status": {
"_eq": "published"
}
},
"limit": 3,
"sort": "-date_created"
}</textarea>
</section>
<section>
<button onClick="convertToString()" class="button">> Convert to String ></button>
<button onClick="convertToObject()" class="button">< Convert to Object <</button>
</section>
<section class="section">
<textarea id="queryString" class="textarea">fields=title,slug&filter[status][_eq]=published&limit=3&sort=-date_created</textarea>
</section>
</main>
</body>
</html>