Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the swagger schema related to errors #129

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TsED.Configuration> = {
...config,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions src/controllers/rest/impl/FileUploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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<unknown> {
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/rest/impl/security/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/rest/impl/security/PassportCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 5 additions & 14 deletions src/engine/impl/HttpErrorRenderers/DefaultHttpRenderEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultRenderObj, Exception> {
public render(obj: HttpErrorRenderObj<Exception>): Promise<DefaultRenderObj> {
export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine<DefaultRenderException, Exception> {
public render(obj: HttpErrorRenderObj<Exception>): Promise<DefaultRenderException> {
return Promise.resolve(this.mapError(obj.internalError));
}

Expand All @@ -24,11 +19,7 @@ export class DefaultHttpRenderEngine implements IHttpErrorRenderEngine<DefaultRe
return false;
}

public mapError(error: Exception): DefaultRenderObj {
return {
name: error.origin?.name ?? error.name,
message: error.message,
status: error.status ?? 500,
};
public mapError(error: Exception): DefaultRenderException {
return new DefaultRenderException(error.origin?.name ?? error.name, error.message, error.status ?? 500);
}
}
4 changes: 3 additions & 1 deletion src/model/dto/EntryModificationDto.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/model/dto/FileUploadResponseDto.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
23 changes: 23 additions & 0 deletions src/model/rest/DefaultRenderException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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")
public name: string;

@Property()
@Description("The thing that went wrong")
public message: string;

@Property()
@Description("the HTTP status")
public status: number;

public constructor(name: string, message: string, status: number) {
this.name = name;
this.message = message;
this.status = status;
}
}