@mochabug/adaptkit
Version:
A cmd to create, emulate and publish Mochabug Adapt plugins
131 lines (128 loc) • 3.86 kB
text/typescript
import {
ConnectError,
mapConnectErrorToHttpStatus
} from '@mochabug/adapt-plugin-toolkit/api';
import {
ExternalConfiguratorRouter,
InternalConfiguratorRouter
} from '@mochabug/adapt-plugin-toolkit/router';
export default {
external: new ExternalConfiguratorRouter()
.useRequestLogging()
.useBearerAuthorization(['/api'])
.useErrorHandling(async (e) => {
console.error(e);
if (e instanceof ConnectError) {
return new Response(null, { status: mapConnectErrorToHttpStatus(e) });
}
return new Response(null, {
status: 500,
statusText: 'Internal server error'
});
})
.add('GET', '/api/config', async (_req, api) => {
const { metadata } = await api.getConfig('metadata');
return new Response(JSON.stringify(metadata), {
headers: {
'Content-Type': 'application/json'
}
});
})
.add('GET', '{*any}', async () => {
return new Response(helloWorldPage, {
headers: {
'Content-Type': 'text/html'
}
});
}),
internal: new InternalConfiguratorRouter()
};
const helloWorldPage = `
<html>
<head>
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron&display=swap');
@keyframes glow {
0% { text-shadow: 0 0 5px #0f0, 0 0 10px #0f0, 0 0 15px #0f0, 0 0 20px #0f0; }
100% { text-shadow: 0 0 10px #0f0, 0 0 20px #0f0, 0 0 30px #0f0, 0 0 40px #0f0; }
}
body {
background-color: #000;
color: #0f0;
font-family: 'Orbitron', sans-serif;
text-align: center;
overflow: hidden;
margin: 0;
padding: 0;
}
h1 {
position: absolute;
top: 30%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 3em;
text-transform: uppercase;
letter-spacing: 4px;
animation: glow 2s infinite alternate;
}
#jsonOutput {
position: absolute;
top: 70%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.5em;
text-transform: uppercase;
letter-spacing: 2px;
animation: glow 2s infinite alternate;
}
button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #0f0;
border: none;
color: black;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px; /* Increase the font size */
margin: 4px 2px;
cursor: pointer;
transition-duration: 0.4s;
animation: glow 2s infinite alternate;
font-family: 'Orbitron', sans-serif;
border-radius: 15px;
transform: rotate(-10deg) skew(10deg, 10deg);
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<button id="assimilateButton">Assimilate</button>
<pre id="jsonOutput"></pre>
<script>
document.getElementById('assimilateButton').addEventListener('click', function() {
const hash = window.location.hash.substring(1).trim();
const token = decodeURIComponent(hash);
fetch('/api/config', {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + token
}
})
.then(response => response.json())
.then(data => {
document.getElementById('jsonOutput').innerText = JSON.stringify(data, null, 2);
this.style.display = 'none'; /* Hide button after being clicked */
})
.catch(error => {
console.error('Error:', error);
document.getElementById('jsonOutput').innerText = 'Error occurred while assimilating data';
});
});
</script>
</body>
</html>
`;