diff --git a/Ghibli.sh b/Ghibli.sh new file mode 100644 index 0000000..a01add9 --- /dev/null +++ b/Ghibli.sh @@ -0,0 +1 @@ +npm run start \ No newline at end of file diff --git a/package.json b/package.json index 51f360e..353762a 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "src/index.ts", "scripts": { "test": "ts-node src/index.ts --resolveJsonModule --trace-deprication", + "start": "ts-node src/index.ts --resolveJsonModule --trace-deprication", "format": "prettier --write \"src/**/*.ts\"" }, "repository": { diff --git a/src/assets/img/favicon-ghibli.png b/src/assets/img/favicon-ghibli.png new file mode 100644 index 0000000..b4e2cfa Binary files /dev/null and b/src/assets/img/favicon-ghibli.png differ diff --git a/src/assets/img/logo.svg b/src/assets/img/logo.svg new file mode 100644 index 0000000..ea582c0 --- /dev/null +++ b/src/assets/img/logo.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/assets/js/swagger.js b/src/assets/js/swagger.js index f91c70c..d923dab 100644 --- a/src/assets/js/swagger.js +++ b/src/assets/js/swagger.js @@ -1,27 +1,23 @@ /* eslint-disable */ -window.addEventListener('load', () => { - const logoElement = document.querySelector('.topbar img'); - if (logoElement) - logoElement.setAttribute( - 'src', - 'https://cdn.discordapp.com/attachments/1052712829028933752/1052712974927794196/Group_160.png' - ); +window.addEventListener("load", () => { + const logoElement = document.querySelector(".topbar img"); + if (logoElement) logoElement.setAttribute("src", "/assets/img/logo.svg"); - document.head.querySelectorAll('link').forEach((elem) => { - if (elem.getAttribute('rel') == 'icon') { - elem.setAttribute('href', 'https://ticketerbot.com/logo.svg'); + document.head.querySelectorAll("link").forEach((elem) => { + if (elem.getAttribute("rel") == "icon") { + elem.setAttribute("href", "/assets/img/favicon-ghibli.png"); } }); - window.scrollTo({ x: 0, y: 0, scrollBehavior: 'smooth' }); + window.scrollTo({ x: 0, y: 0, scrollBehavior: "smooth" }); - document.querySelectorAll('.opblock-tag').forEach((elem) => { + document.querySelectorAll(".opblock-tag").forEach((elem) => { elem.click(); }); - while (document.querySelectorAll('.opblock.is-open').length > 0) { - document.querySelectorAll('.opblock').forEach((elem) => { - if (elem.classList.contains('is-open')) elem.click(); + while (document.querySelectorAll(".opblock.is-open").length > 0) { + document.querySelectorAll(".opblock").forEach((elem) => { + if (elem.classList.contains("is-open")) elem.click(); }); } }); diff --git a/src/docs/films.ts b/src/docs/films.ts new file mode 100644 index 0000000..8ff7276 --- /dev/null +++ b/src/docs/films.ts @@ -0,0 +1,19 @@ +// FILM API DOCUMENTATION + +/** + * @swagger + * tags: + * name: Film + * description: The Film API + */ + +/** + * @swagger + * /films/:id: + * get: + * summary: Sends you specific information about a film + * tags: [Film] + * responses: + * 200: + * description: Film found + */ diff --git a/src/docs/locations.ts b/src/docs/locations.ts new file mode 100644 index 0000000..a53e880 --- /dev/null +++ b/src/docs/locations.ts @@ -0,0 +1,19 @@ +// LOCATION API DOCUMENTATION + +/** + * @swagger + * tags: + * name: Location + * description: The Location API + */ + +/** + * @swagger + * /locations/:id: + * get: + * summary: Sends you specific information about a location + * tags: [Location] + * responses: + * 200: + * description: Location found + */ diff --git a/src/docs/people.ts b/src/docs/people.ts new file mode 100644 index 0000000..8cf0920 --- /dev/null +++ b/src/docs/people.ts @@ -0,0 +1,19 @@ +// PEOPLE API DOCUMENTATION + +/** + * @swagger + * tags: + * name: People + * description: The People API + */ + +/** + * @swagger + * /people/:id: + * get: + * summary: Sends you specific information about a person + * tags: [People] + * responses: + * 200: + * description: People found + */ diff --git a/src/docs/species.ts b/src/docs/species.ts new file mode 100644 index 0000000..d8f6ff6 --- /dev/null +++ b/src/docs/species.ts @@ -0,0 +1,19 @@ +// SPECIES API DOCUMENTATION + +/** + * @swagger + * tags: + * name: Species + * description: The Species API + */ + +/** + * @swagger + * /species/:id: + * get: + * summary: Sends you specific information about a species + * tags: [Species] + * responses: + * 200: + * description: Species found + */ diff --git a/src/docs/vehicles.ts b/src/docs/vehicles.ts new file mode 100644 index 0000000..afc76ea --- /dev/null +++ b/src/docs/vehicles.ts @@ -0,0 +1,19 @@ +// VEHICLE API DOCUMENTATION + +/** + * @swagger + * tags: + * name: Vehicles + * description: The Vehicles API + */ + +/** + * @swagger + * /vehicles/:id: + * get: + * summary: Sends you specific information about a vehicle + * tags: [Vehicles] + * responses: + * 200: + * description: Vehicle found + */ diff --git a/src/index.ts b/src/index.ts index 79ee0bb..0b84842 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ dotenv.config(); // Kill process if port is in use try { - kill(3001, "tcp"); + kill(6543, "tcp"); } catch (error) { /* Empty */ } @@ -21,9 +21,9 @@ dotenv.config(); // Initialize express let api: Express = express(); +// Initialize swaggerUI +require("./util/Swagger").default(api); + // Initialize Routes require("./util/Middleware").default(api); api.use("/", require("./util/RoutesManager").default(api)); - -// Initialize swaggerUI -require("./util/Swagger").default(api); diff --git a/src/routes/.route.ts b/src/routes/.route.ts index 1f8bb58..9f8ac6e 100644 --- a/src/routes/.route.ts +++ b/src/routes/.route.ts @@ -7,7 +7,7 @@ export default (urlName: string): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}`, (req: Request, res: Response) => { - res.send("Online"); + res.redirect("/docs"); }); return api; diff --git a/src/routes/films/.route.ts b/src/routes/films/.route.ts index 4fdd651..75e0816 100644 --- a/src/routes/films/.route.ts +++ b/src/routes/films/.route.ts @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}:id`, (req: Request, res: Response) => { - const searchData = data.films.filter((v: any) => v.id == req.params.id); + let searchData = []; + if (req.params.id.split("-").length == 5) { + searchData = data.films.filter((f: any) => f.id == req.params.id); + } else { + searchData = data.films.filter((f: any) => + f.title.includes(req.params.id) + ); + } - res.json(searchData); + if (searchData.length == 0) { + res.send("null"); + } else { + res.json(searchData); + } }); return api; diff --git a/src/routes/locations/.route.ts b/src/routes/locations/.route.ts index 1c19fb9..3392619 100644 --- a/src/routes/locations/.route.ts +++ b/src/routes/locations/.route.ts @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}:id`, (req: Request, res: Response) => { - const searchData = data.locations.filter((v: any) => v.id == req.params.id); + let searchData = []; + if (req.params.id.split("-").length == 5) { + searchData = data.locations.filter((l: any) => l.id == req.params.id); + } else { + searchData = data.locations.filter((l: any) => + l.name.includes(req.params.id) + ); + } - res.json(searchData); + if (searchData.length == 0) { + res.send("null"); + } else { + res.json(searchData); + } }); return api; diff --git a/src/routes/people/.route.ts b/src/routes/people/.route.ts index fecfe7a..bffe22b 100644 --- a/src/routes/people/.route.ts +++ b/src/routes/people/.route.ts @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}:id`, (req: Request, res: Response) => { - const searchData = data.people.filter((v: any) => v.id == req.params.id); + let searchData = []; + if (req.params.id.split("-").length == 5) { + searchData = data.people.filter((p: any) => p.id == req.params.id); + } else { + searchData = data.people.filter((p: any) => + p.name.includes(req.params.id) + ); + } - res.json(searchData); + if (searchData.length == 0) { + res.send("null"); + } else { + res.json(searchData); + } }); return api; diff --git a/src/routes/species/.route.ts b/src/routes/species/.route.ts index 1f74147..150f460 100644 --- a/src/routes/species/.route.ts +++ b/src/routes/species/.route.ts @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}:id`, (req: Request, res: Response) => { - const searchData = data.species.filter((v: any) => v.id == req.params.id); + let searchData = []; + if (req.params.id.split("-").length == 5) { + searchData = data.species.filter((s: any) => s.id == req.params.id); + } else { + searchData = data.species.filter((s: any) => + s.name.includes(req.params.id) + ); + } - res.json(searchData); + if (searchData.length == 0) { + res.send("null"); + } else { + res.json(searchData); + } }); return api; diff --git a/src/routes/vehicles/.route.ts b/src/routes/vehicles/.route.ts index 5f679e7..81f6937 100644 --- a/src/routes/vehicles/.route.ts +++ b/src/routes/vehicles/.route.ts @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => { // eslint-disable-next-line @typescript-eslint/no-unused-vars api.get(`/${urlName}:id`, (req: Request, res: Response) => { - const searchData = data.vehicles.filter((v: any) => v.id == req.params.id); + let searchData = []; + if (req.params.id.split("-").length == 5) { + searchData = data.vehicles.filter((v: any) => v.id == req.params.id); + } else { + searchData = data.vehicles.filter((v: any) => + v.name.includes(req.params.id) + ); + } - res.json(searchData); + if (searchData.length == 0) { + res.send("null"); + } else { + res.json(searchData); + } }); return api; diff --git a/src/util/Middleware.ts b/src/util/Middleware.ts index 939189b..c6d473a 100644 --- a/src/util/Middleware.ts +++ b/src/util/Middleware.ts @@ -1,14 +1,18 @@ /* Import packages */ const chalk = require("chalk"); -import { Express, Request, Response, NextFunction } from "express"; +import express, { Express, Request, Response, NextFunction } from "express"; import bodyParser from "body-parser"; import cors from "cors"; +import { join } from "path"; // Export middleware export default (api: Express): Express => { // Set trust proxy api.set("trust proxy", 1); + // Static files + api.use("/assets", express.static(join(__dirname, "..", "assets"))); + // Parse body api.use(bodyParser.json()); api.use(bodyParser.urlencoded({ extended: true })); @@ -38,11 +42,11 @@ export default (api: Express): Express => { } // Set port - api.listen(3001, () => { + api.listen(6543, () => { console.log( chalk.red("API") + chalk.grey(" | ") + - chalk.white(`Listening on port ${chalk.greenBright("3001")}`) + chalk.white(`Listening on port ${chalk.greenBright("6543")}`) ); }); diff --git a/src/util/RoutesManager.ts b/src/util/RoutesManager.ts index 3e597ab..305cf35 100644 --- a/src/util/RoutesManager.ts +++ b/src/util/RoutesManager.ts @@ -8,8 +8,6 @@ const chalk = require("chalk"); export default (): Router => { const api: Router = Router(); - api.use("/assets", express.static(join(__dirname, "..", "assets"))); - // Read route directory const routeDir = readdirSync(join(__dirname, "..", "routes")); const dataRaw = readFileSync(join(__dirname, "data", "data.json"));