From 2287e0bd169322e5fedfe59a3421647d44a9e802 Mon Sep 17 00:00:00 2001 From: El Hilali Mouad Date: Fri, 8 Nov 2024 15:52:28 +0100 Subject: [PATCH 1/3] refactor: create module directory, simple structure and an exemple for auth module --- src/modules/accounts/routes/index.ts | 0 src/modules/auth/auth.service.ts | 25 +++++++++++++++++++ src/modules/auth/controllers/index.ts | 19 ++++++++++++++ .../auth/controllers/login.controller.ts | 6 +++++ src/modules/auth/routes/index.ts | 12 +++++++++ src/modules/tasks/routes/index.ts | 0 6 files changed, 62 insertions(+) create mode 100644 src/modules/accounts/routes/index.ts create mode 100644 src/modules/auth/auth.service.ts create mode 100644 src/modules/auth/controllers/index.ts create mode 100644 src/modules/auth/controllers/login.controller.ts create mode 100644 src/modules/auth/routes/index.ts create mode 100644 src/modules/tasks/routes/index.ts diff --git a/src/modules/accounts/routes/index.ts b/src/modules/accounts/routes/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts new file mode 100644 index 0000000..0a09cbd --- /dev/null +++ b/src/modules/auth/auth.service.ts @@ -0,0 +1,25 @@ +import { FastifyInstance } from 'fastify'; +import fp from 'fastify-plugin'; + + +interface AuthService { + login(): void; +} + +declare module 'fastify' { + export interface FastifyInstance { + authService: AuthService; + } +} + +function createAuthService(fastify: FastifyInstance) { + return { + login() {} + }; +} + +export default fp( + async function (fastify) { + fastify.decorate('authService', createAuthService(fastify)); +}, { name: 'authService' }); + diff --git a/src/modules/auth/controllers/index.ts b/src/modules/auth/controllers/index.ts new file mode 100644 index 0000000..34bd094 --- /dev/null +++ b/src/modules/auth/controllers/index.ts @@ -0,0 +1,19 @@ +import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' +import { login } from './login.controller.js' + +const controllers = { + login +} + +const createAuthController = (fastify: FastifyInstance) => { + async function login(request: FastifyRequest, reply: FastifyReply) { + return controllers.login(fastify, request, reply) + } + + + return { + login + } +} + +export default createAuthController diff --git a/src/modules/auth/controllers/login.controller.ts b/src/modules/auth/controllers/login.controller.ts new file mode 100644 index 0000000..0267ade --- /dev/null +++ b/src/modules/auth/controllers/login.controller.ts @@ -0,0 +1,6 @@ +import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; + +export async function login(fastify: FastifyInstance, request: FastifyRequest, reply: FastifyReply) { + // For demonstration purpose + fastify.authService.login() +} diff --git a/src/modules/auth/routes/index.ts b/src/modules/auth/routes/index.ts new file mode 100644 index 0000000..75ab54a --- /dev/null +++ b/src/modules/auth/routes/index.ts @@ -0,0 +1,12 @@ +import { FastifyPluginAsyncTypebox, Type } from "@fastify/type-provider-typebox"; +import createAuthController from '../controllers/index.js' + + + + +const authRoutes: FastifyPluginAsyncTypebox = async (fastify) => { + const authController = createAuthController(fastify) + fastify.post('', {}, authController.login) +} + +export default authRoutes diff --git a/src/modules/tasks/routes/index.ts b/src/modules/tasks/routes/index.ts new file mode 100644 index 0000000..e69de29 From 4d16d0acef0326723a07a365ae874a96cf8bd9f0 Mon Sep 17 00:00:00 2001 From: El Hilali Mouad Date: Fri, 8 Nov 2024 16:31:13 +0100 Subject: [PATCH 2/3] refactor: remove `routes` directory and put the index file to root of the module (index.route) - This allows autoload to work correctly (i guess), as it depends on the parent directory name. Also, we'll only have one route file, so the directory isn't needed. --- src/modules/accounts/{routes/index.ts => index.route.ts} | 0 src/modules/auth/{routes/index.ts => index.route.ts} | 2 +- src/modules/tasks/{routes/index.ts => index.route.ts} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename src/modules/accounts/{routes/index.ts => index.route.ts} (100%) rename src/modules/auth/{routes/index.ts => index.route.ts} (82%) rename src/modules/tasks/{routes/index.ts => index.route.ts} (100%) diff --git a/src/modules/accounts/routes/index.ts b/src/modules/accounts/index.route.ts similarity index 100% rename from src/modules/accounts/routes/index.ts rename to src/modules/accounts/index.route.ts diff --git a/src/modules/auth/routes/index.ts b/src/modules/auth/index.route.ts similarity index 82% rename from src/modules/auth/routes/index.ts rename to src/modules/auth/index.route.ts index 75ab54a..4e632ad 100644 --- a/src/modules/auth/routes/index.ts +++ b/src/modules/auth/index.route.ts @@ -1,5 +1,5 @@ import { FastifyPluginAsyncTypebox, Type } from "@fastify/type-provider-typebox"; -import createAuthController from '../controllers/index.js' +import createAuthController from './controllers/index.js' diff --git a/src/modules/tasks/routes/index.ts b/src/modules/tasks/index.route.ts similarity index 100% rename from src/modules/tasks/routes/index.ts rename to src/modules/tasks/index.route.ts From 8b414428f6f12c0fafa6180d76ab66d53423730d Mon Sep 17 00:00:00 2001 From: El Hilali Mouad Date: Fri, 8 Nov 2024 16:33:33 +0100 Subject: [PATCH 3/3] chore: lint --- src/modules/auth/auth.service.ts | 16 +++++++--------- src/modules/auth/controllers/index.ts | 3 +-- src/modules/auth/controllers/login.controller.ts | 8 ++++---- src/modules/auth/index.route.ts | 5 +---- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 0a09cbd..89b13c4 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -1,6 +1,5 @@ -import { FastifyInstance } from 'fastify'; -import fp from 'fastify-plugin'; - +import { FastifyInstance } from 'fastify' +import fp from 'fastify-plugin' interface AuthService { login(): void; @@ -12,14 +11,13 @@ declare module 'fastify' { } } -function createAuthService(fastify: FastifyInstance) { +function createAuthService (fastify: FastifyInstance) { return { - login() {} - }; + login () {} + } } export default fp( async function (fastify) { - fastify.decorate('authService', createAuthService(fastify)); -}, { name: 'authService' }); - + fastify.decorate('authService', createAuthService(fastify)) + }, { name: 'authService' }) diff --git a/src/modules/auth/controllers/index.ts b/src/modules/auth/controllers/index.ts index 34bd094..4a4cfeb 100644 --- a/src/modules/auth/controllers/index.ts +++ b/src/modules/auth/controllers/index.ts @@ -6,11 +6,10 @@ const controllers = { } const createAuthController = (fastify: FastifyInstance) => { - async function login(request: FastifyRequest, reply: FastifyReply) { + async function login (request: FastifyRequest, reply: FastifyReply) { return controllers.login(fastify, request, reply) } - return { login } diff --git a/src/modules/auth/controllers/login.controller.ts b/src/modules/auth/controllers/login.controller.ts index 0267ade..b1ffa3f 100644 --- a/src/modules/auth/controllers/login.controller.ts +++ b/src/modules/auth/controllers/login.controller.ts @@ -1,6 +1,6 @@ -import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; +import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' -export async function login(fastify: FastifyInstance, request: FastifyRequest, reply: FastifyReply) { - // For demonstration purpose - fastify.authService.login() +export async function login (fastify: FastifyInstance, request: FastifyRequest, reply: FastifyReply) { + // For demonstration purpose + fastify.authService.login() } diff --git a/src/modules/auth/index.route.ts b/src/modules/auth/index.route.ts index 4e632ad..bc7b6ab 100644 --- a/src/modules/auth/index.route.ts +++ b/src/modules/auth/index.route.ts @@ -1,9 +1,6 @@ -import { FastifyPluginAsyncTypebox, Type } from "@fastify/type-provider-typebox"; +import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' import createAuthController from './controllers/index.js' - - - const authRoutes: FastifyPluginAsyncTypebox = async (fastify) => { const authController = createAuthController(fastify) fastify.post('', {}, authController.login)