From 7ff59cbb9d6aa718182fac4a8c50ca44c845e997 Mon Sep 17 00:00:00 2001 From: Ihar Date: Fri, 3 May 2024 20:05:30 +0500 Subject: [PATCH] refactor: put same names as base nest --- analytics-service/src/app.ts | 3 +- api-gateway/package.json | 1 - api-gateway/src/api/service/policy.ts | 10 ++-- api-gateway/src/api/service/tags.ts | 1 - api-gateway/src/api/service/themes.ts | 2 +- api-gateway/src/api/service/tool.ts | 9 ++-- api-gateway/src/app.module.ts | 52 ++----------------- api-gateway/src/app.ts | 37 ------------- api-gateway/src/auth/authorization-helper.ts | 44 ---------------- .../src/helpers/interceptors/multipart.ts | 2 +- 10 files changed, 16 insertions(+), 145 deletions(-) diff --git a/analytics-service/src/app.ts b/analytics-service/src/app.ts index 81a85dea74..d8bd3805df 100644 --- a/analytics-service/src/app.ts +++ b/analytics-service/src/app.ts @@ -18,7 +18,6 @@ import { AppModule } from './app.module.js'; import { SwaggerModule } from '@nestjs/swagger'; import { SwaggerConfig } from './helpers/swagger-config.js'; import { AnalyticsUtils } from './helpers/utils.js'; -import { FastifyAdapter } from '@nestjs/platform-fastify'; const PORT = process.env.PORT || 3020; Promise.all([ @@ -35,7 +34,7 @@ Promise.all([ }, [ 'v2-21-0' ]), - NestFactory.create(AppModule, new FastifyAdapter(), { + NestFactory.create(AppModule, { rawBody: true, bodyParser: false }), diff --git a/api-gateway/package.json b/api-gateway/package.json index fb64e798e8..03d953a86a 100644 --- a/api-gateway/package.json +++ b/api-gateway/package.json @@ -26,7 +26,6 @@ "dotenv": "^16.0.0", "express": "^4.17.1", "express-fileupload": "^1.4.0", - "fastify-raw-body": "4.0.0", "gulp": "^4.0.2", "gulp-copy": "^4.0.1", "gulp-rename": "^2.0.0", diff --git a/api-gateway/src/api/service/policy.ts b/api-gateway/src/api/service/policy.ts index cdae5f2edc..ec496668b5 100644 --- a/api-gateway/src/api/service/policy.ts +++ b/api-gateway/src/api/service/policy.ts @@ -15,9 +15,9 @@ import { ApiImplicitParam } from '@nestjs/swagger/dist/decorators/api-implicit-p import { ApiImplicitQuery } from '@nestjs/swagger/dist/decorators/api-implicit-query.decorator.js'; import { CACHE } from '../../constants/index.js'; import { UseCache } from '../../helpers/decorators/cache.js'; -import { MultipartFile } from '../../helpers/interceptors/types/index.js'; +import { AnyFilesInterceptor } from '../../helpers/interceptors/multipart.js'; import { UploadedFiles } from '../../helpers/decorators/file.js'; -import { FilesInterceptor } from '../../helpers/interceptors/multipart.js'; +import { MultipartFile } from '../../helpers/interceptors/types/index.js'; const ONLY_SR = ' Only users with the Standard Registry role are allowed to make the request.' @@ -1689,10 +1689,10 @@ export class PolicyApi { type: InternalServerErrorDTO }) @HttpCode(HttpStatus.CREATED) - @UseInterceptors(FilesInterceptor()) + @UseInterceptors(AnyFilesInterceptor()) async importPolicyFromFileWithMetadata( @AuthUser() user: IAuthUser, - @UploadedFiles() files: any, + @UploadedFiles() files: MultipartFile[], @Query('versionOfTopicId') versionOfTopicId, ): Promise { try { @@ -1832,7 +1832,7 @@ export class PolicyApi { type: InternalServerErrorDTO }) @HttpCode(HttpStatus.ACCEPTED) - @UseInterceptors(FilesInterceptor()) + @UseInterceptors(AnyFilesInterceptor()) async importPolicyFromFileWithMetadataAsync( @AuthUser() user: IAuthUser, @UploadedFiles() files: MultipartFile[], diff --git a/api-gateway/src/api/service/tags.ts b/api-gateway/src/api/service/tags.ts index 4bf59bad78..21af33f91e 100644 --- a/api-gateway/src/api/service/tags.ts +++ b/api-gateway/src/api/service/tags.ts @@ -8,7 +8,6 @@ import { Auth } from '../../auth/auth.decorator.js'; @Controller('tags') @ApiTags('tags') -@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER) export class TagsApi { @Post('/') @HttpCode(HttpStatus.CREATED) diff --git a/api-gateway/src/api/service/themes.ts b/api-gateway/src/api/service/themes.ts index b3fbf86abd..415ba819e9 100644 --- a/api-gateway/src/api/service/themes.ts +++ b/api-gateway/src/api/service/themes.ts @@ -7,10 +7,10 @@ import { UserRole } from '@guardian/interfaces'; @Controller('themes') @ApiTags('themes') -@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER) export class ThemesApi { @Post('/') @HttpCode(HttpStatus.CREATED) + @Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER) async setThemes(@Req() req, @Response() res): Promise { try { const guardians = new Guardians(); diff --git a/api-gateway/src/api/service/tool.ts b/api-gateway/src/api/service/tool.ts index 90f6d5feff..35cfb071eb 100644 --- a/api-gateway/src/api/service/tool.ts +++ b/api-gateway/src/api/service/tool.ts @@ -11,7 +11,6 @@ import { Put, Req, Response, - UploadedFiles, UseInterceptors, } from '@nestjs/common'; import { TaskAction, UserRole } from '@guardian/interfaces'; @@ -32,9 +31,11 @@ import { TaskManager } from '../../helpers/task-manager.js'; import { ServiceError } from '../../helpers/service-requests-base.js'; import { InternalServerErrorDTO, TaskDTO, ToolDTO } from '../../middlewares/validation/schemas/index.js'; import { ApiImplicitParam } from '@nestjs/swagger/dist/decorators/api-implicit-param.decorator.js'; -import { AnyFilesInterceptor } from '@nestjs/platform-express'; import { UseCache } from '../../helpers/decorators/cache.js'; import { Auth } from '../../auth/auth.decorator.js'; +import { AnyFilesInterceptor } from '../../helpers/interceptors/multipart.js'; +import { UploadedFiles } from '../../helpers/decorators/file.js'; +import { MultipartFile } from '../../helpers/interceptors/types/index.js';; const ONLY_SR = ' Only users with the Standard Registry role are allowed to make the request.' @@ -766,7 +767,7 @@ export class ToolsApi { @Auth(UserRole.STANDARD_REGISTRY) async toolImportFileWithMetadata( @Req() req, - @UploadedFiles() files: any + @UploadedFiles() files: MultipartFile[] ): Promise { const guardian = new Guardians(); try { @@ -894,7 +895,7 @@ export class ToolsApi { @Auth(UserRole.STANDARD_REGISTRY) async toolImportFileWithMetadataAsync( @Req() req, - @UploadedFiles() files: any + @UploadedFiles() files: MultipartFile[] ): Promise { try { const file = files.find(item => item.fieldname === 'file'); diff --git a/api-gateway/src/app.module.ts b/api-gateway/src/app.module.ts index efee7035d8..73ce1cdccd 100644 --- a/api-gateway/src/app.module.ts +++ b/api-gateway/src/app.module.ts @@ -1,4 +1,4 @@ -import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common'; +import { MiddlewareConsumer, Module } from '@nestjs/common'; import { AccountApi } from './api/service/account.js'; import { AnalyticsApi } from './api/service/analytics.js'; import { ArtifactApi } from './api/service/artifact.js'; @@ -13,7 +13,6 @@ import { MetricsApi } from './api/service/metrics.js'; import { ModulesApi } from './api/service/module.js'; import { ToolsApi } from './api/service/tool.js'; import { ProfileApi } from './api/service/profile.js'; -import { AppMiddleware, authorizationHelper, LoggerMiddleware, nextHelper } from './auth/authorization-helper.js'; import { PolicyApi } from './api/service/policy.js'; import { SchemaApi, SingleSchemaApi } from './api/service/schema.js'; import { SettingsApi } from './api/service/settings.js'; @@ -23,7 +22,6 @@ import { TokensApi } from './api/service/tokens.js'; import { TrustChainsApi } from './api/service/trust-chains.js'; import { WizardApi } from './api/service/wizard.js'; import process from 'process'; -import express from 'express'; import hpp from 'hpp'; import { ThemesApi } from './api/service/themes.js'; import { BrandingApi } from './api/service/branding.js'; @@ -40,23 +38,9 @@ import { ProjectsAPI } from './api/service/project.js'; import { AISuggestionsAPI } from './api/service/ai-suggestions.js'; import { cacheProvider } from './helpers/cache-provider.js'; import { CacheService } from './helpers/cache-service.js'; -import fastifyRawBody from 'fastify-raw-body'; -const JSON_REQUEST_LIMIT = process.env.JSON_REQUEST_LIMIT || '1mb'; -const RAW_REQUEST_LIMIT = process.env.RAW_REQUEST_LIMIT || '1gb'; - -// class LogClientSerializer implements Serializer { -// serialize(value: any, options?: Record): any { -// value.data = Buffer.from(JSON.stringify(value), 'utf-8') -// return value; -// } -// } -// -// class LogClientDeserializer implements Deserializer { -// deserialize(value: any, options?: Record): any { -// return JSON.parse(value.toString()) -// } -// } +// const JSON_REQUEST_LIMIT = process.env.JSON_REQUEST_LIMIT || '1mb'; +// const RAW_REQUEST_LIMIT = process.env.RAW_REQUEST_LIMIT || '1gb'; @Module({ imports: [ @@ -117,44 +101,14 @@ const RAW_REQUEST_LIMIT = process.env.RAW_REQUEST_LIMIT || '1gb'; }) export class AppModule { configure(consumer: MiddlewareConsumer) { - // consumer.apply(LoggerMiddleware).forRoutes(AccountApi); - // console.log('AppModule1'); - // consumer.apply(authorizationHelper).forRoutes(ProfileApi); - // consumer.apply(authorizationHelper).forRoutes(PolicyApi); - // consumer.apply(authorizationHelper).forRoutes(SettingsApi); - // consumer.apply(authorizationHelper).forRoutes(SingleSchemaApi); - // consumer.apply(AppMiddleware).forRoutes(SchemaApi); - // consumer.apply(authorizationHelper).forRoutes(ArtifactApi); - // consumer.apply(authorizationHelper).forRoutes(IpfsApi); - // consumer.apply(authorizationHelper).forRoutes(LoggerApi); - // consumer.apply(authorizationHelper).forRoutes(AnalyticsApi); - // consumer.apply(authorizationHelper).forRoutes(ContractsApi); - // consumer.apply(authorizationHelper).forRoutes(ModulesApi); - // consumer.apply(authorizationHelper).forRoutes(ToolsApi); - // consumer.apply(authorizationHelper).forRoutes(TagsApi); - // consumer.apply(authorizationHelper).forRoutes(ThemesApi); - // consumer.apply(authorizationHelper).forRoutes(TokensApi); - // consumer.apply(authorizationHelper).forRoutes(TrustChainsApi); - // consumer.apply(authorizationHelper).forRoutes(WizardApi); - // consumer.apply(authorizationHelper).forRoutes(BrandingApi); - // consumer.apply(authorizationHelper).forRoutes(SuggestionsApi); - // consumer.apply(authorizationHelper).forRoutes(NotificationsApi); - // consumer.apply(authorizationHelper).forRoutes(TaskApi); - // consumer.apply(authorizationHelper).forRoutes(RecordApi); - // consumer.apply(authorizationHelper).forRoutes(AISuggestionsAPI); - // consumer.apply(express.json({ // limit: JSON_REQUEST_LIMIT // })).forRoutes('*'); - // consumer - // .apply(AppMiddleware) - // .forRoutes({ path: '*', method: RequestMethod.ALL }); // consumer.apply(express.raw({ // inflate: true, // limit: RAW_REQUEST_LIMIT, // type: 'binary/octet-stream' // })).forRoutes('*'); - // consumer.apply(fastifyRawBody).forRoutes('*'); consumer.apply(hpp()).forRoutes('*'); } } diff --git a/api-gateway/src/app.ts b/api-gateway/src/app.ts index 61e6233e38..75c07e3873 100644 --- a/api-gateway/src/app.ts +++ b/api-gateway/src/app.ts @@ -11,7 +11,6 @@ import { NestFactory } from '@nestjs/core'; import { MicroserviceOptions, Transport } from '@nestjs/microservices'; import process from 'process'; import { HttpStatus, ValidationPipe } from '@nestjs/common'; -import express, { json } from 'express'; import { SwaggerModule } from '@nestjs/swagger'; import { SwaggerConfig } from './helpers/swagger-config.js'; import { SwaggerModels, SwaggerPaths } from './old-descriptions.js'; @@ -20,55 +19,19 @@ import * as extraModels from './middlewares/validation/schemas/index.js' import { ProjectService } from './helpers/projects.js'; import { AISuggestions } from './helpers/ai-suggestions.js'; import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'; -import fastify from 'fastify'; import fastifyFormbody from '@fastify/formbody' import fastifyMultipart from '@fastify/multipart'; const PORT = process.env.PORT || 3002; -// const restResponseTimeHistogram = new client.Histogram({ -// name: 'api_gateway_rest_response_time_duration_seconds', -// help: 'api-gateway a histogram metric', -// labelNames: ['method', 'route', 'statusCode'], -// buckets: [0.1, 5, 15, 50, 100, 500], -// }); - -export const fastifyInstance = fastify({}); - -// fastifyInstance.addHook('onRoute', (opts) => { -// opts.config = { rawBody: true }; -// }); - -// fastifyInstance.addContentTypeParser( -// ['multipart/form-data'], -// { parseAs: 'string' }, -// fastifyInstance.getDefaultJsonParser('ignore', 'ignore'), -// ); - -// fastifyInstance.addContentTypeParser('multipart/form-data', async function (req) { -// var res = await new Promise((resolve, reject) => resolve(req)) -// return res -// }) - -// export const rowBodyConfig = { -// field: 'rawBody', // change the default request.rawBody property name -// global: true, // add the rawBody to every request. *Default true* -// encoding: 'utf8', // set it in false to set rawBody as a Buffer *Default utf8* -// runFirst: true, // get the body before any preParsing hook change/uncompress it. *Default false* -// }; - Promise.all([ NestFactory.create(AppModule, new FastifyAdapter(), { - // bufferLogs: true, - // logger: ['error', 'warn'], rawBody: true, bodyParser: false, }), MessageBrokerChannel.connect('API_GATEWAY'), ]).then(async ([app, cn]) => { try { - // await app.register(rawBody, rowBodyConfig) - app.connectMicroservice({ transport: Transport.NATS, options: { diff --git a/api-gateway/src/auth/authorization-helper.ts b/api-gateway/src/auth/authorization-helper.ts index fb7cf94562..f61b8a2dde 100644 --- a/api-gateway/src/auth/authorization-helper.ts +++ b/api-gateway/src/auth/authorization-helper.ts @@ -2,9 +2,6 @@ import { NextFunction, Response } from 'express'; import { Users } from '../helpers/users.js'; import { AuthenticatedRequest, IAuthUser, Logger } from '@guardian/common'; import { createParamDecorator, ExecutionContext, HttpException, HttpStatus, Injectable, NestMiddleware } from '@nestjs/common'; -import { ServerResponse, IncomingMessage } from 'http'; - -import { FastifyRequest, FastifyReply } from 'fastify'; export const AuthUser = createParamDecorator((data: string = 'user', ctx: ExecutionContext) => { const req = ctx.switchToHttp().getRequest(); @@ -92,44 +89,3 @@ export function permissionHelper(...roles: string[]) { } } } - -/** - */ -export async function nextHelper(req: AuthenticatedRequest, res: Response, next: Function): Promise { - console.log("nextHelper"); - next(); -} - -@Injectable() -export class LoggerMiddleware implements NestMiddleware { - use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) { - console.log('Request...'); - next(); - } -} - -@Injectable() -export class AppMiddleware implements NestMiddleware { - async use(req: any, res: any, next: Function) { - const authHeader = req.headers.authorization; - if (!authHeader) { - next(); - return; - } - const users = new Users(); - const token = authHeader.split(' ')[1]; - if (authHeader) { - try { - req.user = await users.getUserByToken(token) as IAuthUser; - next(); - return; - } catch (error) { - await new Logger().warn(error.message, ['API_GATEWAY']); - } - } - throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED) - } -} - -const RAW_REQUEST_LIMIT = process.env.RAW_REQUEST_LIMIT || '1gb'; - diff --git a/api-gateway/src/helpers/interceptors/multipart.ts b/api-gateway/src/helpers/interceptors/multipart.ts index eb00e38445..c71fb39ae0 100644 --- a/api-gateway/src/helpers/interceptors/multipart.ts +++ b/api-gateway/src/helpers/interceptors/multipart.ts @@ -10,7 +10,7 @@ import { getFileFromPart } from './utils/index.js'; import { FastifyRequest, MultipartFile, MultipartOptions } from './types/index.js'; -export function FilesInterceptor(options: MultipartOptions = {}): Type { +export function AnyFilesInterceptor(options: MultipartOptions = {}): Type { class MixinInterceptor implements NestInterceptor { async intercept(context: ExecutionContext, next: CallHandler): Promise> { const req = context.switchToHttp().getRequest() as FastifyRequest;