Added open api docs

This commit is contained in:
Thom Werring 2024-03-26 22:27:24 +01:00
parent 8c1ed4d086
commit 0e4716b7b8
4 changed files with 93 additions and 4 deletions

View File

@ -4,6 +4,10 @@ services:
ports: ports:
- "3001:3000" - "3001:3000"
command: "npm run start:dev" command: "npm run start:dev"
depends_on:
- database
volumes:
- ".:/usr/src/app"
database: database:
image: "mysql:8" image: "mysql:8"
command: --default-authentication-plugin=mysql_native_password command: --default-authentication-plugin=mysql_native_password

View File

@ -1,6 +1,13 @@
import { Controller, Get } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { ShipmentsService } from '@/shipments/shipments.service'; import { ShipmentsService } from '@/shipments/shipments.service';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger'; import {
ApiExtraModels,
ApiOkResponse,
ApiSecurity,
ApiTags,
ApiUnauthorizedResponse,
getSchemaPath,
} from '@nestjs/swagger';
import { import {
ShipmentContainerDto, ShipmentContainerDto,
ShipmentDto, ShipmentDto,
@ -13,14 +20,38 @@ export class ShipmentsController {
constructor(private readonly shipmentsService: ShipmentsService) {} constructor(private readonly shipmentsService: ShipmentsService) {}
@Get() @Get()
@ApiTags('Shipping')
@ApiOkResponse({ @ApiOkResponse({
description: 'Returns a list of shipments', description: 'Returns a list of shipments',
schema: { schema: {
items: { type: 'object',
$ref: getSchemaPath(ShipmentDto), 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() { async getShipments() {
return { return {
data: await this.shipmentsService.getAll(), data: await this.shipmentsService.getAll(),

View File

@ -4,6 +4,9 @@ import {
ApiExtraModels, ApiExtraModels,
ApiNotFoundResponse, ApiNotFoundResponse,
ApiOkResponse, ApiOkResponse,
ApiSecurity,
ApiTags,
ApiUnauthorizedResponse,
ApiUnprocessableEntityResponse, ApiUnprocessableEntityResponse,
getSchemaPath, getSchemaPath,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
@ -26,11 +29,58 @@ export class TrackingController {
@ApiUnprocessableEntityResponse({ @ApiUnprocessableEntityResponse({
description: description:
'The shipment has been found, but the shipment data does not match.', '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({ @ApiNotFoundResponse({
description: 'We could not find any shipment matching the request.', 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) @HttpCode(200)
@ApiSecurity('API key')
@ApiTags('Tracking')
track(@Body() body: TrackingDto): Promise<unknown> { track(@Body() body: TrackingDto): Promise<unknown> {
return this.trackingService return this.trackingService
.track(body) .track(body)

View File

@ -6,7 +6,11 @@ export function openApi(app: INestApplication) {
.setTitle('MyFreight Assessment') .setTitle('MyFreight Assessment')
.setDescription('Backend assessment for MyFreight') .setDescription('Backend assessment for MyFreight')
.setVersion('1.0') .setVersion('1.0')
.addTag('MyFreight') .addSecurity('API key', {
type: 'apiKey',
in: 'header',
name: 'Authorization',
})
.build(); .build();
const document = SwaggerModule.createDocument(app, config); const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document); SwaggerModule.setup('api', app, document);