Skip to content

Commit

Permalink
🏗️ build: set eslint and prettier rules
Browse files Browse the repository at this point in the history
  • Loading branch information
agftech committed Oct 25, 2020
1 parent bcb4a62 commit b1abd6b
Show file tree
Hide file tree
Showing 18 changed files with 1,743 additions and 312 deletions.
16 changes: 16 additions & 0 deletions backend/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 100
trim_trailing_whitespace = true
quote_type = single

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions backend/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*.js
node_modules
dist
44 changes: 44 additions & 0 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"prettier/prettier": "error",
"class-methods-use-this": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"camelcase": "off",
"import/prefer-default-export": "off"
},
"overrides": [
{
"files": ["*.js", "*.jsx"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/explicit-module-boundary-types": ["warn"],
"@typescript-eslint/explicit-module-boundary-types": ["error"]
}
}
]
}
66 changes: 41 additions & 25 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"multer": "^1.4.2",
"sqlite3": "^5.0.0",
"typeorm": "^0.2.28",
"yup": "^0.29.3"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/multer": "^1.4.4",
"@types/yup": "^0.29.8",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js",
"lint": "eslint --debug src/",
"lint:write": "eslint --debug src/ --fix",
"prettier": "prettier --write src/**/*.ts*"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"multer": "^1.4.2",
"sqlite3": "^5.0.0",
"typeorm": "^0.2.28",
"yup": "^0.29.3"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/multer": "^1.4.4",
"@types/yup": "^0.29.8",
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"eslint": "7.2.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-prettier": "^6.12.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.1.2",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
9 changes: 9 additions & 0 deletions backend/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
printWidth: 100,
singleQuote: true,
trailingComma: 'all',
bracketSpacing: true,
jsxBracketSameLine: false,
tabWidth: 2,
semi: true,
};
18 changes: 9 additions & 9 deletions backend/src/config/upload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import multer from "multer";
import path from "path";
import multer from 'multer';
import path from 'path';

export default {
storage: multer.diskStorage({
destination: path.join(__dirname, "..", "..", "uploads"),
filename: (_, file, cb) => {
const fileName = `${Date.now()}-${file.originalname}`;
cb(null, fileName);
},
}),
storage: multer.diskStorage({
destination: path.join(__dirname, '..', '..', 'uploads'),
filename: (_, file, cb) => {
const fileName = `${Date.now()}-${file.originalname}`;
cb(null, fileName);
},
}),
};
132 changes: 66 additions & 66 deletions backend/src/controllers/OrphanagesController.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
import { Request, Response } from "express";
import { getRepository } from "typeorm";
import * as Yup from "yup";
import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import * as Yup from 'yup';

import Orphanage from "../models/Orphanage";
import Orphanage from '../models/Orphanage';

import orphanageView from "../views/orphanages_view";
import orphanageView from '../views/orphanages_view';

export default {
async show(req: Request, res: Response) {
const { id } = req.params;
const orphanagesRepository = getRepository(Orphanage);
const orphanageDetail = await orphanagesRepository.findOneOrFail(id, {
relations: ["images"],
});
return res.json(orphanageView.render(orphanageDetail));
},
async index(req: Request, res: Response) {
const orphanagesRepository = getRepository(Orphanage);
const orphanages = await orphanagesRepository.find({
relations: ["images"],
});
return res.json(orphanageView.renderMany(orphanages));
},
async show(req: Request, res: Response) {
const { id } = req.params;
const orphanagesRepository = getRepository(Orphanage);
const orphanageDetail = await orphanagesRepository.findOneOrFail(id, {
relations: ['images'],
});
return res.json(orphanageView.render(orphanageDetail));
},
async index(req: Request, res: Response) {
const orphanagesRepository = getRepository(Orphanage);
const orphanages = await orphanagesRepository.find({
relations: ['images'],
});
return res.json(orphanageView.renderMany(orphanages));
},

async create(req: Request, res: Response) {
const {
name,
latitude,
longitude,
about,
instructions,
opening_hours,
open_on_weekends,
} = req.body;
async create(req: Request, res: Response) {
const {
name,
latitude,
longitude,
about,
instructions,
opening_hours,
open_on_weekends,
} = req.body;

const orphanagesRepository = getRepository(Orphanage);
const orphanagesRepository = getRepository(Orphanage);

const requestImages = req.files as Express.Multer.File[];
const requestImages = req.files as Express.Multer.File[];

const images = requestImages.map((image) => {
return { path: image.filename };
});
const images = requestImages.map((image) => {
return { path: image.filename };
});

const data = {
name,
latitude,
longitude,
about,
instructions,
opening_hours,
open_on_weekends: open_on_weekends === "true",
images,
};
const data = {
name,
latitude,
longitude,
about,
instructions,
opening_hours,
open_on_weekends: open_on_weekends === 'true',
images,
};

const schema = Yup.object().shape({
name: Yup.string().required(),
latitude: Yup.number().required(),
longitude: Yup.number().required(),
about: Yup.string().required().max(300),
instructions: Yup.string().required(),
opening_hours: Yup.string().required(),
open_on_weekends: Yup.boolean().required(),
images: Yup.array(
Yup.object().shape({
path: Yup.string().required(),
})
),
});
const schema = Yup.object().shape({
name: Yup.string().required(),
latitude: Yup.number().required(),
longitude: Yup.number().required(),
about: Yup.string().required().max(300),
instructions: Yup.string().required(),
opening_hours: Yup.string().required(),
open_on_weekends: Yup.boolean().required(),
images: Yup.array(
Yup.object().shape({
path: Yup.string().required(),
}),
),
});

await schema.validate(data, {
abortEarly: false,
});
await schema.validate(data, {
abortEarly: false,
});

const orphanage = orphanagesRepository.create(data);
const orphanage = orphanagesRepository.create(data);

await orphanagesRepository.save(orphanage);
await orphanagesRepository.save(orphanage);

return res.status(201).json(orphanage);
},
return res.status(201).json(orphanage);
},
};
2 changes: 1 addition & 1 deletion backend/src/database/connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createConnection } from "typeorm";
import { createConnection } from 'typeorm';

createConnection();
Loading

0 comments on commit b1abd6b

Please sign in to comment.