Skip to content

Commit

Permalink
Merge pull request #215 from NIAEFEUP/fix/proxy-cors
Browse files Browse the repository at this point in the history
Fix/proxy cors
  • Loading branch information
BrunoRosendo authored Aug 10, 2023
2 parents 70c9d81 + 4e5ffdc commit a176aab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/routes/api/[...endpoint]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { fetchWithAuth } from './proxy';
import { endpoint } from '$lib/api/proxy';

const dispatchToBackend: RequestHandler = async (event) => {
const body = event.request.method === 'GET' ? undefined : await event.request.text();
const body =
event.request.method === 'GET' ||
event.request.method === 'OPTIONS' ||
event.request.method === 'HEAD'
? undefined
: await event.request.text();
return fetchWithAuth(event.cookies, endpoint(event.url), event.request.method, undefined, body);
};

export const GET: RequestHandler = dispatchToBackend;
export const POST: RequestHandler = dispatchToBackend;
export const PUT: RequestHandler = dispatchToBackend;
export const DELETE: RequestHandler = dispatchToBackend;
export const PATCH: RequestHandler = dispatchToBackend;
export const OPTIONS: RequestHandler = dispatchToBackend;
export const HEAD: RequestHandler = dispatchToBackend;
4 changes: 4 additions & 0 deletions src/routes/api/[...endpoint]/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ async function _fetchApi(
const url = new URL(relativeUrl, PUBLIC_API_URL);
headers ??= new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
if (window?.location?.origin) {
headers.append('Origin', window.location.origin);
}
return fetch(url, { method: method, body, headers });
}

Expand Down

0 comments on commit a176aab

Please sign in to comment.