Skip to content

Commit

Permalink
chore: refactor import files and add healthcheck endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarogfn committed Apr 25, 2024
1 parent 36a8880 commit 74f2e67
Show file tree
Hide file tree
Showing 28 changed files with 69 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3000
HOSTNAME=localhost
2 changes: 2 additions & 0 deletions .env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3000
HOSTNAME=localhost
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ node_modules
build
dist
coverage
*.env.*
*.env
4 changes: 0 additions & 4 deletions dev.env

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"node": "22"
},
"scripts": {
"dev": " cross-env MODE=DEV tsx --watch src/index.ts",
"start": "node build/index.js",
"dev": " cross-env MODE=DEV tsx --watch src/index.ts",
"start": "cross-env MODE=PROD node build/index.js",
"prestart": "pnpm run build",
"changeset": "changeset add",
"changeset:emtpy": "changeset add --empty",
Expand Down
4 changes: 2 additions & 2 deletions src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Express from 'express';
import setupRoutes from './routes';
import setupMiddlewares from './middlewares';
import setupRoutes from './routes.js';
import setupMiddlewares from './middlewares.js';

const app = Express();

Expand Down
6 changes: 3 additions & 3 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import dotenv from 'dotenv';

if (process.env['MODE'] === 'DEV') {
dotenv.config({
path: ['dev.env', '.env'],
path: ['.env.dev', '.env'],
});
}

if (process.env['MODE'] === 'PROD') {
dotenv.config({
path: ['prod.env', '.env'],
path: ['.env.prod', '.env'],
});
}

if (process.env['MODE'] === 'QA') {
dotenv.config({
path: ['qa.env', '.env'],
path: ['.env.qa', '.env'],
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/config/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { HttpStatusCode } from '@/shared/protocols/http-client.js';
import { Express } from 'express';

export default (app: Express): void => {
app.get('/healthcheck', (req, res) => {
res.status(HttpStatusCode.ok).json({ status: 'Running :)' });
});
};
6 changes: 4 additions & 2 deletions src/config/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Express } from 'express';
import { cors } from '@/middlewares/cors/cors';
import { bodyParser } from '@/middlewares/body-parser/body-parser';
import { cors } from '@/middlewares/cors/cors.js';
import { bodyParser } from '@/middlewares/body-parser/body-parser.js';
import healthcheck from './healthcheck.js';

export default function setupMiddlewares(app: Express): void {
app.use(bodyParser);
app.use(cors);
app.use(healthcheck);
}
2 changes: 1 addition & 1 deletion src/config/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errorHandler } from '@/middlewares/error-handler/error-handler';
import { errorHandler } from '@/middlewares/error-handler/error-handler.js';
import type { Express } from 'express';
import { glob } from 'glob';
import { resolve } from 'node:path';
Expand Down
8 changes: 4 additions & 4 deletions src/features/user/controllers/user-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpError } from '@/shared/errors/HttpError';
import { UserController } from './user-controller';
import type { Validator } from '@/shared/infra/validator/validator';
import type { Service } from '@/shared/protocols/service';
import { HttpError } from '@/shared/errors/HttpError.js';
import { UserController } from './user-controller.js';
import type { Validator } from '@/shared/infra/validator/validator.js';
import type { Service } from '@/shared/protocols/service.js';

const makeSut = () => {
class ValidatorStub implements Validator {
Expand Down
12 changes: 6 additions & 6 deletions src/features/user/controllers/user-controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Controller } from '@/shared/protocols/controller';
import type { Service } from '@/shared/protocols/service';
import { UserCreateModel } from '../models/user-create-model';
import { Validator } from '@/shared/infra/validator/validator';
import { userCreateSchema } from '../validators';
import { AsyncRequestHandler } from '@/shared/protocols/handlers';
import type { Controller } from '@/shared/protocols/controller.js';
import type { Service } from '@/shared/protocols/service.js';
import { UserCreateModel } from '../models/user-create-model.js';
import { Validator } from '@/shared/infra/validator/validator.js';
import { userCreateSchema } from '../validators/index.js';
import { AsyncRequestHandler } from '@/shared/protocols/handlers.js';

export class UserController implements Controller {
constructor(
Expand Down
6 changes: 3 additions & 3 deletions src/features/user/routes/user-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* istanbul ignore file */

import { Validator } from '@/shared/infra/validator/validator';
import { UserController } from '../controllers/user-controller';
import { UserCreateService } from '../services/user-create-service';
import { Validator } from '@/shared/infra/validator/validator.js';
import { UserController } from '../controllers/user-controller.js';
import { UserCreateService } from '../services/user-create-service.js';

export function userControllerFactory() {
const userServiceFindAll = new UserCreateService();
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/routes/user-routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* istanbul ignore file */

import { Router } from 'express';
import { userControllerFactory } from './user-controller-factory';
import { userControllerFactory } from './user-controller-factory.js';

const router = Router();

Expand Down
20 changes: 10 additions & 10 deletions src/features/user/services/user-create-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { UserCreateService } from "./user-create-service"
import { UserCreateService } from './user-create-service.js';

const makeSut = () => {
const userCreateService = new UserCreateService()
return { userCreateService }
}
const userCreateService = new UserCreateService();

return { userCreateService };
};

describe('UserCreateService', () => {
it('should return the created user', async () => {
const { userCreateService } = makeSut()
const { userCreateService } = makeSut();

const response = await userCreateService.execute({
email: 'valid_email@email.com',
password: 'valid_password',
})
});

expect(response).toStrictEqual({
email: 'valid_email@email.com',
password: 'valid_password',
id: '1',
})
})
})
});
});
});
4 changes: 2 additions & 2 deletions src/features/user/services/user-create-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Service } from '@/shared/protocols/service';
import { UserCreateModel } from '../models/user-create-model';
import type { Service } from '@/shared/protocols/service.js';
import { UserCreateModel } from '../models/user-create-model.js';

export class UserCreateService implements Service {
constructor() {}
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './user-create-schema'
export * from './user-create-schema.js';
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import env from './config/env';
import app from './config/app';
import env from './config/env.js';
import app from './config/app.js';

function startServer() {
const listener = app.listen(Number(env.PORT), env.HOSTNAME, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/body-parser/body-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import express from 'express';
import type { NextFunction, Response, Request } from 'express';
import { bodyParser } from './body-parser';
import { bodyParser } from './body-parser.js';

const makeSut = () => {
const app = express();
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/cors/cors.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import express from 'express';
import type { NextFunction, Response, Request } from 'express';
import { cors } from './cors';
import { cors } from './cors.js';

const makeSut = () => {
const app = express();
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/error-handler/error-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import request from 'supertest';
import { errorHandler } from './error-handler';
import { errorHandler } from './error-handler.js';
import express from 'express';
import type { NextFunction, Response } from 'express';
import { HttpError } from '@/shared/errors/HttpError';
import { HttpError } from '@/shared/errors/HttpError.js';

const makeSut = () => {
const app = express();
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/error-handler/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpError } from '@/shared/errors/HttpError';
import { HttpStatusCode } from '@/shared/protocols/http-client';
import { HttpError } from '@/shared/errors/HttpError.js';
import { HttpStatusCode } from '@/shared/protocols/http-client.js';
import { ErrorRequestHandler } from 'express';

export const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/errors/HttpError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpStatusCode } from '../protocols/http-client';
import { HttpStatusCode } from '../protocols/http-client.js';

export class HttpError extends Error {
constructor(
Expand Down
4 changes: 2 additions & 2 deletions src/shared/errors/ValidateError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpStatusCode } from '../protocols/http-client';
import { HttpError } from './HttpError';
import { HttpStatusCode } from '../protocols/http-client.js';
import { HttpError } from './HttpError.js';

export class ValidationError extends HttpError {
constructor(
Expand Down
4 changes: 2 additions & 2 deletions src/shared/infra/validator/validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi';
import { Validator } from './validator';
import { ValidationError } from '@/shared/errors/ValidateError';
import { Validator } from './validator.js';
import { ValidationError } from '@/shared/errors/ValidateError.js';
import { faker } from '@faker-js/faker';

const makeSut = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/infra/validator/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationError } from '@/shared/errors/ValidateError';
import { ValidationError } from '@/shared/errors/ValidateError.js';
import { ObjectSchema } from 'joi';

export class Validator {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/protocols/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { HttpRequest } from './http';
import type { HttpRequest } from './http.js';

export interface Service<P = any, R = any> {
execute(params: P): Promise<R>;
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"compilerOptions": {
"types": ["vitest/globals"],
"target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"module": "NodeNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"outDir": "./build" /* Redirect output structure to the directory. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"moduleResolution": "NodeNext" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"lib": ["ESNext"],
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
Expand Down

0 comments on commit 74f2e67

Please sign in to comment.