Skip to content

Commit

Permalink
check redirects first thing
Browse files Browse the repository at this point in the history
  • Loading branch information
gpmayorga committed Jun 26, 2024
1 parent 5b02d43 commit 793f869
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ const corsWhitelist = [
const testEnvRegex = new RegExp('https://preview-pr[0-9]*(.k-f.dev)')

exports.handler = async (req: Request, res: Response) => {
if (routes.length < 0) {
return res.status(400).send('No functions defined')
// Check for redirects first
const redirect = routes.find(route => route.source && req.path.endsWith(route.source));
if (redirect) {
return res.redirect(redirect.statusCode, redirect.destination);
}

const origin = req.get('origin')

// Existing CORS and route handling
const origin = req.get('origin');
if (corsWhitelist.indexOf(origin) !== -1 || testEnvRegex.test(origin)) {
res.set('Access-Control-Allow-Origin', origin)
res.set('Access-Control-Allow-Methods', ['GET', 'POST'])
res.set('Access-Control-Allow-Origin', origin);
res.set('Access-Control-Allow-Methods', ['GET', 'POST']);
} else {
return res.status(405).send('Not allowed')
return res.status(405).send('Not allowed');
}

for (let route of routes) {
if (req.path.replace('/', '') === route.name) {
const method = require(`./src/${route.name}`)

return method.default(req, res, route?.options ?? {})
if (route.name && req.path.replace('/', '') === route.name) {
const method = require(`./src/${route.name}`);
return method.default(req, res, route?.options ?? {});
}
}
return res.status(500).send('An error occured')

return res.status(500).send('An error occurred');
}

0 comments on commit 793f869

Please sign in to comment.