Skip to content

Commit

Permalink
refactor: refactor mediatr-js in building-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Jan 6, 2024
1 parent f368d6a commit 5aaa8f2
Show file tree
Hide file tree
Showing 69 changed files with 663 additions and 556 deletions.
4 changes: 2 additions & 2 deletions src/booking/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { RegisterRoutes } from './routes/routes';
import config from 'building-blocks/config/config';
import { collectDefaultMetrics } from 'prom-client';
import { initialSwagger } from 'building-blocks/swagger/swagger';
import { erroHandler } from 'building-blocks/error-handler/erro-handler';
import { initialDbContext } from './data/db.context';
import { initialLogger } from './extensions/logger.extensions';
import { initialOpenTelemetry } from './extensions/otel.extensions';
Expand All @@ -19,6 +18,7 @@ import { registerMediatrHandlers } from './extensions/mediatr.extensions';
import { httpContextMiddleware } from 'building-blocks/context/context';
import { postgresOptions } from './data/data-source';
import { Logger } from 'building-blocks/logging/logger';
import {errorHandler} from "building-blocks/error-handler/error-handler";

const startupApp = async () => {
collectDefaultMetrics();
Expand Down Expand Up @@ -54,7 +54,7 @@ const startupApp = async () => {

RegisterRoutes(app);

app.use(erroHandler);
app.use(errorHandler);

const server = app.listen(config.port, () => {
logger.info(`Listening to http://localhost:${config.port}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IHandler, IRequest, mediatrJs } from 'building-blocks/mediatr-js/mediatr.js';
import { Body, Controller, Post, Route, Security, SuccessResponse } from 'tsoa';
import httpStatus from 'http-status';
import Joi from 'joi';
Expand All @@ -12,6 +11,7 @@ import { IFlightClient } from '../../../http-client/services/flight/flight.clien
import { IPassengerClient } from '../../../http-client/services/passenger/passenger.client';
import { IPublisher } from 'building-blocks/rabbitmq/rabbitmq-publisher';
import mapper from '../../../mappings';
import { IRequest, IRequestHandler, mediatrJs } from 'building-blocks/mediatr-js/mediatr-js';

export class CreateBooking implements IRequest<BookingDto> {
passengerId: number;
Expand Down Expand Up @@ -59,7 +59,7 @@ export class CreateBookingController extends Controller {
}

@injectable()
export class CreateBookingHandler implements IHandler<CreateBooking, BookingDto> {
export class CreateBookingHandler implements IRequestHandler<CreateBooking, BookingDto> {
constructor(
@inject('IBookingRepository') private bookingRepository: IBookingRepository,
@inject('IFlightClient') private flightClientService: IFlightClient,
Expand Down
10 changes: 6 additions & 4 deletions src/booking/src/extensions/mediatr.extensions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { container } from 'tsyringe';
import { mediatrJs } from 'building-blocks/mediatr-js/mediatr.js';
import {CreateBooking, CreateBookingHandler} from "../booking/features/v1/create-booking/create-booking";

import {
CreateBooking,
CreateBookingHandler
} from '../booking/features/v1/create-booking/create-booking';
import { mediatrJs } from 'building-blocks/mediatr-js/mediatr-js';

export const registerMediatrHandlers = () => {
mediatrJs.registerHandler(CreateBooking, container.resolve(CreateBookingHandler));
mediatrJs.registerRequestHandler(CreateBooking, container.resolve(CreateBookingHandler));
};
2 changes: 1 addition & 1 deletion src/building-blocks/context/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="node" />
import { IncomingHttpHeaders } from "http";
import { IncomingHttpHeaders } from 'http';
export declare class HttpContext {
static request: Request;
static response: Response;
Expand Down
2 changes: 1 addition & 1 deletion src/building-blocks/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IncomingHttpHeaders} from "http";
import { IncomingHttpHeaders } from 'http';

export class HttpContext {
static request: Request;
Expand Down
2 changes: 0 additions & 2 deletions src/building-blocks/error-handler/erro-handler.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/building-blocks/error-handler/erro-handler.js.map

This file was deleted.

2 changes: 2 additions & 0 deletions src/building-blocks/error-handler/error-handler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { ErrorRequestHandler } from 'express';
export declare const errorHandler: ErrorRequestHandler;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/building-blocks/error-handler/error-handler.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import UnauthorizedException from '../types/exception/unauthorized.exception';
import ForbiddenException from '../types/exception/forbidden.exception';
import NotFoundException from '../types/exception/not-found.exception';
import ConflictException from '../types/exception/conflict.exception';
import { container } from 'tsyringe';
import { Logger } from '../logging/logger';
import HttpClientException from '../types/exception/http-client.exception';

export const erroHandler: ErrorRequestHandler = (err, req, res, next) => {
export const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
if (err instanceof ApplicationException) {
res.status(httpStatus.BAD_REQUEST).json(
new ProblemDocument({
Expand Down
6 changes: 5 additions & 1 deletion src/building-blocks/jwt/jwt.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import * as express from 'express';
export declare function expressAuthentication(request: express.Request, securityName: string, scopes?: string[]): Promise<any>;
export declare function expressAuthentication(
request: express.Request,
securityName: string,
scopes?: string[]
): Promise<any>;
167 changes: 98 additions & 69 deletions src/building-blocks/jwt/jwt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions src/building-blocks/logging/logger.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export interface ILogger {
debug(message: string): void;
error(message: string | Error): void;
info(message: string): void;
debug(message: string): void;
error(message: string | Error): void;
info(message: string): void;
}
export declare class Logger implements ILogger {
private static logger;
constructor();
debug(message: string): void;
static debug(message: string): void;
info(message: string): void;
static info(message: string): void;
error(message: string | Error): void;
static error(message: string | Error): void;
private static logger;
constructor();
debug(message: string): void;
static debug(message: string): void;
info(message: string): void;
static info(message: string): void;
error(message: string | Error): void;
static error(message: string | Error): void;
}
Loading

0 comments on commit 5aaa8f2

Please sign in to comment.