Skip to content

Commit

Permalink
feat(be): versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 14, 2024
1 parent b272ece commit 75c024e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/backend/src/enums/endpoint-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum EndpointVersion {
v1 = "1",
}
8 changes: 8 additions & 0 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SWAGGER_JSON_PATH, SWAGGER_PATH } from "@metro-now/constants";
import { VersioningType } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";

Expand All @@ -8,6 +9,7 @@ import {
SWAGGER_TITLE,
SWAGGER_VERSION,
} from "src/constants/swagger.const";
import { EndpointVersion } from "src/enums/endpoint-version";
import { LoggerService } from "src/modules/logger/logger.service";

async function bootstrap() {
Expand All @@ -18,6 +20,12 @@ async function bootstrap() {
app.useLogger(new LoggerService());
app.enableCors();

app.enableVersioning({
type: VersioningType.URI,
prefix: "v",
defaultVersion: EndpointVersion.v1,
});

const swaggerDocumentBuilder = new DocumentBuilder()
.setTitle(SWAGGER_TITLE)
.setDescription(SWAGGER_DESCRIPTION)
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/modules/departure/departure.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
HttpStatus,
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import { z } from "zod";

import { QUERY_IDS_COUNT_MAX } from "src/constants/constants";
import { ApiQueries } from "src/decorators/swagger.decorator";
import { EndpointVersion } from "src/enums/endpoint-version";
import { DepartureService } from "src/modules/departure/departure.service";
import {
departureSchema,
Expand All @@ -30,6 +33,7 @@ export class DepartureController {
constructor(private readonly departureService: DepartureService) {}

@Get()
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiQueries([
metroOnlyQuery,
{
Expand Down Expand Up @@ -81,6 +85,7 @@ export class DepartureController {
}

@Get("/platform")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
async getDeparturesByPlatform(@Query("id") id): Promise<DepartureSchema[]> {
const platformSchema = z
.string()
Expand Down
6 changes: 6 additions & 0 deletions apps/backend/src/modules/platform/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import {
HttpStatus,
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiQuery, ApiTags } from "@nestjs/swagger";
import { z } from "zod";

import { ApiDescription, ApiQueries } from "src/decorators/swagger.decorator";
import { EndpointVersion } from "src/enums/endpoint-version";
import { LogInterceptor } from "src/modules/logger/log.interceptor";
import { PlatformService } from "src/modules/platform/platform.service";
import {
Expand All @@ -37,6 +40,7 @@ export class PlatformController {
constructor(private readonly platformService: PlatformService) {}

@Get("/all")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiDescription({
summary: "List of all platforms",
})
Expand All @@ -61,6 +65,7 @@ export class PlatformController {
}

@Get("/closest")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiDescription({
description: `
⚠️ _For better privacy consider using \`/in-box\`_
Expand Down Expand Up @@ -109,6 +114,7 @@ Sort platforms by distance to a given location. Location may be saved in logs.
}

@Get("/in-box")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiDescription({
summary: "List of platforms within a given bounding box",
})
Expand Down
5 changes: 4 additions & 1 deletion apps/backend/src/modules/status/status.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get } from "@nestjs/common";
import { Controller, Get, Version, VERSION_NEUTRAL } from "@nestjs/common";
import { ApiResponse, ApiTags } from "@nestjs/swagger";

import { ApiDescription } from "src/decorators/swagger.decorator";
Expand All @@ -15,6 +15,7 @@ export class StatusController {
constructor(private readonly statusService: StatusService) {}

@Get()
@Version([VERSION_NEUTRAL])
@ApiDescription({
summary: "Backend status",
})
Expand Down Expand Up @@ -45,6 +46,7 @@ export class StatusController {
}

@Get("/geo-functions")
@Version([VERSION_NEUTRAL])
@ApiDescription({ summary: "Geo functions status" })
@ApiResponse({
status: 200,
Expand All @@ -65,6 +67,7 @@ export class StatusController {
}

@Get("/db-data")
@Version([VERSION_NEUTRAL])
@ApiDescription({ summary: "DB data status" })
@ApiResponse({
status: 200,
Expand Down
11 changes: 10 additions & 1 deletion apps/backend/src/modules/stop/stop.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { CacheInterceptor } from "@nestjs/cache-manager";
import { Controller, Get, Query, UseInterceptors } from "@nestjs/common";
import {
Controller,
Get,
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiQuery, ApiTags } from "@nestjs/swagger";

import { EndpointVersion } from "src/enums/endpoint-version";
import { LogInterceptor } from "src/modules/logger/log.interceptor";
import { StopService } from "src/modules/stop/stop.service";
import { metroOnlyQuery } from "src/swagger/query.swagger";
Expand All @@ -13,6 +21,7 @@ export class StopController {
constructor(private readonly stopService: StopService) {}

@Get("/all")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiQuery(metroOnlyQuery)
async getAllStops(
@Query("metroOnly")
Expand Down

0 comments on commit 75c024e

Please sign in to comment.