Add ApiError and handling auth

This commit is contained in:
FullGreaM 2026-04-26 22:25:26 +03:00
parent a9d1c99bd2
commit eab4dcb37d
4 changed files with 100 additions and 6 deletions

View File

@ -1,17 +1,18 @@
import { Router } from "express"; import { Router } from "express";
import { Response } from '../utils/rest-api-answer'; import { ApiResponse } from '../utils/rest-api-answer';
import { NewsItem, getTestNews } from "../utils/news"; import { NewsItem, getTestNews } from "../utils/news";
import authRequired from "../utils/auth-required";
const router = Router(); const router = Router();
router.get('/isAuthenticated', (req, res) => { router.get('/isAuthenticated', (req, res) => {
const response = new Response(req.isAuthenticated); const response = new ApiResponse(req.isAuthenticated);
return res.json(response.getAnswer()); return res.json(response.getAnswer());
}); });
router.get('/news', (req, res) => { router.get('/news', authRequired, (req, res) => {
const newsList : NewsItem[] = getTestNews(); const newsList : NewsItem[] = getTestNews();
const response = new Response(newsList); const response = new ApiResponse(newsList);
return res.json(response.getAnswer()); return res.json(response.getAnswer());
}); });

View File

@ -0,0 +1,13 @@
import { NextFunction, Response, Request } from "express";
import { ApiResponse, ApiError } from "./rest-api-answer";
export default function (req : Request, res: Response, next: NextFunction) {
if (!req.isAuthenticated) {
const error : ApiResponse<ApiError> = new ApiResponse({
code: "AUTH_REQUIRES",
message: "Authentication is required"
});
return res.status(401).json(error);
}
next();
}

View File

@ -1,6 +1,7 @@
export interface NewsItem { export interface NewsItem {
title : string; title : string;
content : string; content : string;
//source: string;
publishedAt : Date; publishedAt : Date;
} }
@ -12,6 +13,66 @@ export function getTestNews (): NewsItem[] {
title: "Lorem Ispum", title: "Lorem Ispum",
content: "Lorem Ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,", content: "Lorem Ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,",
publishedAt: new Date() publishedAt: new Date()
},
{
title: "News Item 1",
content: "This is the content for news item 1. It contains some sample text to demonstrate the news functionality.",
publishedAt: new Date()
},
{
title: "News Item 2",
content: "This is the content for news item 2. Another piece of sample news content for testing purposes.",
publishedAt: new Date()
},
{
title: "News Item 3",
content: "News item 3 brings you the latest updates. Stay informed with our regular news feed.",
publishedAt: new Date()
},
{
title: "News Item 4",
content: "Content for the fourth news item. This is part of the test data for the application.",
publishedAt: new Date()
},
{
title: "News Item 5",
content: "Fifth news item in the list. More sample content to populate the news array.",
publishedAt: new Date()
},
{
title: "News Item 6",
content: "This is news item number six. Continuing to add test data for the news feature.",
publishedAt: new Date()
},
{
title: "News Item 7",
content: "Seventh item in the news feed. Sample text to ensure the array has sufficient data.",
publishedAt: new Date()
},
{
title: "News Item 8",
content: "News item eight provides more content. This helps in testing the display of multiple items.",
publishedAt: new Date()
},
{
title: "News Item 9",
content: "The ninth news item. Almost reaching the total of 13 items including the original.",
publishedAt: new Date()
},
{
title: "News Item 10",
content: "Tenth item in the array. This completes the set of additional news items requested.",
publishedAt: new Date()
},
{
title: "News Item 11",
content: "Eleventh news item. Continuing to add variety to the test data.",
publishedAt: new Date()
},
{
title: "News Item 12",
content: "The twelfth and final news item. This brings the total to 13 items in the array.",
publishedAt: new Date()
} }
]; ];
} }

View File

@ -1,13 +1,32 @@
import { response } from "express"; import { response } from "express";
export class Response<T> { export interface ApiError {
code: string;
message: string;
}
function isApiError(value: unknown): value is ApiError {
return (
value !== null &&
typeof value === "object" &&
"code" in value &&
"message" in value
);
}
export class ApiResponse<T> {
data: T; data: T;
constructor(data: T) { constructor(data: T) {
this.data = data; this.data = data;
} }
getAnswer(): { response: T } { getAnswer(): { response: T } | { error: ApiError } {
if (isApiError(this.data)) {
return {
error: this.data
};
}
return { return {
response: this.data response: this.data
}; };