From 9ffb67ddffc548446e3e2873780a087005eea4b8 Mon Sep 17 00:00:00 2001 From: ogomez-at-wiris Date: Sun, 20 Feb 2022 15:22:18 +0100 Subject: [PATCH] refactor: Move pack to its own interface --- build/interfaces/pack.js | 2 + build/pack_api.js | 12 ++++ build/routes/packs.js | 10 ++++ build/server.js | 45 ++++++++++++++ build/services/pack_service.js | 17 ++++++ package.json | 3 +- src/interfaces/pack.ts | 6 ++ src/pack_api.ts | 14 +++++ src/routes/packs.ts | 11 ++-- src/server.ts | 103 +++++++++++++++++---------------- src/services/pack_service.ts | 44 +++++--------- yarn.lock | 5 ++ 12 files changed, 187 insertions(+), 85 deletions(-) create mode 100644 build/interfaces/pack.js create mode 100644 build/pack_api.js create mode 100644 build/routes/packs.js create mode 100644 build/server.js create mode 100644 build/services/pack_service.js create mode 100644 src/interfaces/pack.ts create mode 100644 src/pack_api.ts diff --git a/build/interfaces/pack.js b/build/interfaces/pack.js new file mode 100644 index 0000000..ce03781 --- /dev/null +++ b/build/interfaces/pack.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/build/pack_api.js b/build/pack_api.js new file mode 100644 index 0000000..6d68856 --- /dev/null +++ b/build/pack_api.js @@ -0,0 +1,12 @@ +"use strict"; +var _a; +Object.defineProperty(exports, "__esModule", { value: true }); +class PackAPI { +} +exports.default = PackAPI; +_a = PackAPI; +PackAPI.getPacks = async (req, res, next) => { + return res.status(200).json({ + message: "hello all packs", + }); +}; diff --git a/build/routes/packs.js b/build/routes/packs.js new file mode 100644 index 0000000..5a39498 --- /dev/null +++ b/build/routes/packs.js @@ -0,0 +1,10 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.packRoutes = void 0; +const express_1 = __importDefault(require("express")); +const pack_api_1 = __importDefault(require("../pack_api")); +exports.packRoutes = express_1.default.Router(); +exports.packRoutes.get("/packs", pack_api_1.default.getPacks); diff --git a/build/server.js b/build/server.js new file mode 100644 index 0000000..d7cc4bf --- /dev/null +++ b/build/server.js @@ -0,0 +1,45 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const http_1 = __importDefault(require("http")); +const express_1 = __importDefault(require("express")); +const morgan_1 = __importDefault(require("morgan")); +const packs_1 = require("./routes/packs"); +const pack_service_1 = __importDefault(require("./services/pack_service")); +const router = (0, express_1.default)(); +/** Logging */ +router.use((0, morgan_1.default)("dev")); +/** Parse the request */ +router.use(express_1.default.urlencoded({ extended: false })); +/** Takes care of JSON data */ +router.use(express_1.default.json()); +/** RULES OF OUR API */ +router.use((req, res, next) => { + // set the CORS policy + res.header("Access-Control-Allow-Origin", "*"); + // set the CORS headers + res.header("Access-Control-Allow-Headers", "origin, X-Requested-With,Content-Type,Accept, Authorization"); + // set the CORS method headers + if (req.method === "OPTIONS") { + res.header("Access-Control-Allow-Methods", "GET POST"); + return res.status(200).json({}); + } + next(); +}); +/** Routes */ +router.use("/", packs_1.packRoutes); +/** Error handling */ +router.use((req, res, next) => { + const error = new Error("not found"); + return res.status(404).json({ + message: error.message, + }); +}); +/** Server */ +const httpServer = http_1.default.createServer(router); +const PORT = process.env.PORT ?? 6060; +pack_service_1.default.getInitialList().then(() => { + httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`)); +}); diff --git a/build/services/pack_service.js b/build/services/pack_service.js new file mode 100644 index 0000000..c3970f8 --- /dev/null +++ b/build/services/pack_service.js @@ -0,0 +1,17 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +var _a; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs_1 = __importDefault(require("fs")); +const PACK_BASE_PATH = "packs"; +class PackService { +} +exports.default = PackService; +_a = PackService; +PackService.getPackList = () => _a.packList; +PackService.getInitialList = async () => { + const packs = await fs_1.default.readdirSync(PACK_BASE_PATH); + packs.forEach((pack) => { }); +}; diff --git a/package.json b/package.json index b70a6ca..3341673 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@types/axios": "^0.14.0", "@types/express": "^4.17.13", "@types/morgan": "^1.9.3", - "@types/node": "^17.0.18" + "@types/node": "^17.0.18", + "prettier": "^2.5.1" } } diff --git a/src/interfaces/pack.ts b/src/interfaces/pack.ts new file mode 100644 index 0000000..e3f00c3 --- /dev/null +++ b/src/interfaces/pack.ts @@ -0,0 +1,6 @@ +interface Pack { + name: string; + size: number; + bpm: string; +} +export default Pack; diff --git a/src/pack_api.ts b/src/pack_api.ts new file mode 100644 index 0000000..526cc43 --- /dev/null +++ b/src/pack_api.ts @@ -0,0 +1,14 @@ +import { Request, Response, NextFunction } from "express"; +import Pack from "./interfaces/pack"; + +export default class PackAPI { + public static getPacks = async ( + req: Request, + res: Response, + next: NextFunction + ) => { + return res.status(200).json({ + message: "hello all packs", + }); + }; +} diff --git a/src/routes/packs.ts b/src/routes/packs.ts index ff2c41a..e0aa66e 100644 --- a/src/routes/packs.ts +++ b/src/routes/packs.ts @@ -1,5 +1,6 @@ -import express from 'express'; -import PackService from '../services/pack_service'; -export const packRoutes = express.Router(); - -packRoutes.get('/packs', PackService.getPacks); +import express from "express"; +import PackAPI from "../pack_api"; + +export const packRoutes = express.Router(); + +packRoutes.get("/packs", PackAPI.getPacks); diff --git a/src/server.ts b/src/server.ts index 2bbbf4b..9a14629 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,51 +1,52 @@ -import http from "http"; -import express, { Express } from "express"; -import morgan from "morgan"; -import { packRoutes } from "./routes/packs"; -import PackService from "./services/pack_service"; - -const router: Express = express(); - -/** Logging */ -router.use(morgan("dev")); -/** Parse the request */ -router.use(express.urlencoded({ extended: false })); -/** Takes care of JSON data */ -router.use(express.json()); - -/** RULES OF OUR API */ -router.use((req, res, next) => { - // set the CORS policy - res.header("Access-Control-Allow-Origin", "*"); - // set the CORS headers - res.header( - "Access-Control-Allow-Headers", - "origin, X-Requested-With,Content-Type,Accept, Authorization" - ); - // set the CORS method headers - if (req.method === "OPTIONS") { - res.header("Access-Control-Allow-Methods", "GET POST"); - return res.status(200).json({}); - } - next(); -}); - -/** Routes */ -router.use("/", packRoutes); - -/** Error handling */ -router.use((req, res, next) => { - const error = new Error("not found"); - return res.status(404).json({ - message: error.message, - }); -}); - -/** Server */ -const httpServer = http.createServer(router); -const PORT: any = process.env.PORT ?? 6060; -PackService.getInitialList().then(() => { - httpServer.listen(PORT, () => - console.log(`The server is running on port ${PORT}`) - ); -}); +import http from "http"; +import express, { Express } from "express"; +import morgan from "morgan"; +import { packRoutes } from "./routes/packs"; +import PackAPI from "./pack_api"; +import PackService from "./services/pack_service"; + +const router: Express = express(); + +/** Logging */ +router.use(morgan("dev")); +/** Parse the request */ +router.use(express.urlencoded({ extended: false })); +/** Takes care of JSON data */ +router.use(express.json()); + +/** RULES OF OUR API */ +router.use((req, res, next) => { + // set the CORS policy + res.header("Access-Control-Allow-Origin", "*"); + // set the CORS headers + res.header( + "Access-Control-Allow-Headers", + "origin, X-Requested-With,Content-Type,Accept, Authorization" + ); + // set the CORS method headers + if (req.method === "OPTIONS") { + res.header("Access-Control-Allow-Methods", "GET POST"); + return res.status(200).json({}); + } + next(); +}); + +/** Routes */ +router.use("/", packRoutes); + +/** Error handling */ +router.use((req, res, next) => { + const error = new Error("not found"); + return res.status(404).json({ + message: error.message, + }); +}); + +/** Server */ +const httpServer = http.createServer(router); +const PORT: any = process.env.PORT ?? 6060; +PackService.getInitialList().then(() => { + httpServer.listen(PORT, () => + console.log(`The server is running on port ${PORT}`) + ); +}); diff --git a/src/services/pack_service.ts b/src/services/pack_service.ts index 45fc267..62bca66 100644 --- a/src/services/pack_service.ts +++ b/src/services/pack_service.ts @@ -1,28 +1,16 @@ -import { Request, Response, NextFunction } from "express"; -import fs from "fs"; -import * as fsWalk from "@nodelib/fs.walk"; -const PACK_BASE_PATH = ''; - -interface Pack { - name: string; - size: number; - bpm: string; -} - -export default class PackService { - public static getInitialList = async () => { - const packs = await fs.readdirSync(PACK_BASE_PATH+"packs"); - console.log(packs); - }; - - public static getPacks = async ( - req: Request, - res: Response, - next: NextFunction - ) => { - // get a list of all packs... - return res.status(200).json({ - message: "hello all packs", - }); - }; -} +import Pack from "../interfaces/pack"; +import fs from "fs"; +import * as fsWalk from "@nodelib/fs.walk"; + +const PACK_BASE_PATH = "packs"; + +export default class PackService { + private static packList: [Pack]; + + public static getPackList = (): Array => this.packList; + + public static getInitialList = async () => { + const packs = await fs.readdirSync(PACK_BASE_PATH); + packs.forEach((pack) => {}); + }; +} diff --git a/yarn.lock b/yarn.lock index cc09bd6..66a7b20 100644 --- a/yarn.lock +++ b/yarn.lock @@ -981,6 +981,11 @@ prepend-http@^2.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897" integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc= +prettier@^2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a" + integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg== + proxy-addr@~2.0.7: version "2.0.7" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"