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:
- "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

View File

@ -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: {
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(),

View File

@ -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<unknown> {
return this.trackingService
.track(body)

View File

@ -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);