Skip to content

Commit

Permalink
fixed API routes for now
Browse files Browse the repository at this point in the history
  • Loading branch information
S1nthesis committed Oct 26, 2023
1 parent 8589380 commit 3d4ead4
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 60 deletions.
26 changes: 0 additions & 26 deletions api/getEvents.ts

This file was deleted.

12 changes: 0 additions & 12 deletions api/json/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/libs/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Need a package that can dynamically switch between the JS `fetch`
// and the `node-fetch` based on usage.
import fetch from "isomorphic-unfetch";
import { NowRequest } from "@vercel/node";
import { VercelRequest } from "@vercel/node";

export async function fetcher<JSON = unknown>(
input: RequestInfo,
Expand All @@ -19,7 +19,7 @@ export async function fetcher<JSON = unknown>(

export const authenticatedFetch = <JSON = unknown>(
input: RequestInfo,
req: NowRequest,
req: VercelRequest,
init?: RequestInit
): Promise<JSON> => {
const { accessToken } = req.cookies;
Expand All @@ -32,7 +32,7 @@ export const authenticatedFetch = <JSON = unknown>(
});
};

export const getBaseUrl = (req: NowRequest): string => {
export const getBaseUrl = (req: VercelRequest): string => {
const httpProto = req.headers["x-forwarded-proto"] || "https";
return `${httpProto}://${req.headers.host}`;
};
12 changes: 6 additions & 6 deletions api/addEvent.ts → src/pages/api/addEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NowRequest, NowResponse } from "@vercel/node";
import { authenticatedRoute } from "../src/libs/middleware";
import { User } from "../src/common/UserProvider";
import { getFirestoreDB } from "../src/libs/firestoreDB";
import { VercelRequest, VercelResponse } from "@vercel/node";
import { authenticatedRoute } from "../../libs/middleware";
import { User } from "../../common/UserProvider";
import { getFirestoreDB } from "../../libs/firestoreDB";
import md5 from "md5";

/**
Expand All @@ -15,8 +15,8 @@ import md5 from "md5";
* ensuring uniqueness and allowing easy lookup.
*/
const addEventHandler = async (
req: NowRequest,
res: NowResponse,
req: VercelRequest,
res: VercelResponse,
user: User
): Promise<void> => {
const db = getFirestoreDB();
Expand Down
31 changes: 31 additions & 0 deletions src/pages/api/getEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { authenticatedRoute } from "../../libs/middleware";
import { User } from "../../common/UserProvider";
import { getFirestoreDB } from "../../libs/firestoreDB";

/**
* Returns a list of all database entries containing
* the current user's userAuthId.
*/
const getEventsHandler = async (
req: VercelRequest,
res: VercelResponse,
user: User
): Promise<void> => {
try {
const db = getFirestoreDB();

const events: Object[] = [];
const snapshot = await db.collection('ScheduledEvents').where('userAuthId', '==', user.authId).get();
snapshot.forEach(doc => {
events.push(doc.data());
});

res.status(200).json({ events: events });
} catch (error) {
console.error('Error in getEventsHandler:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
};

export default authenticatedRoute(getEventsHandler);
8 changes: 4 additions & 4 deletions api/join.ts → src/pages/api/join.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { authenticatedRoute } from "../src/libs/middleware";
import { User } from "../src/common/UserProvider";
import { getBaseUrl, authenticatedFetch } from "../src/libs";
import { getActivityByName } from "../src/libs/activitiesAPI";
import { authenticatedRoute } from "../../libs/middleware";
import { User } from "../../common/UserProvider";
import { getBaseUrl, authenticatedFetch } from "../../libs";
import { getActivityByName } from "../../libs/activitiesAPI";

const joinEventHandler = async (
req: VercelRequest,
Expand Down
6 changes: 3 additions & 3 deletions api/json/[page].ts → src/pages/api/json/[page].ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NowRequest, NowResponse } from "@vercel/node";
import { getActivityByName } from "../../src/libs/activitiesAPI";
import { VercelRequest, VercelResponse } from "@vercel/node";
import { getActivityByName } from "../../../libs/activitiesAPI";

/**
* Return JSON of a specififc activity from /db/activities/{name_of_activity}
*/
const activity = async (req: NowRequest, res: NowResponse): Promise<void> => {
const activity = async (req: VercelRequest, res: VercelResponse): Promise<void> => {
const { page } = req.query;
const activity = await getActivityByName(String(page));
res.status(200).json(activity);
Expand Down
17 changes: 17 additions & 0 deletions src/pages/api/json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { getAllActivities } from "../../../libs/activitiesAPI";

/**
* Return JSON of all activities in /db/activities
*/
const allPages = async (req: VercelRequest, res: VercelResponse): Promise<void> => {
try {
const allPages = await getAllActivities();
res.status(200).json(allPages);
} catch (error) {
console.error("Error in allPages:", error);
res.status(502).json({ error: "Internal Server Error" });
}
};

export default allPages;
12 changes: 6 additions & 6 deletions api/removeEvent.ts → src/pages/api/removeEvent.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { NowRequest, NowResponse } from "@vercel/node";
import { authenticatedRoute } from "../src/libs/middleware";
import { getFirestoreDB } from "../src/libs/firestoreDB";
import { User } from "../src/common/UserProvider";
import { VercelRequest, VercelResponse } from "@vercel/node";
import { authenticatedRoute } from "../../libs/middleware";
import { getFirestoreDB } from "../../libs/firestoreDB";
import { User } from "../../common/UserProvider";
import md5 from "md5";

/**
* Removes the document in the database that contains the current user's
* userAuthId and the passed in eventId, using a hash for easy lookup.
*/
const removeEventHandler = async (
req: NowRequest,
res: NowResponse,
req: VercelRequest,
res: VercelResponse,
user: User
): Promise<void> => {
const db = getFirestoreDB();
Expand Down

0 comments on commit 3d4ead4

Please sign in to comment.