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

Refactor api to ts #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Empty file added api/.env.example
Empty file.
4 changes: 2 additions & 2 deletions api/nodemon.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"watch": ["."],
"watch": ["src"],
"ext": "ts",
"exec": "ts-node ./index.ts"
"exec": "ts-node src/server.ts"
}
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Simple Rest API for manage the project",
"main": "index.js",
"scripts": {
"dev": "nodemon src/server.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon",
"tsc": "tsc"
"build": "tsc -p src"
},
"repository": {
"type": "git",
Expand Down
26 changes: 26 additions & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from 'express';

import AuthRoutes from './routes/auth.route';
import UsersRoutes from './routes/users.route';

class App {
public express: express.Application;

public constructor() {
this.express = express();

this.middlewares();
this.routes();
}

private middlewares(): void {
this.express.use(express.json());
}

private routes(): void {
this.express.use('/', AuthRoutes.router);
this.express.use('/api/user', UsersRoutes.router);
}
}

export default new App().express;
11 changes: 11 additions & 0 deletions api/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request, Response } from 'express';

class AuthController {
public async token(req: Request, res: Response): Promise<Response> {
const a = req.body;

return res.send("Send your credentials");
}
}

export default new AuthController();
12 changes: 12 additions & 0 deletions api/src/controllers/users.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response } from 'express';

class UsersController {
/**
* Usual names for CRUD methods are: index, store, update, delete
*/
public async index(_req: Request, res: Response): Promise<Response> {
return res.send("Hellow world");
}
}

export default new UsersController();
19 changes: 19 additions & 0 deletions api/src/routes/auth.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Router } from 'express';

import AuthController from '../controllers/auth.controller';

class AuthRoutes {
public router: Router;
public authController = AuthController;

constructor() {
this.router = Router();
this.routes();
}

private routes() {
this.router.post('/token', AuthController.token);
}
}

export default new AuthRoutes();
19 changes: 19 additions & 0 deletions api/src/routes/users.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Router } from 'express';

import UsersController from '../controllers/users.controller';

class UsersRoutes {
public router: Router;
public usersController = UsersController;

constructor() {
this.router = Router();
this.routes();
}

private routes() {
this.router.get('/', UsersController.index);
}
}

export default new UsersRoutes();
3 changes: 3 additions & 0 deletions api/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import app from './app';

app.listen(process.env.PORT || 3333);
Empty file.
20 changes: 13 additions & 7 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand All @@ -24,7 +24,7 @@

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
Expand All @@ -33,10 +33,10 @@
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
Expand All @@ -62,5 +62,11 @@

/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
},
"include": [
"./src/"
],
"exclude": [
"node_modules"
]
}