Skip to content

Commit

Permalink
chore: upgrade nestjs to v7 AB#23108
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisvisser committed Aug 14, 2023
1 parent 45e5d55 commit 22c8991
Show file tree
Hide file tree
Showing 13 changed files with 1,162 additions and 2,499 deletions.
3,485 changes: 1,094 additions & 2,391 deletions services/API-service/package-lock.json

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions services/API-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
},
"private": true,
"dependencies": {
"@godaddy/terminus": "^4.6.0",
"@nestjs/common": "^6.11.11",
"@nestjs/core": "^6.11.11",
"@nestjs/microservices": "^6.11.11",
"@nestjs/platform-express": "^6.11.11",
"@nestjs/common": "^7.6.18",
"@nestjs/core": "^7.6.18",
"@nestjs/microservices": "^7.6.18",
"@nestjs/platform-express": "^7.6.18",
"@nestjs/schedule": "^0.4.3",
"@nestjs/swagger": "^4.7.12",
"@nestjs/terminus": "^6.5.6",
"@nestjs/testing": "^6.11.11",
"@nestjs/swagger": "^4.8.2",
"@nestjs/terminus": "^7.2.0",
"@nestjs/testing": "^7.6.18",
"@nestjs/typeorm": "^7.1.5",
"@nestjs/websockets": "^6.11.11",
"@nestjs/websockets": "^7.6.18",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"class-transformer": "^0.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { AdminDataReturnDto } from '../admin-area-dynamic-data/dto/admin-data-re
import { UserRole } from '../user/user-role.enum';
import { AdminAreaDataService } from './admin-area-data.service';
import { UploadAdminAreaDataJsonDto } from './dto/upload-admin-area-data.dto';
import { FILE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@UseGuards(RolesGuard)
Expand All @@ -50,17 +51,7 @@ export class AdminAreaDataController {
})
@Post('upload/csv')
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(FILE_UPLOAD_API_FORMAT)
@UseInterceptors(FileInterceptor('file'))
public async uploadCsv(@UploadedFile() adminAreaData): Promise<void> {
await this.adminAreaDataService.uploadCsv(adminAreaData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { Roles } from '../../roles.decorator';
import { UserRole } from '../user/user-role.enum';
import { Query } from '@nestjs/common/decorators';
import { FILE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@UseGuards(RolesGuard)
Expand Down Expand Up @@ -109,17 +110,7 @@ export class AdminAreaDynamicDataController {
'Upload raster file (.tif) such as a disaster-extent for given disaster-type (used by IBF-pipelines)',
})
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(FILE_UPLOAD_API_FORMAT)
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiResponse({
status: 201,
Expand Down
13 changes: 2 additions & 11 deletions services/API-service/src/api/event/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { UserDecorator } from '../user/user.decorator';
import { FileInterceptor } from '@nestjs/platform-express';
import stream from 'stream';
import { Response } from 'express-serve-static-core';
import { IMAGE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@ApiTags('event')
Expand Down Expand Up @@ -221,17 +222,7 @@ export class EventController {
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiParam({ name: 'eventName', required: false, type: 'string' })
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
image: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(IMAGE_UPLOAD_API_FORMAT)
@ApiResponse({ status: 200, description: 'Post event map image' })
@Post('/event-map-image/:countryCodeISO3/:disasterType/:eventName')
@UseInterceptors(FileInterceptor('image'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { UploadTriggerPerStationDto } from './dto/upload-trigger-per-station';
import { GlofasStationForecastEntity } from './glofas-station-forecast.entity';
import { GlofasStationEntity } from './glofas-station.entity';
import { GlofasStationService } from './glofas-station.service';
import { FILE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@UseGuards(RolesGuard)
Expand Down Expand Up @@ -111,17 +112,7 @@ export class GlofasStationController {
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@Post('upload-csv/:countryCodeISO3')
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(FILE_UPLOAD_API_FORMAT)
@UseInterceptors(FileInterceptor('file'))
public async uploadCsv(
@UploadedFile() glofasStationData,
Expand Down
13 changes: 2 additions & 11 deletions services/API-service/src/api/lines-data/lines-data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RolesGuard } from '../../roles.guard';
import { UserRole } from '../user/user-role.enum';
import { LinesDataService } from './lines-data.service';
import { UploadLinesExposureStatusDto } from './dto/upload-asset-exposure-status.dto';
import { FILE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@ApiTags('lines-data')
Expand All @@ -47,17 +48,7 @@ export class LinesDataController {
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@Post('upload-csv/:linesDataCategory/:countryCodeISO3')
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(FILE_UPLOAD_API_FORMAT)
@UseInterceptors(FileInterceptor('file'))
public async uploadCsv(
@UploadedFile() linesDataData,
Expand Down
13 changes: 2 additions & 11 deletions services/API-service/src/api/point-data/point-data.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { GeoJson } from '../../shared/geo.model';
import { UserRole } from '../user/user-role.enum';
import { PointDataService } from './point-data.service';
import { UploadAssetExposureStatusDto } from './dto/upload-asset-exposure-status.dto';
import { FILE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';

@ApiBearerAuth()
@ApiTags('point-data')
Expand Down Expand Up @@ -75,17 +76,7 @@ export class PointDataController {
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@Post('upload-csv/:pointDataCategory/:countryCodeISO3')
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@ApiBody(FILE_UPLOAD_API_FORMAT)
@UseInterceptors(FileInterceptor('file'))
public async uploadCsv(
@UploadedFile() pointDataData,
Expand Down
5 changes: 3 additions & 2 deletions services/API-service/src/api/user/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createParamDecorator } from '@nestjs/common';
import { ExecutionContext, createParamDecorator } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';
import { User } from './user.model';

export const UserDecorator = createParamDecorator(
(data, req): User => {
(data, ctx: ExecutionContext): User => {
// if route is protected, there is a user set in auth.middleware
const req = ctx.switchToHttp().getRequest();
if (!!req.user) {
return !!data ? req.user[data] : req.user;
}
Expand Down
2 changes: 1 addition & 1 deletion services/API-service/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const DEBUG = ['production', 'test'].indexOf(process.env.NODE_ENV) < 0;
export const PORT = 3000;
export const SCHEME = DEBUG ? 'http' : 'https';
export const SCHEME = DEBUG ? 'http://' : 'https://';

// Configure Internal and External API URL's
// ---------------------------------------------------------------------------
Expand Down
41 changes: 14 additions & 27 deletions services/API-service/src/health.module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import { Module } from '@nestjs/common';
import { Controller, Get, Module } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import {
HealthIndicatorResult,
TerminusModule,
TerminusModuleOptions,
TypeOrmHealthIndicator,
} from '@nestjs/terminus';
import { TypeOrmModule } from '@nestjs/typeorm';

const getTerminusOptions = (
db: TypeOrmHealthIndicator,
): TerminusModuleOptions => ({
endpoints: [
{
url: '/health',
// All the indicator which will be checked when requesting /health
healthIndicators: [
// Set the timeout for a response to 300ms
async (): Promise<HealthIndicatorResult> =>
db.pingCheck('database', { timeout: 300 }),
],
},
],
});
@ApiTags('-- check API --')
@Controller('health')
export class HealthController {
@ApiOperation({ summary: 'Check database connection' })
@Get()
healthCheck(db: TypeOrmHealthIndicator) {
async (): Promise<HealthIndicatorResult> =>
db.pingCheck('database', { timeout: 300 });
}
}

@Module({
imports: [
// Make sure TypeOrmModule is available in the module context
TypeOrmModule.forRoot(),
TerminusModule.forRootAsync({
// Inject the TypeOrmHealthIndicator provided by nestjs/terminus
inject: [TypeOrmHealthIndicator],
useFactory: getTerminusOptions,
}),
],
controllers: [HealthController],
imports: [TerminusModule],
})
export class HealthModule {}
10 changes: 7 additions & 3 deletions services/API-service/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SwaggerDocumentOptions,
SwaggerCustomOptions,
} from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { BadRequestException, ValidationPipe } from '@nestjs/common';
import * as bodyParser from 'body-parser';

async function bootstrap(): Promise<void> {
Expand All @@ -26,7 +26,7 @@ async function bootstrap(): Promise<void> {
.setTitle(apiDocumentationTitle)
.setDescription(apiDocumentationDescription)
.setVersion(apiVersion)
.addServer(`${SCHEME}://`)
.addServer(SCHEME)
.addBearerAuth()
.build();
const swaggerDocumentOptions: SwaggerDocumentOptions = {
Expand All @@ -52,7 +52,11 @@ async function bootstrap(): Promise<void> {
swaggerDocumentOptions,
);
SwaggerModule.setup('/docs', app, document, swaggerCustomOptions);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalPipes(
new ValidationPipe({
exceptionFactory: errors => new BadRequestException(errors),
}),
);
app.use(bodyParser.json({ limit: '25mb' }));
app.use(
bodyParser.urlencoded({
Expand Down
23 changes: 23 additions & 0 deletions services/API-service/src/shared/file-upload-api-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const FILE_UPLOAD_API_FORMAT = {
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
};

export const IMAGE_UPLOAD_API_FORMAT = {
schema: {
type: 'object',
properties: {
image: {
type: 'string',
format: 'binary',
},
},
},
};

0 comments on commit 22c8991

Please sign in to comment.