Skip to content

Commit

Permalink
add withCORS helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Oct 22, 2023
1 parent bcbf922 commit 844c487
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export function makeAPIRouter(
)
.get(
new URLPattern({ pathname: "/seasons" }),
makeSeasonsGetHandler(leaderboardClient),
withCORS(makeSeasonsGetHandler(leaderboardClient)),
)
.get(
new URLPattern({ pathname: "/seasons/:season_id" }),
makeSeasonGetHandler(leaderboardClient),
withCORS(makeSeasonGetHandler(leaderboardClient)),
);
}

Expand Down Expand Up @@ -115,3 +115,21 @@ export function makeOnListen(
function makeInviteURL(applicationID: string) {
return `https://discord.com/api/oauth2/authorize?client_id=${applicationID}&scope=applications.commands`;
}

/**
* withCORS wraps a handler with common CORS headers.
*/
function withCORS(
handle: router.RouterHandler["handle"],
): router.RouterHandler["handle"] {
return async function (request: router.RouterRequest) {
const response = await handle(request);
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET, POST");
response.headers.set(
"Access-Control-Allow-Headers",
"Content-Type, Authorization",
);
return response;
};
}

0 comments on commit 844c487

Please sign in to comment.