redirect endpoint for localhost python API

This commit is contained in:
waltem01 2023-11-30 09:10:58 +01:00
parent bde03f5da4
commit f2f702099f
2 changed files with 37 additions and 11 deletions

View File

@ -10,10 +10,7 @@
clearMatrix();
const form = event.target as HTMLFormElement;
// TODO: Calling API via server-side
const response = await fetch(`http://localhost:8080/${form.dataset.endpoint}`, {
mode: 'cors'
});
const response = await fetch(`/api/redirect?endpoint=${form.dataset.endpoint}`);
const mdata = (await response.json()) as APIResponse;
if (!mdata.success) alert(`Error while processing '${form.dataset.endpoint}'!`);
}
@ -40,10 +37,9 @@
// break;
}
// TODO: Calling API via server-side
const response = await fetch(`http://localhost:8080/${form.dataset.endpoint}`, {
method: form.method,
mode: 'cors',
fdata.append('endpoint', form.dataset.endpoint ?? '');
const response = await fetch('/api/redirect', {
method: 'POST',
body: fdata
});
const mdata = (await response.json()) as APIResponse;
@ -116,11 +112,10 @@
fdata.append('x', x.toString());
fdata.append('y', y.toString());
fdata.append('endpoint', isScaled ? 'rectangle' : 'pixel');
// TODO: Calling API via server-side
const response = await fetch(`http://localhost:8080/${isScaled ? 'rectangle' : 'pixel'}`, {
const response = await fetch('/api/redirect', {
method: 'POST',
mode: 'cors',
body: fdata
});
const mdata = (await response.json()) as APIResponse;

View File

@ -0,0 +1,31 @@
import type { APIResponse } from "$lib/interfaces";
import { json, type RequestHandler } from "@sveltejs/kit";
export const GET: RequestHandler = async ({ url }) => {
const response = { success: false } as APIResponse;
const params = url.searchParams;
const endpoint = params.get('endpoint');
if (endpoint === null) return json(response);
const api_call = await fetch(`http://localhost:8080/${endpoint}`);
const data = await api_call.json() as APIResponse;
return json(data);
};
export const POST: RequestHandler = async ({ request }) => {
const response = { success: false } as APIResponse;
const formData = await request.formData();
const endpoint = formData.get('endpoint');
if (endpoint === null) return json(response);
formData.delete('endpoint');
const api_call = await fetch(`http://localhost:8080/${endpoint}`, {
method: 'POST',
mode: 'cors',
body: formData
});
const data = await api_call.json() as APIResponse;
return json(data);
};