commentary

This commit is contained in:
waltem01 2023-11-30 09:51:30 +01:00
parent 3a105c79dd
commit c57bb60fcb
2 changed files with 121 additions and 17 deletions

View File

@ -1,34 +1,47 @@
<script lang="ts"> <script lang="ts">
import { detectGamepad, gamepadButtonPress } from '$lib/client/gamepad';
import { rgbToHex, type Color, getContrastColor } from '$lib/client/color'; import { rgbToHex, type Color, getContrastColor } from '$lib/client/color';
import { detectGamepad, gamepadButtonPress } from '$lib/client/gamepad';
import { initializeMatrix } from '$lib/client/matrix'; import { initializeMatrix } from '$lib/client/matrix';
import { toRadians } from '$lib/client/miscellaneous'; import { toRadians } from '$lib/client/miscellaneous';
import type { APIResponse } from '$lib/interfaces'; import type { APIResponse } from '$lib/interfaces';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
async function get(event: SubmitEvent) { // Basic GET Method for forms
async function get(event: SubmitEvent | string) {
clearMatrix(); clearMatrix();
const form = event.target as HTMLFormElement; // Get endpoint from form dataset or string input
const endpoint =
event instanceof SubmitEvent ? (event.target as HTMLFormElement).dataset.endpoint : event;
const response = await fetch(`/api/redirect?endpoint=${form.dataset.endpoint}`); // Send request to be redirected to given endpoint
const response = await fetch(`/api/redirect?endpoint=${endpoint}`);
// Await respose from webserver
const mdata = (await response.json()) as APIResponse; const mdata = (await response.json()) as APIResponse;
if (!mdata.success) alert(`Error while processing '${form.dataset.endpoint}'!`); // Basic error handling
if (!mdata.success) alert(`Error while processing '${endpoint}'!`);
} }
// Basic POST Method for forms
async function post(event: SubmitEvent) { async function post(event: SubmitEvent) {
// Get formdata from form element
const form = event.target as HTMLFormElement; const form = event.target as HTMLFormElement;
const fdata = new FormData(form); const fdata = new FormData(form);
// Display action on simulated matrix
switch (form.dataset.endpoint) { switch (form.dataset.endpoint) {
// Save color data from form
case 'color': case 'color':
colorSelect(fdata); colorSelect(fdata);
break; break;
// Draw pixel onto matrix
case 'pixel': case 'pixel':
setPixel(fdata); setPixel(fdata);
break; break;
// Draw rectangle onto matrix
case 'rectangle': case 'rectangle':
drawRectangle(fdata); drawRectangle(fdata);
break; break;
// Draw circle onto matrix
case 'circle': case 'circle':
drawCircle(fdata); drawCircle(fdata);
break; break;
@ -37,103 +50,144 @@
// break; // break;
} }
// Append endpoint to formdata for redirection
fdata.append('endpoint', form.dataset.endpoint ?? ''); fdata.append('endpoint', form.dataset.endpoint ?? '');
// Send request to be redirected to given endpoint
const response = await fetch('/api/redirect', { const response = await fetch('/api/redirect', {
method: 'POST', method: 'POST',
body: fdata body: fdata
}); });
// Await respose from webserver
const mdata = (await response.json()) as APIResponse; const mdata = (await response.json()) as APIResponse;
// Basic error handling
if (!mdata.success) alert(`Error while processing '${form.dataset.endpoint}'!`); if (!mdata.success) alert(`Error while processing '${form.dataset.endpoint}'!`);
} }
// function displayText(formData: FormData) {} // function displayText(formData: FormData) {}
// Draw circle onto matrix
function drawCircle(formData: FormData) { function drawCircle(formData: FormData) {
// Get relevant data from formdata
const x = parseInt((formData.get('x') as string) ?? '191'); const x = parseInt((formData.get('x') as string) ?? '191');
const y = parseInt((formData.get('y') as string) ?? '191'); const y = parseInt((formData.get('y') as string) ?? '191');
const r = parseInt((formData.get('r') as string) ?? '95'); const r = parseInt((formData.get('r') as string) ?? '95');
// Convert stored color data to hex
const hex = rgbToHex(pixelColor); const hex = rgbToHex(pixelColor);
// Loop through angles
for (let a = 0; a < 360; a++) { for (let a = 0; a < 360; a++) {
// Calculate coordinates of pixel
const nx = Math.round(Math.cos(toRadians(a)) * r + x); const nx = Math.round(Math.cos(toRadians(a)) * r + x);
const ny = Math.round(Math.sin(toRadians(a)) * r + y); const ny = Math.round(Math.sin(toRadians(a)) * r + y);
// If within bounds, draw pixel with color
if (nx >= 0 && nx < 192 && ny >= 0 && ny < 192) matrix[ny][nx].color = hex; if (nx >= 0 && nx < 192 && ny >= 0 && ny < 192) matrix[ny][nx].color = hex;
} }
} }
// Draw rectangle onto matrix
function drawRectangle(formData: FormData) { function drawRectangle(formData: FormData) {
// Get relevant data from formdata
const x = parseInt((formData.get('x') as string) ?? '191'); const x = parseInt((formData.get('x') as string) ?? '191');
const y = parseInt((formData.get('y') as string) ?? '191'); const y = parseInt((formData.get('y') as string) ?? '191');
const w = parseInt((formData.get('w') as string) ?? '191'); const w = parseInt((formData.get('w') as string) ?? '191');
const h = parseInt((formData.get('h') as string) ?? '191'); const h = parseInt((formData.get('h') as string) ?? '191');
// Convert stored color data to hex
const hex = rgbToHex(pixelColor); const hex = rgbToHex(pixelColor);
// Loop through every position in rectangle to set color
for (let j = y; j < y + h; j++) for (let i = x; i < x + w; i++) matrix[j][i].color = hex; for (let j = y; j < y + h; j++) for (let i = x; i < x + w; i++) matrix[j][i].color = hex;
} }
// Draw pixel onto matrix
function setPixel(formData: FormData) { function setPixel(formData: FormData) {
// Get relevant data from formdata
const x = parseInt((formData.get('x') as string) ?? '191'); const x = parseInt((formData.get('x') as string) ?? '191');
const y = parseInt((formData.get('y') as string) ?? '191'); const y = parseInt((formData.get('y') as string) ?? '191');
// Set singular coodinate to given color
matrix[y][x].color = rgbToHex(pixelColor); matrix[y][x].color = rgbToHex(pixelColor);
} }
// Clear simulated matrix
function clearMatrix() { function clearMatrix() {
matrix.forEach((y) => y.forEach((x) => (x.color = '000000'))); matrix.forEach((y) => y.forEach((x) => (x.color = '000000')));
} }
function colorSelect(formData: FormData) { function colorSelect(formData: FormData) {
// Get relevant data from formdata
pixelColor.r = parseInt((formData.get('r') as string) ?? '255'); pixelColor.r = parseInt((formData.get('r') as string) ?? '255');
pixelColor.g = parseInt((formData.get('g') as string) ?? '255'); pixelColor.g = parseInt((formData.get('g') as string) ?? '255');
pixelColor.b = parseInt((formData.get('b') as string) ?? '255'); pixelColor.b = parseInt((formData.get('b') as string) ?? '255');
} }
// Mouse event handler for setting pixels clicked on matrix
async function setMousePixel(event: MouseEvent) { async function setMousePixel(event: MouseEvent) {
// Exist if mouse not pressed
if (!mousePressed) return; if (!mousePressed) return;
// Get table cell element
const parent = event.target as HTMLTableCellElement; const parent = event.target as HTMLTableCellElement;
// Extract x and y values from datasets
let x = parseInt(parent.dataset.x ?? '0'); let x = parseInt(parent.dataset.x ?? '0');
let y = parseInt(parent.parentElement?.dataset.y ?? '0'); let y = parseInt(parent.parentElement?.dataset.y ?? '0');
// Convert stored color data to hex
const next = rgbToHex(pixelColor); const next = rgbToHex(pixelColor);
// Ignore if color stays the same to reduce API calls
if (matrix[y][x].color === next) return; if (matrix[y][x].color === next) return;
// Set pixel to color
matrix[y][x].color = next; matrix[y][x].color = next;
// Create new formdata
const fdata = new FormData(); const fdata = new FormData();
const isScaled = scaling > 1; const isScaled = scaling > 1;
// Scale data accordingly
if (isScaled) { if (isScaled) {
x *= scaling; x *= scaling;
y *= scaling; y *= scaling;
// width and height of rectangle are the scale factor
fdata.append('w', scaling.toString()); fdata.append('w', scaling.toString());
fdata.append('h', scaling.toString()); fdata.append('h', scaling.toString());
} }
// append x and y coordinates
fdata.append('x', x.toString()); fdata.append('x', x.toString());
fdata.append('y', y.toString()); fdata.append('y', y.toString());
// append endpoint depending on scale factor
fdata.append('endpoint', isScaled ? 'rectangle' : 'pixel'); fdata.append('endpoint', isScaled ? 'rectangle' : 'pixel');
// Send request to be redirected to given endpoint
const response = await fetch('/api/redirect', { const response = await fetch('/api/redirect', {
method: 'POST', method: 'POST',
body: fdata body: fdata
}); });
// Await respose from webserver
const mdata = (await response.json()) as APIResponse; const mdata = (await response.json()) as APIResponse;
// Basic error handling
if (!mdata.success) alert("Error while processing 'pixel'!"); if (!mdata.success) alert("Error while processing 'pixel'!");
} }
// Correction function to map scale factor to valid values
function correctSlider(event: Event) { function correctSlider(event: Event) {
// Get input element
const slider = event.target as HTMLInputElement; const slider = event.target as HTMLInputElement;
// Read value, round down to next best divisor
let current = parseInt(slider.value); let current = parseInt(slider.value);
while (192 % current !== 0) current--; while (192 % current !== 0) current--;
// Set scaling to new value
scaling = current; scaling = current;
// Scale elements by padding inside accordingly
padding = 0.1875 * scaling; padding = 0.1875 * scaling;
// Reinitialize matrix
matrix = initializeMatrix(scaling); matrix = initializeMatrix(scaling);
} }
// System variables
let scaling = 3, let scaling = 3,
padding = 0.5625; padding = 0.5625;
let mousePressed = false; let mousePressed = false;
@ -143,26 +197,40 @@
const gameCoords = { x: 0, y: 0 }; const gameCoords = { x: 0, y: 0 };
const pixelColor = { r: 255, g: 255, b: 255 } as Color; const pixelColor = { r: 255, g: 255, b: 255 } as Color;
// On client loaded
onMount(() => { onMount(() => {
// API call to clear matrix
get('clear');
// Regularly try detecting gamepad
setInterval(() => { setInterval(() => {
// Pass if is already selected
if (gamepad) return; if (gamepad) return;
gamepad = detectGamepad(); gamepad = detectGamepad();
}, 1000); }, 1000);
// Regularly try detecting button presses
setInterval(() => gamepadButtonPress(gamepad, gameCoords), 100); setInterval(() => gamepadButtonPress(gamepad, gameCoords), 100);
// Listen for gamepad conntect events
window.addEventListener('gamepadconnected', (e) => { window.addEventListener('gamepadconnected', (e) => {
// Set new gamepad
if (e.gamepad.index === 0) gamepad = e.gamepad; if (e.gamepad.index === 0) gamepad = e.gamepad;
}); });
// Listen for gamepad disconnect events
window.addEventListener('gamepaddisconnected', (e) => { window.addEventListener('gamepaddisconnected', (e) => {
// Remove only if it's the same gameapd
if (e.gamepad.id === gamepad?.id) gamepad = null; if (e.gamepad.id === gamepad?.id) gamepad = null;
}); });
}); });
</script> </script>
<!-- Title -->
<h1 class="text-4xl font-bold m-5 text-center">Matrix Control Panel</h1> <h1 class="text-4xl font-bold m-5 text-center">Matrix Control Panel</h1>
<!-- Forms -->
<div class="grid grid-rows-7 p-1"> <div class="grid grid-rows-7 p-1">
<!-- Update -->
<form class="grid grid-cols-2" method="GET" data-endpoint="update" on:submit|preventDefault={get}> <form class="grid grid-cols-2" method="GET" data-endpoint="update" on:submit|preventDefault={get}>
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="update">Update:</label> <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="update">Update:</label>
<input <input
@ -174,6 +242,7 @@
/> />
</form> </form>
<!-- Clear -->
<form class="grid grid-cols-2" method="GET" data-endpoint="clear" on:submit|preventDefault={get}> <form class="grid grid-cols-2" method="GET" data-endpoint="clear" on:submit|preventDefault={get}>
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="clear">Clear:</label> <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="clear">Clear:</label>
<input <input
@ -185,6 +254,7 @@
/> />
</form> </form>
<!-- Set color -->
<form <form
class="grid grid-cols-8" class="grid grid-cols-8"
method="POST" method="POST"
@ -192,6 +262,7 @@
on:submit|preventDefault={post} on:submit|preventDefault={post}
> >
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="color">Set Color:</label> <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="color">Set Color:</label>
<!-- RED color value -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="red">Red:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="red">Red:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -204,6 +275,7 @@
value="255" value="255"
required required
/> />
<!-- GREEN color value -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="green">Green:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="green">Green:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -216,6 +288,7 @@
value="255" value="255"
required required
/> />
<!-- BLUE color value -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="blue">Blue:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="blue">Blue:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -237,14 +310,16 @@
/> />
</form> </form>
<!-- Set pixel -->
<form <form
class="grid grid-cols-6" class="grid grid-cols-6"
method="POST" method="POST"
data-endpoint="pixel" data-endpoint="pixel"
on:submit|preventDefault={post} on:submit|preventDefault={post}
> >
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="pixel">Set Pixel:</label <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="pixel">Set Pixel:</label>
><label class="border-2 border-black border-b-0 border-r-0 pl-1" for="px">X:</label> <!-- X coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="px">X:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
id="px" id="px"
@ -256,6 +331,7 @@
value="0" value="0"
required required
/> />
<!-- Y coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="py">Y:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="py">Y:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -277,6 +353,7 @@
/> />
</form> </form>
<!-- Draw rectangle -->
<form <form
class="grid grid-cols-10" class="grid grid-cols-10"
method="POST" method="POST"
@ -285,7 +362,9 @@
> >
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="rectangle" <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="rectangle"
>Rectangle:</label >Rectangle:</label
><label class="border-2 border-black border-b-0 border-r-0 pl-1" for="rx">X:</label> >
<!-- X coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="rx">X:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
id="rx" id="rx"
@ -297,6 +376,7 @@
value="177" value="177"
required required
/> />
<!-- Y coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="ry">Y:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="ry">Y:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -308,7 +388,9 @@
step="1" step="1"
value="0" value="0"
required required
/><label class="border-2 border-black border-b-0 border-r-0 pl-1" for="w">W:</label> />
<!-- Width -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="w">W:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
id="w" id="w"
@ -320,6 +402,7 @@
value="15" value="15"
required required
/> />
<!-- Height -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="h">H:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="h">H:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -341,14 +424,16 @@
/> />
</form> </form>
<!-- Draw circle -->
<form <form
class="grid grid-cols-8" class="grid grid-cols-8"
method="POST" method="POST"
data-endpoint="circle" data-endpoint="circle"
on:submit|preventDefault={post} on:submit|preventDefault={post}
> >
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="circle">Circle:</label <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="circle">Circle:</label>
><label class="border-2 border-black border-b-0 border-r-0 pl-1" for="cx">X:</label> <!-- X coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="cx">X:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
id="cx" id="cx"
@ -360,6 +445,7 @@
value="20" value="20"
required required
/> />
<!-- Y coordinate -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="cy">Y:</label> <label class="border-2 border-black border-b-0 border-r-0 pl-1" for="cy">Y:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
@ -371,7 +457,9 @@
step="1" step="1"
value="171" value="171"
required required
/><label class="border-2 border-black border-b-0 border-r-0 pl-1" for="radius">R:</label> />
<!-- Radius -->
<label class="border-2 border-black border-b-0 border-r-0 pl-1" for="radius">R:</label>
<input <input
class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-b-0 border-r-0 cursor-text pl-1 bg-gray-100"
id="radius" id="radius"
@ -393,10 +481,9 @@
</form> </form>
<form class="grid grid-cols-8" method="POST" data-endpoint="text" on:submit|preventDefault={post}> <form class="grid grid-cols-8" method="POST" data-endpoint="text" on:submit|preventDefault={post}>
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="text">Text:</label><label <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="text">Text:</label>
class="border-2 border-black border-r-0 border-b-0 pl-1" <!-- X coordinate -->
for="tx">X:</label <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="tx">X:</label>
>
<input <input
class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 bg-gray-100"
id="tx" id="tx"
@ -408,6 +495,7 @@
value="8" value="8"
required required
/> />
<!-- Y coordinate -->
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="ty">Y:</label> <label class="border-2 border-black border-r-0 border-b-0 pl-1" for="ty">Y:</label>
<input <input
class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 bg-gray-100" class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 bg-gray-100"
@ -419,7 +507,9 @@
step="1" step="1"
value="9" value="9"
required required
/><label class="border-2 border-black border-r-0 border-b-0 pl-1" for="tin">Input:</label> />
<!-- Text input -->
<label class="border-2 border-black border-r-0 border-b-0 pl-1" for="tin">Input:</label>
<input <input
class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 pr-1 bg-gray-100" class="border-2 border-black border-r-0 border-b-0 cursor-text pl-1 pr-1 bg-gray-100"
id="tin" id="tin"
@ -439,6 +529,7 @@
<form class="grid grid-cols-3" on:submit|preventDefault> <form class="grid grid-cols-3" on:submit|preventDefault>
<label class="border-2 border-black border-r-0 pl-1" for="text">Scaling:</label> <label class="border-2 border-black border-r-0 pl-1" for="text">Scaling:</label>
<!-- Slider -->
<div class="flex pl-1 pr-1 border-2 border-black border-r-0 bg-gray-100"> <div class="flex pl-1 pr-1 border-2 border-black border-r-0 bg-gray-100">
<input <input
class="w-full cursor-pointer" class="w-full cursor-pointer"
@ -453,6 +544,7 @@
required required
/> />
</div> </div>
<!-- Number -->
<input <input
class="border-2 border-black cursor-text pl-1 bg-gray-100" class="border-2 border-black cursor-text pl-1 bg-gray-100"
type="number" type="number"
@ -465,7 +557,9 @@
/> />
</form> </form>
</div> </div>
<!-- Matricies -->
<div class="flex justify-center"> <div class="flex justify-center">
<!-- Color display -->
<table id="main" class="mt-5 border-2 border-black"> <table id="main" class="mt-5 border-2 border-black">
{#each matrix as row} {#each matrix as row}
<tr> <tr>
@ -475,6 +569,7 @@
</tr> </tr>
{/each} {/each}
</table> </table>
<!-- Overlay => Events + Effects -->
<div class="absolute z-10"> <div class="absolute z-10">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions --> <!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<table <table

View File

@ -1,22 +1,31 @@
import type { APIResponse } from "$lib/interfaces"; import type { APIResponse } from "$lib/interfaces";
import { json, type RequestHandler } from "@sveltejs/kit"; import { json, type RequestHandler } from "@sveltejs/kit";
// GET Endpoint for redirection API
export const GET: RequestHandler = async ({ url }) => { export const GET: RequestHandler = async ({ url }) => {
// Get endpoint on main API from searchparams
const params = url.searchParams; const params = url.searchParams;
const endpoint = params.get('endpoint'); const endpoint = params.get('endpoint');
// Return if param not found
if (endpoint === null) return json({ success: false } as APIResponse); if (endpoint === null) return json({ success: false } as APIResponse);
// Call API and respond with new data
const api_call = await fetch(`http://localhost:8080/${endpoint}`); const api_call = await fetch(`http://localhost:8080/${endpoint}`);
const data = await api_call.json() as APIResponse; const data = await api_call.json() as APIResponse;
return json(data); return json(data);
}; };
// POST Endpoint for redirection API
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
// Get data from form
const formData = await request.formData(); const formData = await request.formData();
const endpoint = formData.get('endpoint'); const endpoint = formData.get('endpoint');
// Return if endpoint not found
if (endpoint === null) return json({ success: false } as APIResponse); if (endpoint === null) return json({ success: false } as APIResponse);
// Remove endpoint to save on datatransfer
formData.delete('endpoint'); formData.delete('endpoint');
// Call API and respond with new data
const api_call = await fetch(`http://localhost:8080/${endpoint}`, { const api_call = await fetch(`http://localhost:8080/${endpoint}`, {
method: 'POST', method: 'POST',
mode: 'cors', mode: 'cors',