Skip to content

Commit

Permalink
Merge pull request #1065 from mikecao/dev
Browse files Browse the repository at this point in the history
Added CORS to website API endpoints.
  • Loading branch information
mikecao authored Apr 4, 2022
2 parents 17aaa55 + bf5068d commit 493f5a5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pages/api/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async (req, res) => {
return badRequest(res, 'Account already exists');
}
}
console.log('------------------\n', data);

const updated = await updateAccount(user_id, data);

return ok(res, updated);
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/active.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getActiveVisitors } from 'lib/queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

export default async (req, res) => {
if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import moment from 'moment-timezone';
import { getEventMetrics } from 'lib/queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

const unitTypes = ['year', 'month', 'hour', 'day'];

export default async (req, res) => {
if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { deleteWebsite, getWebsiteById } from 'lib/queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

export default async (req, res) => {
const { id } = req.query;

const websiteId = +id;

if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/metrics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'lib/queries';
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

const sessionColumns = ['browser', 'os', 'device', 'country', 'language'];
const pageviewColumns = ['url', 'referrer'];
Expand All @@ -26,6 +27,8 @@ function getColumn(type) {

export default async (req, res) => {
if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/pageviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import moment from 'moment-timezone';
import { getPageviewStats } from 'lib/queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

const unitTypes = ['year', 'month', 'hour', 'day'];

export default async (req, res) => {
if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/api/website/[id]/stats.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getWebsiteStats } from 'lib/queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

export default async (req, res) => {
if (req.method === 'GET') {
await useCors(req, res);

if (!(await allowQuery(req))) {
return unauthorized(res);
}
Expand Down

1 comment on commit 493f5a5

@vercel
Copy link

@vercel vercel bot commented on 493f5a5 Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.