diff --git a/src/api/index.ts b/src/api/index.ts index 71256b1..c2e3014 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,17 +1,18 @@ 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 authRequired from "../utils/auth-required"; const router = Router(); router.get('/isAuthenticated', (req, res) => { - const response = new Response(req.isAuthenticated); + const response = new ApiResponse(req.isAuthenticated); return res.json(response.getAnswer()); }); -router.get('/news', (req, res) => { +router.get('/news', authRequired, (req, res) => { const newsList : NewsItem[] = getTestNews(); - const response = new Response(newsList); + const response = new ApiResponse(newsList); return res.json(response.getAnswer()); }); diff --git a/src/utils/auth-required.ts b/src/utils/auth-required.ts new file mode 100644 index 0000000..ee0a2c4 --- /dev/null +++ b/src/utils/auth-required.ts @@ -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 = new ApiResponse({ + code: "AUTH_REQUIRES", + message: "Authentication is required" + }); + return res.status(401).json(error); + } + next(); +} \ No newline at end of file diff --git a/src/utils/news.ts b/src/utils/news.ts index aefd90a..04b55e1 100644 --- a/src/utils/news.ts +++ b/src/utils/news.ts @@ -1,6 +1,7 @@ export interface NewsItem { title : string; content : string; + //source: string; publishedAt : Date; } @@ -12,6 +13,66 @@ export function getTestNews (): NewsItem[] { 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,", 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() } ]; } \ No newline at end of file diff --git a/src/utils/rest-api-answer.ts b/src/utils/rest-api-answer.ts index f0ea484..885d275 100644 --- a/src/utils/rest-api-answer.ts +++ b/src/utils/rest-api-answer.ts @@ -1,13 +1,32 @@ import { response } from "express"; -export class Response { +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 { data: T; constructor(data: T) { this.data = data; } - getAnswer(): { response: T } { + getAnswer(): { response: T } | { error: ApiError } { + if (isApiError(this.data)) { + return { + error: this.data + }; + } return { response: this.data };