universal image fetcher
This commit is contained in:
@@ -4,6 +4,12 @@ import { json } from '@sveltejs/kit';
|
||||
|
||||
/** @type {import('./$types').RequestHandler} */
|
||||
export async function GET({ url, params, request, fetch, cookies }) {
|
||||
// add the param format = json to the url or add additional if anothre param is already present
|
||||
if (url.search) {
|
||||
url.search = url.search + '&format=json';
|
||||
} else {
|
||||
url.search = '?format=json';
|
||||
}
|
||||
return handleRequest(url, params, request, fetch, cookies);
|
||||
}
|
||||
|
||||
@@ -13,7 +19,7 @@ export async function POST({ url, params, request, fetch, cookies }) {
|
||||
}
|
||||
|
||||
export async function PATCH({ url, params, request, fetch, cookies }) {
|
||||
return handleRequest(url, params, request, fetch, cookies);
|
||||
return handleRequest(url, params, request, fetch, cookies, true);
|
||||
}
|
||||
|
||||
export async function PUT({ url, params, request, fetch, cookies }) {
|
||||
@@ -26,9 +32,20 @@ export async function DELETE({ url, params, request, fetch, cookies }) {
|
||||
|
||||
// Implement other HTTP methods as needed (PUT, DELETE, etc.)
|
||||
|
||||
async function handleRequest(url: any, params: any, request: any, fetch: any, cookies: any) {
|
||||
async function handleRequest(
|
||||
url: any,
|
||||
params: any,
|
||||
request: any,
|
||||
fetch: any,
|
||||
cookies: any,
|
||||
requreTrailingSlash: boolean | undefined = false
|
||||
) {
|
||||
const path = params.path;
|
||||
const targetUrl = `${endpoint}/api/${path}${url.search}/`;
|
||||
let targetUrl = `${endpoint}/api/${path}${url.search}`;
|
||||
|
||||
if (requreTrailingSlash && !targetUrl.endsWith('/')) {
|
||||
targetUrl += '/';
|
||||
}
|
||||
|
||||
const headers = new Headers(request.headers);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user