From 8f971000ab7b1c21992006ade877921ca110532f Mon Sep 17 00:00:00 2001 From: VictoriqueMoe Date: Thu, 21 Mar 2024 11:42:06 +0000 Subject: [PATCH 1/2] fix the swagger schema related to errors --- src/Server.ts | 4 ++-- .../rest/impl/FileUploadController.ts | 15 ++++++------- .../rest/impl/security/AdminController.ts | 5 +++-- .../rest/impl/security/PassportCtrl.ts | 4 ++-- .../DefaultHttpRenderEngine.ts | 19 +++++------------ src/model/rest/DefaultRenderException.ts | 21 +++++++++++++++++++ 6 files changed, 41 insertions(+), 27 deletions(-) create mode 100644 src/model/rest/DefaultRenderException.ts diff --git a/src/Server.ts b/src/Server.ts index fada416..8f16877 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -44,8 +44,8 @@ import { fileURLToPath } from "node:url"; import { ExpressRateLimitTypeOrmStore } from "typeorm-rate-limit-store"; import { ExpressRateLimitStoreModel } from "./model/db/ExpressRateLimitStore.model.js"; import { Exception, TooManyRequests } from "@tsed/exceptions"; -import { DefaultRenderObj } from "./engine/impl/index.js"; import { Logger } from "@tsed/logger"; +import { DefaultRenderException } from "./model/rest/DefaultRenderException.js"; const opts: Partial = { ...config, @@ -240,7 +240,7 @@ export class Server implements BeforeRoutesInit { } } - private parseError(error: Exception): DefaultRenderObj { + private parseError(error: Exception): DefaultRenderException { return { name: error.origin?.name ?? error.name, message: error.message, diff --git a/src/controllers/rest/impl/FileUploadController.ts b/src/controllers/rest/impl/FileUploadController.ts index df6272f..2fd624b 100644 --- a/src/controllers/rest/impl/FileUploadController.ts +++ b/src/controllers/rest/impl/FileUploadController.ts @@ -2,7 +2,7 @@ import { Controller, Inject } from "@tsed/di"; import { Delete, Description, Example, Examples, Get, Name, Patch, Put, Returns, Summary } from "@tsed/schema"; import { StatusCodes } from "http-status-codes"; import { FileUploadResponseDto } from "../../../model/dto/FileUploadResponseDto.js"; -import { BadRequest, Forbidden, UnsupportedMediaType } from "@tsed/exceptions"; +import { BadRequest } from "@tsed/exceptions"; import { MultipartFile, PathParams, type PlatformMulterFile, QueryParams, Req, Res } from "@tsed/common"; import { BodyParams } from "@tsed/platform-params"; import { FileService } from "../../../services/FileService.js"; @@ -11,11 +11,12 @@ import { BaseRestController } from "../BaseRestController.js"; import { Logger } from "@tsed/logger"; import { EntryModificationDto } from "../../../model/dto/EntryModificationDto.js"; import type { Request, Response } from "express"; +import { DefaultRenderException } from "../../../model/rest/DefaultRenderException.js"; @Controller("/") @Description("This is the API documentation for uploading and sharing files.") @Name("File Uploader") -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") export class FileUploadController extends BaseRestController { public constructor( @Inject() private fileUploadService: FileService, @@ -26,9 +27,9 @@ export class FileUploadController extends BaseRestController { @Put() @Returns(StatusCodes.CREATED, FileUploadResponseDto).Description("If the file was stored successfully") - @Returns(StatusCodes.BAD_REQUEST, BadRequest).Description("If the request was malformed") + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException).Description("If the request was malformed") @Returns(StatusCodes.OK, FileUploadResponseDto).Description("If the file already exists") - @Returns(StatusCodes.UNSUPPORTED_MEDIA_TYPE, UnsupportedMediaType).Description( + @Returns(StatusCodes.UNSUPPORTED_MEDIA_TYPE, DefaultRenderException).Description( "If the media type of the file specified was blocked", ) @Example({ @@ -121,7 +122,7 @@ export class FileUploadController extends BaseRestController { @Get("/:token") @Returns(StatusCodes.OK, FileUploadResponseDto) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Get entry info such as when it will expire and the URL") @Summary("Get entry info via token") public getInfo( @@ -141,7 +142,7 @@ export class FileUploadController extends BaseRestController { @Patch("/:token") @Returns(StatusCodes.OK, FileUploadResponseDto) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Modify an entry such as password, expiry and other settings") @Summary("Modify components of an entry") public modifyEntry( @@ -158,7 +159,7 @@ export class FileUploadController extends BaseRestController { @Delete("/:token") @Returns(StatusCodes.OK, Boolean) - @Returns(StatusCodes.BAD_REQUEST, BadRequest) + @Returns(StatusCodes.BAD_REQUEST, DefaultRenderException) @Description("Delete a file via the token") @Summary("Delete a file from a token") public async deleteEntry(@PathParams("token") token: string): Promise { diff --git a/src/controllers/rest/impl/security/AdminController.ts b/src/controllers/rest/impl/security/AdminController.ts index f4acf05..fb78bcc 100644 --- a/src/controllers/rest/impl/security/AdminController.ts +++ b/src/controllers/rest/impl/security/AdminController.ts @@ -4,14 +4,15 @@ import { AdminService } from "../../../../services/AdminService.js"; import { Authorize } from "@tsed/passport"; import { BodyParams } from "@tsed/platform-params"; import { PlatformResponse, QueryParams, Res } from "@tsed/common"; -import { Forbidden, NotFound } from "@tsed/exceptions"; +import { NotFound } from "@tsed/exceptions"; import { BaseRestController } from "../../BaseRestController.js"; import { StatusCodes } from "http-status-codes"; import type { DatatableColumn, DatatableOrder, DatatableSearch } from "../../../../utils/typeings.js"; +import { DefaultRenderException } from "../../../../model/rest/DefaultRenderException.js"; @Hidden() @Controller("/admin") -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") @Authorize("loginAuthProvider") export class AdminController extends BaseRestController { public constructor(@Inject() private adminService: AdminService) { diff --git a/src/controllers/rest/impl/security/PassportCtrl.ts b/src/controllers/rest/impl/security/PassportCtrl.ts index 3669daf..26d8072 100644 --- a/src/controllers/rest/impl/security/PassportCtrl.ts +++ b/src/controllers/rest/impl/security/PassportCtrl.ts @@ -9,12 +9,12 @@ import { BaseRestController } from "../../BaseRestController.js"; import { CustomUserInfoModel } from "../../../../model/auth/CustomUserInfoModel.js"; import { UserService } from "../../../../services/UserService.js"; import { CaptchaMiddleWare } from "../../../../middleware/endpoint/CaptchaMiddleWare.js"; -import { Forbidden } from "@tsed/exceptions"; +import { DefaultRenderException } from "../../../../model/rest/DefaultRenderException.js"; @Controller("/auth") @Scope(ProviderScope.SINGLETON) @Hidden() -@Returns(StatusCodes.FORBIDDEN, Forbidden).Description("If your IP has been blocked") +@Returns(StatusCodes.FORBIDDEN, DefaultRenderException).Description("If your IP has been blocked") export class PassportCtrl extends BaseRestController { public constructor(@Inject() private usersService: UserService) { super(); diff --git a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts index 6b29f09..24094fc 100644 --- a/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts +++ b/src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts @@ -3,19 +3,14 @@ import { Exception } from "@tsed/exceptions"; import type { HttpErrorRenderObj } from "../../../utils/typeings.js"; import { Injectable, ProviderScope } from "@tsed/di"; import { HTTP_RENDER_ENGINE } from "../../../model/di/tokens.js"; - -export type DefaultRenderObj = { - name: string; - message: string; - status: number; -}; +import { DefaultRenderException } from "../../../model/rest/DefaultRenderException.js"; @Injectable({ scope: ProviderScope.SINGLETON, type: HTTP_RENDER_ENGINE, }) -export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine { - public render(obj: HttpErrorRenderObj): Promise { +export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine { + public render(obj: HttpErrorRenderObj): Promise { return Promise.resolve(this.mapError(obj.internalError)); } @@ -24,11 +19,7 @@ export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine Date: Thu, 21 Mar 2024 12:08:57 +0000 Subject: [PATCH 2/2] update schema names --- src/model/dto/EntryModificationDto.ts | 4 +++- src/model/dto/FileUploadResponseDto.ts | 4 +++- src/model/rest/DefaultRenderException.ts | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/model/dto/EntryModificationDto.ts b/src/model/dto/EntryModificationDto.ts index c660b23..1b73c1b 100644 --- a/src/model/dto/EntryModificationDto.ts +++ b/src/model/dto/EntryModificationDto.ts @@ -1,4 +1,4 @@ -import { Description, Optional, Property } from "@tsed/schema"; +import { Description, Name, Optional, Property } from "@tsed/schema"; import { BadRequest } from "@tsed/exceptions"; import { BeforeDeserialize } from "@tsed/json-mapper"; @@ -15,6 +15,8 @@ import { BeforeDeserialize } from "@tsed/json-mapper"; } return data; }) +@Name("WaifuModification") +@Description("A modify request to change components of an entry") export class EntryModificationDto { @Property() @Optional() diff --git a/src/model/dto/FileUploadResponseDto.ts b/src/model/dto/FileUploadResponseDto.ts index 12eb620..2b4ae3c 100644 --- a/src/model/dto/FileUploadResponseDto.ts +++ b/src/model/dto/FileUploadResponseDto.ts @@ -1,8 +1,10 @@ -import { Description, Nullable, Property } from "@tsed/schema"; +import { Description, Name, Nullable, Property } from "@tsed/schema"; import { FileUploadModel } from "../db/FileUpload.model.js"; import { Builder } from "builder-pattern"; import { ObjectUtils } from "../../utils/Utils.js"; +@Name("WaifuResponse") +@Description("This is a standard response for the service, containing info about the entry") export class FileUploadResponseDto { @Property() @Description("Used for file info and deleting") diff --git a/src/model/rest/DefaultRenderException.ts b/src/model/rest/DefaultRenderException.ts index 9e5b2a3..138fe2a 100644 --- a/src/model/rest/DefaultRenderException.ts +++ b/src/model/rest/DefaultRenderException.ts @@ -1,5 +1,7 @@ -import { Description, Property } from "@tsed/schema"; +import { Description, Name, Property } from "@tsed/schema"; +@Name("WaifuError") +@Description("A standard error, all errors from the service will take this shape") export class DefaultRenderException { @Property() @Description("The name of the error, this is normally the HTTP exception thrown")