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

Add eslint to backend #404

Merged
merged 19 commits into from
Jan 26, 2024
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
31 changes: 31 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Lint

on:
pull_request:
branches:
- master

jobs:
lint:
name: Lint and check formatting
runs-on: ubuntu-latest

concurrency:
group: lint-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

steps:
- name: Check Out Repo
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'

- name: Install Dependencies
run: npm install

- name: Lint
run: npm run lint
10 changes: 10 additions & 0 deletions api/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
root: true,
env: { node: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
};
2,763 changes: 2,107 additions & 656 deletions api/package-lock.json

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
"scripts": {
"start": "node dist/app.js",
"dev": "tsc-watch --onSuccess \"npm start\"",
"build": "tsc"
"build": "tsc",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
},
"dependencies": {
"@vendia/serverless-express": "^4.10.1",
"axios": "^1.6.5",
"connect-mongodb-session": "^3.1.1",
"connect-mongodb-session": "^5.0.0",
"cookie-parser": "^1.4.5",
"dotenv-flow": "^4.1.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
"mongodb": "^3.6.6",
"mongodb": "^6.3.0",
"mongodb-client-encryption": "^6.0.0",
"morgan": "^1.10.0",
"node-fetch": "^2.6.1",
"passport": "^0.6.0",
"passport": "^0.7.0",
"passport-google-oauth": "^2.0.0",
"typescript": "^5.3.3"
},
Expand All @@ -30,9 +30,11 @@
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/morgan": "^1.9.3",
"@types/node-fetch": "^2.5.12",
"@types/passport": "^1.0.7",
"@types/passport-google-oauth": "^1.0.42",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"eslint": "^8.56.0",
"tsc-watch": "^6.0.4"
}
}
6 changes: 3 additions & 3 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import { SESSION_LENGTH } from './config/constants';
const app = express();

// Setup mongo store for sessions
let mongoStore = MongoDBStore(session);
const mongoStore = MongoDBStore(session);

if (process.env.MONGO_URL) {
let store = new mongoStore({
const store = new mongoStore({
uri: process.env.MONGO_URL,
databaseName: DB_NAME,
collection: COLLECTION_NAMES.SESSIONS,
Expand Down Expand Up @@ -91,7 +91,7 @@ app.use('/api', router);
/**
* Error Handler
*/
app.use(function (req, res, next) {
app.use(function (req, res) {
console.error(req);
res.status(500).json({ error: `Internal Serverless Error - '${req}'` });
});
Expand Down
5 changes: 3 additions & 2 deletions api/src/config/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
@module PassportConfig
*/

import { User } from 'express-session';
import passport from 'passport';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';

passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function (user: any, done) {
passport.deserializeUser(function (user: false | User | null | undefined, done) {
done(null, user);
});

Expand All @@ -29,7 +30,7 @@ passport.use(
if (profile.emails && profile.emails.length! > 0) {
email = profile.emails[0].value;
}
var userData = {
const userData = {
id: profile.id,
email: email,
name: profile.displayName,
Expand Down
21 changes: 10 additions & 11 deletions api/src/controllers/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
*/

import express, { Request } from 'express';
import fetch from 'node-fetch';
import { getCourseQuery } from '../helpers/gql';
var router = express.Router();
const router = express.Router();

/**
* PPAPI proxy for course data
*/
router.get('/api', (req: Request<{}, {}, {}, { courseID: string }>, res) => {
let r = fetch(process.env.PUBLIC_API_URL + 'courses/' + encodeURIComponent(req.query.courseID), {
router.get('/api', (req: Request<never, unknown, never, { courseID: string }, never>, res) => {
const r = fetch(process.env.PUBLIC_API_URL + 'courses/' + encodeURIComponent(req.query.courseID), {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Expand All @@ -25,11 +24,11 @@ router.get('/api', (req: Request<{}, {}, {}, { courseID: string }>, res) => {
/**
* PPAPI proxy for course data
*/
router.post('/api/batch', (req: Request<{}, {}, { courses: string[] }>, res) => {
router.post('/api/batch', (req: Request<never, unknown, { courses: string[] }, never>, res) => {
if (req.body.courses.length == 0) {
res.json({});
} else {
let r = fetch(process.env.PUBLIC_API_GRAPHQL_URL, {
const r = fetch(process.env.PUBLIC_API_GRAPHQL_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -42,9 +41,9 @@ router.post('/api/batch', (req: Request<{}, {}, { courses: string[] }>, res) =>
r.then((response) => response.json()).then((data) =>
res.json(
Object.fromEntries(
Object.entries(data.data)
.filter(([_, x]) => x !== null)
.map(([_, x]) => [(x as { id: string }).id, x]),
Object.values(data.data)
.filter((x) => x !== null)
.map((x) => [(x as { id: string }).id, x]),
),
),
);
Expand All @@ -54,8 +53,8 @@ router.post('/api/batch', (req: Request<{}, {}, { courses: string[] }>, res) =>
/**
* PPAPI proxy for grade distribution
*/
router.get('/api/grades', (req: Request<{}, {}, {}, { department: string; number: string }>, res) => {
let r = fetch(
router.get('/api/grades', (req: Request<never, unknown, never, { department: string; number: string }>, res) => {
const r = fetch(
process.env.PUBLIC_API_URL +
'grades/raw?department=' +
encodeURIComponent(req.query.department) +
Expand Down
15 changes: 7 additions & 8 deletions api/src/controllers/professors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
*/

import express, { Request } from 'express';
import fetch from 'node-fetch';
import { getProfessorQuery } from '../helpers/gql';

var router = express.Router();
const router = express.Router();

/**
* PPAPI proxy for professor data
*/
router.get('/api', function (req: Request<{}, {}, {}, { ucinetid: string }>, res) {
let r = fetch(process.env.PUBLIC_API_URL + 'instructors/' + req.query.ucinetid);
router.get('/api', function (req: Request<never, unknown, never, { ucinetid: string }>, res) {
const r = fetch(process.env.PUBLIC_API_URL + 'instructors/' + req.query.ucinetid);

r.then((response) => response.json()).then((data) => res.send(data));
});

/**
* PPAPI proxy for professor data
*/
router.post('/api/batch', (req: Request<{}, {}, { professors: string[] }>, res) => {
router.post('/api/batch', (req: Request<never, unknown, { professors: string[] }, never>, res) => {
if (req.body.professors.length == 0) {
res.json({});
} else {
let r = fetch(process.env.PUBLIC_API_GRAPHQL_URL, {
const r = fetch(process.env.PUBLIC_API_GRAPHQL_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -41,8 +40,8 @@ router.post('/api/batch', (req: Request<{}, {}, { professors: string[] }>, res)
/**
* PPAPI proxy for grade distribution
*/
router.get('/api/grades/:name', function (req, res, next) {
let r = fetch(process.env.PUBLIC_API_URL + 'grades/raw?instructor=' + encodeURIComponent(req.params.name));
router.get('/api/grades/:name', function (req, res) {
const r = fetch(process.env.PUBLIC_API_URL + 'grades/raw?instructor=' + encodeURIComponent(req.params.name));

let status = 200;

Expand Down
25 changes: 9 additions & 16 deletions api/src/controllers/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@
*/

import express from 'express';
import { ObjectID } from 'mongodb';
import {
COLLECTION_NAMES,
getCollection,
addDocument,
getDocuments,
deleteDocument,
deleteDocuments,
} from '../helpers/mongo';
import { ObjectId } from 'mongodb';
import { COLLECTION_NAMES, addDocument, getDocuments, deleteDocument, deleteDocuments } from '../helpers/mongo';
import { GenericObject } from '../types/types';

var router = express.Router();
const router = express.Router();

/**
* Get all reports
*/
router.get('/', async (req, res, next) => {
let reports = await getDocuments(COLLECTION_NAMES.REPORTS, {}); // get all reports in collection
router.get('/', async (req, res) => {
const reports = await getDocuments(COLLECTION_NAMES.REPORTS, {}); // get all reports in collection

res.json(reports);
});

/**
* Add a report
*/
router.post('/', async (req, res, next) => {
router.post('/', async (req, res) => {
console.log(`Adding Report: ${JSON.stringify(req.body)}`);

await addDocument(COLLECTION_NAMES.REPORTS, req.body);
Expand All @@ -39,16 +32,16 @@ router.post('/', async (req, res, next) => {
/**
* Delete a report
*/
router.delete('/', async (req, res, next) => {
router.delete('/', async (req, res) => {
let status;
if (req.body.id) {
console.log(`Deleting report ${req.body.id}`);
status = await deleteDocument(COLLECTION_NAMES.REPORTS, {
_id: new ObjectID(req.body.id),
_id: new ObjectId(req.body.id),
});
} else {
console.log(`Deleting reports with reviewID ${req.body.reviewID}`);
let query: GenericObject = {};
const query: GenericObject = {};
if (req.body.reviewID) query['reviewID'] = req.body.reviewID;

if (Object.keys(query).length === 0) return; // avoid deleting all documents if no filters are specified
Expand Down
Loading
Loading