From 80f11f96b7bbe2c4b984da3e3501ecd6a0a30580 Mon Sep 17 00:00:00 2001 From: Mairis Berzins Date: Sun, 14 Jul 2024 17:56:01 +0300 Subject: [PATCH] api core module refactor pt. 2 --- .../container.ts => di/di.container.ts} | 16 ++++++++-------- .../identifiers.ts => di/di.identifiers.ts} | 0 .../server-config-fn.ts} | 2 +- .../server-error-config-fn.ts} | 6 +++--- .../api/src/features/auth/auth.controller.ts | 2 +- packages/api/src/features/auth/auth.provider.ts | 2 +- packages/api/src/features/auth/auth.service.ts | 2 +- .../api/src/features/card/card.controller.ts | 2 +- .../api/src/features/game/game.controller.ts | 2 +- .../api/src/features/user/user.controller.ts | 2 +- packages/api/src/index.ts | 10 +++++----- 11 files changed, 23 insertions(+), 23 deletions(-) rename packages/api/src/core/{inversify/container.ts => di/di.container.ts} (66%) rename packages/api/src/core/{inversify/identifiers.ts => di/di.identifiers.ts} (100%) rename packages/api/src/core/{inversify/config-function.ts => server/server-config-fn.ts} (51%) rename packages/api/src/core/{inversify/error-config-function.ts => server/server-error-config-fn.ts} (61%) diff --git a/packages/api/src/core/inversify/container.ts b/packages/api/src/core/di/di.container.ts similarity index 66% rename from packages/api/src/core/inversify/container.ts rename to packages/api/src/core/di/di.container.ts index 7b31bd3..4fb4389 100644 --- a/packages/api/src/core/inversify/container.ts +++ b/packages/api/src/core/di/di.container.ts @@ -14,15 +14,15 @@ import { GameServiceMock } from '../../features/game/game.service.mock'; import { IGameService } from '../../features/game/game.service.type'; import { UserService } from '../../features/user/user.service'; import { IUserService } from '../../features/user/user.service.type'; -import { Middleware, Services } from './identifiers'; +import { Middleware, Services } from './di.identifiers'; -const container = new Container(); +const diContainer = new Container(); -container.bind(Middleware.Auth).to(AuthMiddleware); +diContainer.bind(Middleware.Auth).to(AuthMiddleware); -container.bind(Services.Auth).to(AuthService); -container.bind(Services.Card).to(CardService); -container.bind(Services.Game).to(GameServiceMock); -container.bind(Services.User).to(UserService); +diContainer.bind(Services.Auth).to(AuthService); +diContainer.bind(Services.Card).to(CardService); +diContainer.bind(Services.Game).to(GameServiceMock); +diContainer.bind(Services.User).to(UserService); -export { container }; +export { diContainer as container }; diff --git a/packages/api/src/core/inversify/identifiers.ts b/packages/api/src/core/di/di.identifiers.ts similarity index 100% rename from packages/api/src/core/inversify/identifiers.ts rename to packages/api/src/core/di/di.identifiers.ts diff --git a/packages/api/src/core/inversify/config-function.ts b/packages/api/src/core/server/server-config-fn.ts similarity index 51% rename from packages/api/src/core/inversify/config-function.ts rename to packages/api/src/core/server/server-config-fn.ts index 0772a43..c18284a 100644 --- a/packages/api/src/core/inversify/config-function.ts +++ b/packages/api/src/core/server/server-config-fn.ts @@ -1,3 +1,3 @@ import { ConfigFunction } from 'inversify-express-utils'; -export const configFunction: ConfigFunction = () => {}; +export const serverConfigFn: ConfigFunction = () => {}; diff --git a/packages/api/src/core/inversify/error-config-function.ts b/packages/api/src/core/server/server-error-config-fn.ts similarity index 61% rename from packages/api/src/core/inversify/error-config-function.ts rename to packages/api/src/core/server/server-error-config-fn.ts index 6f60aea..e981c5a 100644 --- a/packages/api/src/core/inversify/error-config-function.ts +++ b/packages/api/src/core/server/server-error-config-fn.ts @@ -2,7 +2,7 @@ import { ErrorResponse } from '@tspark/common'; import { NextFunction, Request, Response } from 'express'; import { ConfigFunction } from 'inversify-express-utils'; -export const errorConfigFunction: ConfigFunction = (app) => { +export const serverErorConfigFn: ConfigFunction = (app) => { app.use( ( err: Error, @@ -10,8 +10,8 @@ export const errorConfigFunction: ConfigFunction = (app) => { res: Response, next: NextFunction, ) => { - console.error('Something went wrong!', err.stack); - return res.status(500).json({ error: 'Something went wrong!' }); + console.error('Oops! Something went wrong.', err); + return res.status(500).json({ error: 'Oops! Something went wrong.' }); }, ); }; diff --git a/packages/api/src/features/auth/auth.controller.ts b/packages/api/src/features/auth/auth.controller.ts index 168b732..39d4598 100644 --- a/packages/api/src/features/auth/auth.controller.ts +++ b/packages/api/src/features/auth/auth.controller.ts @@ -5,7 +5,7 @@ import jwt from 'jsonwebtoken'; import { mapper } from '../../core/auto-mapper/mapper'; import { config } from '../../core/config/config'; import { BaseController } from '../../core/controllers/base.controller'; -import { Services } from '../../core/inversify/identifiers'; +import { Services } from '../../core/di/di.identifiers'; import { User } from '../user/user.entity'; import { IAuthService } from './auth.service.type'; diff --git a/packages/api/src/features/auth/auth.provider.ts b/packages/api/src/features/auth/auth.provider.ts index 6163e2a..28fea93 100644 --- a/packages/api/src/features/auth/auth.provider.ts +++ b/packages/api/src/features/auth/auth.provider.ts @@ -3,7 +3,7 @@ import { Request } from 'express'; import { inject, injectable } from 'inversify'; import { mapper } from '../../core/auto-mapper/mapper'; import { BaseController } from '../../core/controllers/base.controller'; -import { Services } from '../../core/inversify/identifiers'; +import { Services } from '../../core/di/di.identifiers'; import { User } from '../user/user.entity'; import { IAuthProvider } from './auth.provider.type'; import { IAuthService } from './auth.service.type'; diff --git a/packages/api/src/features/auth/auth.service.ts b/packages/api/src/features/auth/auth.service.ts index 499b0a5..b0aab2d 100644 --- a/packages/api/src/features/auth/auth.service.ts +++ b/packages/api/src/features/auth/auth.service.ts @@ -3,7 +3,7 @@ import bcrypt from 'bcrypt'; import { inject, injectable } from 'inversify'; import jwt, { JwtPayload } from 'jsonwebtoken'; import { config } from '../../core/config/config'; -import { Services } from '../../core/inversify/identifiers'; +import { Services } from '../../core/di/di.identifiers'; import { User } from '../user/user.entity'; import { userRepository } from '../user/user.repository'; import { IUserService } from '../user/user.service.type'; diff --git a/packages/api/src/features/card/card.controller.ts b/packages/api/src/features/card/card.controller.ts index c6afe93..2d7d3f5 100644 --- a/packages/api/src/features/card/card.controller.ts +++ b/packages/api/src/features/card/card.controller.ts @@ -12,7 +12,7 @@ import { requestParam, response, } from 'inversify-express-utils'; -import { Middleware, Services } from '../../core/inversify/identifiers'; +import { Middleware, Services } from '../../core/di/di.identifiers'; import { ICardService } from './card.service.type'; @controller('/card', Middleware.Auth) diff --git a/packages/api/src/features/game/game.controller.ts b/packages/api/src/features/game/game.controller.ts index 7e51d64..300fcb6 100644 --- a/packages/api/src/features/game/game.controller.ts +++ b/packages/api/src/features/game/game.controller.ts @@ -4,7 +4,7 @@ import { controller, httpGet, } from 'inversify-express-utils'; -import { Services } from '../../core/inversify/identifiers'; +import { Services } from '../../core/di/di.identifiers'; import { IGameService } from './game.service.type'; @controller('/game') diff --git a/packages/api/src/features/user/user.controller.ts b/packages/api/src/features/user/user.controller.ts index ef0c96b..203fcd0 100644 --- a/packages/api/src/features/user/user.controller.ts +++ b/packages/api/src/features/user/user.controller.ts @@ -4,7 +4,7 @@ import { controller, httpGet, } from 'inversify-express-utils'; -import { Services } from '../../core/inversify/identifiers'; +import { Services } from '../../core/di/di.identifiers'; import { IUserService } from './user.service.type'; @controller('/user') diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index a3d2f6f..91d7226 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -9,9 +9,9 @@ import { InversifyExpressServer } from 'inversify-express-utils'; import { createMaps } from './core/auto-mapper/mapper'; import { config } from './core/config/config'; import { appDataSource } from './core/db/app-data-source'; -import { configFunction } from './core/inversify/config-function'; -import { container } from './core/inversify/container'; -import { errorConfigFunction } from './core/inversify/error-config-function'; +import { container } from './core/di/di.container'; +import { serverConfigFn } from './core/server/server-config-fn'; +import { serverErorConfigFn } from './core/server/server-error-config-fn'; import { AuthProvider } from './features/auth/auth.provider'; createMaps(); @@ -47,8 +47,8 @@ const server = new InversifyExpressServer( AuthProvider, ); -server.setConfig(configFunction); -server.setErrorConfig(errorConfigFunction); +server.setConfig(serverConfigFn); +server.setErrorConfig(serverErorConfigFn); appDataSource .initialize()