Skip to content

Commit

Permalink
allow all CORS origin
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Feb 4, 2025
1 parent 2b39b09 commit 4836acc
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions pages/api/climbing-tiles/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { getClimbingTile } from '../../../src/server/climbing-tiles/getClimbingTile';
import { Tile } from '../../../src/types';

const CORS_ORIGINS = [
'http://localhost:3000',
'http://127.0.0.1:3000',
'https://osmapp.org',
];

const addCorsHeaders = (req: NextApiRequest, res: NextApiResponse) => {
const origin = req.headers.origin;
if (CORS_ORIGINS.includes(origin)) {
if (origin) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
}
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
};

export default async (req: NextApiRequest, res: NextApiResponse) => {
Expand All @@ -33,7 +26,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {

const geojson = await getClimbingTile(tileNumber);

res.status(200).send(geojson);
res.status(200).setHeader('Content-Type', 'application/json').send(geojson);
} catch (err) {
console.error(err); // eslint-disable-line no-console
res.status(err.code ?? 400).send(String(err));
Expand Down

0 comments on commit 4836acc

Please sign in to comment.