Skip to content

Commit

Permalink
Merge pull request #54 from codex-team/fix/eslint
Browse files Browse the repository at this point in the history
fix(eslint): new eslint errors resolved
  • Loading branch information
neSpecc authored Aug 16, 2023
2 parents 7bd7a13 + ffe1255 commit 1ee8a84
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"cSpell.words": [
"Fastify",
"Middlewares",
"openai",
"openapi",
"pino"
]
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ const start = async (): Promise<void> => {
}
};

start();
try {
await start();
} catch (err) {
logger.fatal('Failed to start application ' + err);
process.exit(1);
}
10 changes: 7 additions & 3 deletions src/infrastructure/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ export default async function runMetricsServer(): Promise<void> {
});

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) => {
Expand All @@ -48,7 +50,9 @@ export default async function runMetricsServer(): Promise<void> {
date: new Date(),
};

reply.status(StatusCodes.OK).send(data);
return reply
.status(StatusCodes.OK)
.send(data);
});

await metricsServer.listen({
Expand Down
8 changes: 8 additions & 0 deletions src/infrastructure/utils/notEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Checks for null/undefined/''/{}
*
* @param v value to check
*/
export default function notEmpty<T>(v: T | undefined | null | object): v is T {
return v !== undefined && v !== null && v !== '' && (typeof v !== 'object' || Object.keys(v).length > 0);
}
24 changes: 13 additions & 11 deletions src/presentation/http/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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,
});
Expand All @@ -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: {
Expand All @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions src/presentation/http/middlewares/authRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -49,9 +48,7 @@ export default (authService: AuthService): preHandlerHookHandler => {
message: 'Invalid access token',
};

reply.send(response);

return;
return reply.send(response);
}
};
};
3 changes: 2 additions & 1 deletion src/presentation/http/middlewares/withUser.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/presentation/http/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ const AuthRouter: FastifyPluginCallback<AuthRouterOptions> = (fastify, opts, don
message: 'Session is not valid',
};

reply.send(response);

return;
return reply.send(response);
}

const accessToken = opts.authService.signAccessToken({ id: userSession.userId });
Expand All @@ -69,7 +67,7 @@ const AuthRouter: FastifyPluginCallback<AuthRouterOptions> = (fastify, opts, don
},
};

reply.send(response);
return reply.send(response);
});

/**
Expand All @@ -84,7 +82,7 @@ const AuthRouter: FastifyPluginCallback<AuthRouterOptions> = (fastify, opts, don
data: 'OK',
};

reply.status(StatusCodes.OK).send(response);
await reply.status(StatusCodes.OK).send(response);
});
done();
};
Expand Down
3 changes: 2 additions & 1 deletion src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,7 +128,7 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (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',
Expand Down
6 changes: 2 additions & 4 deletions src/presentation/http/router/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const OauthRouter: FastifyPluginCallback<OauthRouterOptions> = (fastify, opts, d
message: 'User not found',
};

reply.send(response);

return;
return reply.send(response);
}

/**
Expand All @@ -67,7 +65,7 @@ const OauthRouter: FastifyPluginCallback<OauthRouterOptions> = (fastify, opts, d
/**
* Show page with script passing parent window postMessage with tokens
*/
reply.type('text/html').send(`
return reply.type('text/html').send(`
<html>
<head>
<script>
Expand Down
9 changes: 7 additions & 2 deletions src/repository/ai.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import notEmpty from '@infrastructure/utils/notEmpty.js';
import type OpenAIApi from './transport/openai-api';
import type { GetCompletionResponsePayload } from './transport/openai-api/types/GetCompletionResponsePayload';

/**
* Repository that estabishes access to data from business logic
* Repository that establishes access to data from business logic
*/
export default class AIRepository {
/**
Expand Down Expand Up @@ -30,6 +31,10 @@ export default class AIRepository {

const response = await this.openaiTransport.postWithToken<GetCompletionResponsePayload>('/chat/completions', body);

return response?.choices[0].message.content || '';
if (response !== null && notEmpty(response.choices[0].message.content)) {
return response.choices[0].message.content;
}

return '';
}
}
4 changes: 2 additions & 2 deletions src/repository/storage/postgres/orm/sequelize/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export default class UserSequelizeStorage {
/**
* Check if id or email is provided
*/
if (id) {
if (id !== undefined) {
/**
* Find user by id
*/
Expand All @@ -254,7 +254,7 @@ export default class UserSequelizeStorage {
id,
},
});
} else if (email) {
} else if (email !== undefined) {
/**
* Find user by email
*/
Expand Down

0 comments on commit 1ee8a84

Please sign in to comment.