Skip to content

Commit

Permalink
type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelos231 committed Jun 26, 2023
1 parent d096c6b commit 107e0d1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
14 changes: 10 additions & 4 deletions pages/api/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { verify } from "@server/helpers/validateToken";
import { NextApiResponse, NextApiRequest } from "next";
import { NOTAUTH } from "@constants/auth";

export interface AuthenticatedRequest extends NextApiRequest {
user?: any;
export interface IAuthRes extends NextApiRequest {
user: {
Email: string;
Name: string;
};
}

type HandlerFunc<T> = (req: T, res: NextApiResponse<any>) => Promise<any>;
type HandlerFunc<T extends IAuthRes> = (
req: T,
res: NextApiResponse<any>
) => Promise<any>;

export const authMiddleware =
<T extends AuthenticatedRequest>(handler: HandlerFunc<T>): HandlerFunc<T> =>
<T extends IAuthRes>(handler: HandlerFunc<T>): HandlerFunc<T> =>
async (req, res) => {
const token: string = String(req.headers["authorization"]);

Expand Down
9 changes: 6 additions & 3 deletions pages/api/middleware/paginate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { NextApiResponse, NextApiRequest } from "next";
import { NOTAUTH } from "@constants/auth";

export interface PaginatedRequest extends NextApiRequest {
export interface IPaginate extends NextApiRequest {
PAGE_SIZE: string;
skipValue: string;
}

type HandlerFunc<T> = (req: T, res: NextApiResponse<any>) => Promise<any>;
type HandlerFunc<T extends IPaginate> = (
req: T,
res: NextApiResponse<any>
) => Promise<any>;

export const paginateMiddleware =
<T extends PaginatedRequest>(handler: HandlerFunc<T>): HandlerFunc<T> =>
<T extends IPaginate>(handler: HandlerFunc<T>): HandlerFunc<T> =>
async (req, res) => {
const skipValue = req.headers["skipvalue"] as string;
const PAGE_SIZE = req.headers["page_size"] as string;
Expand Down
5 changes: 4 additions & 1 deletion pages/api/middleware/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export type HandlerFunc = number;
import { IAuthRes } from "../authMiddleware";
import { IPaginate } from "../paginate";

export type IAuthPag = IAuthRes & IPaginate;
3 changes: 2 additions & 1 deletion pages/api/user/UserLikedPosts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getLikedUserPosts, CountLikedPostssDB } from "@server/db/posts";
import { authMiddleware } from "../middleware/authMiddleware";
import { paginateMiddleware } from "../middleware/paginate";
import { IAuthPag } from "../middleware/types";

export default authMiddleware(
export default authMiddleware<IAuthPag>(
paginateMiddleware(async function Handler(req, res) {
try {
const Name = String(req.user.Name);
Expand Down
3 changes: 2 additions & 1 deletion pages/api/user/fetchUserSpecificData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { getPostsByUser } from "@server/db/posts";
import { authMiddleware } from "../middleware/authMiddleware";
import { paginateMiddleware } from "../middleware/paginate";
import { CountCreatedPostssDB } from "@server/db/posts";
import { IAuthPag } from "../middleware/types";

export default authMiddleware(
export default authMiddleware<IAuthPag>(
paginateMiddleware(async function Handler(req, res) {
try {
const userEmail = String(req.user.Email);
Expand Down

0 comments on commit 107e0d1

Please sign in to comment.