Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/api/commit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import request from "supertest";
import { app } from "../test-util";
describe("Commits api works fine", () => {
it("get endpoint working", async () => {
const res = await request(app).get(
"/ahmetilhan24/github-ui-widgets/commits"
);
expect(res.statusCode).toEqual(200);
});
});
8 changes: 8 additions & 0 deletions __tests__/api/profile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import request from "supertest";
import { app } from "../test-util";
describe("profile(user-profile) api works fine", () => {
it("get endpoint working", async () => {
const res = await request(app).get("/ahmetilhan24/profile");
expect(res.statusCode).toEqual(200);
});
});
8 changes: 8 additions & 0 deletions __tests__/api/repository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import request from "supertest";
import { app } from "../test-util";
describe("Repositories api works fine", () => {
it("get endpoint working", async () => {
const res = await request(app).get("/ahmetilhan24/repositories");
expect(res.statusCode).toEqual(200);
});
});
2 changes: 2 additions & 0 deletions __tests__/test-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import server from "../src/api/server";
export const app = server.app;
13 changes: 13 additions & 0 deletions __tests__/utils/req-validator.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
repoNameValidator,
usernameValidator,
} from "../../src/api/utils/req-validator.util";

describe("req-validator-utils works fine", () => {
it("usernameValidator works fine", () => {
expect(usernameValidator({ username: "asd" })).toBeTruthy();
});
it("repoNameValidator works fine", () => {
expect(repoNameValidator({ repo: "asd" })).toBeTruthy();
});
});
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.spec.ts"],
testPathIgnorePatterns: ["/node_modules/", "/demo/", "/dist/", "/src/"],
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
"dev": "NODE_ENV=development nodemon ./src/api/server.ts",
"build": "webpack --mode production",
"start": "NODE_ENV=production node ./dist/server.js",
"start-gulp": "npx gulp"
"start-gulp": "npx gulp",
"test": "NODE_ENV=test jest"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jest": "^29.1.0",
"@types/supertest": "^2.0.12",
"copy-webpack-plugin": "^11.0.0",
"dayjs": "^1.11.5",
"dotenv": "^16.0.2",
"gulp": "^4.0.2",
"gulp-sass": "^4.1.0",
"jest": "^29.1.1",
"nodemon": "^2.0.20",
"supertest": "^6.2.4",
"ts-jest": "^29.0.3",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4",
Expand All @@ -34,4 +40,4 @@
"express": "^4.18.1",
"express-handlebars": "^6.0.6"
}
}
}
3 changes: 3 additions & 0 deletions src/api/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ dotenv.config();
export default () => {
return {
API_PORT: process.env.API_PORT,
GIT_TOKEN: process.env.GIT_TOKEN,
GIT_PASSWORD: process.env.GIT_PASSWORD,
GIT_USERNAME: process.env.GIT_USERNAME,
};
};
1 change: 1 addition & 0 deletions src/api/constants/error.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export default {
UNKNOWN_ERR: "An unknown error has occurred",
NOT_RESULT: (relevance: string) => `Not enough ${relevance} found`,
NOT_FOUND: `Not found`,
BAD_REQUEST: "Bad request",
};
14 changes: 12 additions & 2 deletions src/api/controllers/commit.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ import errorConstants from "../constants/error.constants";
import CommitService from "../services/commit.service";
import ICommit from "../types/commit.interface";
import IController from "../types/controller.interface";
import {
repoNameValidator,
usernameValidator,
} from "../utils/req-validator.util";
class CommitController implements IController {
public async get(
req: Request,
res: Response,
next: NextFunction
): Promise<boolean> {
): Promise<any> {
if (!usernameValidator(req.params) || !repoNameValidator(req.params)) {
res.status(400).json({
msg: errorConstants.BAD_REQUEST,
});
return next(errorConstants.BAD_REQUEST);
}
const response: AxiosResponse<Array<ICommit>> =
await CommitService.getCommitsByRepo(
req.params.username,
req.params.repo
);
if (!response) {
res.status(404).json({ msg: errorConstants.NOT_RESULT("Commit") });
res.status(500).json({ msg: errorConstants.SERVER_ERR });
return false;
}
const data = response.data.map((item: ICommit) => ({
Expand Down
11 changes: 9 additions & 2 deletions src/api/controllers/repository.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import { NextFunction, Request, Response } from "express";
import errorConstants from "../constants/error.constants";
import RepositoryService from "../services/repository.service";
import IRepository from "../types/repository.interface";
import { usernameValidator } from "../utils/req-validator.util";
class RepositoryController {
public async get(
req: Request,
res: Response,
next: NextFunction
): Promise<boolean> {
): Promise<any> {
if (!usernameValidator(req.params)) {
res.status(400).json({
msg: errorConstants.BAD_REQUEST + "Username is not defined",
});
return next(errorConstants.BAD_REQUEST);
}
const response: AxiosResponse<IRepository> =
await RepositoryService.getReposByUsername(req.params.username);
if (!response) {
res.status(404).json({ msg: errorConstants.NOT_RESULT("Repository") });
res.status(500).json({ msg: errorConstants.SERVER_ERR });
return;
}
res.render("repository-list", {
Expand Down
11 changes: 9 additions & 2 deletions src/api/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ import { NextFunction, Request, Response } from "express";
import errorConstants from "../constants/error.constants";
import UserService from "../services/user.service";
import IUser from "../types/user.interface";
import { usernameValidator } from "../utils/req-validator.util";
class UserController {
public async get(
req: Request,
res: Response,
next: NextFunction
): Promise<boolean> {
): Promise<any> {
if (!usernameValidator(req.params)) {
res.status(400).json({
msg: errorConstants.BAD_REQUEST + "Username is not defined",
});
return next(errorConstants.BAD_REQUEST);
}
const response: AxiosResponse<IUser> = await UserService.getUserByUsername(
req.params.username
);
if (!response) {
res.status(404).json({ msg: errorConstants.NOT_RESULT("User") });
res.status(500).json({ msg: errorConstants.SERVER_ERR });
return;
}
res.render("user-card", {
Expand Down
6 changes: 3 additions & 3 deletions src/api/plugins/axios.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ baseHTTP.interceptors.response.use(
}
console.error(error.response.data);
ErrorHandler.basic(error.response.data);
const status = statusCodes.find(
const statusCode = statusCodes.find(
(item: IHttpStatus) => item.status === error.response?.status
);
if (status) {
console.error(status.msg);
if (statusCode) {
console.error(statusCode.msg);
}
}
);
Expand Down
17 changes: 10 additions & 7 deletions src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import express, { Application } from "express";
import config from "./config";
import Router from "./router";
class Server {
app: Application = express();
public app: Application = express();
constructor() {
this.init();
}
init = () => {
config(this.app);
Router.init(this.app);
this.app.use(express.json({ limit: "100mb" }));
this.app.use("/static", express.static(process.cwd() + "/src/public"));
this.listen();
};
listen = () => {
const port: unknown = process.env.API_PORT;
if (port) {
listen = (): void => {
const port = process.env.API_PORT || 5555;
//!For test development environment
const isTesting: boolean = process.env.NODE_ENV === "test" || false;
if (port && !isTesting) {
this.app.listen(port, () => {
console.log(`Port listening at ${port}`);
});
}
};
}

//! start server
const server = new Server();
server.init();
export default new Server();
20 changes: 20 additions & 0 deletions src/api/utils/req-validator.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request } from "express";

export const usernameValidator = (params: {
[key: string]: string;
}): boolean => {
//@ts-ignored
if (!!params?.username) {
return true;
}
return false;
};

export const repoNameValidator = (params: {
[key: string]: string;
}): boolean => {
if (!!params?.repo) {
return true;
}
return false;
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"outDir": "dist" //Specifies the transpiler directory.
},
"include": ["src"],
"exclude": ["node_modules", "dist", "demo"]
"exclude": ["node_modules", "dist", "demo", "__tests__"]
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Projects */
Expand Down
Loading