refactor: Move pack to its own interface

master
ogomez-at-wiris 2022-02-20 15:22:18 +01:00
parent 9ea34bdc30
commit 9ffb67ddff
12 changed files with 187 additions and 85 deletions

2
build/interfaces/pack.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

12
build/pack_api.js Normal file
View File

@ -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",
});
};

10
build/routes/packs.js Normal file
View File

@ -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);

45
build/server.js Normal file
View File

@ -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}`));
});

View File

@ -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) => { });
};

View File

@ -22,6 +22,7 @@
"@types/axios": "^0.14.0", "@types/axios": "^0.14.0",
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"@types/morgan": "^1.9.3", "@types/morgan": "^1.9.3",
"@types/node": "^17.0.18" "@types/node": "^17.0.18",
"prettier": "^2.5.1"
} }
} }

6
src/interfaces/pack.ts Normal file
View File

@ -0,0 +1,6 @@
interface Pack {
name: string;
size: number;
bpm: string;
}
export default Pack;

14
src/pack_api.ts Normal file
View File

@ -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",
});
};
}

View File

@ -1,5 +1,6 @@
import express from 'express'; import express from "express";
import PackService from '../services/pack_service'; import PackAPI from "../pack_api";
export const packRoutes = express.Router(); export const packRoutes = express.Router();
packRoutes.get('/packs', PackService.getPacks); packRoutes.get("/packs", PackAPI.getPacks);

View File

@ -2,6 +2,7 @@ import http from "http";
import express, { Express } from "express"; import express, { Express } from "express";
import morgan from "morgan"; import morgan from "morgan";
import { packRoutes } from "./routes/packs"; import { packRoutes } from "./routes/packs";
import PackAPI from "./pack_api";
import PackService from "./services/pack_service"; import PackService from "./services/pack_service";
const router: Express = express(); const router: Express = express();

View File

@ -1,28 +1,16 @@
import { Request, Response, NextFunction } from "express"; import Pack from "../interfaces/pack";
import fs from "fs"; import fs from "fs";
import * as fsWalk from "@nodelib/fs.walk"; import * as fsWalk from "@nodelib/fs.walk";
const PACK_BASE_PATH = '';
interface Pack { const PACK_BASE_PATH = "packs";
name: string;
size: number;
bpm: string;
}
export default class PackService { export default class PackService {
public static getInitialList = async () => { private static packList: [Pack];
const packs = await fs.readdirSync(PACK_BASE_PATH+"packs");
console.log(packs);
};
public static getPacks = async ( public static getPackList = (): Array<Pack> => this.packList;
req: Request,
res: Response, public static getInitialList = async () => {
next: NextFunction const packs = await fs.readdirSync(PACK_BASE_PATH);
) => { packs.forEach((pack) => {});
// get a list of all packs...
return res.status(200).json({
message: "hello all packs",
});
}; };
} }

View File

@ -981,6 +981,11 @@ prepend-http@^2.0.0:
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897" resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc= 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: proxy-addr@~2.0.7:
version "2.0.7" version "2.0.7"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"