bazzar/api/assets/index.html

166 lines
5.3 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bazzar</title>
2022-04-16 21:09:50 +02:00
<style>
@import url(https://fonts.googleapis.com/css?family=Questrial);
@import url(https://fonts.googleapis.com/css?family=Arvo);
@font-face {
src: url(https://lea.verou.me/logo.otf);
font-family: 'LeaVerou';
}
fieldset {
display: flex;
justify-content: space-between;
width: 600px;
}
2022-04-17 22:15:09 +02:00
2022-04-16 21:09:50 +02:00
fieldset > label {
width: 45%;
}
2022-04-17 22:15:09 +02:00
2022-04-16 21:09:50 +02:00
fieldset > input, fieldset > textarea {
width: 54%;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/components/prism-json.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/themes/prism-dark.min.css">
</head>
<body>
2022-04-16 21:09:50 +02:00
<div style="display: flex;justify-content: space-between;">
<div style="width: 49%">
2022-04-17 22:15:09 +02:00
<form style="width: 100%">
2022-04-18 22:07:52 +02:00
<fieldset>
<label for="bearer">bearer</label>
<input name="bearer" id="bearer" />
</fieldset>
2022-04-17 22:15:09 +02:00
<fieldset>
<label for="op">Operation</label>
<select name="op" id="op">
<option value="auto-login">Auto login</option>
<option value="get-products">Get products</option>
<option value="create-product">Create product</option>
</select>
</fieldset>
2022-04-16 21:09:50 +02:00
<fieldset>
<label for="method">Method</label>
<select id="method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PATCH">PATCH</option>
<option value="DELETE">DELETE</option>
</select>
</fieldset>
<fieldset>
<label for="path">Path</label><input id="path" type="text">
</fieldset>
<fieldset>
<label for="params">Params</label><textarea id="params"></textarea>
</fieldset>
<input type="submit">
</form>
</div>
2022-04-16 21:09:50 +02:00
<div style="width: 49%">
2022-04-17 22:15:09 +02:00
<pre style="background: black; width: 100%; min-height: 300px"><code
id="output"
class="language-json"
style="overflow: auto"
></code></pre>
</div>
</div>
<script>
2022-04-16 21:09:50 +02:00
Prism.manual = true;
const out = document.querySelector('#output');
const form = document.querySelector("form");
const urlEl = form.querySelector('#path');
const paramsEl = form.querySelector('#params');
const mthEl = form.querySelector('#method');
2022-04-17 22:15:09 +02:00
const opEL = form.querySelector('#op');
2022-04-18 22:07:52 +02:00
const bearerEl = form.querySelector('#bearer');
2022-04-17 22:15:09 +02:00
const send = (method, path, params) => {
2022-04-18 22:07:52 +02:00
const bearer = bearerEl.value || '';
2022-04-17 22:15:09 +02:00
const rest = method === 'GET'
? {}
: { body: JSON.stringify(params), headers: { 'Content-Type': 'application/json' } };
2022-04-18 22:07:52 +02:00
if (bearer.length) {
if (!rest.headers) rest.headers = {};
rest.headers["Authorization"] = `Bearer ${bearer}`;
}
2022-04-17 22:15:09 +02:00
path = method === 'GET'
? `${ path }?${ JSON.stringify(params) }`
: path;
fetch(`${ path }`, { ...rest, method })
.then(async (res) => {
if (res.status < 400) {
const json = await res.json();
out.innerHTML = Prism.highlight(JSON.stringify(json, null, 4), Prism.languages.json, 'json');
} else {
out.innerHTML = await res.text();
}
})
}
opEL.addEventListener('change', () => {
switch (opEL.value) {
2022-04-17 23:11:58 +02:00
case 'auto-login': {
2022-04-17 22:15:09 +02:00
paramsEl.value = `login=Eraden\npassword=text`
2022-04-17 23:11:58 +02:00
mthEl.value = 'POST';
urlEl.value = '/admin/sign-in';
break;
}
case 'get-products': {
mthEl.value = 'GET';
urlEl.value = '/admin/api/v1/products';
break;
}
case 'create-product': {
2022-04-17 22:15:09 +02:00
const p = {
name: 'Foo',
short_description: 'asd',
long_description: 'asjdoiajd ajio djaso idja s',
price_major: 12,
price_minor: 0,
};
2022-04-17 23:11:58 +02:00
paramsEl.value = Object.entries(p).map(([k, v]) => `${ k }=${ v }`).join('\n');
mthEl.value = 'POST';
urlEl.value = '/admin/api/v1/product';
break;
}
2022-04-17 22:15:09 +02:00
}
})
form.addEventListener('submit', (ev) => {
ev.preventDefault();
ev.stopPropagation();
let path = urlEl.value;
let params = {};
const method = mthEl.value;
2022-04-16 20:20:59 +02:00
paramsEl.value.split("\n").forEach(s => {
if (!s.length) return;
2022-04-16 21:09:50 +02:00
let [k, ...v] = s.split("=");
2022-04-17 22:15:09 +02:00
v = Array(v).join('=')
try {
v = JSON.parse(v);
} catch (_) {
}
params[k] = v;
});
2022-04-17 22:15:09 +02:00
send(method, path, params)
});
</script>
</body>
</html>