From 0e4716b7b82c8d1e7cd803b8aba0b041691621ee Mon Sep 17 00:00:00 2001 From: Thom Werring Date: Tue, 26 Mar 2024 22:27:24 +0100 Subject: [PATCH] Added open api docs --- docker-compose.yml | 4 +++ src/shipments/shipments.controller.ts | 37 ++++++++++++++++++-- src/tracking/tracking.controller.ts | 50 +++++++++++++++++++++++++++ src/utils/openApi.ts | 6 +++- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a65b4ac..0aca8dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,6 +4,10 @@ services: ports: - "3001:3000" command: "npm run start:dev" + depends_on: + - database + volumes: + - ".:/usr/src/app" database: image: "mysql:8" command: --default-authentication-plugin=mysql_native_password diff --git a/src/shipments/shipments.controller.ts b/src/shipments/shipments.controller.ts index c523b30..4ce2519 100644 --- a/src/shipments/shipments.controller.ts +++ b/src/shipments/shipments.controller.ts @@ -1,6 +1,13 @@ import { Controller, Get } from '@nestjs/common'; import { ShipmentsService } from '@/shipments/shipments.service'; -import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger'; +import { + ApiExtraModels, + ApiOkResponse, + ApiSecurity, + ApiTags, + ApiUnauthorizedResponse, + getSchemaPath, +} from '@nestjs/swagger'; import { ShipmentContainerDto, ShipmentDto, @@ -13,14 +20,38 @@ export class ShipmentsController { constructor(private readonly shipmentsService: ShipmentsService) {} @Get() + @ApiTags('Shipping') @ApiOkResponse({ description: 'Returns a list of shipments', schema: { - items: { - $ref: getSchemaPath(ShipmentDto), + type: 'object', + properties: { + data: { + items: { + $ref: getSchemaPath(ShipmentDto), + }, + }, }, }, }) + @ApiUnauthorizedResponse({ + description: 'Unauthorized request', + schema: { + type: 'object', + properties: { + message: { + type: 'string', + }, + error: { + type: 'string', + }, + statusCode: { + type: 'number', + }, + }, + }, + }) + @ApiSecurity('API key') async getShipments() { return { data: await this.shipmentsService.getAll(), diff --git a/src/tracking/tracking.controller.ts b/src/tracking/tracking.controller.ts index 2fd8c5e..48c2066 100644 --- a/src/tracking/tracking.controller.ts +++ b/src/tracking/tracking.controller.ts @@ -4,6 +4,9 @@ import { ApiExtraModels, ApiNotFoundResponse, ApiOkResponse, + ApiSecurity, + ApiTags, + ApiUnauthorizedResponse, ApiUnprocessableEntityResponse, getSchemaPath, } from '@nestjs/swagger'; @@ -26,11 +29,58 @@ export class TrackingController { @ApiUnprocessableEntityResponse({ description: 'The shipment has been found, but the shipment data does not match.', + schema: { + type: 'object', + properties: { + message: { + type: 'string', + }, + error: { + type: 'string', + }, + statusCode: { + type: 'number', + }, + }, + }, }) @ApiNotFoundResponse({ description: 'We could not find any shipment matching the request.', + schema: { + type: 'object', + properties: { + message: { + type: 'string', + }, + error: { + type: 'string', + }, + statusCode: { + type: 'number', + }, + }, + }, + }) + @ApiUnauthorizedResponse({ + description: 'Unauthorized request', + schema: { + type: 'object', + properties: { + message: { + type: 'string', + }, + error: { + type: 'string', + }, + statusCode: { + type: 'number', + }, + }, + }, }) @HttpCode(200) + @ApiSecurity('API key') + @ApiTags('Tracking') track(@Body() body: TrackingDto): Promise { return this.trackingService .track(body) diff --git a/src/utils/openApi.ts b/src/utils/openApi.ts index c3685a4..7896c85 100644 --- a/src/utils/openApi.ts +++ b/src/utils/openApi.ts @@ -6,7 +6,11 @@ export function openApi(app: INestApplication) { .setTitle('MyFreight Assessment') .setDescription('Backend assessment for MyFreight') .setVersion('1.0') - .addTag('MyFreight') + .addSecurity('API key', { + type: 'apiKey', + in: 'header', + name: 'Authorization', + }) .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document);