Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change codebase to TypeScript #14

Merged
merged 18 commits into from
Dec 19, 2019
Merged
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bower_components

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
dist/

# Dependency directories
node_modules/
Expand Down
7 changes: 7 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"watch": "src/**/*.ts",
"execMap": {
"ts": "ts-node"
},
"exec": "ts-node -r tsconfig-paths/register --cacheDirectory tmp/tscache src/server.ts"
}
7,002 changes: 0 additions & 7,002 deletions package-lock.json

This file was deleted.

36 changes: 22 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "This repo serves as the backend for the Developer Circles Eldoret Community website.",
"main": "server.js",
"scripts": {
"start": "nodemon --require @babel/register --require @babel/register ./src/server.js",
"test": "nyc mocha --recursive --require @babel/register --require @babel/polyfill --exit ./src/test/*.test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"dev": "nodemon --exec \"ts-node\" ./src/server.ts",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"test": "nyc mocha -r ts-node/register --exit ./src/test/*.test.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"build": "tsc --build tsconfig.json"
},
"repository": {
"type": "git",
Expand All @@ -20,33 +21,40 @@
},
"homepage": "",
"dependencies": {
"@babel/polyfill": "^7.7.0",
"@types/pg": "^7.11.2",
"@types/supertest": "^2.0.8",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"cors": "^2.8.4",
"custom-env": "^1.0.2",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"dotenv-config": "^0.1.1",
"express": "^4.17.1",
"nodemon": "^1.19.4",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"pg": "^7.14.0"
miami78 marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@babel/register": "^7.7.0",
"@types/chai": "^4.2.7",
"@types/express": "^4.17.2",
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.17",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"coveralls": "^3.0.7",
"eslint": "^6.6.0",
"eslint-config-airbnb": "^18.0.1",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-node": "^4.0.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-react": "^7.17.0",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"mocha": "^6.2.2",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"supertest": "^4.0.2"
"supertest": "^4.0.2",
"ts-node": "^8.5.4",
"tslint": "^5.20.1",
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^3.7.3"
}
}
9 changes: 9 additions & 0 deletions src/DB/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.getEvents = {
miami78 marked this conversation as resolved.
Show resolved Hide resolved
id: 1,
title: 'Community Challenge',
eventLocation: 'Sirikwa Hotel',
miami78 marked this conversation as resolved.
Show resolved Hide resolved
startTime: '2019-08-15 21:05:15.723336+07',
endTime: '2019-08-15 21:05:15.723336+07',
media: 'https://photos.app.goo.gl/323EcdkmFTzrfLd96',
miami78 marked this conversation as resolved.
Show resolved Hide resolved
speakers: 'Marvin Kweyu'
miami78 marked this conversation as resolved.
Show resolved Hide resolved
};
16 changes: 16 additions & 0 deletions src/DB/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// import { Pool } from "pg";
miami78 marked this conversation as resolved.
Show resolved Hide resolved

// const pool = new Pool({
// //ignore this im getting stuck on destructuring and passing my env variables with Typescript
// connectionString: "postgres://postgres:50filthyCENT!@localhost:5432/devceldoret"
// });

// class Database {
// static async query(query:any, value:any, isArray = false) {
// const response = await pool.query(query, value);
// const result = isArray ? response.rows : response.rows[0];
// return result;
// }
// }

// export default Database;
10 changes: 10 additions & 0 deletions src/Utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ErrorHandler extends Error {
status: number;
constructor(message:string, status:number) {
super(message);
this.status = status;
}
}

export default ErrorHandler;

18 changes: 0 additions & 18 deletions src/Utils/helper.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/Utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require("dotenv").config({ path: `${__dirname}/.env` });
const pg = require("pg");

const {
APP_ENV,
DB_HOST,
DB_USER,
DB_PASS
} = process.env;

if (process.env.NODE_ENV === "development")
console.log(process.env.APP_ENV)
miami78 marked this conversation as resolved.
Show resolved Hide resolved

module.exports = new pg.Pool();
18 changes: 18 additions & 0 deletions src/controllers/eventController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// import EventModel from "../models/events";
miami78 marked this conversation as resolved.
Show resolved Hide resolved

// class EventController {
miami78 marked this conversation as resolved.
Show resolved Hide resolved
// static async getEvents(req:any, res:any, next:any) {
// try {
// const events = await EventModel.getEvents(1,"Community Challenge","Sirikwa Hotel",'2019-08-15 21:05:15.723336+07','2019-08-15 21:05:15.723336+07',"https://photos.app.goo.gl/323EcdkmFTzrfLd96","Marvin Kweyu", "chksavjhcb");
// const data = [...events];
// res.status(200).json({
// status: "success",
// data
// });
// } catch (error) {
// next(error);
// }
// }
// }

// export default EventController;
19 changes: 19 additions & 0 deletions src/models/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import Database from "../DB/index";
// import ErrorHandler from "../Utils/errorHandler";

// class GetEvent {
// static async getEvents( id:number, title:string, eventLocation:string, startTime:any, endTime:any, media:string, speakers:string, eventInfo:string ) {
// const values = [id, title, eventLocation, startTime, endTime, media, speakers , eventInfo];
// const response = await Database.query('SELECT * FROM events WHERE id = $1', values).catch(
// error => {
// throw new ErrorHandler(error.message, 400);
// }
// );
// if (!response) {
// throw new Error("Event does not exist");
// }
// return response;
// }
// }

// export default GetEvent;
8 changes: 8 additions & 0 deletions src/routes/eventRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";
miami78 marked this conversation as resolved.
Show resolved Hide resolved
// import EventsController from "../controllers/eventController";

const router = Router();

// router.get("/events", EventsController.getEvents);
miami78 marked this conversation as resolved.
Show resolved Hide resolved

export default router;
15 changes: 10 additions & 5 deletions src/server.js → src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "@babel/polyfill";
require('custom-env').env()
import express from "express";
import bodyParser from "body-parser";
import helper from "./Utils/helper";
import EventRoute from "./routes/eventRoute";
const helper = require('./Utils/helper')

const app = express();

Expand All @@ -13,6 +14,10 @@ app.get("/", (request, response) => {
response.json({ message: "Server starts successfully!" });
});

// routes
app.use("/api/v1", EventRoute);
miami78 marked this conversation as resolved.
Show resolved Hide resolved

// CORS
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-type");
Expand All @@ -23,9 +28,9 @@ app.use((req, res, next) => {
next();
});

app.set("port", helper.PORT);
app.listen(helper.PORT, () => {
console.log("app is running on port ", helper.PORT);
app.set("port", process.env.PORT || "3000");
app.listen(process.env.PORT || "3000", () => {
console.log("app is running on port ", process.env.PORT || "3000");
});

export default app;
46 changes: 46 additions & 0 deletions src/test/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// import { expect } from "chai";

// import request from "supertest";

// const event = require("../DB/db.js");
// const Database = require ("../DB/index");

// import app from "../server";

// describe("DevC", () => {
// // gets json response
// describe("GET events", () => {
// let id;
// let title;
// before(done => {
// Database.query(
// // Insert default Event into table events
// "INSERT INTO events (id, title) VALUES ($1, $2) RETURNING id",
// [event.getEvents.id, event.getEvents.title]
// ).then((value: { id: number,title:string; }) => {
// id = value.id;
// title = value.title;
// done();
// });
// });
// it("gets event information with the id of 1", done => {
// request(app)
// .get("/api/v1/events")
// .set("Content-Type", "application/json")
// .then(res => {
// const {
// body: {
// status,
// data: { value }
// }
// } = res;
// expect(res.status).to.equal(200);
// expect(status).to.equal("success");
// expect(value).to.have.property('id');
// expect(value).to.have.property('title');
// done();
// })
// .catch(error => done(error));
// });
// });
// });
2 changes: 1 addition & 1 deletion src/test/server.test.js → src/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import request from "supertest";

import app from "../server";

describe("Teamwork", () => {
describe("DevC", () => {
// gets json response
describe("GET /", () => {
it("responds with json", done => {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
}
9 changes: 9 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
miami78 marked this conversation as resolved.
Show resolved Hide resolved
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {},
"rules": {
"no-console": [false]
},
"rulesDirectory": []
}
Loading