From ffe1255a63e476ce302436d96b52aff5cf346e63 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Wed, 16 Aug 2023 20:50:50 +0300 Subject: [PATCH] fix(eslint): new eslint errors resolved --- .vscode/settings.json | 2 ++ src/index.ts | 7 +++++- src/infrastructure/metrics/index.ts | 10 +++++--- src/infrastructure/utils/notEmpty.ts | 8 +++++++ src/presentation/http/http-server.ts | 24 ++++++++++--------- .../http/middlewares/authRequired.ts | 11 ++++----- src/presentation/http/middlewares/withUser.ts | 3 ++- src/presentation/http/router/auth.ts | 8 +++---- src/presentation/http/router/note.ts | 3 ++- src/presentation/http/router/oauth.ts | 6 ++--- src/repository/ai.repository.ts | 9 +++++-- .../storage/postgres/orm/sequelize/user.ts | 4 ++-- 12 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 src/infrastructure/utils/notEmpty.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index a63c8936..23087599 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "cSpell.words": [ "Fastify", "Middlewares", + "openai", + "openapi", "pino" ] } diff --git a/src/index.ts b/src/index.ts index 882743d0..c5963ac4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,4 +27,9 @@ const start = async (): Promise => { } }; -start(); +try { + await start(); +} catch (err) { + logger.fatal('Failed to start application ' + err); + process.exit(1); +} diff --git a/src/infrastructure/metrics/index.ts b/src/infrastructure/metrics/index.ts index 9fe7ce35..0450cc67 100644 --- a/src/infrastructure/metrics/index.ts +++ b/src/infrastructure/metrics/index.ts @@ -31,14 +31,16 @@ export default async function runMetricsServer(): Promise { }); metricsServer.get('/', (_request, reply) => { - reply + return reply .code(StatusCodes.OK) .type('text/html') .send(homePage); }); metricsServer.get('/metrics', async (_request, reply) => { - reply.code(StatusCodes.OK).send(await register.metrics()); + return reply + .code(StatusCodes.OK) + .send(await register.metrics()); }); metricsServer.get('/health', async (_request, reply) => { @@ -48,7 +50,9 @@ export default async function runMetricsServer(): Promise { date: new Date(), }; - reply.status(StatusCodes.OK).send(data); + return reply + .status(StatusCodes.OK) + .send(data); }); await metricsServer.listen({ diff --git a/src/infrastructure/utils/notEmpty.ts b/src/infrastructure/utils/notEmpty.ts new file mode 100644 index 00000000..96c6ee75 --- /dev/null +++ b/src/infrastructure/utils/notEmpty.ts @@ -0,0 +1,8 @@ +/** + * Checks for null/undefined/''/{} + * + * @param v value to check + */ +export default function notEmpty(v: T | undefined | null | object): v is T { + return v !== undefined && v !== null && v !== '' && (typeof v !== 'object' || Object.keys(v).length > 0); +} diff --git a/src/presentation/http/http-server.ts b/src/presentation/http/http-server.ts index 2f741804..fa443dce 100644 --- a/src/presentation/http/http-server.ts +++ b/src/presentation/http/http-server.ts @@ -55,7 +55,7 @@ export default class HttpServer implements API { /** * Register openapi documentation */ - this.server.register(fastifySwagger, { + await this.server.register(fastifySwagger, { openapi: { info: { title: 'NoteX openapi', @@ -68,14 +68,14 @@ export default class HttpServer implements API { }, }); - this.server.register(cookie, { + await this.server.register(cookie, { secret: this.config.cookieSecret, }); /** * Serve openapi UI and JSON scheme */ - this.server.register(fastifySwaggerUI, { + await this.server.register(fastifySwaggerUI, { routePrefix: '/openapi', uiConfig: { docExpansion: 'list', @@ -87,31 +87,31 @@ export default class HttpServer implements API { /** * Register all routers */ - this.server.register(NoteRouter, { + await this.server.register(NoteRouter, { prefix: '/note', noteService: domainServices.noteService, middlewares: middlewares, }); - this.server.register(OauthRouter, { + await this.server.register(OauthRouter, { prefix: '/oauth', userService: domainServices.userService, authService: domainServices.authService, cookieDomain: this.config.cookieDomain, }); - this.server.register(AuthRouter, { + await this.server.register(AuthRouter, { prefix: '/auth', authService: domainServices.authService, }); - this.server.register(UserRouter, { + await this.server.register(UserRouter, { prefix: '/user', userService: domainServices.userService, middlewares: middlewares, }); - this.server.register(AIRouter, { + await this.server.register(AIRouter, { prefix: '/ai', aiService: domainServices.aiService, }); - this.server.register(EditorToolsRouter, { + await this.server.register(EditorToolsRouter, { prefix: '/editor-tools', editorToolsService: domainServices.editorToolsService, }); @@ -120,7 +120,7 @@ export default class HttpServer implements API { /** * Register oauth2 plugin */ - this.server.register(fastifyOauth2, { + await this.server.register(fastifyOauth2, { name: 'googleOAuth2', scope: ['profile', 'email'], credentials: { @@ -137,7 +137,9 @@ export default class HttpServer implements API { /** * Allow cors for allowed origins from config */ - this.server.register(cors, { origin: this.config.allowedOrigins }); + await this.server.register(cors, { + origin: this.config.allowedOrigins, + }); await this.server.listen({ host: this.config.host, diff --git a/src/presentation/http/middlewares/authRequired.ts b/src/presentation/http/middlewares/authRequired.ts index 13675211..35c79f9f 100644 --- a/src/presentation/http/middlewares/authRequired.ts +++ b/src/presentation/http/middlewares/authRequired.ts @@ -2,6 +2,7 @@ import type { preHandlerHookHandler } from 'fastify'; import type AuthService from '@domain/service/auth.js'; import { StatusCodes } from 'http-status-codes'; import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js'; +import notEmpty from '@infrastructure/utils/notEmpty.js'; /** * Middleware for private routes @@ -23,15 +24,13 @@ export default (authService: AuthService): preHandlerHookHandler => { /** * If authorization header is not present, return unauthorized response */ - if (!authorizationHeader) { + if (!notEmpty(authorizationHeader)) { const response: ErrorResponse = { status: StatusCodes.UNAUTHORIZED, message: 'Missing authorization header', }; - reply.send(response); - - return; + return reply.send(response); } /** @@ -49,9 +48,7 @@ export default (authService: AuthService): preHandlerHookHandler => { message: 'Invalid access token', }; - reply.send(response); - - return; + return reply.send(response); } }; }; diff --git a/src/presentation/http/middlewares/withUser.ts b/src/presentation/http/middlewares/withUser.ts index 48589a1e..0b5f4c4b 100644 --- a/src/presentation/http/middlewares/withUser.ts +++ b/src/presentation/http/middlewares/withUser.ts @@ -1,5 +1,6 @@ import type { preHandlerHookHandler } from 'fastify'; import type AuthService from '@domain/service/auth.js'; +import notEmpty from '@infrastructure/utils/notEmpty.js'; /** * Middleware for routes, which should have user data @@ -21,7 +22,7 @@ export default (authService: AuthService): preHandlerHookHandler => { /** * If authorization header is not present, return unauthorized response */ - if (!authorizationHeader) { + if (!notEmpty(authorizationHeader)) { done(); return; diff --git a/src/presentation/http/router/auth.ts b/src/presentation/http/router/auth.ts index c1e83c85..25181036 100644 --- a/src/presentation/http/router/auth.ts +++ b/src/presentation/http/router/auth.ts @@ -52,9 +52,7 @@ const AuthRouter: FastifyPluginCallback = (fastify, opts, don message: 'Session is not valid', }; - reply.send(response); - - return; + return reply.send(response); } const accessToken = opts.authService.signAccessToken({ id: userSession.userId }); @@ -69,7 +67,7 @@ const AuthRouter: FastifyPluginCallback = (fastify, opts, don }, }; - reply.send(response); + return reply.send(response); }); /** @@ -84,7 +82,7 @@ const AuthRouter: FastifyPluginCallback = (fastify, opts, don data: 'OK', }; - reply.status(StatusCodes.OK).send(response); + await reply.status(StatusCodes.OK).send(response); }); done(); }; diff --git a/src/presentation/http/router/note.ts b/src/presentation/http/router/note.ts index 1a1b7064..5c177581 100644 --- a/src/presentation/http/router/note.ts +++ b/src/presentation/http/router/note.ts @@ -5,6 +5,7 @@ import type { ErrorResponse, SuccessResponse } from '@presentation/http/types/Ht import type { Note, NotePublicId } from '@domain/entities/note.js'; import type NotesSettings from '@domain/entities/notesSettings.js'; import type { Middlewares } from '@presentation/http/middlewares/index.js'; +import notEmpty from '@infrastructure/utils/notEmpty.js'; /** * Get note by id options @@ -127,7 +128,7 @@ const NoteRouter: FastifyPluginCallback = (fastify, opts, don /** * Check if note does not exist */ - if (!noteSettings) { + if (!notEmpty(noteSettings)) { const response: ErrorResponse = { status: StatusCodes.NOT_FOUND, message: 'Note not found', diff --git a/src/presentation/http/router/oauth.ts b/src/presentation/http/router/oauth.ts index b018392c..8fea2f5a 100644 --- a/src/presentation/http/router/oauth.ts +++ b/src/presentation/http/router/oauth.ts @@ -53,9 +53,7 @@ const OauthRouter: FastifyPluginCallback = (fastify, opts, d message: 'User not found', }; - reply.send(response); - - return; + return reply.send(response); } /** @@ -67,7 +65,7 @@ const OauthRouter: FastifyPluginCallback = (fastify, opts, d /** * Show page with script passing parent window postMessage with tokens */ - reply.type('text/html').send(` + return reply.type('text/html').send(`