Skip to content

Commit f5676d0

Browse files
committed
Removed pages router completely
1 parent 4d83870 commit f5676d0

15 files changed

Lines changed: 262 additions & 226 deletions

File tree

apps/web/pages/api/auth/code/generate.ts renamed to apps/web/app/api/auth/code/generate/route.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import { NextApiRequest, NextApiResponse } from "next";
2-
import { responses } from "../../../../config/strings";
3-
import { generateUniquePasscode, hashCode } from "../../../../ui-lib/utils";
4-
import VerificationToken from "../../../../models/VerificationToken";
1+
import { NextRequest } from "next/server";
2+
import { responses } from "@/config/strings";
3+
import { generateUniquePasscode, hashCode } from "@/ui-lib/utils";
4+
import VerificationToken from "@/models/VerificationToken";
55
import pug from "pug";
6-
import MagicCodeEmailTemplate from "../../../../templates/magic-code-email";
6+
import MagicCodeEmailTemplate from "@/templates/magic-code-email";
77
import DomainModel, { Domain } from "@models/Domain";
88
import { generateEmailFrom } from "@/lib/utils";
99
import { addMailJob } from "@/services/queue";
1010

11-
export default async function handler(
12-
req: NextApiRequest,
13-
res: NextApiResponse,
14-
) {
15-
if (req.method !== "GET") {
16-
return res.status(405).json({ message: "Not allowed" });
17-
}
11+
export const dynamic = "force-dynamic";
1812

13+
export async function GET(req: NextRequest) {
1914
const domain = await DomainModel.findOne<Domain>({
20-
name: req.headers.domain,
15+
name: req.headers.get("domain"),
2116
});
2217
if (!domain) {
23-
return res.status(404).json({ message: "Domain not found" });
18+
return Response.json({ message: "Domain not found" }, { status: 404 });
2419
}
2520

26-
const { email } = req.query;
21+
const searchParams = req.nextUrl.searchParams;
22+
const email = searchParams.get("email");
2723
if (!email) {
28-
return;
24+
return Response.json({ message: "Email is required" }, { status: 400 });
2925
}
3026
const code = generateUniquePasscode();
3127

@@ -54,10 +50,13 @@ export default async function handler(
5450
}),
5551
});
5652
} catch (err: any) {
57-
res.status(500).json({
58-
error: err.message,
59-
});
53+
return Response.json(
54+
{
55+
error: err.message,
56+
},
57+
{ status: 500 },
58+
);
6059
}
6160

62-
res.status(200).json({});
61+
return Response.json({});
6362
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import type { NextApiRequest } from "next";
2-
import schema from "../../graphql";
1+
import { NextRequest } from "next/server";
2+
import schema from "@/graphql";
33
import { graphql } from "graphql";
4-
import { getAddress } from "../../lib/utils";
4+
import { getAddress } from "@/lib/utils";
55
import User from "@models/User";
66
import DomainModel, { Domain } from "@models/Domain";
77
import { auth } from "@/auth";
88

9-
export const config = {
10-
api: {
11-
bodyParser: {
12-
sizeLimit: "3mb",
13-
},
14-
},
15-
};
16-
179
async function updateLastActive(user: any) {
1810
const dateNow = new Date();
1911
dateNow.setUTCHours(0, 0, 0, 0);
@@ -26,22 +18,15 @@ async function updateLastActive(user: any) {
2618
}
2719
}
2820

29-
export default async function handler(
30-
req: NextApiRequest,
31-
res: NextApiResponse,
32-
) {
33-
if (req.method !== "POST") {
34-
return res.status(405).json({ message: "Not allowed" });
35-
}
36-
21+
export async function POST(req: NextRequest) {
3722
const domain = await DomainModel.findOne<Domain>({
38-
name: req.headers.domain,
23+
name: req.headers.get("domain"),
3924
});
4025
if (!domain) {
41-
return res.status(404).json({ message: "Domain not found" });
26+
return Response.json({ message: "Domain not found" }, { status: 404 });
4227
}
4328

44-
const session = await auth(req, res);
29+
const session = await auth();
4530

4631
let user;
4732
if (session) {
@@ -56,24 +41,32 @@ export default async function handler(
5641
}
5742
}
5843

59-
if (!req.body.hasOwnProperty("query")) {
60-
res.status(400).json({ error: "Query is missing" });
44+
const body = await req.json();
45+
if (!body.hasOwnProperty("query")) {
46+
return Response.json({ error: "Query is missing" }, { status: 400 });
6147
}
6248

63-
const { query, variables } = req.body.query;
64-
const hostname = req.headers["host"] || "";
65-
const protocol = req.headers["x-forwarded-proto"];
49+
let query, variables;
50+
if (typeof body.query === "string") {
51+
query = body.query;
52+
variables = body.variables;
53+
} else {
54+
query = body.query.query;
55+
variables = body.query.variables;
56+
}
57+
const hostname = req.headers.get("host") || "";
58+
const protocol = req.headers.get("x-forwarded-proto") || "http";
6659
const contextValue = {
6760
user,
6861
subdomain: domain,
6962
address: getAddress(hostname, protocol),
7063
};
7164
const response = await graphql({
7265
schema,
73-
source: query || req.body.query,
66+
source: query,
7467
rootValue: null,
7568
contextValue,
7669
variableValues: variables,
7770
});
78-
return res.status(200).json(response);
71+
return Response.json(response);
7972
}

apps/web/pages/api/media/[mediaId]/[type].ts renamed to apps/web/app/api/media/[mediaId]/[type]/route.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { NextApiRequest, NextApiResponse } from "next";
1+
import { NextRequest } from "next/server";
22
import { responses } from "@/config/strings";
33
import * as medialitService from "@/services/medialit";
44
import { UIConstants as constants } from "@courselit/common-models";
55
import { checkPermission } from "@courselit/utils";
6-
import UserModel, { User } from "@models/User";
6+
import UserModel from "@models/User";
7+
import { InternalUser, InternalCourse } from "@courselit/common-logic";
78
import DomainModel, { Domain } from "@models/Domain";
89
import { auth } from "@/auth";
9-
import CourseModel, { Course } from "@models/Course";
10+
import CourseModel from "@models/Course";
1011
import LessonModel, { Lesson } from "@models/Lesson";
1112
import PageModel, { Page } from "@models/Page";
1213

@@ -21,22 +22,18 @@ const types = [
2122

2223
type MediaType = (typeof types)[number];
2324

24-
export default async function handler(
25-
req: NextApiRequest,
26-
res: NextApiResponse,
25+
export async function DELETE(
26+
req: NextRequest,
27+
{ params }: { params: Promise<{ mediaId: string; type: string }> },
2728
) {
28-
if (req.method !== "DELETE") {
29-
return res.status(405).json({ message: "Not allowed" });
30-
}
31-
3229
const domain = await DomainModel.findOne<Domain>({
33-
name: req.headers.domain,
30+
name: req.headers.get("domain"),
3431
});
3532
if (!domain) {
36-
return res.status(404).json({ message: "Domain not found" });
33+
return Response.json({ message: "Domain not found" }, { status: 404 });
3734
}
3835

39-
const session = await auth(req, res);
36+
const session = await auth();
4037

4138
let user;
4239
if (session) {
@@ -48,31 +45,37 @@ export default async function handler(
4845
}
4946

5047
if (!user) {
51-
return res.status(401).json({});
48+
return Response.json({}, { status: 401 });
5249
}
5350

54-
const { mediaId, type } = req.query;
51+
const mediaId = (await params).mediaId;
52+
const type = (await params).type;
5553
if (!types.includes(type as MediaType)) {
56-
return res.status(400).json({ message: "Bad request" });
54+
return Response.json({ message: "Bad request" }, { status: 400 });
5755
}
5856

5957
if (
6058
!(await isActionAllowed(user, type as any, mediaId as string, domain))
6159
) {
62-
("");
63-
return res.status(403).json({ message: responses.action_not_allowed });
60+
return Response.json(
61+
{ message: responses.action_not_allowed },
62+
{ status: 403 },
63+
);
6464
}
6565

6666
try {
67-
let response = await medialitService.deleteMedia(<string>mediaId);
68-
return res.status(200).json({ message: responses.success });
67+
await medialitService.deleteMedia(<string>mediaId);
68+
return Response.json({ message: responses.success });
6969
} catch (err: any) {
70-
return res.status(500).json({ error: responses.internal_error });
70+
return Response.json(
71+
{ error: responses.internal_error },
72+
{ status: 500 },
73+
);
7174
}
7275
}
7376

7477
async function isActionAllowed(
75-
user: User,
78+
user: InternalUser,
7679
type: MediaType,
7780
mediaId: string,
7881
domain: Domain,
@@ -85,7 +88,7 @@ async function isActionAllowed(
8588

8689
switch (type) {
8790
case "course":
88-
const course = await CourseModel.findOne<Course>({
91+
const course = await CourseModel.findOne<InternalCourse>({
8992
domain: domain._id,
9093
"featuredImage.mediaId": mediaId,
9194
});
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
import { NextApiRequest, NextApiResponse } from "next";
2-
import { responses } from "../../../config/strings";
3-
import * as medialitService from "../../../services/medialit";
1+
import { NextRequest } from "next/server";
2+
import { responses } from "@/config/strings";
3+
import * as medialitService from "@/services/medialit";
44
import { UIConstants as constants } from "@courselit/common-models";
55
import { checkPermission } from "@courselit/utils";
66
import User from "@models/User";
77
import DomainModel, { Domain } from "@models/Domain";
88
import { auth } from "@/auth";
99
import { error } from "@/services/logger";
1010

11-
export default async function handler(
12-
req: NextApiRequest,
13-
res: NextApiResponse,
14-
) {
15-
if (req.method !== "POST") {
16-
return res.status(405).json({ message: "Not allowed" });
17-
}
18-
11+
export async function POST(req: NextRequest) {
1912
const domain = await DomainModel.findOne<Domain>({
20-
name: req.headers.domain,
13+
name: req.headers.get("domain"),
2114
});
2215
if (!domain) {
23-
return res.status(404).json({ message: "Domain not found" });
16+
return Response.json({ message: "Domain not found" }, { status: 404 });
2417
}
2518

26-
const session = await auth(req, res);
19+
const session = await auth();
2720

2821
let user;
2922
if (session) {
@@ -35,24 +28,27 @@ export default async function handler(
3528
}
3629

3730
if (!user) {
38-
return res.status(401).json({});
31+
return Response.json({}, { status: 401 });
3932
}
4033

4134
if (
4235
!checkPermission(user!.permissions, [constants.permissions.manageMedia])
4336
) {
44-
return res.status(403).json({ message: responses.action_not_allowed });
37+
return Response.json(
38+
{ message: responses.action_not_allowed },
39+
{ status: 403 },
40+
);
4541
}
4642

4743
try {
4844
let response = await medialitService.getPresignedUrlForUpload(
4945
domain.name,
5046
);
51-
return res.status(200).json({ url: response });
47+
return Response.json({ url: response });
5248
} catch (err: any) {
5349
error(err.message, {
5450
stack: err.stack,
5551
});
56-
return res.status(500).json({ error: err.message });
52+
return Response.json({ error: err.message }, { status: 500 });
5753
}
5854
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { NextRequest } from "next/server";
2+
import { responses } from "@/config/strings";
3+
import User from "@models/User";
4+
import DomainModel, { Domain } from "@models/Domain";
5+
6+
export async function GET(
7+
req: NextRequest,
8+
{ params }: { params: Promise<{ token: string }> },
9+
) {
10+
const domain = await DomainModel.findOne<Domain>({
11+
name: req.headers.get("domain"),
12+
});
13+
if (!domain) {
14+
return Response.json({ message: "Domain not found" }, { status: 404 });
15+
}
16+
17+
const token = (await params).token;
18+
19+
const user = await User.findOne({ unsubscribeToken: token });
20+
21+
if (!user) {
22+
return Response.json({ message: responses.unsubscribe_success });
23+
}
24+
25+
await user.updateOne({ subscribedToUpdates: false });
26+
27+
return Response.json({ message: responses.unsubscribe_success });
28+
}

apps/web/graphql/courses/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import types from "./types";
1010
import {
1111
getCourse,
1212
getCoursesAsAdmin,
13-
getCourses,
13+
// getCourses,
1414
// getEnrolledCourses,
1515
getCourseOrThrow,
1616
getMembers,

0 commit comments

Comments
 (0)